1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

test: bring coverage to 100% for X10D.Collections.SpanExtensions

This commit is contained in:
Oliver Booth 2023-04-02 00:36:31 +01:00
parent f10ff4a36c
commit 95cd3e8cbc
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025

View File

@ -16,6 +16,16 @@ public class SpanTest
Assert.AreEqual(0, count);
}
[TestMethod]
public void Count_ShouldReturn0_GivenEmptyReadOnlySpan()
{
ReadOnlySpan<int> span = ReadOnlySpan<int>.Empty;
int count = span.Count(2);
Assert.AreEqual(0, count);
}
[TestMethod]
public void Count_ShouldReturn8_GivenSpanWith8MatchingElements()
{
@ -27,7 +37,17 @@ public class SpanTest
}
[TestMethod]
public void Split_OnEmptySpan_ShouldYieldNothing()
public void Count_ShouldReturn8_GivenReadOnlySpanWith8MatchingElements()
{
ReadOnlySpan<int> span = stackalloc int[16] {1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2};
int count = span.Count(2);
Assert.AreEqual(8, count);
}
[TestMethod]
public void Split_OnEmptySpan_ShouldYieldNothing_UsingCharDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = ReadOnlySpan<char>.Empty;
@ -41,7 +61,49 @@ public class SpanTest
}
[TestMethod]
public void Split_OnOneWord_ShouldYieldLength1()
public void Split_OnEmptySpan_ShouldYieldNothing_UsingCharDelimiter_GivenSpan()
{
Span<char> span = Span<char>.Empty;
var index = 0;
foreach (ReadOnlySpan<char> unused in span.Split(' '))
{
index++;
}
Assert.AreEqual(0, index);
}
[TestMethod]
public void Split_OnEmptySpan_ShouldYieldNothing_UsingStringDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = ReadOnlySpan<char>.Empty;
var index = 0;
foreach (ReadOnlySpan<char> unused in span.Split(" "))
{
index++;
}
Assert.AreEqual(0, index);
}
[TestMethod]
public void Split_OnEmptySpan_ShouldYieldNothing_UsingStringDelimiter_GivenSpan()
{
Span<char> span = Span<char>.Empty;
var index = 0;
foreach (ReadOnlySpan<char> unused in span.Split(" "))
{
index++;
}
Assert.AreEqual(0, index);
}
[TestMethod]
public void Split_OnOneWord_ShouldYieldLength1_UsingCharDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello ".AsSpan();
@ -60,9 +122,11 @@ public class SpanTest
}
[TestMethod]
public void Split_OnTwoWords_ShouldYieldLength2()
public void Split_OnOneWord_ShouldYieldLength1_UsingCharDelimiter_GivenSpan()
{
ReadOnlySpan<char> span = "Hello World ".AsSpan();
ReadOnlySpan<char> source = "Hello ".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
@ -71,9 +135,149 @@ public class SpanTest
{
Assert.AreEqual("Hello", subSpan.ToString());
}
else if (index == 1)
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnOneWord_ShouldYieldLength1_UsingStringDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello ".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
if (index == 0)
{
Assert.AreEqual("Hello", subSpan.ToString());
}
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnOneWord_ShouldYieldLength1_UsingStringDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello ".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
if (index == 0)
{
Assert.AreEqual("Hello", subSpan.ToString());
}
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnOneWordWithoutDelimiter_ShouldYieldLength1_UsingCharDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
{
if (index == 0)
{
Assert.AreEqual("Hello", subSpan.ToString());
}
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnOneWordWithoutDelimiter_ShouldYieldLength1_UsingCharDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
{
if (index == 0)
{
Assert.AreEqual("Hello", subSpan.ToString());
}
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnOneWordWithoutDelimiter_ShouldYieldLength1_UsingStringDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
if (index == 0)
{
Assert.AreEqual("Hello", subSpan.ToString());
}
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnOneWordWithoutDelimiter_ShouldYieldLength1_UsingStringDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
if (index == 0)
{
Assert.AreEqual("Hello", subSpan.ToString());
}
index++;
}
Assert.AreEqual(1, index);
}
[TestMethod]
public void Split_OnTwoWords_ShouldYieldLength2_UsingCharDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello World ".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello", subSpan.ToString());
break;
case 1:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
@ -83,24 +287,185 @@ public class SpanTest
}
[TestMethod]
public void Split_OnThreeWords_ShouldYieldLength3()
public void Split_OnTwoWords_ShouldYieldLength2_UsingCharDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello World ".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello", subSpan.ToString());
break;
case 1:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
}
Assert.AreEqual(2, index);
}
[TestMethod]
public void Split_OnTwoWords_ShouldYieldLength2_UsingStringDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello World ".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello", subSpan.ToString());
break;
case 1:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
}
Assert.AreEqual(2, index);
}
[TestMethod]
public void Split_OnTwoWords_ShouldYieldLength2_UsingStringDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello World ".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello", subSpan.ToString());
break;
case 1:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
}
Assert.AreEqual(2, index);
}
[TestMethod]
public void Split_OnThreeWords_ShouldYieldLength3_UsingCharDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello, the World ".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
{
if (index == 0)
switch (index)
{
case 0:
Assert.AreEqual("Hello,", subSpan.ToString());
}
else if (index == 1)
{
break;
case 1:
Assert.AreEqual("the", subSpan.ToString());
}
else if (index == 2)
{
break;
case 2:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
}
Assert.AreEqual(3, index);
}
[TestMethod]
public void Split_OnThreeWords_ShouldYieldLength3_UsingCharDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello, the World ".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(' '))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello,", subSpan.ToString());
break;
case 1:
Assert.AreEqual("the", subSpan.ToString());
break;
case 2:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
}
Assert.AreEqual(3, index);
}
[TestMethod]
public void Split_OnThreeWords_ShouldYieldLength3_UsingStringDelimiter_GivenReadOnlySpan()
{
ReadOnlySpan<char> span = "Hello, the World ".AsSpan();
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello,", subSpan.ToString());
break;
case 1:
Assert.AreEqual("the", subSpan.ToString());
break;
case 2:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;
}
Assert.AreEqual(3, index);
}
[TestMethod]
public void Split_OnThreeWords_ShouldYieldLength3_UsingStringDelimiter_GivenSpan()
{
ReadOnlySpan<char> source = "Hello, the World ".AsSpan();
Span<char> span = stackalloc char[source.Length];
source.CopyTo(span);
var index = 0;
foreach (ReadOnlySpan<char> subSpan in span.Split(" "))
{
switch (index)
{
case 0:
Assert.AreEqual("Hello,", subSpan.ToString());
break;
case 1:
Assert.AreEqual("the", subSpan.ToString());
break;
case 2:
Assert.AreEqual("World", subSpan.ToString());
break;
}
index++;