Skip to content
Closed
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 @@ -793,12 +793,15 @@ public int IndexOf(T item, int startIndex, int count)
/// <returns>The 0-based index into the array where the item was found; or -1 if it could not be found.</returns>
public int IndexOf(T item, int startIndex, int count, IEqualityComparer<T>? equalityComparer)
{
if (count == 0 && startIndex == 0)
if (count == 0)
{
return -1;
if (startIndex == 0 || startIndex == this.Count)
{
return -1;
}
}

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

equalityComparer ??= EqualityComparer<T>.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,15 @@ public int IndexOf(T item, int startIndex, int count, IEqualityComparer<T>? equa
ImmutableArray<T> self = this;
self.ThrowNullRefIfNotInitialized();

if (count == 0 && startIndex == 0)
if (count == 0)
{
return -1;
if (startIndex == 0 || startIndex == self.Length)
{
return -1;
}
}

Requires.Range(startIndex >= 0 && startIndex < self.Length, nameof(startIndex));
Requires.Range(startIndex >= 0 && startIndex <= self.Length, nameof(startIndex));
Requires.Range(count >= 0 && startIndex + count <= self.Length, nameof(count));

equalityComparer ??= EqualityComparer<T>.Default;
Expand Down Expand Up @@ -253,12 +256,15 @@ public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer<T>?
ImmutableArray<T> self = this;
self.ThrowNullRefIfNotInitialized();

if (startIndex == 0 && count == 0)
if (count == 0)
{
return -1;
if (startIndex == 0 || startIndex == self.Length)
{
return -1;
}
}

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

equalityComparer ??= EqualityComparer<T>.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2855,5 +2855,28 @@ public StructWithReferenceTypeField(string foo)
this.foo = foo;
}
}

[Theory]
[InlineData(0, 0)] // startIndex 0, count 0
[InlineData(4, 0)] // startIndex == Length, count 0
[InlineData(2, 0)] // intermediate startIndex, count 0
public void IndexOf_CountZero_ReturnsMinusOne(int startIndex, int count)
{
var array = ImmutableArray.Create(1, 2, 3, 4);
Assert.Equal(-1, array.IndexOf(2, startIndex, count));
}

[Fact]
public void IndexOf_EmptyArray_CountZero_ReturnsMinusOne()
{
Assert.Equal(-1, ImmutableArray<int>.Empty.IndexOf(1, 0, 0));
}

[Fact]
public void IndexOf_Builder_CountZero_ReturnsMinusOne()
{
var builder = ImmutableArray.Create(1, 2, 3, 4).ToBuilder();
Assert.Equal(-1, builder.IndexOf(2, builder.Count, 0));
}
}
}