Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
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
26 changes: 26 additions & 0 deletions src/System.Memory/tests/ReadOnlySpan/CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ public static void TryCopyTo()
Assert.Equal<int>(src, dst);
}

[Fact]
public static void TryCopyToArraySegmentImplicit()
{
int[] src = { 1, 2, 3 };
int[] dst = { 5, 99, 100, 101, 10 };
var segment = new ArraySegment<int>(dst, 1, 3);

ReadOnlySpan<int> srcSpan = new ReadOnlySpan<int>(src);
bool success = srcSpan.TryCopyTo(segment);
Assert.True(success);
Assert.Equal<int>(src, segment);
}

[Fact]
public static void TryCopyToEmpty()
{
int[] src = { };
int[] dst = { 99, 100, 101 };

ReadOnlySpan<int> srcSpan = new ReadOnlySpan<int>(src);
bool success = srcSpan.TryCopyTo(dst);
Assert.True(success);
int[] expected = { 99, 100, 101 };
Assert.Equal<int>(expected, dst);
}

[Fact]
public static void TryCopyToLonger()
{
Expand Down
17 changes: 17 additions & 0 deletions src/System.Memory/tests/ReadOnlySpan/Equality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,22 @@ public static void EmptySpansNotUnified()
Assert.False(left == right);
Assert.False(!(left != right));
}

[Fact]
public static void CannotCallEqualsOnSpan()
{
ReadOnlySpan<int> left = new ReadOnlySpan<int>(new int[0]);
try
{
#pragma warning disable 0618
bool result = left.Equals(new object());
#pragma warning restore 0618
Assert.True(false);
}
catch (Exception ex)
{
Assert.True(ex is NotSupportedException);
}
}
}
}
29 changes: 29 additions & 0 deletions src/System.Memory/tests/ReadOnlySpan/GetHashCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.SpanTests
{
public static partial class ReadOnlySpanTests
{
[Fact]
public static void CannotCallGetHashCodeOnSpan()
{
ReadOnlySpan<int> span = new ReadOnlySpan<int>(new int[0]);

try
{
#pragma warning disable 0618
int result = span.GetHashCode();
#pragma warning restore 0618
Assert.True(false);
}
catch (Exception ex)
{
Assert.True(ex is NotSupportedException);
}
}
}
}
83 changes: 79 additions & 4 deletions src/System.Memory/tests/ReadOnlySpan/IndexOf.byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Numerics;
using Xunit;

namespace System.SpanTests
Expand All @@ -16,10 +17,27 @@ public static void ZeroLengthIndexOf_Byte()
Assert.Equal(-1, idx);
}

[Fact]
public static void DefaultFilledIndexOf_Byte()
{
for (int length = 0; length <= byte.MaxValue; length++)
{
byte[] a = new byte[length];
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);

for (int i = 0; i < length; i++)
{
byte target0 = default(byte);
int idx = span.IndexOf(target0);
Assert.Equal(0, idx);
}
}
}

[Fact]
public static void TestMatch_Byte()
{
for (int length = 0; length < 32; length++)
for (int length = 0; length < byte.MaxValue; length++)
{
byte[] a = new byte[length];
for (int i = 0; i < length; i++)
Expand All @@ -37,15 +55,72 @@ public static void TestMatch_Byte()
}
}

[Fact]
public static void TestNoMatch_Byte()
{
var rnd = new Random(42);
for (int length = 0; length <= byte.MaxValue; length++)
{
byte[] a = new byte[length];
byte target = (byte)rnd.Next(0, 256);
for (int i = 0; i < length; i++)
{
byte val = (byte)(i + 1);
a[i] = val == target ? (byte)(target + 1) : val;
}
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);

int idx = span.IndexOf(target);
Assert.Equal(-1, idx);
}
}

[Fact]
public static void TestAllignmentNoMatch_Byte()
{
byte[] array = new byte[4 * Vector<byte>.Count];
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count);
int idx = span.IndexOf((byte)'1');
Assert.Equal(-1, idx);

span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count - 3);
idx = span.IndexOf((byte)'1');
Assert.Equal(-1, idx);
}
}

[Fact]
public static void TestAllignmentMatch_Byte()
{
byte[] array = new byte[4 * Vector<byte>.Count];
for (int i = 0; i < array.Length; i++)
{
array[i] = 5;
}
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count);
int idx = span.IndexOf(5);
Assert.Equal(0, idx);

span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count - 3);
idx = span.IndexOf(5);
Assert.Equal(0, idx);
}
}

[Fact]
public static void TestMultipleMatch_Byte()
{
for (int length = 2; length < 32; length++)
for (int length = 2; length < byte.MaxValue; length++)
{
byte[] a = new byte[length];
for (int i = 0; i < length; i++)
{
a[i] = (byte)(i + 1);
byte val = (byte)(i + 1);
a[i] = val == 200 ? (byte)201 : val;
}

a[length - 1] = 200;
Expand All @@ -60,7 +135,7 @@ public static void TestMultipleMatch_Byte()
[Fact]
public static void MakeSureNoChecksGoOutOfRange_Byte()
{
for (int length = 0; length < 100; length++)
for (int length = 0; length < byte.MaxValue; length++)
{
byte[] a = new byte[length + 2];
a[0] = 99;
Expand Down
21 changes: 21 additions & 0 deletions src/System.Memory/tests/ReadOnlySpan/SequenceEqual.byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ public static void SameSpanSequenceEqual_Byte()
Assert.True(b);
}

[Fact]
public static void SequenceEqualArrayImplicit_Byte()
{
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> first = new ReadOnlySpan<byte>(a, 0, 3);
bool b = first.SequenceEqual(a);
Assert.True(b);
}

[Fact]
public static void SequenceEqualArraySegmentImplicit_Byte()
{
byte[] src = { 1, 2, 3 };
byte[] dst = { 5, 1, 2, 3, 10 };
var segment = new ArraySegment<byte>(dst, 1, 3);

ReadOnlySpan<byte> first = new ReadOnlySpan<byte>(src, 0, 3);
bool b = first.SequenceEqual(segment);
Assert.True(b);
}

[Fact]
public static void LengthMismatchSequenceEqual_Byte()
{
Expand Down
26 changes: 26 additions & 0 deletions src/System.Memory/tests/Span/Clear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public static void ClearValueTypeWithoutReferencesLonger()
Assert.Equal<int>(expected, actual);
}

[Fact]
public static void ClearValueTypeWithoutReferencesPointerSize()
{
long[] actual = new long[15];
for (int i = 0; i < actual.Length; i++)
{
actual[i] = i + 1;
}
long[] expected = new long[actual.Length];

var span = new Span<long>(actual);
span.Clear();
Assert.Equal<long>(expected, actual);
}

[Fact]
public static void ClearReferenceType()
{
Expand All @@ -175,6 +190,17 @@ public static void ClearReferenceTypeLonger()
Assert.Equal<string>(expected, actual);
}

[Fact]
public static void ClearEnumType()
{
TestEnum[] actual = {TestEnum.e0, TestEnum.e1, TestEnum.e2};
TestEnum[] expected = {default(TestEnum), default(TestEnum), default(TestEnum) };

var span = new Span<TestEnum>(actual);
span.Clear();
Assert.Equal<TestEnum>(expected, actual);
}

[Fact]
public static void ClearValueTypeWithReferences()
{
Expand Down
26 changes: 26 additions & 0 deletions src/System.Memory/tests/Span/CopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ public static void TryCopyTo()
Assert.Equal<int>(src, dst);
}

[Fact]
public static void TryCopyToArraySegmentImplicit()
{
int[] src = { 1, 2, 3 };
int[] dst = { 5, 99, 100, 101, 10 };
var segment = new ArraySegment<int>(dst, 1, 3);

Span<int> srcSpan = new Span<int>(src);
bool success = srcSpan.TryCopyTo(segment);
Assert.True(success);
Assert.Equal<int>(src, segment);
}

[Fact]
public static void TryCopyToEmpty()
{
int[] src = {};
int[] dst = { 99, 100, 101 };

Span<int> srcSpan = new Span<int>(src);
bool success = srcSpan.TryCopyTo(dst);
Assert.True(success);
int[] expected = { 99, 100, 101 };
Assert.Equal<int>(expected, dst);
}

[Fact]
public static void TryCopyToLonger()
{
Expand Down
18 changes: 18 additions & 0 deletions src/System.Memory/tests/Span/Equality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,23 @@ public static void EmptySpansNotUnified()
Assert.False(left == right);
Assert.False(!(left != right));
}

[Fact]
public static void CannotCallEqualsOnSpan()
{
Span<int> left = new Span<int>(new int[0]);

try
{
#pragma warning disable 0618
bool result = left.Equals(new object());
#pragma warning restore 0618
Assert.True(false);
}
catch (Exception ex)
{
Assert.True(ex is NotSupportedException);
}
}
}
}
Loading