Skip to content

Commit

Permalink
Spans.cs: Allow empty spans.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-lucas6 committed Aug 29, 2022
1 parent f5d6729 commit 3d68552
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
23 changes: 23 additions & 0 deletions src/Geralt.Tests/SpansTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,27 @@ public void Concat_SixSpans()
Span<byte> expected = Concat(Array1, Array2, Array3, Array4, Array5, Array6);
Assert.IsTrue(concatenated.SequenceEqual(expected));
}

[TestMethod]
public void Concat_TwoSpansBothEmpty()
{
Span<byte> empty1 = Span<byte>.Empty;
Span<byte> empty2 = Span<byte>.Empty;
Span<byte> concatenated = stackalloc byte[empty1.Length + empty2.Length];
Spans.Concat(concatenated, empty1, empty2);
Span<byte> expected = Concat(empty1.ToArray(), empty2.ToArray());
Assert.IsTrue(concatenated.SequenceEqual(expected));
}

[TestMethod]
public void Concat_TwoSpansOneEmpty()
{
Span<byte> empty = Span<byte>.Empty;
Span<byte> concatenated = stackalloc byte[empty.Length + Array2.Length];
Spans.Concat(concatenated, empty, Array2);
Span<byte> expected = Concat(empty.ToArray(), Array2);
Assert.IsTrue(concatenated.SequenceEqual(expected));
Spans.Concat(concatenated, Array2, empty);
Assert.IsTrue(concatenated.SequenceEqual(expected));
}
}
20 changes: 0 additions & 20 deletions src/Geralt/Helpers/Spans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public static void Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<
checked
{
Validation.EqualToSize(nameof(buffer), buffer.Length, a.Length + b.Length);
Validation.NotEmpty(nameof(a), a.Length);
Validation.NotEmpty(nameof(b), b.Length);
Validation.EqualToSize(nameof(a), a.Length, buffer.Length - b.Length);
Validation.EqualToSize(nameof(b), b.Length, buffer.Length - a.Length);
a.CopyTo(buffer.Slice(start: 0, a.Length));
Expand All @@ -21,9 +19,6 @@ public static void Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<
checked
{
Validation.EqualToSize(nameof(buffer), buffer.Length, a.Length + b.Length + c.Length);
Validation.NotEmpty(nameof(a), a.Length);
Validation.NotEmpty(nameof(b), b.Length);
Validation.NotEmpty(nameof(c), c.Length);
Validation.EqualToSize(nameof(a), a.Length, buffer.Length - b.Length - c.Length);
Validation.EqualToSize(nameof(b), b.Length, buffer.Length - a.Length - c.Length);
Validation.EqualToSize(nameof(c), c.Length, buffer.Length - a.Length - b.Length);
Expand All @@ -38,10 +33,6 @@ public static void Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<
checked
{
Validation.EqualToSize(nameof(buffer), buffer.Length, a.Length + b.Length + c.Length + d.Length);
Validation.NotEmpty(nameof(a), a.Length);
Validation.NotEmpty(nameof(b), b.Length);
Validation.NotEmpty(nameof(c), c.Length);
Validation.NotEmpty(nameof(d), d.Length);
Validation.EqualToSize(nameof(a), a.Length, buffer.Length - b.Length - c.Length - d.Length);
Validation.EqualToSize(nameof(b), b.Length, buffer.Length - a.Length - c.Length - d.Length);
Validation.EqualToSize(nameof(c), c.Length, buffer.Length - a.Length - b.Length - d.Length);
Expand All @@ -58,11 +49,6 @@ public static void Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<
checked
{
Validation.EqualToSize(nameof(buffer), buffer.Length, a.Length + b.Length + c.Length + d.Length + e.Length);
Validation.NotEmpty(nameof(a), a.Length);
Validation.NotEmpty(nameof(b), b.Length);
Validation.NotEmpty(nameof(c), c.Length);
Validation.NotEmpty(nameof(d), d.Length);
Validation.NotEmpty(nameof(e), e.Length);
Validation.EqualToSize(nameof(a), a.Length, buffer.Length - b.Length - c.Length - d.Length - e.Length);
Validation.EqualToSize(nameof(b), b.Length, buffer.Length - a.Length - c.Length - d.Length - e.Length);
Validation.EqualToSize(nameof(c), c.Length, buffer.Length - a.Length - b.Length - d.Length - e.Length);
Expand All @@ -81,12 +67,6 @@ public static void Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<
checked
{
Validation.EqualToSize(nameof(buffer), buffer.Length, a.Length + b.Length + c.Length + d.Length + e.Length + f.Length);
Validation.NotEmpty(nameof(a), a.Length);
Validation.NotEmpty(nameof(b), b.Length);
Validation.NotEmpty(nameof(c), c.Length);
Validation.NotEmpty(nameof(d), d.Length);
Validation.NotEmpty(nameof(e), e.Length);
Validation.NotEmpty(nameof(f), f.Length);
Validation.EqualToSize(nameof(a), a.Length, buffer.Length - b.Length - c.Length - d.Length - e.Length - f.Length);
Validation.EqualToSize(nameof(b), b.Length, buffer.Length - a.Length - c.Length - d.Length - e.Length - f.Length);
Validation.EqualToSize(nameof(c), c.Length, buffer.Length - a.Length - b.Length - d.Length - e.Length - f.Length);
Expand Down

0 comments on commit 3d68552

Please sign in to comment.