示例#1
0
        public void encodeInt32()
        {
            int[] testData =
            {
                0,              -1, 1, 127, -128,
                Int32.MinValue, Int32.MaxValue
            };
            BFlatBuilder encoder = new BFlatBuilder(new byte[1024], 0);

            foreach (int value in testData)
            {
                encoder.encodeTag(BFlatEncoding.Type.Int32, "va");
                encoder.encode(value);
            }

            BFlatParser parser = new BFlatParser();

            int index = 0;

            foreach (BFlatValue value in parser.parse(encoder.data, 0, encoder.position))
            {
                Assert.AreEqual(value.getInt32(), testData[index++]);
                Assert.AreEqual(value.getTag(), "va");
            }
        }
示例#2
0
        public void encodeDoubleArray()
        {
            double[] testData =
            {
                0,               -1.0, 1.0, 127.01, -128.01,
                Double.MinValue, Double.MaxValue
            };
            BFlatBuilder encoder = new BFlatBuilder(new byte[1024], 0);

            encoder.encodeTagArray(BFlatEncoding.Type.Double, "value", testData.Length);
            foreach (double value in testData)
            {
                encoder.encode(value);
            }

            BFlatParser parser = new BFlatParser();

            foreach (BFlatValue value in parser.parse(encoder.data, 0, encoder.position))
            {
                Assert.AreEqual(value.getTag(), "value");
                Assert.AreEqual(testData.Length, value.getArrayLength());
                for (int arrayIndex = 0; arrayIndex < value.getArrayLength(); ++arrayIndex)
                {
                    Assert.AreEqual(testData[arrayIndex], value.getDouble(arrayIndex), 0.1);
                }
            }
        }
示例#3
0
        public void encodeUnicodeString()
        {
            int MAX_STRING = 1024;

            char[] tagData = Enumerable.Repeat('㇃', MAX_STRING).ToArray();

            char[]       data    = Enumerable.Repeat('Ԁ', MAX_STRING).ToArray();
            BFlatBuilder encoder =
                new BFlatBuilder(new byte[MAX_STRING * MAX_STRING * 4], 0);

            for (int length = 0; length < MAX_STRING; ++length)
            {
                encoder.encodeTag(BFlatEncoding.Type.String, new String(tagData, 0, length + 1));
                encoder.encode(new String(data, 0, length));
            }

            BFlatParser parser = new BFlatParser();
            int         index  = 0;

            foreach (BFlatValue value in parser.parse(encoder.data, 0, encoder.position))
            {
                Assert.AreEqual(value.getTag(), new String(tagData, 0, index + 1));
                Assert.AreEqual(value.getString(), new String(data, 0, index++));
            }
        }
示例#4
0
        public void TestDouble()
        {
            double[] testData =
            {
                0,               -1.0, 1.0, 127.01, -128.01,
                Double.MinValue, Double.MaxValue
            };
            BFlatBuilder encoder = new BFlatBuilder(new byte[1024], 0);

            foreach (double value in testData)
            {
                encoder.encodeTag(BFlatEncoding.Type.Double, "value");
                encoder.encode(value);
            }

            BFlatParser parser = new BFlatParser();

            int index = 0;

            foreach (BFlatValue value in parser.parse(encoder.data, 0, encoder.position))
            {
                Assert.AreEqual(value.getDouble(), testData[index++], 0.01);
                Assert.AreEqual(value.getTag(), "value");
            }
        }
示例#5
0
        public void encodeInt16()
        {
            short[]      testData = { 0, -1, 1, 127, -128, 128, -129, -32768, 32767 };
            BFlatBuilder encoder  = new BFlatBuilder(new byte[1024], 0);

            foreach (short value in testData)
            {
                encoder.encodeTag(BFlatEncoding.Type.Int16, "v");
                encoder.encode(value);
            }

            BFlatParser parser = new BFlatParser();

            int index = 0;

            foreach (BFlatValue value in parser.parse(encoder.data, 0, encoder.position))
            {
                Assert.AreEqual(value.getInt16(), testData[index++]);
                Assert.AreEqual(value.getTag(), "v");
            }
        }
示例#6
0
        public void nullStringDoubleReuse()
        {
            byte[] data =
            {
                (byte)0x4,  (byte)0x6E, (byte)0x75, (byte)0x6C, (byte)0x6C, (byte)0x8,  (byte)0x10, (byte)0x73, (byte)0x74, (byte)0x72,
                (byte)0x69, (byte)0x6E, (byte)0x67, (byte)0x20, (byte)0x67, (byte)0x6F, (byte)0x65, (byte)0x73, (byte)0x20, (byte)0x68,
                (byte)0x65, (byte)0x72, (byte)0x65, (byte)0x1,  (byte)0x61, (byte)0x3E, (byte)0x64, (byte)0x6F, (byte)0x75, (byte)0x62,
                (byte)0x6C, (byte)0x65, (byte)0x73, (byte)0x68, (byte)0x91, (byte)0xED, (byte)0x7C, (byte)0xFF, (byte)0x23, (byte)0x40
            };

            String[]    expected = { "null=null", "string goes here=\"a\"", "double=9.999" };
            BFlatParser p        = new BFlatParser().parse(data);
            int         i        = 0;
            BFlatValue  prev     = null;

            foreach (BFlatValue v in p)
            {
                if (prev != null)
                {
                    Assert.IsTrue(Object.ReferenceEquals(v, prev));
                }
                Assert.AreEqual(expected[i], v.ToString());
                ++i;
                v.reuse();
                prev = v;
            }

            p.parse(data);
            i    = 0;
            prev = null;
            foreach (BFlatValue v in p)
            {
                Assert.IsFalse(Object.ReferenceEquals(v, prev));
                Assert.AreEqual(expected[i], v.ToString());
                ++i;
                prev = v;
            }
        }