Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/PbfLite.Benchmark/PbfBlockWriterCollectionsBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Linq;

namespace PbfLite.Benchmark;
Expand All @@ -13,15 +14,15 @@ public class PbfBlockWriterCollectionsBenchmarks
public int WriteSmallSingleCollection()
{
var writer = PbfBlockWriter.Create(buffer);
writer.WriteSingleCollection(SingleValuesSmall);
writer.WriteSingleCollection(SingleValuesSmall.AsSpan());
return writer.Position;
}

[Benchmark]
public int WriteLargeSingleCollection()
{
var writer = PbfBlockWriter.Create(buffer);
writer.WriteSingleCollection(SingleValuesLarge);
writer.WriteSingleCollection(SingleValuesLarge.AsSpan());
return writer.Position;
}
}
2 changes: 1 addition & 1 deletion src/PbfLite.Tests/CollectionsRoundTripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void UIntCollection_RoundTrip_SingleElement()

// Write
var writer = PbfBlockWriter.Create(buffer);
writer.WriteUIntCollection(originalData);
writer.WriteUIntCollection(originalData.AsSpan());

// Read
var reader = PbfBlockReader.Create(writer.Block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void StreamReader_WithCollections_MatchesBlockReader()

// Write a collection
uint[] testData = { 10, 20, 30, 40, 50 };
writer.WriteUIntCollection(testData);
writer.WriteUIntCollection(testData.AsSpan());

// Read with BlockReader
var blockReader = PbfBlockReader.Create(writer.Block);
Expand Down
102 changes: 101 additions & 1 deletion src/PbfLite.Tests/PbfBlockWriterTests.Collections.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand All @@ -18,7 +19,7 @@ public void WriteUIntCollection_WritesLongCollection()
var expectedBytes = encodedLength.Concat(expectedContent).ToArray();

var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);
writer.WriteUIntCollection(items);
writer.WriteUIntCollection(items.AsSpan());

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}
Expand All @@ -34,6 +35,17 @@ public void WriteUIntCollection_MultipleElements_WritesDataWithLengthPrefix(uint
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new uint[] { 0, 128, 16384, 2097152, 268435456, 4294967295 }, new byte[] { 0x14, 0x00, 0x80, 0x01, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x80, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F })]
public void WriteUIntCollection_WithIEnumerable_WritesDataWithLengthPrefix(uint[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteUIntCollection((IEnumerable<uint>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new ulong[] { 0, 1, 18446744073709551615UL }, new byte[] { 0x0C, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 })]
public void WriteULongCollection_MultipleElements_WritesDataWithLengthPrefix(ulong[] items, byte[] expectedBytes)
Expand All @@ -45,6 +57,17 @@ public void WriteULongCollection_MultipleElements_WritesDataWithLengthPrefix(ulo
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new ulong[] { 0, 1, 18446744073709551615UL }, new byte[] { 0x0C, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 })]
public void WriteULongCollection_WithIEnumerable_WritesDataWithLengthPrefix(ulong[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteULongCollection((IEnumerable<ulong>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new int[] { 0, 1, -1, 2147483647, -2147483648 }, new byte[] { 0x11, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x80, 0x80, 0x80, 0x08 })]
public void WriteIntCollection_MultipleElements_WritesDataWithLengthPrefix(int[] items, byte[] expectedBytes)
Expand All @@ -56,6 +79,17 @@ public void WriteIntCollection_MultipleElements_WritesDataWithLengthPrefix(int[]
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new int[] { 0, 1, -1, 2147483647, -2147483648 }, new byte[] { 0x11, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x80, 0x80, 0x80, 0x08 })]
public void WriteIntCollection_WithIEnumerable_WritesDataWithLengthPrefix(int[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteIntCollection((IEnumerable<int>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new int[] { 0, -1, 1, 2147483647, -2147483648 }, new byte[] { 0x0D, 0x00, 0x01, 0x02, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F })]
public void WriteSignedIntCollection_MultipleElements_WritesDataWithLengthPrefix(int[] items, byte[] expectedBytes)
Expand All @@ -67,6 +101,17 @@ public void WriteSignedIntCollection_MultipleElements_WritesDataWithLengthPrefix
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new int[] { 0, -1, 1, 2147483647, -2147483648 }, new byte[] { 0x0D, 0x00, 0x01, 0x02, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F })]
public void WriteSignedIntCollection_WithIEnumerable_WritesDataWithLengthPrefix(int[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteSignedIntCollection((IEnumerable<int>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new long[] { 0, 1, -1, 9223372036854775807, -9223372036854775808 }, new byte[] { 0x1F, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01 })]
public void WriteLongCollection_MultipleElements_WritesDataWithLengthPrefix(long[] items, byte[] expectedBytes)
Expand All @@ -78,6 +123,17 @@ public void WriteLongCollection_MultipleElements_WritesDataWithLengthPrefix(long
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new long[] { 0, 1, -1, 9223372036854775807, -9223372036854775808 }, new byte[] { 0x1F, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01 })]
public void WriteLongCollection_WithIEnumerable_WritesDataWithLengthPrefix(long[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteLongCollection((IEnumerable<long>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new long[] { 0, -1, 1, 9223372036854775807, -9223372036854775808 }, new byte[] { 0x17, 0x00, 0x01, 0x02, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 })]
public void WriteSignedLongCollection_MultipleElements_WritesDataWithLengthPrefix(long[] items, byte[] expectedBytes)
Expand All @@ -89,6 +145,17 @@ public void WriteSignedLongCollection_MultipleElements_WritesDataWithLengthPrefi
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new long[] { 0, -1, 1, 9223372036854775807, -9223372036854775808 }, new byte[] { 0x17, 0x00, 0x01, 0x02, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 })]
public void WriteSignedLongCollection_WithIEnumerable_WritesDataWithLengthPrefix(long[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteSignedLongCollection((IEnumerable<long>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new bool[] { false, true }, new byte[] { 0x02, 0x00, 0x01 })]
public void WriteBooleanCollection_MultipleElements_WritesDataWithLengthPrefix(bool[] items, byte[] expectedBytes)
Expand All @@ -100,6 +167,17 @@ public void WriteBooleanCollection_MultipleElements_WritesDataWithLengthPrefix(b
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new bool[] { false, true }, new byte[] { 0x02, 0x00, 0x01 })]
public void WriteBooleanCollection_WithIEnumerable_WritesDataWithLengthPrefix(bool[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteBooleanCollection((IEnumerable<bool>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new float[] { 0, 3.14f, 0.00014f, 12000.1f }, new byte[] { 0x10, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xF5, 0x48, 0x40, 0xF7, 0xCC, 0x12, 0x39, 0x66, 0x80, 0x3B, 0x46 })]
public void WriteSingleCollection_MultipleElements_WritesDataWithLengthPrefix(float[] items, byte[] expectedBytes)
Expand All @@ -111,6 +189,17 @@ public void WriteSingleCollection_MultipleElements_WritesDataWithLengthPrefix(fl
SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new float[] { 0, 3.14f, 0.00014f, 12000.1f }, new byte[] { 0x10, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xF5, 0x48, 0x40, 0xF7, 0xCC, 0x12, 0x39, 0x66, 0x80, 0x3B, 0x46 })]
public void WriteSingleCollection_WithIEnumerable_WritesDataWithLengthPrefix(float[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteSingleCollection((IEnumerable<float>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new double[] { 0, 3.14, 0.00014, 12000.1 }, new byte[] { 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x85, 0xEB, 0x51, 0xB8, 0x1E, 0x09, 0x40, 0xD2, 0xFB, 0xC6, 0xD7, 0x9E, 0x59, 0x22, 0x3F, 0xCD, 0xCC, 0xCC, 0xCC, 0x0C, 0x70, 0xC7, 0x40 })]
public void WriteDoubleCollection_MultipleElements_WritesDataWithLengthPrefix(double[] items, byte[] expectedBytes)
Expand All @@ -121,5 +210,16 @@ public void WriteDoubleCollection_MultipleElements_WritesDataWithLengthPrefix(do

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}

[Theory]
[InlineData(new double[] { 0, 3.14, 0.00014, 12000.1 }, new byte[] { 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x85, 0xEB, 0x51, 0xB8, 0x1E, 0x09, 0x40, 0xD2, 0xFB, 0xC6, 0xD7, 0x9E, 0x59, 0x22, 0x3F, 0xCD, 0xCC, 0xCC, 0xCC, 0x0C, 0x70, 0xC7, 0x40 })]
public void WriteDoubleCollection_WithIEnumerable_WritesDataWithLengthPrefix(double[] items, byte[] expectedBytes)
{
var writer = PbfBlockWriter.Create(new byte[expectedBytes.Length]);

writer.WriteDoubleCollection((IEnumerable<double>)items);

SpanAssert.Equal<byte>(expectedBytes, writer.Block);
}
}
}
Loading
Loading