public static UnpackObject ( Stream source ) : MessagePackObject | ||
source | Stream | The |
Результат | MessagePackObject |
public void TestUnpackObject_Stream_Scalar_AsIs() { using (var stream = new MemoryStream(new byte[] { 0x1 })) { Assert.That(Unpacking.UnpackObject(stream).Equals(1)); } }
public void TestArray_Splitted() { foreach ( var count in new[] { 0, // empty 1, // only one 2, // minimum multiple 0xf, // max fix array size 0x10, // min array16 size 0xffff, // max array16 size 0x10000, // min array32 size } ) { using (var output = new MemoryStream()) { Packer.Create(output).PackCollection(Enumerable.Range(0, count).ToArray()); output.Position = 0; using (var splitted = new SplittingStream(output)) { Assert.That( Enumerable.Range(0, count).ToArray(), Is.EqualTo(Unpacking.UnpackObject(splitted).AsEnumerable().Select(item => item.AsInt32()).ToArray()) ); } } } }
public void TestDictionary_Splitted() { foreach ( var count in new[] { 0, // empty 1, // only one 2, // minimum multiple 0xf, // max fix map size 0x10, // min map16 size 0xffff, // max map16 size 0x10000, // min map32 size } ) { using (var output = new MemoryStream()) { Packer.Create(output).Pack(Enumerable.Range(0, count).ToDictionary(item => item.ToString(), item => item)); output.Position = 0; using (var splitted = new SplittingStream(output)) { Assert.That( Enumerable.Range(0, count).ToDictionary(item => item.ToString(), item => item), Is.EqualTo(Unpacking.UnpackObject(splitted).AsDictionary().ToDictionary(kv => kv.Key.AsString(), kv => kv.Value.AsInt32())) ); } } } }
public void TestChars_Splitted() { foreach ( var count in new[] { 0, // empty 1, // only one 2, // minimum multiple 0x1f, // max fix raw size 0x20, // min str8 size 0xFF, // max str8 size 0x100, // min str16 size 0xffff, // max str16 size 0x10000, // min str32 size } ) { using (var output = new MemoryStream()) { Packer.Create(output).PackString(String.Concat(Enumerable.Range(0, count).Select(i => (i % 10).ToString()).ToArray())); output.Position = 0; using (var splitted = new SplittingStream(output)) { Assert.AreEqual( String.Concat(Enumerable.Range(0, count).Select(i => (i % 10).ToString()).ToArray()), Unpacking.UnpackObject(splitted).AsString() ); } } } }
public void TestBytes_Splitted() { foreach ( var count in new[] { 0, // empty 1, // only one 2, // minimum multiple 0xFF, // max bin8 size 0x100, // min bin16 size 0xffff, // max bin16 size 0x10000, // min bin32 size } ) { using (var output = new MemoryStream()) { Packer.Create(output).PackBinary(Enumerable.Range(0, count).Select(i => ( byte )(i % Byte.MaxValue)).ToArray()); output.Position = 0; using (var splitted = new SplittingStream(output)) { Assert.That( Enumerable.Range(0, count).Select(i => ( byte )(i % Byte.MaxValue)).ToArray(), Is.EqualTo(Unpacking.UnpackObject(splitted).AsBinary()) ); } } } }
public void TestUnpackObject_Stream_Nil_AsIs() { using (var stream = new MemoryStream(new byte[] { 0xC0 })) { Assert.That(Unpacking.UnpackObject(stream).IsNil); } }
public void TestUnpackObject_ByteArray_Int32_Scalar_AsIs() { var result = Unpacking.UnpackObject(new byte[] { 0xFF, 0x1, 0xFF }, 1); Assert.That(result.ReadCount, Is.EqualTo(1)); Assert.That(result.Value.Equals(1)); }
public void TestUnpackObject_Stream_NestedArray_AsIs() { using (var stream = new MemoryStream(new byte[] { 0x96, 0x90, 0x91, 0x1, 0x80, 0x81, 0x1, 0x1, 0xA0, 0xA1, ( byte )'A' })) { var result = Unpacking.UnpackObject(stream); AssertNested(result.AsList()); } }
public void TestUnpackObject_Stream_EmptyMap_AsIs() { using (var stream = new MemoryStream(new byte[] { 0x80 })) { var result = Unpacking.UnpackObject(stream); Assert.That(result.IsArray, Is.False); Assert.That(result.IsMap, Is.True); Assert.That(result.IsRaw, Is.False); Assert.That(result.AsDictionary(), Is.Not.Null.And.Empty); } }
public void TestUnpackObject_Stream_Map_AsIs() { using (var stream = new MemoryStream(new byte[] { 0x81, 0x1, 0x1 })) { var result = Unpacking.UnpackObject(stream); Assert.That(result.IsArray, Is.False); Assert.That(result.IsMap, Is.True); Assert.That(result.IsRaw, Is.False); Assert.That(result.AsDictionary(), Is.Not.Null.And.Count.EqualTo(1).And.All.EqualTo(new KeyValuePair <MessagePackObject, MessagePackObject>(1, 1))); } }
public void TestUnpackObject_Stream_Array_AsIs() { using (var stream = new MemoryStream(new byte[] { 0x91, 0x1 })) { var result = Unpacking.UnpackObject(stream); Assert.That(result.IsArray, Is.True); Assert.That(result.IsMap, Is.False); Assert.That(result.IsRaw, Is.False); Assert.That(result.AsList(), Is.Not.Null.And.Length.EqualTo(1).And.All.EqualTo(new MessagePackObject(1))); } }
public void TestUnpackObject_Stream_Raw_AsIs() { using (var stream = new MemoryStream(new byte[] { 0xA1, ( byte )'A' })) { var result = Unpacking.UnpackObject(stream); Assert.That(result.IsArray, Is.False); Assert.That(result.IsMap, Is.False); Assert.That(result.IsRaw, Is.True); Assert.That(result.AsBinary(), Is.EqualTo(new byte[] { ( byte )'A' })); Assert.That(result.AsString(), Is.EqualTo("A")); } }
private static MessagePackObject UnpackOne(MemoryStream output) { output.Seek(0L, SeekOrigin.Begin); if (_traceUnpackingBytes) { Console.WriteLine("Unpaking:"); foreach (var b in output.ToArray().Take(100)) { Console.Write(b.ToString("x2")); } Console.WriteLine(); } return(Unpacking.UnpackObject(output)); }
public void TestByte_Splitted() { var value = Byte.MaxValue; using (var output = new MemoryStream()) { Packer.Create(output).Pack(value); output.Position = 0; using (var splitted = new SplittingStream(output)) { var mpo = Unpacking.UnpackObject(splitted); Assert.AreEqual(value, ( Byte )mpo); Assert.AreEqual(value, mpo.AsByte()); } } }
public void TestMultipleObjectInStream() { using (var stream = new MemoryStream()) { var packer = Packer.Create(stream); packer.Pack(1); packer.PackString("1"); stream.Position = 0; var item = Unpacking.UnpackObject(stream); Assert.That(item, Is.Not.Null); Assert.That(item.IsTypeOf <int>().Value); Assert.That(item.UnderlyingType.GetIsPrimitive(), Is.True); Assert.That(item.AsInt32(), Is.EqualTo(1)); item = Unpacking.UnpackObject(stream); Assert.That(item, Is.Not.Null); Assert.That(item.UnderlyingType, Is.EqualTo(typeof(String))); Assert.That(item.IsTypeOf <string>().Value); Assert.That(item.AsString(), Is.EqualTo("1")); } }
public void TestUnpackObject_Stream_NestedMap_AsIs() { using (var stream = new MemoryStream(new byte[] { 0x86, 0x1, 0x90, 0x2, 0x91, 0x1, 0x3, 0x80, 0x4, 0x81, 0x1, 0x1, 0x5, 0xA0, 0x6, 0xA1, ( byte )'A' })) { var result = Unpacking.UnpackObject(stream); var dictionary = result.AsDictionary(); Assert.That(dictionary, Is.Not.Null.And.Count.EqualTo(6)); AssertNested( new MessagePackObject[] { dictionary [1], dictionary [2], dictionary [3], dictionary [4], dictionary [5], dictionary [6] } ); } }
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 TestUnpackObject_ByteArray_Int32_OffsetIsTooBig() { Assert.Throws <ArgumentException>(() => Unpacking.UnpackObject(new byte[] { 0x1 }, 1)); }
public void TestUnpackObject_Stream_Null() { Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackObject(default(Stream))); }
public void TestUnpackObject_ByteArray_ByteArrayIsNull() { Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackObject(default(byte[]))); }
public void TestUnpackObject_ByteArray_Int32_ByteArrayIsNull() { Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackObject(null, 0)); }
public void TestUnpackObject_ByteArray_Int32_ByteArrayIsEmpty() { Assert.Throws <ArgumentException>(() => Unpacking.UnpackObject(new byte[0], 0)); }
public void TestUnpackObject_ByteArray_Int32_OffsetIsNegative() { Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackObject(new byte[] { 0x1 }, -1)); }