From b1ee226a57f70128f2f76082e208102c9ad1fb2a Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Thu, 19 Feb 2026 01:33:55 +0900 Subject: [PATCH 1/3] Use Requires.Range in LastIndexOf Change `Requires.Argument(index - count + 1 >= 0)` to `Requires.Range(index - count + 1 >= 0, nameof(count))` in `ImmutableList.Node.LastIndexOf` to align with the validation used in `ImmutableArray.LastIndexOf`. --- .../src/System/Collections/Immutable/ImmutableList_1.Node.cs | 2 +- .../System.Collections.Immutable/tests/ImmutableListTestBase.cs | 1 + .../System.Collections.Immutable/tests/IndexOfTests.cs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) 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/ImmutableListTestBase.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs index e7237c9de74562..74a1d743bf1125 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs @@ -182,6 +182,7 @@ public void FindLastIndexTest() 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(2, 4, n => n == 1)); 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)); diff --git a/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs b/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs index ed15b40f361637..023726110062bd 100644 --- a/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs +++ b/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs @@ -104,6 +104,7 @@ public static void LastIndexOfTest( 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, 2, 4, EqualityComparer.Default)); Assert.Throws(() => lastIndexOfItemIndexCountEQ(collection1256, 6, 4, 4, EqualityComparer.Default)); Assert.Equal(-1, lastIndexOfItem(emptyCollection, 5)); From 35b481ee619af15c01f03d5616f5977747d38b4c Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Thu, 19 Feb 2026 01:46:55 +0900 Subject: [PATCH 2/3] Fix test case in FindLastIndexTest --- .../System.Collections.Immutable/tests/ImmutableListTestBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs index 74a1d743bf1125..6b6c4f2dcf57e2 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs @@ -182,8 +182,8 @@ public void FindLastIndexTest() 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(2, 4, n => n == 1)); Assert.Throws(() => this.GetListQuery(multipleElementList).FindLastIndex(4, n => n == 1)); + Assert.Throws(() => this.GetListQuery(multipleElementList).FindLastIndex(2, 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)); From 8cd336bf260dd941d791d75edb6703bb7d75b8af Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Thu, 19 Feb 2026 13:23:01 +0900 Subject: [PATCH 3/3] Fix test case to also validate the parameter name --- .../tests/ImmutableArrayBuilderTest.cs | 4 ++- .../tests/ImmutableArrayTest.cs | 4 ++- .../tests/ImmutableListBuilderTest.cs | 4 ++- .../tests/ImmutableListTest.cs | 8 +++-- .../tests/ImmutableListTestBase.cs | 14 ++++---- .../tests/IndexOfTests.cs | 34 ++++++++++--------- 6 files changed, 41 insertions(+), 27 deletions(-) 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 6b6c4f2dcf57e2..135ccf95ab23b9 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs @@ -177,15 +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(2, 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 023726110062bd..d41ef7224a8ede 100644 --- a/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs +++ b/src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs @@ -85,27 +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, 2, 4, EqualityComparer.Default)); - 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));