Exemple #1
0
 public void TestEncode_Min96()
 {
     Assert.That(
         new Timestamp(-9223372036854775808L, 0).Encode(),
         Is.EqualTo(MessagePackExtendedTypeObject.Unpack(0xFF, new byte[] { 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0 }))
         );
 }
Exemple #2
0
        /// <summary>
        ///		Decodes specified <see cref="MessagePackExtendedTypeObject"/> and returns an equivalant <see cref="Timestamp"/>.
        /// </summary>
        /// <param name="value"><see cref="MessagePackExtendedTypeObject"/> which is native representation of <see cref="Timestamp"/>.</param>
        /// <returns><see cref="Timestamp"/>.</returns>
        /// <exception cref="ArgumentException">
        ///		<paramref name="value"/> does not represent msgpack timestamp. Specifically, the type code is not equal to <see cref="TypeCode"/> value.
        ///		Or, <paramref name="value"/> does not have valid msgpack timestamp structure.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///		<paramref name="value"/> have invalid nanoseconds value.
        /// </exception>
        /// <remarks>
        ///		A definition of valid msgpack time stamp is:
        ///		<list type="bullet">
        ///			<item>Its type code is <c>0xFF</c>(<c>-1</c>).</item>
        ///			<item>Its length is 4, 8, or 12 bytes.</item>
        ///			<item>Its nanoseconds part is between 0 and 999,999,999.</item>
        ///		</list>
        /// </remarks>
        public static Timestamp Decode(MessagePackExtendedTypeObject value)
        {
            if (value.TypeCode != TypeCode)
            {
                throw new ArgumentException("The value's type code must be 0xFF.", "value");
            }

            switch (value.Body.Length)
            {
            case 4:
            {
                // timespan32 format
                return(new Timestamp(BigEndianBinary.ToUInt32(value.Body, 0), 0));
            }

            case 8:
            {
                // timespan64 format
                var payload = BigEndianBinary.ToUInt64(value.Body, 0);
                return(new Timestamp(unchecked (( long )(payload & 0x00000003ffffffffL)), unchecked (( int )(payload >> 34))));
            }

            case 12:
            {
                // timespan96 format
                return(new Timestamp(BigEndianBinary.ToInt64(value.Body, sizeof(int)), unchecked (( int )BigEndianBinary.ToUInt32(value.Body, 0))));
            }

            default:
            {
                throw new ArgumentException("The value's length is not valid.", "value");
            }
            }
        }
Exemple #3
0
 public void TestEncode_Min64()
 {
     Assert.That(
         new Timestamp(0L, 1).Encode(),
         Is.EqualTo(MessagePackExtendedTypeObject.Unpack(0xFF, new byte[] { 0, 0, 0, 0x4, 0, 0, 0, 0 }))
         );
 }
Exemple #4
0
 public void TestEncode_Max64()
 {
     Assert.That(
         new Timestamp(17179869183L, 999999999).Encode(),
         Is.EqualTo(MessagePackExtendedTypeObject.Unpack(0xFF, new byte[] { 0xEE, 0x6B, 0x27, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }))
         );
 }
Exemple #5
0
 public void TestEncode_Max32()
 {
     Assert.That(
         new Timestamp(4294967295L, 0).Encode(),
         Is.EqualTo(MessagePackExtendedTypeObject.Unpack(0xFF, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }))
         );
 }
Exemple #6
0
 public void TestEncode_Max96()
 {
     Assert.That(
         new Timestamp(9223372036854775807L, 999999999).Encode(),
         Is.EqualTo(MessagePackExtendedTypeObject.Unpack(0xFF, new byte[] { 0x3B, 0x9A, 0xC9, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }))
         );
 }
Exemple #7
0
        public void TestToString_ExtendedTypeObject_AsIs()
        {
            var mpeto = new MessagePackExtendedTypeObject(123, new byte[] { 1, 2, 3 });

            Assert.AreEqual(
                mpeto.ToString(),
                new MessagePackObject(mpeto).ToString()
                );
        }
        public void TestProperties()
        {
            byte typeCode = 0;
            var  body     = new byte[] { 1 };
            var  actual   = new MessagePackExtendedTypeObject(typeCode, body);

            Assert.That(actual.TypeCode, Is.EqualTo(typeCode));
            Assert.That(actual.Body, Is.SameAs(body));
            Assert.That(actual.GetBody(), Is.Not.SameAs(body).And.EqualTo(body));
        }
        public void TestEquality_Self()
        {
            var left  = new MessagePackExtendedTypeObject(0, new byte[] { 1 });
            var right = left;

            Assert.That(left.Equals(right), Is.True, "IEquatable.Equals");
            Assert.That(left.Equals(( object )right), Is.True, "Equals");
            Assert.That(left == right, Is.True, "==");
            Assert.That(left != right, Is.False, "!=");
        }
Exemple #10
0
        /// <summary>
        ///		Encodes this instance to a <see cref="MessagePackExtendedTypeObject"/>.
        /// </summary>
        /// <returns>A <see cref="MessagePackExtendedTypeObject"/> which equivalant to this instance.</returns>
        public MessagePackExtendedTypeObject Encode()
        {
            if ((this.unixEpochSeconds >> 34) != 0)
            {
                // timestamp 96
                var value = this;
                var body  = new byte[12];
                body[0]  = unchecked (( byte )((this.nanoseconds >> 24) & 0xFF));
                body[1]  = unchecked (( byte )((this.nanoseconds >> 16) & 0xFF));
                body[2]  = unchecked (( byte )((this.nanoseconds >> 8) & 0xFF));
                body[3]  = unchecked (( byte )((this.nanoseconds) & 0xFF));
                body[4]  = unchecked (( byte )((this.unixEpochSeconds >> 56) & 0xFF));
                body[5]  = unchecked (( byte )((this.unixEpochSeconds >> 48) & 0xFF));
                body[6]  = unchecked (( byte )((this.unixEpochSeconds >> 40) & 0xFF));
                body[7]  = unchecked (( byte )((this.unixEpochSeconds >> 32) & 0xFF));
                body[8]  = unchecked (( byte )((this.unixEpochSeconds >> 24) & 0xFF));
                body[9]  = unchecked (( byte )((this.unixEpochSeconds >> 16) & 0xFF));
                body[10] = unchecked (( byte )((this.unixEpochSeconds >> 8) & 0xFF));
                body[11] = unchecked (( byte )(this.unixEpochSeconds & 0xFF));

                return(MessagePackExtendedTypeObject.Unpack(TypeCode, body));
            }
            else
            {
                var encoded = ((( ulong )this.nanoseconds) << 34) | unchecked (( ulong )this.unixEpochSeconds);
                if ((encoded & 0xFFFFFFFF00000000L) == 0)
                {
                    // timestamp 32
                    var value = unchecked (( uint )encoded);
                    var body  = new byte[4];
                    body[0] = unchecked (( byte )((encoded >> 24) & 0xFF));
                    body[1] = unchecked (( byte )((encoded >> 16) & 0xFF));
                    body[2] = unchecked (( byte )((encoded >> 8) & 0xFF));
                    body[3] = unchecked (( byte )(encoded & 0xFF));

                    return(MessagePackExtendedTypeObject.Unpack(TypeCode, body));
                }
                else
                {
                    // timestamp 64
                    var body = new byte[8];
                    body[0] = unchecked (( byte )((encoded >> 56) & 0xFF));
                    body[1] = unchecked (( byte )((encoded >> 48) & 0xFF));
                    body[2] = unchecked (( byte )((encoded >> 40) & 0xFF));
                    body[3] = unchecked (( byte )((encoded >> 32) & 0xFF));
                    body[4] = unchecked (( byte )((encoded >> 24) & 0xFF));
                    body[5] = unchecked (( byte )((encoded >> 16) & 0xFF));
                    body[6] = unchecked (( byte )((encoded >> 8) & 0xFF));
                    body[7] = unchecked (( byte )(encoded & 0xFF));

                    return(MessagePackExtendedTypeObject.Unpack(TypeCode, body));
                }
            }
        }
Exemple #11
0
        public virtual bool ReadMessagePackExtendedTypeObject(out MessagePackExtendedTypeObject result)
        {
            if (!this.Read())
            {
                result = default(MessagePackExtendedTypeObject);
                return(false);
            }

            result = this.LastReadData.AsMessagePackExtendedTypeObject();
            return(true);
        }
Exemple #12
0
        public override bool ReadMessagePackExtendedTypeObject(out MessagePackExtendedTypeObject result)
        {
            this.DiscardCompletedStacks();

            if (this._itemsCount.Count == 0)
            {
                result = default(MessagePackExtendedTypeObject);
                return(false);
            }

            if (!this._root.ReadMessagePackExtendedTypeObject(out result))
            {
                return(false);
            }

            switch (this._internalRoot.CollectionType)
            {
            case CollectionType.Array:
            {
                this._itemsCount.Push(this._root.ItemsCount);
                this._unpacked.Push(0);
                this._isMap.Push(false);
                break;
            }

            case CollectionType.Map:
            {
                this._itemsCount.Push(this._root.ItemsCount * 2);
                this._unpacked.Push(0);
                this._isMap.Push(true);
                break;
            }

            default:
            {
                this._unpacked.Push(this._unpacked.Pop() + 1);
                break;
            }
            }

            return(true);
        }
        public void TestEquality_ValueEqual()
        {
            foreach (var testCase in
                     new[]
            {
                Tuple.Create(0, 0, new byte[] { 1 }, new byte[] { 1 }, true),
                Tuple.Create(0, 1, new byte[] { 1 }, new byte[] { 1 }, false),
                Tuple.Create(0, 0, new byte[] { 1 }, new byte[] { 1, 2 }, false),
                Tuple.Create(0, 0, new byte[] { 1 }, new byte[] { 2 }, false),
            })
            {
                checked
                {
                    MessagePackObject left  = new MessagePackExtendedTypeObject(( byte )testCase.Item1, testCase.Item3);
                    MessagePackObject right = new MessagePackExtendedTypeObject(( byte )testCase.Item2, testCase.Item4);

                    Assert.That(left.Equals(right), Is.EqualTo(testCase.Item5), "IEquatable.Equals");
                    Assert.That(left.Equals(( object )right), Is.EqualTo(testCase.Item5), "Equals");
                    Assert.That(left == right, Is.EqualTo(testCase.Item5), "==");
                    Assert.That(left != right, Is.EqualTo(!testCase.Item5), "!=");
                }
            }
        }
Exemple #14
0
        public void TestExts()
        {
            var sw = new Stopwatch();

            foreach (
                var count in
                new[]
            {
                0,                                 // empty
                1,                                 // fixext1
                2,                                 // fixext2
                4,                                 // fixext4
                8,                                 // fixext8
                16,                                // fixext16
                17,                                // min ext8 size
                0xff,                              // max ext8 size
                0x100,                             // min ext16 size
                0xffff,                            // max ext16 size
                0x10000,                           // min ext32 size
            }
                )
            {
                sw.Reset();
                sw.Start();
                var output = new MemoryStream();
                var value  = new MessagePackExtendedTypeObject(
                    1, Enumerable.Range(0, count).Select(i => ( byte )(i % 0x100)).ToArray());
                Packer.Create(output, PackerCompatibilityOptions.None).PackExtendedTypeValue(value);
                Assert.AreEqual(
                    value,
                    UnpackOne(output).AsMessagePackExtendedTypeObject()
                    );
                sw.Stop();
            }

            Console.WriteLine("Ext: {0:0.###} msec/byte", sw.ElapsedMilliseconds / 0x10000);
        }
Exemple #15
0
 public void TestExts_Splitted()
 {
     foreach (
         var count in
         new[]
     {
         0,                                 // empty
         1,                                 // fixext1
         2,                                 // fixext2
         4,                                 // fixext4
         8,                                 // fixext8
         16,                                // fixext16
         17,                                // min ext8 size
         0xff,                              // max ext8 size
         0x100,                             // min ext16 size
         0xffff,                            // max ext16 size
         0x10000,                           // min ext32 size
     }
         )
     {
         using (var output = new MemoryStream())
         {
             var value = new MessagePackExtendedTypeObject(
                 1, Enumerable.Range(0, count).Select(i => ( byte )(i % 0x100)).ToArray());
             Packer.Create(output, PackerCompatibilityOptions.None).PackExtendedTypeValue(value);
             output.Position = 0;
             using (var splitted = new SplittingStream(output))
             {
                 Assert.AreEqual(
                     value,
                     Unpacking.UnpackObject(splitted).AsMessagePackExtendedTypeObject()
                     );
             }
         }
     }
 }
        public void TestToObject_ExtendedTypeObject_Success()
        {
            var value = new MessagePackExtendedTypeObject(1, new byte[] { 1 });

            TestToObjectCore(value, value);
        }
Exemple #17
0
 public void TestDecode_InvalidLength_13()
 {
     Assert.Throws <ArgumentException>(() => Timestamp.Decode(MessagePackExtendedTypeObject.Unpack(0xFF, new byte [13])));
 }