Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -791,36 +791,36 @@ internal int IndexOf(T item, int index, int count, IEqualityComparer<T>? equalit
/// The object to locate in the <see cref="ImmutableList{T}"/>. The value
/// can be null for reference types.
/// </param>
/// <param name="index">The zero-based starting index of the backward search.</param>
/// <param name="startIndex">The zero-based starting index of the backward search.</param>
/// <param name="count">The number of elements in the section to search.</param>
/// <param name="equalityComparer">The equality comparer to use for testing the match of two elements.</param>
/// <returns>
/// The zero-based index of the last occurrence of <paramref name="item"/> within the range of elements
/// in the <see cref="ImmutableList{T}"/> that contains <paramref name="count"/> number of elements
/// and ends at <paramref name="index"/>, if found; otherwise, -1.
/// and ends at <paramref name="startIndex"/>, if found; otherwise, -1.
/// </returns>
internal int LastIndexOf(T item, int index, int count, IEqualityComparer<T>? equalityComparer)
internal int LastIndexOf(T item, int startIndex, int count, IEqualityComparer<T>? equalityComparer)
{
if (index == 0 && count == 0)
if (startIndex == 0 && count == 0)
{
return -1;
}

Requires.Range(index >= 0 && index < this.Count, nameof(index));
Requires.Range(startIndex >= 0 && startIndex < this.Count, nameof(startIndex));
Requires.Range(count >= 0 && count <= this.Count, nameof(count));
Requires.Argument(index - count + 1 >= 0);
Requires.Range(startIndex - count + 1 >= 0, nameof(count));

equalityComparer ??= EqualityComparer<T>.Default;
using (var enumerator = new Enumerator(this, startIndex: index, count: count, reversed: true))
using (var enumerator = new Enumerator(this, startIndex: startIndex, count: count, reversed: true))
{
while (enumerator.MoveNext())
{
if (equalityComparer.Equals(item, enumerator.Current))
{
return index;
return startIndex;
}

index--;
startIndex--;
}
}

Expand Down Expand Up @@ -1246,7 +1246,7 @@ internal int FindLastIndex(int startIndex, int count, Predicate<T> match)

Requires.Range(startIndex >= 0 && startIndex < this.Count, nameof(startIndex));
Requires.Range(count >= 0 && count <= this.Count, nameof(count));
Requires.Range(startIndex - count + 1 >= 0, nameof(startIndex));
Requires.Range(startIndex - count + 1 >= 0, nameof(count));

using (var enumerator = new Enumerator(this, startIndex: startIndex, count: count, reversed: true))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,17 @@ public void FindLastIndexTest()
ImmutableList<int> 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<ArgumentOutOfRangeException>(() => this.GetListQuery(singleElementList).FindLastIndex(1, n => n == 10));
Assert.Throws<ArgumentOutOfRangeException>(() => this.GetListQuery(singleElementList).FindLastIndex(1, 1, n => n == 10));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => this.GetListQuery(singleElementList).FindLastIndex(1, n => n == 10));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => this.GetListQuery(singleElementList).FindLastIndex(1, 1, n => n == 10));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => this.GetListQuery(singleElementList).FindLastIndex(0, 2, n => n == 10));

ImmutableList<int> multipleElementList = ImmutableList.Create(1, 2, 3, 4);
Assert.Equal(2, this.GetListQuery(multipleElementList).FindLastIndex(3, 4, n => n == 3));
Assert.Throws<ArgumentOutOfRangeException>(() => this.GetListQuery(multipleElementList).FindLastIndex(4, n => n == 1));
Assert.Throws<ArgumentOutOfRangeException>(() => this.GetListQuery(multipleElementList).FindLastIndex(4, 1, n => n == 1));
Assert.Throws<ArgumentOutOfRangeException>(() => this.GetListQuery(multipleElementList).FindLastIndex(4, 4, n => n == 1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(4, n => n == 1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => this.GetListQuery(multipleElementList).FindLastIndex(2, 4, n => n == 1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(4, 1, n => n == 1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => this.GetListQuery(multipleElementList).FindLastIndex(4, 4, n => n == 1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("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<int> list = ImmutableList<int>.Empty.AddRange(Enumerable.Range(100, 5).Concat(Enumerable.Range(100, 5)));
Expand Down
29 changes: 15 additions & 14 deletions src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ public static void LastIndexOfTest<TCollection>(
TCollection singleCollection = factory(new[] { 10 });
TCollection collection1256 = factory(new[] { 1, 2, 5, 6 });

Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndex(collection1256, 2, 5));
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 6, 4, 4, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(singleCollection, 100, 1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(singleCollection, 100, -1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndex(collection1256, 2, 5));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => lastIndexOfItemIndexCountEQ(collection1256, 6, 2, 4, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("startIndex", () => lastIndexOfItemIndexCountEQ(collection1256, 6, 4, 4, EqualityComparer<int>.Default));

Assert.Equal(-1, lastIndexOfItem(emptyCollection, 5));
Assert.Equal(-1, lastIndexOfItemEQ(emptyCollection, 5, EqualityComparer<int>.Default));
Expand Down
Loading