Пример #1
0
        public void fix_map()
        {
            var f = new MsgPackFormatter();

            f.BeginMap(2);
            f.Key("0"); f.Value(1);
            f.Key("2"); f.Value(3);
            f.EndMap();
            var bytes =
                f.GetStoreBytes();

            ;

            Assert.AreEqual(new Byte[] {
                0x82,       // map2

                0xa1, 0x30, // "0"
                0x01,       // 1

                0xa1, 0x32, // "2"
                0x03        // 3
            }, bytes.ToEnumerable());

            var value = MsgPackParser.Parse(bytes);

            Assert.AreEqual(2, value.GetObjectCount());
            Assert.AreEqual(1, value["0"].GetValue());
            Assert.AreEqual(3, value["2"].GetValue());
        }
Пример #2
0
        public void array129()
        {
            {
                var i128 = Enumerable.Range(0, 128).ToArray();
                var f    = new MsgPackFormatter();
                f.Serialize(i128);
                var bytes128     = f.GetStoreBytes();
                var deserialized = MsgPackParser.Parse(bytes128);
                Assert.AreEqual(128, deserialized.GetArrayCount());
                for (int i = 0; i < i128.Length; ++i)
                {
                    Assert.AreEqual(i128[i], deserialized[i].GetValue());
                }
            }

            {
                var i129 = Enumerable.Range(0, 129).ToArray();
                var f    = new MsgPackFormatter();
                f.Serialize(i129);
                var bytes129     = f.GetStoreBytes();
                var deserialized = MsgPackParser.Parse(bytes129);
                Assert.AreEqual(129, deserialized.GetArrayCount());
                for (int i = 0; i < i129.Length; ++i)
                {
                    Assert.AreEqual(i129[i], deserialized[i].GetValue());
                }
            }
        }
Пример #3
0
        public void fix_array()
        {
            var f = new MsgPackFormatter();

            // Object[] not supported
            f.Serialize(new[] { 0, 1, false, (Object)null });
            var _bytes = f.GetStoreBytes();
            var bytes  = _bytes.Array.Skip(_bytes.Offset).Take(_bytes.Count).ToArray();

            Assert.AreEqual(new Byte[] {
                (Byte)MsgPackType.FIX_ARRAY_0x4,
                (Byte)MsgPackType.POSITIVE_FIXNUM,
                (Byte)MsgPackType.POSITIVE_FIXNUM_0x01,
                (Byte)MsgPackType.FALSE,
                (Byte)MsgPackType.NIL
            }, bytes);

            var parsed = MsgPackParser.Parse(bytes);

            Assert.AreEqual(4, parsed.GetArrayCount());
            Assert.AreEqual(0, parsed[0].GetValue());
            Assert.AreEqual(1, parsed[1].GetValue());
            Assert.False((Boolean)parsed[2].GetValue());
            Assert.AreEqual(null, parsed[3].GetValue());
        }
Пример #4
0
        public void str()
        {
            var f = new MsgPackFormatter();

            f.Value("文字列");
            var bytes = f.GetStoreBytes();

            var v = MsgPackParser.Parse(bytes).GetValue();

            Assert.AreEqual("文字列", v);
        }
Пример #5
0
        public void nil()
        {
            {
                var f = new MsgPackFormatter();
                f.Null();
                var bytes = f.GetStoreBytes();
                Assert.AreEqual(new Byte[] { 0xC0 }, bytes.ToEnumerable());

                var parsed = MsgPackParser.Parse(bytes);
                Assert.True(parsed.IsNull());
            }
        }
Пример #6
0
        public void raw16()
        {
            var src = Enumerable.Range(0, 50).Select(x => (Byte)x).ToArray();
            var f   = new MsgPackFormatter();

            f.Value(src);
            var bytes = f.GetStoreBytes();

            var v = MsgPackParser.Parse(bytes).Value.GetBody();

            Assert.True(src.SequenceEqual(v.ToEnumerable()));
        }
Пример #7
0
        public void fix_raw()
        {
            var src = new Byte[] { 0, 1, 2 };
            var f   = new MsgPackFormatter();

            f.Value(src);
            var bytes = f.GetStoreBytes();

            var v = MsgPackParser.Parse(bytes).Value.GetBody();

            Assert.True(src.SequenceEqual(v.ToEnumerable()));
        }
Пример #8
0
        public void negative_fixnum()
        {
            for (SByte i = -32; i < 0; ++i)
            {
                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #9
0
        public void fix_str()
        {
            for (int i = 1; i < 32; ++i)
            {
                var str = String.Join("", Enumerable.Range(0, i).Select(_ => "0").ToArray());
                var f   = new MsgPackFormatter();
                f.Value(str);
                var bytes = f.GetStoreBytes();

                var value = MsgPackParser.Parse(bytes);
                Assert.AreEqual(i, ((String)value.GetValue()).Length);
            }
        }
Пример #10
0
        public void positive_fixnum()
        {
            for (Byte i = 0; i < 128; ++i)
            {
                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();
                Assert.AreEqual(new Byte[] { i }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #11
0
        public void TimeTest()
        {
            var f = new MsgPackFormatter();

            {
                f.GetStore().Clear();
                var time = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
                f.Value(time);

                var bytes = f.GetStoreBytes().ArrayOrCopy();
                unchecked
                {
                    Assert.AreEqual(new byte[]
                    {
                        (byte)MsgPackType.FIX_EXT_4, (byte)-1, 0, 0, 0, 0
                    }, bytes);
                }
                var parsed = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(time, parsed);
            }

            {
                var time = new DateTimeOffset(2018, 12, 8, 2, 12, 15, TimeSpan.Zero);
                Assert.AreEqual(1544235135, time.ToUnixTimeSeconds());
                f.GetStore().Clear();
                f.Value(time);
                var bytes  = f.GetStoreBytes().ArrayOrCopy();
                var parsed = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(time, parsed);
            }

            {
                f.GetStore().Clear();
                Assert.Catch(() =>
                {
                    f.Value(new DateTimeOffset());
                });
            }
        }
Пример #12
0
        public void False()
        {
            var f = new MsgPackFormatter();

            f.Value(false);
            var bytes = f.GetStoreBytes();

            Assert.AreEqual(new Byte[] { 0xC2 }, bytes.ToEnumerable());

            var value = MsgPackParser.Parse(bytes);
            var j     = value.GetBoolean();

            Assert.AreEqual(false, j);
        }
Пример #13
0
        public void int128Test()
        {
            int i = 128;
            var f = new MsgPackFormatter();

            f.Value(i);
            var bytes = f.GetStoreBytes();

            Assert.AreEqual(new Byte[] {
                0xcc, 0x80,
            }, bytes.ToEnumerable());
            var j = MsgPackParser.Parse(bytes).GetValue();

            Assert.AreEqual(i, j);
        }
Пример #14
0
        public void uint32()
        {
            {
                UInt32 i = 0xFFFF + 20;

                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();
                Assert.AreEqual(new Byte[] {
                    0xce, 0x00, 0x01, 0x00, 0x13
                }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #15
0
        public void cast_large_type()
        {
            {
                Byte i = 0x7F + 20;

                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();
                Assert.AreEqual(new Byte[] {
                    0xcc, 0x93,
                }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #16
0
        public void int8()
        {
            {
                SByte i = -64;

                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();

                Assert.AreEqual(new Byte[] {
                    0xd0, 0xc0,
                }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #17
0
        public void int16()
        {
            {
                Int16 i = -150;

                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();

                Assert.AreEqual(new Byte[] {
                    0xd1, 0xFF, 0x6a
                }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #18
0
        public void int32()
        {
            {
                Int32 i = -35000;

                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();

                Assert.AreEqual(new Byte[] {
                    0xd2, 0xff, 0xff, 0x77, 0x48
                }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #19
0
        public void int64()
        {
            {
                Int64 i = -2147483650;

                var f = new MsgPackFormatter();
                f.Value(i);
                var bytes = f.GetStoreBytes();

                Assert.AreEqual(new Byte[] {
                    0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe
                }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Пример #20
0
        public void array16()
        {
            var f    = new MsgPackFormatter();
            var data = Enumerable.Range(0, 20).Select(x => (Object)x).ToArray();

            f.Serialize(data);
            var bytes = f.GetStoreBytes();

            var value = MsgPackParser.Parse(bytes);

            Assert.IsTrue(value.IsArray());
            Assert.AreEqual(20, value.GetArrayCount());
            for (int i = 0; i < 20; ++i)
            {
                Assert.AreEqual(i, value[i].GetValue());
            }
        }
Пример #21
0
        public void Float64()
        {
            var i         = 1.1;
            var double_be = new Byte[] {
                (Byte)MsgPackType.DOUBLE, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a,
            };

            var f = new MsgPackFormatter();

            f.Value(i);
            var bytes = f.GetStoreBytes();

            var value = MsgPackParser.Parse(bytes);
            var body  = value.Value.Bytes;

            Assert.AreEqual(double_be, body.ToEnumerable().ToArray());

            Assert.AreEqual(i, value.GetValue());
        }
Пример #22
0
        public void Float32()
        {
            var i        = 1.1f;
            var float_be = new byte[]
            {
                (Byte)MsgPackType.FLOAT, 0x3f, 0x8c, 0xcc, 0xcd
            };

            var f = new MsgPackFormatter();

            f.Value(i);
            var bytes = f.GetStoreBytes();

            var value = MsgPackParser.Parse(bytes);
            var body  = value.Value.Bytes;

            Assert.AreEqual(float_be, body.ToEnumerable().ToArray());

            Assert.AreEqual(i, value.GetValue());
        }
Пример #23
0
        public void map16()
        {
            var w    = new MsgPackFormatter();
            int size = 18;

            w.BeginMap(size);
            for (int i = 0; i < size; ++i)
            {
                w.Value(i.ToString());
                w.Value(i + 5);
            }
            var bytes = w.GetStoreBytes().ToEnumerable().ToArray();


            var expected = new Byte[] {
                0xde,       // map18
                0x0, 0x12,  // 18

                0xa1, 0x30, // "0"
                0x5,

                0xa1, 0x31,     // "1"
                0x6,

                0xa1, 0x32,     // "2"
                0x7,

                0xa1, 0x33,     // "3"
                0x8,

                0xa1, 0x34,     // "4"
                0x9,

                0xa1, 0x35,     // "5"
                0xa,

                0xa1, 0x36,     // "6"
                0xb,

                0xa1, 0x37,     // "7"
                0xc,

                0xa1, 0x38,     // "8"
                0xd,

                0xa1, 0x39,     // "9"
                0xe,

                0xa2, 0x31, 0x30,     // "10"
                0xf,

                0xa2, 0x31, 0x31,     // "11"
                0x10,

                0xa2, 0x31, 0x32,     // "12"
                0x11,

                0xa2, 0x31, 0x33,     // "13"
                0x12,

                0xa2, 0x31, 0x34,     // "14"
                0x13,

                0xa2, 0x31, 0x35,     // "15"
                0x14,

                0xa2, 0x31, 0x36,     // "16"
                0x15,

                0xa2, 0x31, 0x37,     // "17",
                0x16
            };

            Assert.AreEqual(expected, bytes);

            var value = MsgPackParser.Parse(bytes);

            Assert.AreEqual(15, value["10"].GetValue());
        }