1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 23:58:48 +00:00

test: cover Span<char> overloads of CountSubstring

This commit is contained in:
Oliver Booth 2023-03-30 21:22:45 +01:00
parent f360311d9c
commit 4c62367303
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025

View File

@ -9,25 +9,49 @@ public class CharSpanTests
[TestMethod]
public void CountSubstring_ShouldHonor_StringComparison()
{
Assert.AreEqual(0, "Hello World".AsSpan().CountSubstring('E'));
Assert.AreEqual(0, "Hello World".AsSpan().CountSubstring("E".AsSpan()));
Assert.AreEqual(1, "Hello World".AsSpan().CountSubstring("E".AsSpan(), StringComparison.OrdinalIgnoreCase));
var readOnlySpan = "Hello World".AsSpan();
Span<char> span = stackalloc char[readOnlySpan.Length];
readOnlySpan.CopyTo(span);
Assert.AreEqual(0, readOnlySpan.CountSubstring('E'));
Assert.AreEqual(0, readOnlySpan.CountSubstring("E".AsSpan()));
Assert.AreEqual(1, readOnlySpan.CountSubstring("E".AsSpan(), StringComparison.OrdinalIgnoreCase));
Assert.AreEqual(0, span.CountSubstring('E'));
Assert.AreEqual(0, span.CountSubstring("E".AsSpan()));
Assert.AreEqual(1, span.CountSubstring("E".AsSpan(), StringComparison.OrdinalIgnoreCase));
}
[TestMethod]
public void CountSubstring_ShouldReturn0_GivenNoInstanceChar()
{
Assert.AreEqual(0, "Hello World".AsSpan().CountSubstring('z'));
Assert.AreEqual(0, "Hello World".AsSpan().CountSubstring("z".AsSpan()));
Assert.AreEqual(0, "Hello World".AsSpan().CountSubstring("z".AsSpan(), StringComparison.OrdinalIgnoreCase));
var readOnlySpan = "Hello World".AsSpan();
Span<char> span = stackalloc char[readOnlySpan.Length];
readOnlySpan.CopyTo(span);
Assert.AreEqual(0, readOnlySpan.CountSubstring('z'));
Assert.AreEqual(0, readOnlySpan.CountSubstring("z".AsSpan()));
Assert.AreEqual(0, readOnlySpan.CountSubstring("z".AsSpan(), StringComparison.OrdinalIgnoreCase));
Assert.AreEqual(0, span.CountSubstring('z'));
Assert.AreEqual(0, span.CountSubstring("z".AsSpan()));
Assert.AreEqual(0, span.CountSubstring("z".AsSpan(), StringComparison.OrdinalIgnoreCase));
}
[TestMethod]
public void CountSubstring_ShouldReturn1_GivenSingleInstanceChar()
{
Assert.AreEqual(1, "Hello World".AsSpan().CountSubstring('e'));
Assert.AreEqual(1, "Hello World".AsSpan().CountSubstring("e".AsSpan()));
Assert.AreEqual(1, "Hello World".AsSpan().CountSubstring("e".AsSpan(), StringComparison.OrdinalIgnoreCase));
var readOnlySpan = "Hello World".AsSpan();
Span<char> span = stackalloc char[readOnlySpan.Length];
readOnlySpan.CopyTo(span);
Assert.AreEqual(1, readOnlySpan.CountSubstring('e'));
Assert.AreEqual(1, readOnlySpan.CountSubstring("e".AsSpan()));
Assert.AreEqual(1, readOnlySpan.CountSubstring("e".AsSpan(), StringComparison.OrdinalIgnoreCase));
Assert.AreEqual(1, span.CountSubstring('e'));
Assert.AreEqual(1, span.CountSubstring("e".AsSpan()));
Assert.AreEqual(1, span.CountSubstring("e".AsSpan(), StringComparison.OrdinalIgnoreCase));
}
[TestMethod]