Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 903f52c

Browse files
authored
Improve Span/ReadOnlySpan test coverage (#26342)
1 parent f08bc7b commit 903f52c

File tree

12 files changed

+80
-24
lines changed

12 files changed

+80
-24
lines changed

src/System.Memory/tests/Memory/Span.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public static void SpanFromCtorArrayZeroLength()
6666
Memory<int> memory;
6767

6868
memory = new Memory<int>(empty);
69-
memory.Span.Validate();
69+
memory.Span.ValidateNonNullEmpty();
7070

7171
memory = new Memory<int>(empty, 0, empty.Length);
72-
memory.Span.Validate();
72+
memory.Span.ValidateNonNullEmpty();
7373

7474
OwnedMemory<int> owner = new CustomMemoryForTest<int>(empty);
7575
owner.Memory.Span.Validate();

src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
44
<PropertyGroup>
55
<IncludePerformanceTests>true</IncludePerformanceTests>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
67
<ProjectGuid>{18482C55-6B57-41E8-BBC4-383B9E2C7AA2}</ProjectGuid>
78
</PropertyGroup>
89
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />

src/System.Memory/tests/ReadOnlyMemory/Span.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public static void SpanFromCtorArrayZeroLength()
6666
ReadOnlyMemory<int> memory;
6767

6868
memory = new ReadOnlyMemory<int>(empty);
69-
memory.Span.Validate();
69+
memory.Span.ValidateNonNullEmpty();
7070

7171
memory = new ReadOnlyMemory<int>(empty, 0, empty.Length);
72-
memory.Span.Validate();
72+
memory.Span.ValidateNonNullEmpty();
7373

7474
OwnedMemory<int> owner = new CustomMemoryForTest<int>(empty);
7575
((ReadOnlyMemory<int>)owner.Memory).Span.Validate();

src/System.Memory/tests/ReadOnlySpan/AsReadOnlySpan.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void EmptyArrayAsReadOnlySpan()
4646
{
4747
int[] empty = Array.Empty<int>();
4848
ReadOnlySpan<int> span = empty.AsReadOnlySpan();
49-
span.Validate();
49+
span.ValidateNonNullEmpty();
5050
}
5151

5252
[Fact]
@@ -86,12 +86,12 @@ public static void ZeroLengthArraySegmentAsReadOnlySpan()
8686
int[] empty = Array.Empty<int>();
8787
ArraySegment<int> emptySegment = new ArraySegment<int>(empty);
8888
ReadOnlySpan<int> span = emptySegment.AsReadOnlySpan();
89-
span.Validate();
89+
span.ValidateNonNullEmpty();
9090

9191
int[] a = { 19, -17 };
9292
ArraySegment<int> segmentInt = new ArraySegment<int>(a, 1, 0);
9393
ReadOnlySpan<int> spanInt = segmentInt.AsReadOnlySpan();
94-
spanInt.Validate();
94+
spanInt.ValidateNonNullEmpty();
9595
}
9696

9797
[Fact]
@@ -108,8 +108,7 @@ public static void StringAsReadOnlySpanEmptyString()
108108
{
109109
string s = "";
110110
ReadOnlySpan<char> span = s.AsReadOnlySpan();
111-
char[] expected = s.ToCharArray();
112-
span.Validate(expected);
111+
span.ValidateNonNullEmpty();
113112
}
114113

115114
[Fact]

src/System.Memory/tests/ReadOnlySpan/CtorArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public static void CtorArrayZeroLength()
6363
ReadOnlySpan<int> span;
6464

6565
span = new ReadOnlySpan<int>(empty);
66-
span.Validate();
66+
span.ValidateNonNullEmpty();
6767

6868
span = new ReadOnlySpan<int>(empty, 0, empty.Length);
69-
span.Validate();
69+
span.ValidateNonNullEmpty();
7070
}
7171

7272
[Fact]

src/System.Memory/tests/ReadOnlySpan/CtorArrayIntInt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void CtorArrayIntIntStartEqualsLength()
7373
// Valid for start to equal the array length. This returns an empty span that starts "just past the array."
7474
int[] a = { 91, 92, 93 };
7575
ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 3, 0);
76-
span.Validate();
76+
span.ValidateNonNullEmpty();
7777
}
7878
}
7979
}

src/System.Memory/tests/ReadOnlySpan/NonPortableCast.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace System.SpanTests
1111
public static partial class ReadOnlySpanTests
1212
{
1313
[Fact]
14-
public static void PortableCastUIntToUShort()
14+
public static void NonPortableCastUIntToUShort()
1515
{
1616
uint[] a = { 0x44332211, 0x88776655 };
1717
ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(a);
@@ -22,14 +22,34 @@ public static void PortableCastUIntToUShort()
2222
}
2323

2424
[Fact]
25-
public static void PortableCastToTypeContainsReferences()
25+
public static void NonPortableCastShortToLong()
26+
{
27+
short[] a = { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
28+
ReadOnlySpan<short> span = new ReadOnlySpan<short>(a);
29+
ReadOnlySpan<long> asLong = span.NonPortableCast<short, long>();
30+
31+
Assert.True(Unsafe.AreSame<long>(ref Unsafe.As<short, long>(ref MemoryMarshal.GetReference(span)), ref MemoryMarshal.GetReference(asLong)));
32+
asLong.Validate<long>(0x4567345623451234);
33+
}
34+
35+
[Fact]
36+
public static unsafe void NonPortableCastOverflow()
37+
{
38+
ReadOnlySpan<TestHelpers.TestStructExplicit> span = new ReadOnlySpan<TestHelpers.TestStructExplicit>(null, Int32.MaxValue);
39+
40+
TestHelpers.AssertThrows<OverflowException, TestHelpers.TestStructExplicit>(span, (_span) => _span.NonPortableCast<TestHelpers.TestStructExplicit, byte>().DontBox());
41+
TestHelpers.AssertThrows<OverflowException, TestHelpers.TestStructExplicit>(span, (_span) => _span.NonPortableCast<TestHelpers.TestStructExplicit, ulong>().DontBox());
42+
}
43+
44+
[Fact]
45+
public static void NonPortableCastToTypeContainsReferences()
2646
{
2747
ReadOnlySpan<uint> span = new ReadOnlySpan<uint>(Array.Empty<uint>());
2848
TestHelpers.AssertThrows<ArgumentException, uint>(span, (_span) => _span.NonPortableCast<uint, StructWithReferences>().DontBox());
2949
}
3050

3151
[Fact]
32-
public static void PortableCastFromTypeContainsReferences()
52+
public static void NonPortableCastFromTypeContainsReferences()
3353
{
3454
ReadOnlySpan<StructWithReferences> span = new ReadOnlySpan<StructWithReferences>(Array.Empty<StructWithReferences>());
3555
TestHelpers.AssertThrows<ArgumentException, StructWithReferences>(span, (_span) => _span.NonPortableCast<StructWithReferences, uint>().DontBox());

src/System.Memory/tests/Span/AsSpan.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void ZeroLengthArrayAsSpan()
3939
{
4040
int[] empty = Array.Empty<int>();
4141
Span<int> span = empty.AsSpan();
42-
span.Validate();
42+
span.ValidateNonNullEmpty();
4343
}
4444

4545
[Fact]
@@ -79,12 +79,12 @@ public static void ZeroLengthArraySegmentAsSpan()
7979
int[] empty = Array.Empty<int>();
8080
ArraySegment<int> segmentEmpty = new ArraySegment<int>(empty);
8181
Span<int> spanEmpty = segmentEmpty.AsSpan();
82-
spanEmpty.Validate();
82+
spanEmpty.ValidateNonNullEmpty();
8383

8484
int[] a = { 91, 92, -93, 94 };
8585
ArraySegment<int> segmentInt = new ArraySegment<int>(a, 0, 0);
8686
Span<int> spanInt = segmentInt.AsSpan();
87-
spanInt.Validate();
87+
spanInt.ValidateNonNullEmpty();
8888
}
8989
}
9090
}

src/System.Memory/tests/Span/CtorArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public static void CtorArrayZeroLength()
6262
Span<int> span;
6363

6464
span = new Span<int>(empty);
65-
span.Validate();
65+
span.ValidateNonNullEmpty();
6666

6767
span = new Span<int>(empty, 0, empty.Length);
68-
span.Validate();
68+
span.ValidateNonNullEmpty();
6969
}
7070

7171
[Fact]

src/System.Memory/tests/Span/CtorArrayIntInt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void CtorArrayIntIntStartEqualsLength()
7373
// Valid for start to equal the array length. This returns an empty span that starts "just past the array."
7474
int[] a = { 91, 92, 93 };
7575
Span<int> span = new Span<int>(a, 3, 0);
76-
span.Validate();
76+
span.ValidateNonNullEmpty();
7777
}
7878
}
7979
}

0 commit comments

Comments
 (0)