Example #1
0
        public void Union_2Items_Table_SerializationTest()
        {
            byte[] expectedData =
            {
                4,     0,   0, 0,   // offset to table
                244, 255, 255, 255, // soffset to vtable
                1,                  // discriminator (1 byte)
                0,     0,   0,      // padding
                12,    0,   0, 0,   // uoffset_t to table data
                8,     0,           // vtable length
                12,    0,           // table length
                4,     0,           // discriminator offset
                8,     0,           // value offset

                // table data
                248, 255, 255, 255, // soffset to vtable
                123,   0,   0, 0,   // value of index 0
                6,     0,           // vtable length
                8,     0,           // table length
                4, 0                // offset of index 0
            };

            var union = new UnionTable <SimpleTable, SimpleStruct>
            {
                Item = new FlatBufferUnion <SimpleTable, SimpleStruct>(new SimpleTable {
                    Int = 123
                })
            };

            Span <byte> data  = new byte[100];
            int         count = FlatBufferSerializer.Default.Serialize(union, data);

            Assert.IsTrue(expectedData.AsSpan().SequenceEqual(data.Slice(0, count)));
        }
        public void Union_Struct_Location()
        {
            var location = new Location
            {
                X = 1.0f, Y = 2.0f, Z = 3.0f
            };

            var table = new UnionTable
            {
                Union = new FlatBufferUnion <BasicTypes, Location, string>(location)
            };

            Span <byte> memory = new byte[10240];
            int         size   = FlatBufferSerializer.Default.Serialize(table, memory);

            var oracle = Oracle.UnionTable.GetRootAsUnionTable(
                new FlatBuffers.ByteBuffer(memory.Slice(0, size).ToArray()));

            Assert.AreEqual(2, (int)oracle.ValueType);
            var bt = oracle.Value <Oracle.Location>().Value;

            Assert.AreEqual(bt.X, location.X);
            Assert.AreEqual(bt.Y, location.Y);
            Assert.AreEqual(bt.Z, location.Z);
        }
Example #3
0
        public void Union_2Items_Struct_SerializationTest()
        {
            byte[] expectedData =
            {
                4,     0,   0,0,    // offset to table
                246, 255, 255,255,  // soffset to vtable
                16,    0,   0,0,    // uoffset_t to struct data
                2,     0,     // discriminator (1 byte), padding
                8,     0,     // vtable length
                9,     0,     // table length
                8,     0,     // discriminator offset
                4,     0,     // value offset

                0,     0,     // padding

                // struct data
                123,   0,   0,0, 0, 0, 0, 0
            };

            var union = new UnionTable <string, SimpleStruct>
            {
                Item = new FlatBufferUnion <string, SimpleStruct>(new SimpleStruct {
                    Long = 123
                })
            };

            Span <byte> data  = new byte[100];
            int         count = FlatBufferSerializer.Default.Serialize(union, data);

            Assert.IsTrue(expectedData.AsSpan().SequenceEqual(data.Slice(0, count)));
        }
        public void Union_Table_BasicTypes()
        {
            var simple = new BasicTypes
            {
                Bool   = true,
                Byte   = GetRandom <byte>(),
                SByte  = GetRandom <sbyte>(),
                Double = GetRandom <double>(),
                Float  = GetRandom <float>(),
                Int    = GetRandom <int>(),
                Long   = GetRandom <long>(),
                Short  = GetRandom <short>(),
                String = "foobar",
                UInt   = GetRandom <uint>(),
                ULong  = GetRandom <ulong>(),
                UShort = GetRandom <ushort>(),
            };

            var table = new UnionTable
            {
                Union = new FlatBufferUnion <BasicTypes, Location, string>(simple)
            };

            Span <byte> memory = new byte[10240];
            int         size   = FlatBufferSerializer.Default.Serialize(table, memory);

            var oracle = Oracle.UnionTable.GetRootAsUnionTable(
                new FlatBuffers.ByteBuffer(memory.Slice(0, size).ToArray()));

            Assert.AreEqual(1, (int)oracle.ValueType);
            var bt = oracle.Value <Oracle.BasicTypes>().Value;

            Assert.IsTrue(bt.Bool);
            Assert.AreEqual(bt.Byte, simple.Byte);
            Assert.AreEqual(bt.SByte, simple.SByte);

            Assert.AreEqual(bt.UShort, simple.UShort);
            Assert.AreEqual(bt.Short, simple.Short);

            Assert.AreEqual(bt.UInt, simple.UInt);
            Assert.AreEqual(bt.Int, simple.Int);

            Assert.AreEqual(bt.ULong, simple.ULong);
            Assert.AreEqual(bt.Long, simple.Long);

            Assert.AreEqual(bt.Float, simple.Float);
            Assert.AreEqual(bt.Double, simple.Double);
            Assert.AreEqual(bt.String, simple.String);
        }
        public void Union_NotSet()
        {
            var table = new UnionTable
            {
                Union = null,
            };

            Span <byte> memory = new byte[10240];
            int         size   = FlatBufferSerializer.Default.Serialize(table, memory);

            var oracle = Oracle.UnionTable.GetRootAsUnionTable(
                new FlatBuffers.ByteBuffer(memory.Slice(0, size).ToArray()));

            Assert.AreEqual(0, (int)oracle.ValueType);
        }
Example #6
0
        private static T2 CreateAndDeserialize2 <T1, T2>(T1 one, T2 two)
        {
            var table = new UnionTable <T1, T2>
            {
                Item = new FlatBufferUnion <T1, T2>(two)
            };

            byte[] buffer = new byte[1024];

            FlatBufferSerializer.Default.Serialize(table, buffer);

            var parseResult = FlatBufferSerializer.Default.Parse <UnionTable <T1, T2> >(buffer);

            Assert.AreEqual(2, parseResult.Item.Discriminator);

            return(parseResult.Item.Item2);
        }
        public void Union_String()
        {
            var table = new UnionTable
            {
                Union = new FlatBufferUnion <BasicTypes, Location, string>("foobar")
            };

            Span <byte> memory = new byte[10240];
            int         size   = FlatBufferSerializer.Default.Serialize(table, memory);

            var oracle = Oracle.UnionTable.GetRootAsUnionTable(
                new FlatBuffers.ByteBuffer(memory.Slice(0, size).ToArray()));

            // There is nothing to assert here, since the google C# SDK does not support reading strings
            // out of a union. All indications are that we are doing it right, but there's no way to test
            // this particular scenario.
            Assert.AreEqual(3, (int)oracle.ValueType);
            Assert.Inconclusive();
        }