Exemple #1
0
        public void Struct()
        {
            var mb    = MessageBuilder.Create(8);
            var ds    = new DynamicSerializerState(mb);
            var alloc = mb.Allocator;

            ds.SetStruct(3, 2);
            Assert.IsFalse(ds.IsAllocated);
            Assert.ThrowsException <InvalidOperationException>(() => ds.SetListOfPointers(1));
            Assert.ThrowsException <InvalidOperationException>(() => ds.SetListOfStructs(3, 2, 1));
            Assert.ThrowsException <InvalidOperationException>(() => ds.SetListOfValues(8, 3));
            Assert.ThrowsException <InvalidOperationException>(() => ds.SetStruct(2, 3));
            ds.SetStruct(3, 2);
            ds.Allocate();
            Assert.IsTrue(ds.IsAllocated);
            Assert.AreEqual(3, ds.StructDataSection.Length);
            ds.StructWriteData(0, 16, 0x4321);
            ds.StructWriteData(64, 32, 0x87654321);
            ds.StructWriteData(128, 64, 0x1234567812345678);
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => ds.StructWriteData(191, 2, 1));

            var ds2 = ds.BuildPointer(0);

            ds2.SetStruct(1, 0);
            ds2.WriteData(0, 1.23);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => ds.Link(2, ds));

            Assert.AreEqual(1, alloc.Segments.Count);
            Assert.AreEqual(7, alloc.Segments[0].Length);

            DeserializerState d = ds;

            Assert.AreEqual(ObjectKind.Struct, d.Kind);
            Assert.AreEqual(3, d.StructDataCount);
            Assert.AreEqual(2, d.StructPtrCount);
            Assert.AreEqual(0x4321, d.ReadDataUShort(0));
            Assert.AreEqual(0x87654321, d.ReadDataUInt(64));
            Assert.IsTrue(0x1234567812345678 == d.ReadDataULong(128));
            var p0 = d.StructReadPointer(0);

            Assert.AreEqual(ObjectKind.Struct, p0.Kind);
            Assert.AreEqual(1.23, p0.ReadDataDouble(0));
            var p1 = d.StructReadPointer(1);

            Assert.AreEqual(ObjectKind.Nil, p1.Kind);
        }
Exemple #2
0
        public void StructWithPrimitives()
        {
            var mb    = MessageBuilder.Create(8);
            var alloc = mb.Allocator;
            var ds    = new DynamicSerializerState(mb);

            ds.SetStruct(10, 0);
            ds.WriteData(0, ulong.MaxValue - 1, ulong.MaxValue);
            ds.WriteData(64, long.MaxValue - 1, -123);
            ds.WriteData(128, uint.MaxValue - 1, 123);
            ds.WriteData(160, int.MaxValue - 1, -123);
            ds.WriteData(192, (ushort)(ushort.MaxValue - 1), (ushort)321);
            ds.WriteData(208, (short)(short.MaxValue - 1), (short)321);
            ds.WriteData(224, (byte)(byte.MaxValue - 1), (byte)111);
            ds.WriteData(232, (sbyte)(sbyte.MaxValue - 1), (sbyte)(-111));
            ds.WriteData(240, false, false);
            ds.WriteData(241, false, true);
            ds.WriteData(242, true, false);
            ds.WriteData(243, true, true);
            ds.WriteData(256, 12.34, 0.5);
            ds.WriteData(320, 1.2f, 0.5f);

            DeserializerState d = ds;

            Assert.AreEqual(ulong.MaxValue - 1, d.ReadDataULong(0, ulong.MaxValue));
            Assert.AreEqual(long.MaxValue - 1, d.ReadDataLong(64, -123));
            Assert.AreEqual(uint.MaxValue - 1, d.ReadDataUInt(128, 123));
            Assert.AreEqual(int.MaxValue - 1, d.ReadDataInt(160, -123));
            Assert.AreEqual(ushort.MaxValue - 1, d.ReadDataUShort(192, 321));
            Assert.AreEqual(short.MaxValue - 1, d.ReadDataShort(208, 321));
            Assert.AreEqual(byte.MaxValue - 1, d.ReadDataByte(224, 111));
            Assert.AreEqual(sbyte.MaxValue - 1, d.ReadDataSByte(232, -111));
            Assert.AreEqual(false, d.ReadDataBool(240, false));
            Assert.AreEqual(false, d.ReadDataBool(241, true));
            Assert.AreEqual(true, d.ReadDataBool(242, false));
            Assert.AreEqual(true, d.ReadDataBool(243, true));
            Assert.AreEqual(12.34, d.ReadDataDouble(256, 0.5));
            Assert.AreEqual(1.2f, d.ReadDataFloat(320, 0.5f));
        }