Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Nov 8, 2023
1 parent 0eae76c commit 8148f98
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 79 deletions.
145 changes: 73 additions & 72 deletions src/Tests/PolyfillExtensionsTests_Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ partial class PolyfillExtensionsTests
[Test]
public void ReadOnlySpan_ZeroLengthContains()
{
ReadOnlySpan<int> span = new ReadOnlySpan<int>(Array.Empty<int>());
var span = new ReadOnlySpan<int>(Array.Empty<int>());

bool found = span.Contains(0);
var found = span.Contains(0);
Assert.False(found);
}

[Test]
public void ReadOnlySpan_TestContains()
{
for (int length = 0; length < 32; length++)
for (var length = 0; length < 32; length++)
{
int[] a = new int[length];
for (int i = 0; i < length; i++)
var a = new int[length];
for (var i = 0; i < length; i++)
{
a[i] = 10 * (i + 1);
}
ReadOnlySpan<int> span = new ReadOnlySpan<int>(a);
var span = new ReadOnlySpan<int>(a);

for (int targetIndex = 0; targetIndex < length; targetIndex++)
for (var targetIndex = 0; targetIndex < length; targetIndex++)
{
int target = a[targetIndex];
bool found = span.Contains(target);
var target = a[targetIndex];
var found = span.Contains(target);
Assert.True(found);
}
}
Expand All @@ -33,47 +33,48 @@ public void ReadOnlySpan_TestContains()
[Test]
public void ReadOnlySpan_TestMultipleContains()
{
for (int length = 2; length < 32; length++)
for (var length = 2; length < 32; length++)
{
int[] a = new int[length];
for (int i = 0; i < length; i++)
var a = new int[length];
for (var i = 0; i < length; i++)
{
a[i] = 10 * (i + 1);
}

a[length - 1] = 5555;
a[length - 2] = 5555;

ReadOnlySpan<int> span = new ReadOnlySpan<int>(a);
bool found = span.Contains(5555);
var span = new ReadOnlySpan<int>(a);
var found = span.Contains(5555);
Assert.True(found);
}
}

[Test]
public void ReadOnlySpan_ZeroLengthContains_String()
{
ReadOnlySpan<string> span = new ReadOnlySpan<string>(Array.Empty<string>());
bool found = span.Contains("a");
var span = new ReadOnlySpan<string>(Array.Empty<string>());
var found = span.Contains("a");
Assert.False(found);
}

[Test]
public void ReadOnlySpan_TestMatchContains_String()
{
for (int length = 0; length < 32; length++)
for (var length = 0; length < 32; length++)
{
string[] a = new string[length];
for (int i = 0; i < length; i++)
var a = new string[length];
for (var i = 0; i < length; i++)
{
a[i] = (10 * (i + 1)).ToString();
}
ReadOnlySpan<string> span = new ReadOnlySpan<string>(a);

for (int targetIndex = 0; targetIndex < length; targetIndex++)
var span = new ReadOnlySpan<string>(a);

for (var targetIndex = 0; targetIndex < length; targetIndex++)
{
string target = a[targetIndex];
bool found = span.Contains(target);
var target = a[targetIndex];
var found = span.Contains(target);
Assert.True(found);
}
}
Expand All @@ -83,67 +84,67 @@ public void ReadOnlySpan_TestMatchContains_String()
public void ReadOnlySpan_TestNoMatchContains_String()
{
var rnd = new Random(42);
for (int length = 0; length <= byte.MaxValue; length++)
for (var length = 0; length <= byte.MaxValue; length++)
{
string[] a = new string[length];
string target = (rnd.Next(0, 256)).ToString();
for (int i = 0; i < length; i++)
var a = new string[length];
var target = rnd.Next(0, 256).ToString();
for (var i = 0; i < length; i++)
{
string val = (i + 1).ToString();
a[i] = val == target ? (target + 1) : val;
var val = (i + 1).ToString();
a[i] = val == target ? target + 1 : val;
}
ReadOnlySpan<string> span = new ReadOnlySpan<string>(a);
var span = new ReadOnlySpan<string>(a);

bool found = span.Contains(target);
var found = span.Contains(target);
Assert.False(found);
}
}

[Test]
public void ReadOnlySpan_TestMultipleMatchContains_String()
{
for (int length = 2; length < 32; length++)
for (var length = 2; length < 32; length++)
{
string[] a = new string[length];
for (int i = 0; i < length; i++)
var a = new string[length];
for (var i = 0; i < length; i++)
{
a[i] = (10 * (i + 1)).ToString();
}

a[length - 1] = "5555";
a[length - 2] = "5555";

ReadOnlySpan<string> span = new ReadOnlySpan<string>(a);
bool found = span.Contains("5555");
var span = new ReadOnlySpan<string>(a);
var found = span.Contains("5555");
Assert.True(found);
}
}

[Test]
public void Span_ZeroLengthContains()
{
Span<int> span = new Span<int>(Array.Empty<int>());
var span = new Span<int>(Array.Empty<int>());

bool found = span.Contains(0);
var found = span.Contains(0);
Assert.False(found);
}

[Test]
public void Span_TestContains()
{
for (int length = 0; length < 32; length++)
for (var length = 0; length < 32; length++)
{
int[] a = new int[length];
for (int i = 0; i < length; i++)
var a = new int[length];
for (var i = 0; i < length; i++)
{
a[i] = 10 * (i + 1);
}
Span<int> span = new Span<int>(a);
var span = new Span<int>(a);

for (int targetIndex = 0; targetIndex < length; targetIndex++)
for (var targetIndex = 0; targetIndex < length; targetIndex++)
{
int target = a[targetIndex];
bool found = span.Contains(target);
var target = a[targetIndex];
var found = span.Contains(target);
Assert.True(found);
}
}
Expand All @@ -152,47 +153,47 @@ public void Span_TestContains()
[Test]
public void Span_TestMultipleContains()
{
for (int length = 2; length < 32; length++)
for (var length = 2; length < 32; length++)
{
int[] a = new int[length];
for (int i = 0; i < length; i++)
var a = new int[length];
for (var i = 0; i < length; i++)
{
a[i] = 10 * (i + 1);
}

a[length - 1] = 5555;
a[length - 2] = 5555;

Span<int> span = new Span<int>(a);
bool found = span.Contains(5555);
var span = new Span<int>(a);
var found = span.Contains(5555);
Assert.True(found);
}
}

[Test]
public void Span_ZeroLengthContains_String()
{
Span<string> span = new Span<string>(Array.Empty<string>());
bool found = span.Contains("a");
var span = new Span<string>(Array.Empty<string>());
var found = span.Contains("a");
Assert.False(found);
}

[Test]
public void Span_TestMatchContains_String()
{
for (int length = 0; length < 32; length++)
for (var length = 0; length < 32; length++)
{
string[] a = new string[length];
for (int i = 0; i < length; i++)
var a = new string[length];
for (var i = 0; i < length; i++)
{
a[i] = (10 * (i + 1)).ToString();
}
Span<string> span = new Span<string>(a);
var span = new Span<string>(a);

for (int targetIndex = 0; targetIndex < length; targetIndex++)
for (var targetIndex = 0; targetIndex < length; targetIndex++)
{
string target = a[targetIndex];
bool found = span.Contains(target);
var target = a[targetIndex];
var found = span.Contains(target);
Assert.True(found);
}
}
Expand All @@ -202,38 +203,38 @@ public void Span_TestMatchContains_String()
public void Span_TestNoMatchContains_String()
{
var rnd = new Random(42);
for (int length = 0; length <= byte.MaxValue; length++)
for (var length = 0; length <= byte.MaxValue; length++)
{
string[] a = new string[length];
string target = (rnd.Next(0, 256)).ToString();
for (int i = 0; i < length; i++)
var a = new string[length];
var target = rnd.Next(0, 256).ToString();
for (var i = 0; i < length; i++)
{
string val = (i + 1).ToString();
a[i] = val == target ? (target + 1) : val;
var val = (i + 1).ToString();
a[i] = val == target ? target + 1 : val;
}
Span<string> span = new Span<string>(a);
var span = new Span<string>(a);

bool found = span.Contains(target);
var found = span.Contains(target);
Assert.False(found);
}
}

[Test]
public void Span_TestMultipleMatchContains_String()
{
for (int length = 2; length < 32; length++)
for (var length = 2; length < 32; length++)
{
string[] a = new string[length];
for (int i = 0; i < length; i++)
var a = new string[length];
for (var i = 0; i < length; i++)
{
a[i] = (10 * (i + 1)).ToString();
}

a[length - 1] = "5555";
a[length - 2] = "5555";

Span<string> span = new Span<string>(a);
bool found = span.Contains("5555");
var span = new Span<string>(a);
var found = span.Contains("5555");
Assert.True(found);
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Tests/PolyfillExtensionsTests_Task.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// ReSharper disable ArrangeObjectCreationWhenTypeNotEvident
// ReSharper disable MethodSupportsCancellation
partial class PolyfillExtensionsTests
{
private static T? AssertThrowsAsync<T>(string expectedParamName, AsyncTestDelegate action)
where T : ArgumentException
{
T? exception = Assert.ThrowsAsync<T>(action);
var exception = Assert.ThrowsAsync<T>(action);

Assert.AreEqual(expectedParamName, exception?.ParamName);

Expand All @@ -13,7 +15,7 @@ partial class PolyfillExtensionsTests
[Test]
public void Task_WaitAsync_InvalidTimeout_Throws()
{
foreach (TimeSpan timeout in new[] { TimeSpan.FromMilliseconds(-2), TimeSpan.MaxValue, TimeSpan.MinValue })
foreach (var timeout in new[] { TimeSpan.FromMilliseconds(-2), TimeSpan.MaxValue, TimeSpan.MinValue })
{
#if NET5_0_OR_GREATER
AssertThrowsAsync<ArgumentOutOfRangeException>("timeout", async () => await new TaskCompletionSource().Task.WaitAsync(timeout));
Expand Down Expand Up @@ -93,8 +95,8 @@ public void Task_WaitAsync_TimeoutOrCanceled_Throws()
Assert.ThrowsAsync<TimeoutException>(async () => await tcs.Task.WaitAsync(TimeSpan.FromMilliseconds(1)));
Assert.ThrowsAsync<TimeoutException>(async () => await tcs.Task.WaitAsync(TimeSpan.FromMilliseconds(1), cts.Token));

Task assert1 = ((Task)tcs.Task).WaitAsync(cts.Token);
Task assert2 = ((Task)tcs.Task).WaitAsync(Timeout.InfiniteTimeSpan, cts.Token);
var assert1 = ((Task)tcs.Task).WaitAsync(cts.Token);
var assert2 = ((Task)tcs.Task).WaitAsync(Timeout.InfiniteTimeSpan, cts.Token);
Task assert3 = tcs.Task.WaitAsync(cts.Token);
Task assert4 = tcs.Task.WaitAsync(Timeout.InfiniteTimeSpan, cts.Token);
Assert.False(assert1.IsCompleted);
Expand All @@ -112,18 +114,18 @@ public void Task_WaitAsync_TimeoutOrCanceled_Throws()
[Test]
public async Task Task_WaitAsync_NoCancellationOrTimeoutOccurs_Success()
{
CancellationTokenSource cts = new CancellationTokenSource();
var cts = new CancellationTokenSource();

#if NET5_0_OR_GREATER
var tcs = new TaskCompletionSource();
Task t = tcs.Task.WaitAsync(TimeSpan.FromDays(1), cts.Token);
var t = tcs.Task.WaitAsync(TimeSpan.FromDays(1), cts.Token);
Assert.False(t.IsCompleted);
tcs.SetResult();
await t;
#endif

var tcsg = new TaskCompletionSource<int>();
Task<int> tg = tcsg.Task.WaitAsync(TimeSpan.FromDays(1), cts.Token);
var tg = tcsg.Task.WaitAsync(TimeSpan.FromDays(1), cts.Token);
Assert.False(tg.IsCompleted);
tcsg.SetResult(42);
Assert.AreEqual(42, await tg);
Expand Down

0 comments on commit 8148f98

Please sign in to comment.