Exemplo n.º 1
0
        public void scalarDoubleStringDouble()
        {
            // BFLAT encoding of (101, {'double': 123.45, 'long string tag': 'the quick brown fox jumped over the lazy dog', 'another double': -123.99})
            byte[] data =
            {
                (byte)0x3E, (byte)0x64, (byte)0x6F, (byte)0x75, (byte)0x62, (byte)0x6C, (byte)0x65, (byte)0xCD, (byte)0xCC, (byte)0xCC,
                (byte)0xCC, (byte)0xCC, (byte)0xDC, (byte)0x5E, (byte)0x40, (byte)0x8,  (byte)0xF,  (byte)0x6C, (byte)0x6F, (byte)0x6E,
                (byte)0x67, (byte)0x20, (byte)0x73, (byte)0x74, (byte)0x72, (byte)0x69, (byte)0x6E, (byte)0x67, (byte)0x20, (byte)0x74,
                (byte)0x61, (byte)0x67, (byte)0x2C, (byte)0x74, (byte)0x68, (byte)0x65, (byte)0x20, (byte)0x71, (byte)0x75, (byte)0x69,
                (byte)0x63, (byte)0x6B, (byte)0x20, (byte)0x62, (byte)0x72, (byte)0x6F, (byte)0x77, (byte)0x6E, (byte)0x20, (byte)0x66,
                (byte)0x6F, (byte)0x78, (byte)0x20, (byte)0x6A, (byte)0x75, (byte)0x6D, (byte)0x70, (byte)0x65, (byte)0x64, (byte)0x20,
                (byte)0x6F, (byte)0x76, (byte)0x65, (byte)0x72, (byte)0x20, (byte)0x74, (byte)0x68, (byte)0x65, (byte)0x20, (byte)0x6C,
                (byte)0x61, (byte)0x7A, (byte)0x79, (byte)0x20, (byte)0x64, (byte)0x6F, (byte)0x67, (byte)0x38, (byte)0xE,  (byte)0x61,
                (byte)0x6E, (byte)0x6F, (byte)0x74, (byte)0x68, (byte)0x65, (byte)0x72, (byte)0x20, (byte)0x64, (byte)0x6F, (byte)0x75,
                (byte)0x62, (byte)0x6C, (byte)0x65, (byte)0x8F, (byte)0xC2, (byte)0xF5, (byte)0x28, (byte)0x5C, (byte)0xFF, (byte)0x5E,
                (byte)0xC0
            };

            BFlatParser p = new BFlatParser().parse(data);

            Assert.IsTrue(p.MoveNext());
            Assert.AreEqual(p.Current.ToString(), "double=123.45");
            p.MoveNext();
            Assert.AreEqual(p.Current.ToString(), "long string tag=\"the quick brown fox jumped over the lazy dog\"");
            p.MoveNext();
            Assert.AreEqual(p.Current.ToString(), "another double=-123.99");
        }
Exemplo n.º 2
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");
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
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++));
            }
        }
Exemplo n.º 5
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");
            }
        }
Exemplo n.º 6
0
        public void binaryArray()
        {
            byte[] data =
            {
                (byte)0x96, (byte)0x62, (byte)0x69, (byte)0x6E, (byte)0x61, (byte)0x72, (byte)0x79, (byte)0x6,  (byte)0x0,  (byte)0x1,
                (byte)0x61, (byte)0x3,  (byte)0x61, (byte)0x61, (byte)0x61, (byte)0x4,  (byte)0x61, (byte)0x61, (byte)0x61, (byte)0x61,
                (byte)0x1,  (byte)0x61, (byte)0x0
            };
            BFlatParser p = new BFlatParser().parse(data);

            Assert.IsTrue(p.MoveNext());
            Assert.AreEqual(p.Current.ToString(), "binary=[\"\", \"a\", \"aaa\", \"aaaa\", \"a\", \"\"]");
        }
Exemplo n.º 7
0
        public void leb128Array()
        {
            byte[] data =
            {
                (byte)0xCE, (byte)0x6C, (byte)0x65, (byte)0x62, (byte)0x31, (byte)0x32, (byte)0x38, (byte)0x9,  (byte)0x0, (byte)0x7F,
                (byte)0x1,  (byte)0x81, (byte)0x7F, (byte)0xFF, (byte)0x0,  (byte)0x80, (byte)0x7F, (byte)0x80, (byte)0x1, (byte)0x80,
                (byte)0x80, (byte)0x7C, (byte)0x80, (byte)0x80, (byte)0x4
            };
            BFlatParser p = new BFlatParser().parse(data);

            Assert.IsTrue(p.MoveNext());
            Assert.AreEqual(p.Current.ToString(), "leb128=[0, -1, 1, -127, 127, -128, 128, -65536, 65536]");
        }
Exemplo n.º 8
0
        public void longTagName()
        {
            // BFLAT encoding of {"this is a longer tag name":1}
            byte[] data =
            {
                0x28, 0x19, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,     0x20
                ,     0x61, 0x20, 0x6C, 0x6F, 0x6E, 0x67, 0x65, 0x72,0x20, 0x74
                ,     0x61, 0x67, 0x20, 0x6E, 0x61, 0x6D, 0x65,  0x1, 0x0, 0x0
                , 0x0
            };

            BFlatParser p = new BFlatParser().parse(data);

            Assert.IsTrue(p.MoveNext());
            Assert.AreEqual(p.Current.ToString(), "this is a longer tag name=1");
        }
Exemplo n.º 9
0
        public void scalarInt32()
        {
            // BFLAT encoding of {"foo":1} (int32)
            byte[] data = { 0x2B, 0x66, 0x6F, 0x6F, 0x1, 0x0, 0x0, 0x0 };

            BFlatParser p = new BFlatParser().parse(data);

            Assert.IsTrue(p.MoveNext());
            BFlatValue v = p.Current;

            Assert.IsNotNull(v);
            Assert.AreEqual(BFlatEncoding.Type.Int32, v.getType());
            Assert.IsFalse(v.isArray());
            Assert.AreEqual("foo", v.getTag());
            Assert.AreEqual(1, v.getLong());
            Assert.AreEqual(1, v.getInt32());
            Assert.IsFalse(v.isNull());
            Assert.AreEqual(v.ToString(), "foo=1");
            Assert.IsFalse(p.MoveNext());
        }
Exemplo n.º 10
0
        public void nullStringDouble()
        {
            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
            };

            BFlatParser p = new BFlatParser().parse(data);

            Assert.IsTrue(p.MoveNext());
            Assert.AreEqual("null=null", p.Current.ToString());
            p.MoveNext();
            Assert.AreEqual("string goes here=\"a\"", p.Current.ToString());
            p.MoveNext();
            Assert.AreEqual("double=9.999", p.Current.ToString());
            Assert.IsFalse(p.MoveNext());
        }
Exemplo n.º 11
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");
            }
        }
Exemplo n.º 12
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;
            }
        }