Skip to content

Commit e6c14ce

Browse files
committed
feat(ReadOnlySequence): TryFillSpan method interface and tests
1 parent f73de91 commit e6c14ce

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

src/msgpack.spec/MsgPackSpec.ExtensionMethods.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static T GetFirst<T>(this ReadOnlySequence<T> ros)
2828
return memory.Span[0];
2929
}
3030

31-
throw new InvalidOperationException("We should never get here, because it means that non-empty sequence is empty.")
31+
throw new InvalidOperationException("We should never get here, because it means that non-empty sequence is empty.");
3232
}
3333

3434
/// <summary>
@@ -38,11 +38,10 @@ public static T GetFirst<T>(this ReadOnlySequence<T> ros)
3838
/// <param name="ros">Sequence of elements</param>
3939
/// <param name="destination">Span to copy data to.</param>
4040
/// <typeparam name="T">Type of element</typeparam>
41-
/// <returns><c>true</c> if copy successful, <c>false</c> otherwise.</returns>
41+
/// <returns><c>true</c> if span is fully filled, <c>false</c> otherwise.</returns>
4242
public static bool TryFillSpan<T>(this ReadOnlySequence<T> ros, Span<T> destination)
4343
{
4444
if (destination.IsEmpty) return true;
45-
if (ros.IsSingleSegment) return ros.First.Span.TryCopyTo(destination);
4645

4746
var span = destination;
4847
foreach (var memory in ros)

tests/msgpack.spec.tests/ReadOnlySequence/MultipleSegments/GetFirst.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ProGaudi.MsgPack.Tests.ReadOnlySequence.MultipleSegments
66
{
7-
public class PublicExtensions
7+
public class GetFirst
88
{
99
[Theory]
1010
[InlineData(new byte[] {1})]
@@ -32,7 +32,7 @@ public void GetFirstNonByteNonEmpty(int[] data)
3232
[Fact]
3333
public void GetFirstNonByteEmpty()
3434
{
35-
var e = Should.Throw<IndexOutOfRangeException>(() => System.Array.Empty<byte>().ToMultipleSegments().GetFirst());
35+
var e = Should.Throw<IndexOutOfRangeException>(() => System.Array.Empty<int>().ToMultipleSegments().GetFirst());
3636
e.Message.ShouldBe("ReadOnlySequence is too short. Expected: 1, actual length: 0");
3737
}
3838
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace ProGaudi.MsgPack.Tests.ReadOnlySequence.MultipleSegments
6+
{
7+
public class TryFillSpan
8+
{
9+
[Theory]
10+
[InlineData(new byte[] {1})]
11+
[InlineData(new byte[] {byte.MaxValue, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})]
12+
public void TryFillSpanByteNonEmpty(byte[] data)
13+
{
14+
var destination = new byte[5];
15+
data.ToMultipleSegments().TryFillSpan(destination).ShouldBe(data.Length >= destination.Length);
16+
var copiedLength = Math.Min(data.Length, destination.Length);
17+
destination.AsSpan().Slice(0, copiedLength).ToArray().ShouldBe(data.AsSpan().Slice(0, copiedLength).ToArray());
18+
}
19+
20+
[Fact]
21+
public void TryFillSpanByteEmpty()
22+
{
23+
var empty = System.Array.Empty<byte>();
24+
empty.ToMultipleSegments().TryFillSpan(empty).ShouldBeTrue();
25+
}
26+
27+
[Theory]
28+
[InlineData(new[] {1})]
29+
[InlineData(new[] {int.MaxValue, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})]
30+
public void TryFillSpanNonByteNonEmpty(int[] data)
31+
{
32+
var destination = new int[5];
33+
data.ToMultipleSegments().TryFillSpan(destination).ShouldBe(data.Length >= destination.Length);
34+
var copiedLength = Math.Min(data.Length, destination.Length);
35+
destination.AsSpan().Slice(0, copiedLength).ToArray().ShouldBe(data.AsSpan().Slice(0, copiedLength).ToArray());
36+
}
37+
38+
[Fact]
39+
public void TryFillSpanNonByteEmpty()
40+
{
41+
var empty = System.Array.Empty<byte>();
42+
empty.ToMultipleSegments().TryFillSpan(empty).ShouldBeTrue();
43+
}
44+
}
45+
}

tests/msgpack.spec.tests/ReadOnlySequence/SingleSegment/GetFirst.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ProGaudi.MsgPack.Tests.ReadOnlySequence.SingleSegment
66
{
7-
public class PublicExtensions
7+
public class GetFirst
88
{
99
[Theory]
1010
[InlineData(new byte[] {1})]
@@ -32,7 +32,7 @@ public void GetFirstNonByteNonEmpty(int[] data)
3232
[Fact]
3333
public void GetFirstNonByteEmpty()
3434
{
35-
var e = Should.Throw<IndexOutOfRangeException>(() => System.Array.Empty<byte>().ToSingleSegment().GetFirst());
35+
var e = Should.Throw<IndexOutOfRangeException>(() => System.Array.Empty<int>().ToSingleSegment().GetFirst());
3636
e.Message.ShouldBe("ReadOnlySequence is too short. Expected: 1, actual length: 0");
3737
}
3838
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace ProGaudi.MsgPack.Tests.ReadOnlySequence.SingleSegment
6+
{
7+
public class TryFillSpan
8+
{
9+
[Theory]
10+
[InlineData(new byte[] {1})]
11+
[InlineData(new byte[] {byte.MaxValue, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})]
12+
public void TryFillSpanByteNonEmpty(byte[] data)
13+
{
14+
var destination = new byte[5];
15+
data.ToSingleSegment().TryFillSpan(destination).ShouldBe(data.Length >= destination.Length);
16+
var copiedLength = Math.Min(data.Length, destination.Length);
17+
destination.AsSpan().Slice(0, copiedLength).ToArray().ShouldBe(data.AsSpan().Slice(0, copiedLength).ToArray());
18+
}
19+
20+
[Fact]
21+
public void TryFillSpanByteEmpty()
22+
{
23+
var empty = System.Array.Empty<byte>();
24+
empty.ToSingleSegment().TryFillSpan(empty).ShouldBeTrue();
25+
}
26+
27+
[Theory]
28+
[InlineData(new[] {1})]
29+
[InlineData(new[] {int.MaxValue, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})]
30+
public void TryFillSpanNonByteNonEmpty(int[] data)
31+
{
32+
var destination = new int[5];
33+
data.ToSingleSegment().TryFillSpan(destination).ShouldBe(data.Length >= destination.Length);
34+
var copiedLength = Math.Min(data.Length, destination.Length);
35+
destination.AsSpan().Slice(0, copiedLength).ToArray().ShouldBe(data.AsSpan().Slice(0, copiedLength).ToArray());
36+
}
37+
38+
[Fact]
39+
public void TryFillSpanNonByteEmpty()
40+
{
41+
var empty = System.Array.Empty<byte>();
42+
empty.ToSingleSegment().TryFillSpan(empty).ShouldBeTrue();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)