Exemplo n.º 1
0
        public void Union_Table_BasicTypes()
        {
            var builder          = new FlatBuffers.FlatBufferBuilder(1024);
            var basicTypesOffset = Oracle.BasicTypes.CreateBasicTypes(
                builder,
                Bool: true,
                Byte: GetRandom <byte>(),
                SByte: GetRandom <sbyte>(),
                UShort: GetRandom <ushort>(),
                Short: GetRandom <short>(),
                UInt: GetRandom <uint>(),
                Int: GetRandom <int>(),
                ULong: GetRandom <ulong>(),
                Long: GetRandom <long>(),
                Float: GetRandom <float>(),
                Double: GetRandom <double>(),
                StringOffset: builder.CreateString("foobar"));

            var offset = Oracle.UnionTable.CreateUnionTable(
                builder,
                Oracle.Union.BasicTypes,
                basicTypesOffset.Value);

            builder.Finish(offset.Value);
            byte[] realBuffer = builder.DataBuffer.ToSizedArray();

            var oracle     = Oracle.UnionTable.GetRootAsUnionTable(new FlatBuffers.ByteBuffer(realBuffer)).Value <Oracle.BasicTypes>().Value;
            var unionTable = FlatBufferSerializer.Default.Parse <UnionTable>(realBuffer);

            Assert.AreEqual(1, unionTable.Union.Discriminator);
            BasicTypes parsed = unionTable.Union.Item1;

            Assert.IsNotNull(parsed);

            Assert.IsTrue(parsed.Bool);
            Assert.AreEqual(oracle.Byte, parsed.Byte);
            Assert.AreEqual(oracle.SByte, parsed.SByte);

            Assert.AreEqual(oracle.UShort, parsed.UShort);
            Assert.AreEqual(oracle.Short, parsed.Short);

            Assert.AreEqual(oracle.UInt, parsed.UInt);
            Assert.AreEqual(oracle.Int, parsed.Int);

            Assert.AreEqual(oracle.ULong, parsed.ULong);
            Assert.AreEqual(oracle.Long, parsed.Long);

            Assert.AreEqual(oracle.Float, parsed.Float);
            Assert.AreEqual(oracle.Double, parsed.Double);
            Assert.AreEqual("foobar", parsed.String);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        private void SimpleTypesTest <TSpanWriter>(TSpanWriter writer) where TSpanWriter : ISpanWriter
        {
            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>(),
            };

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

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

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

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

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

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

            Assert.AreEqual(oracle.Float, simple.Float);
            Assert.AreEqual(oracle.Double, simple.Double);
            Assert.AreEqual(oracle.String, simple.String);
        }