From 95cd3e8cbcd0e0eb982acdee8536f19b4fa0a3e3 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 2 Apr 2023 00:36:31 +0100 Subject: [PATCH] test: bring coverage to 100% for X10D.Collections.SpanExtensions --- X10D.Tests/src/Collections/SpanTest.cs | 391 ++++++++++++++++++++++++- 1 file changed, 378 insertions(+), 13 deletions(-) diff --git a/X10D.Tests/src/Collections/SpanTest.cs b/X10D.Tests/src/Collections/SpanTest.cs index 106bf80..7506ca9 100644 --- a/X10D.Tests/src/Collections/SpanTest.cs +++ b/X10D.Tests/src/Collections/SpanTest.cs @@ -16,6 +16,16 @@ public class SpanTest Assert.AreEqual(0, count); } + [TestMethod] + public void Count_ShouldReturn0_GivenEmptyReadOnlySpan() + { + ReadOnlySpan span = ReadOnlySpan.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 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 span = ReadOnlySpan.Empty; @@ -41,7 +61,49 @@ public class SpanTest } [TestMethod] - public void Split_OnOneWord_ShouldYieldLength1() + public void Split_OnEmptySpan_ShouldYieldNothing_UsingCharDelimiter_GivenSpan() + { + Span span = Span.Empty; + + var index = 0; + foreach (ReadOnlySpan unused in span.Split(' ')) + { + index++; + } + + Assert.AreEqual(0, index); + } + + [TestMethod] + public void Split_OnEmptySpan_ShouldYieldNothing_UsingStringDelimiter_GivenReadOnlySpan() + { + ReadOnlySpan span = ReadOnlySpan.Empty; + + var index = 0; + foreach (ReadOnlySpan unused in span.Split(" ")) + { + index++; + } + + Assert.AreEqual(0, index); + } + + [TestMethod] + public void Split_OnEmptySpan_ShouldYieldNothing_UsingStringDelimiter_GivenSpan() + { + Span span = Span.Empty; + + var index = 0; + foreach (ReadOnlySpan unused in span.Split(" ")) + { + index++; + } + + Assert.AreEqual(0, index); + } + + [TestMethod] + public void Split_OnOneWord_ShouldYieldLength1_UsingCharDelimiter_GivenReadOnlySpan() { ReadOnlySpan span = "Hello ".AsSpan(); @@ -60,9 +122,11 @@ public class SpanTest } [TestMethod] - public void Split_OnTwoWords_ShouldYieldLength2() + public void Split_OnOneWord_ShouldYieldLength1_UsingCharDelimiter_GivenSpan() { - ReadOnlySpan span = "Hello World ".AsSpan(); + ReadOnlySpan source = "Hello ".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); var index = 0; foreach (ReadOnlySpan 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 span = "Hello ".AsSpan(); + + var index = 0; + foreach (ReadOnlySpan subSpan in span.Split(" ")) + { + if (index == 0) { - Assert.AreEqual("World", subSpan.ToString()); + Assert.AreEqual("Hello", subSpan.ToString()); + } + + index++; + } + + Assert.AreEqual(1, index); + } + + [TestMethod] + public void Split_OnOneWord_ShouldYieldLength1_UsingStringDelimiter_GivenSpan() + { + ReadOnlySpan source = "Hello ".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan 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 span = "Hello".AsSpan(); + + var index = 0; + foreach (ReadOnlySpan 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 source = "Hello".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan 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 span = "Hello".AsSpan(); + + var index = 0; + foreach (ReadOnlySpan 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 source = "Hello".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan 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 span = "Hello World ".AsSpan(); + + var index = 0; + foreach (ReadOnlySpan 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 source = "Hello World ".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan 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 span = "Hello World ".AsSpan(); + + var index = 0; + foreach (ReadOnlySpan 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 source = "Hello World ".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan 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 span = "Hello, the World ".AsSpan(); var index = 0; foreach (ReadOnlySpan subSpan in span.Split(' ')) { - if (index == 0) + switch (index) { - Assert.AreEqual("Hello,", subSpan.ToString()); + case 0: + Assert.AreEqual("Hello,", subSpan.ToString()); + break; + case 1: + Assert.AreEqual("the", subSpan.ToString()); + break; + case 2: + Assert.AreEqual("World", subSpan.ToString()); + break; } - else if (index == 1) + + index++; + } + + Assert.AreEqual(3, index); + } + + [TestMethod] + public void Split_OnThreeWords_ShouldYieldLength3_UsingCharDelimiter_GivenSpan() + { + ReadOnlySpan source = "Hello, the World ".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan subSpan in span.Split(' ')) + { + switch (index) { - Assert.AreEqual("the", subSpan.ToString()); + case 0: + Assert.AreEqual("Hello,", subSpan.ToString()); + break; + case 1: + Assert.AreEqual("the", subSpan.ToString()); + break; + case 2: + Assert.AreEqual("World", subSpan.ToString()); + break; } - else if (index == 2) + + index++; + } + + Assert.AreEqual(3, index); + } + + [TestMethod] + public void Split_OnThreeWords_ShouldYieldLength3_UsingStringDelimiter_GivenReadOnlySpan() + { + ReadOnlySpan span = "Hello, the World ".AsSpan(); + + var index = 0; + foreach (ReadOnlySpan subSpan in span.Split(" ")) + { + switch (index) { - Assert.AreEqual("World", subSpan.ToString()); + 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 source = "Hello, the World ".AsSpan(); + Span span = stackalloc char[source.Length]; + source.CopyTo(span); + + var index = 0; + foreach (ReadOnlySpan 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++;