diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs index de18332bd01788..d89f04ef667460 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs @@ -808,7 +808,7 @@ internal int LastIndexOf(T item, int index, int count, IEqualityComparer? equ Requires.Range(index >= 0 && index < this.Count, nameof(index)); Requires.Range(count >= 0 && count <= this.Count, nameof(count)); - Requires.Argument(index - count + 1 >= 0); + Requires.Range(index - count + 1 >= 0, nameof(count)); equalityComparer ??= EqualityComparer.Default; using (var enumerator = new Enumerator(this, startIndex: index, count: count, reversed: true)) diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs index 841bfc451e332d..56d8ebadf4cbd3 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs @@ -277,7 +277,9 @@ public void LastIndexOf() (b, v, eq) => b.LastIndexOf(v, b.Count > 0 ? b.Count - 1 : 0, b.Count, eq), (b, v, i) => b.LastIndexOf(v, i), (b, v, i, c) => b.LastIndexOf(v, i, c), - (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq)); + (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq), + "startIndex", + "count"); } [Fact] diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs index f42957f3fac3a1..5dfe21f43cb783 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs @@ -802,7 +802,9 @@ public void LastIndexOf() (b, v, eq) => b.LastIndexOf(v, eq), (b, v, i) => b.LastIndexOf(v, i), (b, v, i, c) => b.LastIndexOf(v, i, c), - (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq)); + (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq), + "startIndex", + "count"); } [Theory] diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs index 741dfbd085cde4..bdb308977c69d1 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs @@ -345,7 +345,9 @@ public void LastIndexOf() (b, v, eq) => b.LastIndexOf(v, b.Count > 0 ? b.Count - 1 : 0, b.Count, eq), (b, v, i) => b.LastIndexOf(v, i), (b, v, i, c) => b.LastIndexOf(v, i, c), - (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq)); + (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq), + "index", + "count"); } [Fact] diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs index 429c6a01e19241..6137f1b968a7b4 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTest.cs @@ -512,14 +512,18 @@ public void LastIndexOf() (b, v, eq) => b.LastIndexOf(v, eq), (b, v, i) => b.LastIndexOf(v, i), (b, v, i, c) => b.LastIndexOf(v, i, c), - (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq)); + (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq), + "index", + "count"); IndexOfTests.LastIndexOfTest( seq => (IImmutableList)ImmutableList.CreateRange(seq), (b, v) => b.LastIndexOf(v), (b, v, eq) => b.LastIndexOf(v, eq), (b, v, i) => b.LastIndexOf(v, i), (b, v, i, c) => b.LastIndexOf(v, i, c), - (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq)); + (b, v, i, c, eq) => b.LastIndexOf(v, i, c, eq), + "index", + "count"); } [Fact] diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs index e7237c9de74562..135ccf95ab23b9 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs @@ -177,14 +177,17 @@ public void FindLastIndexTest() ImmutableList singleElementList = ImmutableList.Create(10); Assert.Equal(0, this.GetListQuery(singleElementList).FindLastIndex(0, 1, n => n == 10)); Assert.Equal(-1, this.GetListQuery(singleElementList).FindLastIndex(0, 1, n => n == 99)); - Assert.Throws(() => this.GetListQuery(singleElementList).FindLastIndex(1, n => n == 10)); - Assert.Throws(() => this.GetListQuery(singleElementList).FindLastIndex(1, 1, n => n == 10)); + AssertExtensions.Throws("startIndex", () => this.GetListQuery(singleElementList).FindLastIndex(1, n => n == 10)); + AssertExtensions.Throws("startIndex", () => this.GetListQuery(singleElementList).FindLastIndex(1, 1, n => n == 10)); + AssertExtensions.Throws("count", () => this.GetListQuery(singleElementList).FindLastIndex(0, 2, n => n == 10)); ImmutableList multipleElementList = ImmutableList.Create(1, 2, 3, 4); Assert.Equal(2, this.GetListQuery(multipleElementList).FindLastIndex(3, 4, n => n == 3)); - Assert.Throws(() => this.GetListQuery(multipleElementList).FindLastIndex(4, n => n == 1)); - Assert.Throws(() => this.GetListQuery(multipleElementList).FindLastIndex(4, 1, n => n == 1)); - Assert.Throws(() => this.GetListQuery(multipleElementList).FindLastIndex(4, 4, n => n == 1)); + AssertExtensions.Throws("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(4, n => n == 1)); + AssertExtensions.Throws("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(2, 4, n => n == 1)); + AssertExtensions.Throws("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(4, 1, n => n == 1)); + AssertExtensions.Throws("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(4, 4, n => n == 1)); + AssertExtensions.Throws("count", () => this.GetListQuery(multipleElementList).FindLastIndex(3, 5, n => n == 1)); // Create a list with contents: 100,101,102,103,104,100,101,102,103,104 ImmutableList list = ImmutableList.Empty.AddRange(Enumerable.Range(100, 5).Concat(Enumerable.Range(100, 5))); diff --git a/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs b/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs index ed15b40f361637..d41ef7224a8ede 100644 --- a/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs +++ b/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs @@ -85,26 +85,29 @@ public static void LastIndexOfTest( Func, int> lastIndexOfItemEQ, Func lastIndexOfItemIndex, Func lastIndexOfItemIndexCount, - Func, int> lastIndexOfItemIndexCountEQ) + Func, int> lastIndexOfItemIndexCountEQ, + string expectedIndexParamName, + string expectedCountParamName) { TCollection emptyCollection = factory(new int[0]); TCollection singleCollection = factory(new[] { 10 }); TCollection collection1256 = factory(new[] { 1, 2, 5, 6 }); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer.Default)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer.Default)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, EqualityComparer.Default)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, EqualityComparer.Default)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer.Default)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer.Default)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50))); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50))); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, new CustomComparer(50))); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, new CustomComparer(50))); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1))); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1))); - Assert.Throws(() => lastIndexOfItemIndex(collection1256, 2, 5)); - Assert.Throws(() => lastIndexOfItemIndexCountEQ(collection1256, 6, 4, 4, EqualityComparer.Default)); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer.Default)); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer.Default)); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, EqualityComparer.Default)); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, EqualityComparer.Default)); + AssertExtensions.Throws(expectedCountParamName, () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer.Default)); + AssertExtensions.Throws(expectedCountParamName, () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer.Default)); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50))); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50))); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, new CustomComparer(50))); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, new CustomComparer(50))); + AssertExtensions.Throws(expectedCountParamName, () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1))); + AssertExtensions.Throws(expectedCountParamName, () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1))); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndex(collection1256, 2, 5)); + AssertExtensions.Throws(expectedCountParamName, () => lastIndexOfItemIndexCountEQ(collection1256, 6, 2, 4, EqualityComparer.Default)); + AssertExtensions.Throws(expectedIndexParamName, () => lastIndexOfItemIndexCountEQ(collection1256, 6, 4, 4, EqualityComparer.Default)); Assert.Equal(-1, lastIndexOfItem(emptyCollection, 5)); Assert.Equal(-1, lastIndexOfItemEQ(emptyCollection, 5, EqualityComparer.Default));