Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IEnumerator<T> on ref struct enumerators #106309

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor exception handling and improve tests
- Updated `Slice` method in `ReadOnlyTensorSpan<T>` to use `ThrowHelper.ThrowIndexOutOfRangeException()` for consistent error handling.
- Removed unnecessary `using System.Reflection;` directive from `TensorSpanTests.cs`.
- Adjusted `TensorSpan_SliceTest` to call `TensorSpan_TestEnumerator(spanInt);` after defining `spanInt` and updated exception checks.
- Introduced a `value` variable in `TensorSpan_TestEnumerator<T>` to accurately restore the enumerator's current value for improved assertions.
  • Loading branch information
AlexRadch committed Jan 9, 2025
commit 642f647be05327e25be1c40d50987bbab3538d25
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ internal ReadOnlyTensorSpan<T> Slice(scoped ReadOnlySpan<nint> lengths)
public ReadOnlyTensorSpan<T> Slice(params scoped ReadOnlySpan<NRange> ranges)
{
if (ranges.Length != Lengths.Length)
throw new ArgumentOutOfRangeException(nameof(ranges), "Number of dimensions to slice does not equal the number of dimensions in the span");
ThrowHelper.ThrowIndexOutOfRangeException();

ReadOnlyTensorSpan<T> toReturn;
scoped Span<nint> lengths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Xunit;

Expand Down Expand Up @@ -1954,11 +1953,11 @@ public static void TensorSpan_SliceTest()
int[] a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
int[] results = new int[9];
TensorSpan<int> spanInt = a.AsTensorSpan(3, 3);
TensorSpan_TestEnumerator(spanInt);

Assert.Throws<IndexOutOfRangeException>(() => a.AsTensorSpan(2, 3).Slice(0..1));
Assert.Throws<IndexOutOfRangeException>(() => a.AsTensorSpan(2, 3).Slice(1..2));
Assert.Throws<ArgumentOutOfRangeException>(() => a.AsTensorSpan(2, 3).Slice(0..1, 5..6));
TensorSpan_TestEnumerator(spanInt);

var sp = spanInt.Slice(1..3, 1..3);
Assert.Equal(5, sp[0, 0]);
Expand Down Expand Up @@ -2124,10 +2123,11 @@ private static void TensorSpan_TestEnumerator<T>(TensorSpan<T> span)
Assert.True(enumerator.MoveNext());
ref var current = ref enumerator.Current;

var value = current;
Assert.Equal(span[curIndexes], current);
current++;
Assert.Equal(span[curIndexes], current);
current--;
current = value;
Assert.Equal(span[curIndexes], current);
}
Assert.False(enumerator.MoveNext());
Expand Down
Loading