public void fix_map()
        {
            var bytes = new MsgPackFormatter()
                        .BeginMap(2)
                        .Key("0").Value(1)
                        .Key("2").Value(3)
                        .EndMap()
                        .GetStore().Bytes;

            ;

            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.Count);
            Assert.AreEqual(1, value["0"].GetValue());
            Assert.AreEqual(3, value["2"].GetValue());
        }
Example #2
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());
        }
Example #3
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());
                }
            }
        }
Example #4
0
        public void ReadTest()
        {
            var data = new int[] { -108, 0, 1, -90, 108, 111, 103, 103, 101, 114, -110, -91, 69, 114, 114, 111, 114, -94, 101, 50 }
            .Select(x => (Byte)x).ToArray();
            var parsed = MsgPackParser.Parse(data);

            Assert.True(parsed.IsArray());
        }
        public void str()
        {
            var bytes = new MsgPackFormatter().Value("文字列").GetStore().Bytes;

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

            Assert.AreEqual("文字列", v);
        }
        public void raw16()
        {
            var src   = Enumerable.Range(0, 50).Select(x => (Byte)x).ToArray();
            var bytes = new MsgPackFormatter().Value(src).GetStore().Bytes;

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

            Assert.True(src.SequenceEqual(v.ToEnumerable()));
        }
        public void fix_raw()
        {
            var src   = new Byte[] { 0, 1, 2 };
            var bytes = new MsgPackFormatter().Value(src).GetStore().Bytes;

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

            Assert.True(src.SequenceEqual(v.ToEnumerable()));
        }
        public void negative_fixnum()
        {
            for (SByte i = -32; i < 0; ++i)
            {
                var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
        public void nil()
        {
            {
                var bytes = new MsgPackFormatter().Null().GetStore().Bytes;
                Assert.AreEqual(new Byte[] { 0xC0 }, bytes.ToEnumerable());

                var parsed = MsgPackParser.Parse(bytes);
                Assert.True(parsed.IsNull);
            }
        }
        public void positive_fixnum()
        {
            for (Byte i = 0; i < 128; ++i)
            {
                var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;
                Assert.AreEqual(new Byte[] { i }, bytes.ToEnumerable());

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
        public void fix_str()
        {
            for (int i = 1; i < 32; ++i)
            {
                var str   = String.Join("", Enumerable.Range(0, i).Select(_ => "0").ToArray());
                var bytes = new MsgPackFormatter().Value(str).GetStore().Bytes;

                var value = MsgPackParser.Parse(bytes);
                Assert.AreEqual(i, ((String)value.GetValue()).Length);
            }
        }
        public void False()
        {
            var bytes = new MsgPackFormatter().Value(false).GetStore().Bytes;

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

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

            Assert.AreEqual(false, j);
        }
        public void int128Test()
        {
            int i     = 128;
            var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

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

            Assert.AreEqual(i, j);
        }
        public void uint32()
        {
            {
                UInt32 i = 0xFFFF + 20;

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

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
        public void cast_large_type()
        {
            {
                Byte i = 0x7F + 20;

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

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Example #16
0
        public void True()
        {
            var f = new MsgPackFormatter();

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

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

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

            Assert.AreEqual(true, j);
        }
        public void int8()
        {
            {
                SByte i = -64;

                var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

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

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
        public void int16()
        {
            {
                Int16 i = -150;

                var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

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

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
        public void int64()
        {
            {
                Int64 i = -2147483650;

                var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

                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);
            }
        }
        public void int32()
        {
            {
                Int32 i = -35000;

                var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

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

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
        public void Float64()
        {
            var i         = 1.1;
            var double_be = new Byte[] {
                0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a,
            };

            var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

            var value = MsgPackParser.Parse(bytes);
            var body  = value.GetBody();

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

            Assert.AreEqual(i, value.GetValue());
        }
Example #22
0
        public void uint16()
        {
            {
                UInt16 i = 0xFF + 20;

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

                var j = MsgPackParser.Parse(bytes).GetValue();
                Assert.AreEqual(i, j);
            }
        }
Example #23
0
        public void uint8()
        {
            {
                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);
            }
        }
        public void Float32()
        {
            var i        = 1.1f;
            var float_be = new byte[]
            {
                0x3f, 0x8c, 0xcc, 0xcd
            };

            var bytes = new MsgPackFormatter().Value(i).GetStore().Bytes;

            var value = MsgPackParser.Parse(bytes);
            var body  = value.GetBody();

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

            Assert.AreEqual(i, value.GetValue());
        }
Example #25
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());
            }
        }
Example #26
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());
        }
Example #27
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());
        }
Example #28
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());
                });
            }
        }
        public void fix_array()
        {
            var f = new MsgPackFormatter();

            f.Value(new[] { 0, 1, false, (Object)null });
            var bytes = f.GetStore().Bytes;

            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.ToEnumerable());

            var parsed = MsgPackParser.Parse(bytes);

            Assert.AreEqual(4, parsed.Count);
            Assert.AreEqual(0, parsed[0].GetValue());
            Assert.AreEqual(1, parsed[1].GetValue());
            Assert.False((Boolean)parsed[2].GetValue());
            Assert.AreEqual(null, parsed[3].GetValue());
        }
        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.GetStore().Bytes.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());
        }