mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-04-21 02:26:31 +02:00
coverage + fixes
This commit is contained in:
parent
955bab45e1
commit
2bfa620a2d
@ -16,7 +16,7 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
public class SerializationTests
|
public class SerializationTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ListOfBits()
|
public void ListOfBits1()
|
||||||
{
|
{
|
||||||
void CheckList(IEnumerable<bool> items)
|
void CheckList(IEnumerable<bool> items)
|
||||||
{
|
{
|
||||||
@ -69,6 +69,34 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
CheckList(list3);
|
CheckList(list3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListOfBits2()
|
||||||
|
{
|
||||||
|
var b = MessageBuilder.Create();
|
||||||
|
var wrong = b.CreateObject<DynamicSerializerState>();
|
||||||
|
wrong.SetListOfValues(8, 7);
|
||||||
|
wrong.Allocate();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListWriteValue(0, true));
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListWriteValues(new bool[7]));
|
||||||
|
var list = b.CreateObject<DynamicSerializerState>();
|
||||||
|
list.SetListOfValues(1, 70);
|
||||||
|
list.ListWriteValue(0, true);
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(-1, true));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(70, true));
|
||||||
|
var values = new bool[70];
|
||||||
|
values[63] = true;
|
||||||
|
values[65] = true;
|
||||||
|
list.ListWriteValues(values, true);
|
||||||
|
var los = list.Rewrap<ListOfBitsSerializer>();
|
||||||
|
Assert.IsTrue(!los[63]);
|
||||||
|
Assert.IsFalse(!los[64]);
|
||||||
|
Assert.IsTrue(!los[65]);
|
||||||
|
Assert.IsFalse(!los[66]);
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => list.ListWriteValues(null));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValues(new bool[1]));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValues(new bool[71]));
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ListOfCaps()
|
public void ListOfCaps()
|
||||||
{
|
{
|
||||||
@ -229,11 +257,72 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
Assert.AreEqual(3.0f, list3[3]);
|
Assert.AreEqual(3.0f, list3[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListOfBytes()
|
||||||
|
{
|
||||||
|
var b = MessageBuilder.Create();
|
||||||
|
var wrong = b.CreateObject<DynamicSerializerState>();
|
||||||
|
wrong.SetListOfValues(1, 64);
|
||||||
|
wrong.Allocate();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListWriteValue(0, (byte)1));
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListGetBytes());
|
||||||
|
var list = b.CreateObject<DynamicSerializerState>();
|
||||||
|
list.SetListOfValues(8, 3);
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(-1, (byte)1));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(64, (byte)1));
|
||||||
|
list.ListWriteValue(1, (byte)1);
|
||||||
|
list.ListWriteValue(2, (byte)2);
|
||||||
|
CollectionAssert.AreEqual(new byte[] { 0, 1, 2 }, list.ListGetBytes().ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListOfUShorts()
|
||||||
|
{
|
||||||
|
var b = MessageBuilder.Create();
|
||||||
|
var wrong = b.CreateObject<DynamicSerializerState>();
|
||||||
|
wrong.SetListOfValues(1, 64);
|
||||||
|
wrong.Allocate();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListWriteValue(0, (ushort)1));
|
||||||
|
var list = b.CreateObject<DynamicSerializerState>();
|
||||||
|
list.SetListOfValues(16, 3);
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(-1, (ushort)1));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(64, (ushort)1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListOfUInts()
|
||||||
|
{
|
||||||
|
var b = MessageBuilder.Create();
|
||||||
|
var wrong = b.CreateObject<DynamicSerializerState>();
|
||||||
|
wrong.SetListOfValues(1, 64);
|
||||||
|
wrong.Allocate();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListWriteValue(0, 1u));
|
||||||
|
var list = b.CreateObject<DynamicSerializerState>();
|
||||||
|
list.SetListOfValues(32, 3);
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(-1, 1u));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(64, 1u));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListOfULongs()
|
||||||
|
{
|
||||||
|
var b = MessageBuilder.Create();
|
||||||
|
var wrong = b.CreateObject<DynamicSerializerState>();
|
||||||
|
wrong.SetListOfValues(1, 64);
|
||||||
|
wrong.Allocate();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => wrong.ListWriteValue(0, 1ul));
|
||||||
|
var list = b.CreateObject<DynamicSerializerState>();
|
||||||
|
list.SetListOfValues(64, 3);
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(-1, 1ul));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.ListWriteValue(64, 1ul));
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ListOfStructs1()
|
public void ListOfStructs1()
|
||||||
{
|
{
|
||||||
var b = MessageBuilder.Create();
|
var b = MessageBuilder.Create();
|
||||||
var list = b.CreateObject<ListOfStructsSerializer<SomeStruct.WRITER>>();
|
var list = b.CreateObject<ListOfStructsSerializer<SomeStruct.WRITER>>();
|
||||||
|
|
||||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.Init(-1));
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.Init(-1));
|
||||||
Assert.ThrowsException<InvalidOperationException>(() => { var _ = list[0]; });
|
Assert.ThrowsException<InvalidOperationException>(() => { var _ = list[0]; });
|
||||||
list.Init(4);
|
list.Init(4);
|
||||||
@ -273,6 +362,25 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
Assert.ThrowsException<InvalidOperationException>(() => list.StructWriteData(0, 1, 1));
|
Assert.ThrowsException<InvalidOperationException>(() => list.StructWriteData(0, 1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListOfStructs3()
|
||||||
|
{
|
||||||
|
var b = MessageBuilder.Create();
|
||||||
|
var list = b.CreateObject<ListOfStructsSerializer<SomeStruct.WRITER>>();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => list.Any());
|
||||||
|
list.Init(3);
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
item.SomeText = i.ToString();
|
||||||
|
Assert.AreEqual(item, list[i]);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
Assert.AreEqual("0", list[0].SomeText);
|
||||||
|
Assert.AreEqual("1", list[1].SomeText);
|
||||||
|
Assert.AreEqual("2", list[2].SomeText);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ListOfText()
|
public void ListOfText()
|
||||||
{
|
{
|
||||||
@ -844,11 +952,13 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void StructReadCapNoCapTable()
|
public void NoCapTable()
|
||||||
{
|
{
|
||||||
var dss = new DynamicSerializerState(MessageBuilder.Create());
|
var dss = new DynamicSerializerState(MessageBuilder.Create());
|
||||||
dss.SetStruct(0, 1);
|
dss.SetStruct(0, 1);
|
||||||
Assert.ThrowsException<InvalidOperationException>(() => dss.ReadCap(0));
|
Assert.ThrowsException<InvalidOperationException>(() => dss.ReadCap(0));
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => dss.ProvideCapability(new TestInterfaceImpl2()));
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => dss.ProvideCapability(new TestInterface_Skeleton()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@ -921,5 +1031,52 @@ namespace Capnp.Net.Runtime.Tests
|
|||||||
Assert.ThrowsException<InvalidOperationException>(() => dss.SetCapability(8));
|
Assert.ThrowsException<InvalidOperationException>(() => dss.SetCapability(8));
|
||||||
Assert.ThrowsException<InvalidOperationException>(() => dss.SetCapability(null));
|
Assert.ThrowsException<InvalidOperationException>(() => dss.SetCapability(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void StructReadData()
|
||||||
|
{
|
||||||
|
var mb = MessageBuilder.Create();
|
||||||
|
var dss = mb.CreateObject<DynamicSerializerState>();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => dss.StructReadData(0, 1));
|
||||||
|
dss.SetStruct(2, 0);
|
||||||
|
Assert.AreEqual(0ul, dss.StructReadData(0, 64));
|
||||||
|
dss.Allocate();
|
||||||
|
Assert.AreEqual(0ul, dss.StructReadData(0, 64));
|
||||||
|
Assert.AreEqual(0ul, dss.StructReadData(256, 64));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss.StructReadData(0, -1));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss.StructReadData(1, 64));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss.StructReadData(0, 65));
|
||||||
|
Assert.ThrowsException<OverflowException>(() => dss.StructReadData(ulong.MaxValue, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TryGetPointer()
|
||||||
|
{
|
||||||
|
var mb = MessageBuilder.Create();
|
||||||
|
var dss = mb.CreateObject<DynamicSerializerState>();
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => dss.TryGetPointer(0));
|
||||||
|
dss.SetStruct(0, 1);
|
||||||
|
Assert.IsNull(dss.TryGetPointer(0));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss.TryGetPointer(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ListBuildStruct()
|
||||||
|
{
|
||||||
|
var mb = MessageBuilder.Create();
|
||||||
|
var dss1 = mb.CreateObject<DynamicSerializerState>();
|
||||||
|
dss1.SetStruct(1, 1);
|
||||||
|
Assert.ThrowsException<InvalidOperationException>(() => dss1.ListBuildStruct(0));
|
||||||
|
var dss2 = mb.CreateObject<DynamicSerializerState>();
|
||||||
|
dss2.SetListOfStructs(2, 1, 1);
|
||||||
|
var s0 = dss2.ListBuildStruct(0);
|
||||||
|
Assert.AreEqual(s0, dss2.ListBuildStruct(0));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss2.ListBuildStruct(-1));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss2.ListBuildStruct(2));
|
||||||
|
var s1 = dss2.ListBuildStruct<TestSerializerStateStruct11>(1);
|
||||||
|
Assert.AreEqual(s1, dss2.ListBuildStruct<TestSerializerStateStruct11>(1));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss2.ListBuildStruct<TestSerializerStateStruct11>(-1));
|
||||||
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => dss2.ListBuildStruct<TestSerializerStateStruct11>(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,6 +80,7 @@ namespace Capnp
|
|||||||
{
|
{
|
||||||
SegmentIndex = other.SegmentIndex;
|
SegmentIndex = other.SegmentIndex;
|
||||||
Offset = other.Offset;
|
Offset = other.Offset;
|
||||||
|
WordsAllocated = other.WordsAllocated;
|
||||||
ListElementCount = other.ListElementCount;
|
ListElementCount = other.ListElementCount;
|
||||||
StructDataCount = other.StructDataCount;
|
StructDataCount = other.StructDataCount;
|
||||||
StructPtrCount = other.StructPtrCount;
|
StructPtrCount = other.StructPtrCount;
|
||||||
@ -712,6 +713,9 @@ namespace Capnp
|
|||||||
if (index >= data.Length)
|
if (index >= data.Length)
|
||||||
return 0; // Assume backwards-compatible change
|
return 0; // Assume backwards-compatible change
|
||||||
|
|
||||||
|
if (count < 0)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(count));
|
||||||
|
|
||||||
if (relBitOffset + count > 64)
|
if (relBitOffset + count > 64)
|
||||||
throw new ArgumentOutOfRangeException(nameof(count));
|
throw new ArgumentOutOfRangeException(nameof(count));
|
||||||
|
|
||||||
@ -773,6 +777,9 @@ namespace Capnp
|
|||||||
if (Kind != ObjectKind.Struct && Kind != ObjectKind.ListOfPointers)
|
if (Kind != ObjectKind.Struct && Kind != ObjectKind.ListOfPointers)
|
||||||
throw new InvalidOperationException("This is not a struct or list of pointers");
|
throw new InvalidOperationException("This is not a struct or list of pointers");
|
||||||
|
|
||||||
|
if (index < 0 || index >= _linkedStates!.Length)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(index));
|
||||||
|
|
||||||
var state = _linkedStates![index];
|
var state = _linkedStates![index];
|
||||||
|
|
||||||
if (state == null) return null;
|
if (state == null) return null;
|
||||||
@ -870,6 +877,9 @@ namespace Capnp
|
|||||||
if (Kind != ObjectKind.ListOfStructs)
|
if (Kind != ObjectKind.ListOfStructs)
|
||||||
throw new InvalidOperationException("This is not a list of structs");
|
throw new InvalidOperationException("This is not a list of structs");
|
||||||
|
|
||||||
|
if (index < 0 || index >= _linkedStates!.Length)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(index));
|
||||||
|
|
||||||
ref var state = ref _linkedStates![index];
|
ref var state = ref _linkedStates![index];
|
||||||
|
|
||||||
if (state == null)
|
if (state == null)
|
||||||
@ -897,6 +907,9 @@ namespace Capnp
|
|||||||
if (Kind != ObjectKind.ListOfStructs)
|
if (Kind != ObjectKind.ListOfStructs)
|
||||||
throw new InvalidOperationException("This is not a list of structs");
|
throw new InvalidOperationException("This is not a list of structs");
|
||||||
|
|
||||||
|
if (index < 0 || index >= _linkedStates!.Length)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(index));
|
||||||
|
|
||||||
ref var state = ref _linkedStates![index];
|
ref var state = ref _linkedStates![index];
|
||||||
|
|
||||||
if (state == null)
|
if (state == null)
|
||||||
@ -1045,7 +1058,7 @@ namespace Capnp
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The list bytes</returns>
|
/// <returns>The list bytes</returns>
|
||||||
/// <exception cref="InvalidOperationException">The underlying object was not set to a list of bytes.</exception>
|
/// <exception cref="InvalidOperationException">The underlying object was not set to a list of bytes.</exception>
|
||||||
Span<byte> ListGetBytes()
|
public Span<byte> ListGetBytes()
|
||||||
{
|
{
|
||||||
if (Kind != ObjectKind.ListOfBytes)
|
if (Kind != ObjectKind.ListOfBytes)
|
||||||
throw new InvalidOperationException("This is not a list of bytes");
|
throw new InvalidOperationException("This is not a list of bytes");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user