Example #1
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 array16()
        {
            var f    = new MsgPackFormatter();
            var data = Enumerable.Range(0, 20).Select(x => (Object)x).ToArray();

            f.Value(data);
            var bytes = f.GetStore().Bytes;

            var value = MsgPackParser.Parse(bytes);

            Assert.IsTrue(value.IsArray);
            Assert.AreEqual(20, value.Count);
            for (int i = 0; i < 20; ++i)
            {
                Assert.AreEqual(i, value[i].GetValue());
            }
        }
        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());
        }