Skip to content

Commit

Permalink
Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clipperhouse committed Jul 30, 2024
1 parent 8bd1246 commit fa9be85
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 216 deletions.
24 changes: 24 additions & 0 deletions Split/SplitEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,28 @@ internal SplitEnumerator(ReadOnlySpan<T> source, SearchValues<T> searchValues)
public bool MoveNext() => _enumerator.MoveNext();
public ReadOnlySpan<T> Current => _enumerator._span[_enumerator._startCurrent.._enumerator._endCurrent];
public SplitEnumerator<T> GetEnumerator() => this;


/// <summary>
/// Iterate over all tokens and collects them into a list, allocating a new array for each token.
/// </summary>
/// <returns>List<byte[]> or List<char[]>, depending on the input</returns>
public List<T[]> ToList()
{
var result = new List<T[]>();
foreach (var token in this)
{
result.Add(token.ToArray());
}
return result;
}

/// <summary>
/// Iterates over all tokens and collects them into an array, allocating a new array for each token.
/// </summary>
/// <returns>byte[][] or char[][], depending on the input</returns>
public T[][] ToArray()
{
return this.ToList().ToArray();
}
}
216 changes: 0 additions & 216 deletions Tests/Split.T.cs

This file was deleted.

56 changes: 56 additions & 0 deletions Tests/Split.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Tests;

using System.Text;
using Split.Extensions;
using Xunit;

public class SplitTests
{
static readonly string abcd = "abc𝚫";
static readonly string emoji = "👍🐶";
static readonly string efghi = "éfghi";

static string RandomString(int length)
{
var b = new byte[length];
return Encoding.UTF8.GetString(b);
}

readonly Random rnd = new();

static readonly string[] testSeparators = ["", " ", ",", ", ", "🐶", "𝚫", "🌏👍", abcd, emoji, efghi, RandomString(4)];

static IEnumerable<string> TestStrings()
{
yield return "";
yield return abcd;
yield return emoji;
yield return efghi;

foreach (string sep in testSeparators)
{
yield return sep;
var center = abcd + sep + emoji + sep + efghi;
yield return center;
yield return sep + center;
yield return center + sep;
yield return sep + center + sep;
yield return RandomString(15);
}
}

[Fact]
public void Strings()
{
foreach (string s in TestStrings())
{
foreach (var sep in testSeparators)
{
var got = s.SplitOn(sep).ToList().Select(g => new string(g));
var expected = s.Split(sep);

Assert.Equal(expected, got);
}
}
}
}

0 comments on commit fa9be85

Please sign in to comment.