UnpackDictionaryCount() public static method

Unpacks count of the dictionary entries value from the specified Stream.

The processed bytes count can be calculated via P:Stream.Position of source when the P:Stream.CanSeek is true.

When the type of packed value is not known, use UnpackObject(Stream) instead.

/// is null. /// /// The of is false. /// /// is not valid MessagePack stream. /// Note that the state of will be unpredictable espicially it is not seekable. /// /// The unpacked result in the is not compatible to nullable . /// Note that the state of will be unpredictable espicially it is not seekable. ///
public static UnpackDictionaryCount ( Stream source ) : Int64?
source Stream The which contains Message Pack binary stream.
return Int64?
        public void TestUnpackDictionaryCount_ByteArray_FixMap0Value_AsMap16_AsIs()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0xDE, 0x00, 0x00 });

            Assert.AreEqual(3, result.ReadCount);
            Assert.AreEqual(0, result.Value);
        }
        public void TestUnpackDictionaryCount_ByteArray_Null_Nil()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0xC0 });

            Assert.AreEqual(1, result.ReadCount);
            Assert.IsNull(result.Value);
        }
        public void TestUnpackDictionaryCount_ByteArray_FixMapMaxValue_AsFixMap15_AsIs()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0x8F }.Concat(CreateDictionaryBodyBinary(0xF)).ToArray());

            Assert.AreEqual(1, result.ReadCount);
            Assert.AreEqual(0xF, result.Value);
        }
        public void TestUnpackDictionaryCount_ByteArray_Offset_OffsetIsValid_OffsetIsRespected()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0xFF, 0x80, 0xFF }, 1);

            Assert.AreEqual(1, result.ReadCount);
            Assert.AreEqual(0, result.Value);
        }
        public void TestUnpackDictionaryCount_ByteArray_Map32MinValue_AsMap32_AsIs()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0xDF, 0x00, 0x01, 0x00, 0x00 }.Concat(CreateDictionaryBodyBinary(0x10000)).ToArray());

            Assert.AreEqual(5, result.ReadCount);
            Assert.AreEqual(0x10000, result.Value);
        }
        public void TestUnpackDictionaryCount_ByteArray_Map16MaxValue_AsMap16_AsIs()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0xDE, 0xFF, 0xFF }.Concat(CreateDictionaryBodyBinary(0xFFFF)).ToArray());

            Assert.AreEqual(3, result.ReadCount);
            Assert.AreEqual(0xFFFF, result.Value);
        }
Beispiel #7
0
        public void TestUnpackDictionaryCount_DictionaryCountIsGreaterThanInt32MaxValue()
        {
            var result = Unpacking.UnpackDictionaryCount(new byte[] { 0xDF, 0x80, 0x00, 0x00, 0x00, 0xFF });

            Assert.That(result.ReadCount, Is.EqualTo(5));
            Assert.That(result.Value, Is.EqualTo(Int32.MaxValue + 1L));
        }
 public void TestUnpackDictionaryCount_Stream_Map32MinValue_AsMap32_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0xDF, 0x00, 0x01, 0x00, 0x00 }.Concat(CreateDictionaryBodyBinary(0x10000)).ToArray()))
     {
         var result = Unpacking.UnpackDictionaryCount(buffer);
         Assert.AreEqual(5, buffer.Position);
         Assert.AreEqual(0x10000, result);
     }
 }
 public void TestUnpackDictionaryCount_Stream_Map16MaxValue_AsMap16_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0xDE, 0xFF, 0xFF }.Concat(CreateDictionaryBodyBinary(0xFFFF)).ToArray()))
     {
         var result = Unpacking.UnpackDictionaryCount(buffer);
         Assert.AreEqual(3, buffer.Position);
         Assert.AreEqual(0xFFFF, result);
     }
 }
 public void TestUnpackDictionaryCount_Stream_FixMap0Value_AsFixMap0_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0x80 }))
     {
         var result = Unpacking.UnpackDictionaryCount(buffer);
         Assert.AreEqual(1, buffer.Position);
         Assert.AreEqual(0, result);
     }
 }
 public void TestUnpackDictionaryCount_Stream_FixMap1Value_AsFixMap1_AsIs()
 {
     using (var buffer = new MemoryStream(new byte[] { 0x81 }.Concat(CreateDictionaryBodyBinary(1)).ToArray()))
     {
         var result = Unpacking.UnpackDictionaryCount(buffer);
         Assert.AreEqual(1, buffer.Position);
         Assert.AreEqual(0x1, result);
     }
 }
 public void TestUnpackDictionaryCount_ByteArray_NotMap()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackDictionaryCount(new byte[] { 0x1 }));
 }
 public void TestUnpackDictionaryCount_Stream_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackDictionaryCount(default(Stream)));
 }
 public void TestUnpackDictionaryCount_ByteArray_Offset_Empty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackDictionaryCount(new byte[0], 0));
 }
 public void TestUnpackDictionaryCount_ByteArray_Offset_OffsetIsNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackDictionaryCount(new byte[] { 0x1 }, -1));
 }
 public void TestUnpackDictionaryCount_ByteArray_Offset_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackDictionaryCount(default(byte[]), 0));
 }
Beispiel #17
0
        public void TestMap()
        {
            var emptyMap = new Dictionary <int, int>();
            {
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(emptyMap);
                Assert.AreEqual(0, Unpacking.UnpackDictionaryCount(new MemoryStream(output.ToArray())));
                Assert.AreEqual(0, Unpacking.UnpackDictionaryCount(output.ToArray()).Value);
            }

            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var m   = new Dictionary <int, int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    m[j] = j;
                }
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(m);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackDictionaryCount(streamInput));
                for (int j = 0; j < len; j++)
                {
                    int value;
                    Assert.IsTrue(m.TryGetValue(Unpacking.UnpackInt32(streamInput), out value));
                    Assert.AreEqual(value, Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackDictionaryCount(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var keyUar = Unpacking.UnpackInt32(byteArrayInput, offset);
                    Assert.AreNotEqual(0, keyUar.ReadCount);
                    int value;
                    Assert.IsTrue(m.TryGetValue(keyUar.Value, out value));
                    var valueUar = Unpacking.UnpackInt32(byteArrayInput, offset + keyUar.ReadCount);
                    Assert.AreEqual(value, valueUar.Value);
                    offset += keyUar.ReadCount + valueUar.ReadCount;
                }
            }

            for (int i = 0; i < 100; i++)
            {
                var m   = new Dictionary <string, int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    m[j.ToString()] = j;
                }
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(m);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackDictionaryCount(streamInput));
                for (int j = 0; j < len; j++)
                {
                    int value;
                    Assert.IsTrue(m.TryGetValue(Unpacking.UnpackString(streamInput), out value));
                    Assert.AreEqual(value, Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackDictionaryCount(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var usr = Unpacking.UnpackString(byteArrayInput, offset);
                    Assert.AreNotEqual(0, usr.ReadCount);
                    int value;
                    Assert.IsTrue(m.TryGetValue(usr.Value, out value));
                    var uar = Unpacking.UnpackInt32(byteArrayInput, offset + usr.ReadCount);
                    Assert.AreEqual(value, uar.Value);
                    offset += usr.ReadCount + uar.ReadCount;
                }
            }
        }
 public void TestUnpackDictionaryCount_ByteArray_Offset_OffsetIsTooBig()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackDictionaryCount(new byte[] { 0x1 }, 1));
 }