Exemple #1
0
        public void ReserveBytes1()
        {
            var builder = new BlobBuilder(16);
            var writer0 = new BlobWriter(builder.ReserveBytes(0));
            var writer1 = new BlobWriter(builder.ReserveBytes(1));
            var writer2 = new BlobWriter(builder.ReserveBytes(2));

            Assert.Equal(3, builder.Count);
            AssertEx.Equal(new byte[] { 0, 0, 0 }, builder.ToArray());

            Assert.Equal(0, writer0.Length);
            Assert.Equal(0, writer0.RemainingBytes);

            writer1.WriteBoolean(true);
            Assert.Equal(1, writer1.Length);
            Assert.Equal(0, writer1.RemainingBytes);

            writer2.WriteByte(1);
            Assert.Equal(2, writer2.Length);
            Assert.Equal(1, writer2.RemainingBytes);
        }
Exemple #2
0
        public void LinkSuffix2()
        {
            var builder1 = new BlobBuilder(16);

            builder1.WriteBytes(1, 16);

            var builder2 = new BlobBuilder(16);

            builder2.WriteBytes(2, 16);

            builder1.LinkSuffix(builder2);

            AssertEx.Equal(new byte[]
            {
                0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
                0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02
            }, builder1.ToArray());

            Assert.Equal(32, builder1.Count);
            Assert.Equal(16, builder2.Count);
        }
Exemple #3
0
        public void LinkSuffix_Empty3()
        {
            var builder1 = new BlobBuilder(16);

            builder1.ReserveBytes(16);
            builder1.ReserveBytes(0);

            var builder2 = new BlobBuilder(16);

            builder2.ReserveBytes(16);
            builder2.ReserveBytes(0);
            builder1.LinkSuffix(builder2);

            AssertEx.Equal(new byte[]
            {
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            }, builder1.ToArray());

            Assert.Equal(32, builder1.Count);
            Assert.Equal(16, builder2.Count);
        }
Exemple #4
0
        public void WriteBytes1()
        {
            var writer = new BlobBuilder(4);

            writer.WriteBytes(new byte[] { 1, 2, 3, 4 });
            writer.WriteBytes(new byte[] { });
            writer.WriteBytes(new byte[] { }, 0, 0);
            writer.WriteBytes(new byte[] { 5, 6, 7, 8 });
            writer.WriteBytes(new byte[] { 9 });
            writer.WriteBytes(new byte[] { 0x0a }, 0, 0);
            writer.WriteBytes(new byte[] { 0x0b }, 0, 1);
            writer.WriteBytes(new byte[] { 0x0c }, 1, 0);
            writer.WriteBytes(new byte[] { 0x0d, 0x0e }, 1, 1);

            AssertEx.Equal(new byte[]
            {
                0x01, 0x02, 0x03, 0x04,
                0x05, 0x06, 0x07, 0x08,
                0x09,
                0x0b,
                0x0e
            }, writer.ToArray());
        }
Exemple #5
0
        public void FromHash()
        {
            var id1 = BlobContentId.FromHash(ImmutableArray.Create <byte>(
                                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20));

            AssertEx.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x48, 0x89, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 }, id1.Guid.ToByteArray());
            AssertEx.Equal(new byte[] { 0x11, 0x12, 0x13, 0x94 }, BitConverter.GetBytes(id1.Stamp));

            var id2 = BlobContentId.FromHash(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

            AssertEx.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, id2.Guid.ToByteArray());
            AssertEx.Equal(new byte[] { 0x00, 0x00, 0x00, 0x80 }, BitConverter.GetBytes(id2.Stamp));

            var id3 = BlobContentId.FromHash(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });

            AssertEx.Equal(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x4f, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, id3.Guid.ToByteArray());
            AssertEx.Equal(new byte[] { 0xff, 0xff, 0xff, 0xff }, BitConverter.GetBytes(id3.Stamp));

            Assert.Throws <ArgumentNullException>(() => BlobContentId.FromHash(default(ImmutableArray <byte>)));
            Assert.Throws <ArgumentNullException>(() => BlobContentId.FromHash(null));
            Assert.Throws <ArgumentException>(() => BlobContentId.FromHash(new byte[0]));
            Assert.Throws <ArgumentException>(() => BlobContentId.FromHash(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }));
        }
Exemple #6
0
        public void WritePrimitive()
        {
            var writer = new BlobBuilder(17);

            writer.WriteUInt32(0x11223344);
            writer.WriteUInt16(0x5566);
            writer.WriteByte(0x77);
            writer.WriteUInt64(0x8899aabbccddeeff);
            writer.WriteInt32(-1);
            writer.WriteInt16(-2);
            writer.WriteSByte(-3);
            writer.WriteBoolean(true);
            writer.WriteBoolean(false);
            writer.WriteInt64(unchecked ((long)0xfedcba0987654321));
            writer.WriteDateTime(new DateTime(0x1112223334445556));
            writer.WriteDecimal(102030405060.70m);
            writer.WriteDouble(double.NaN);
            writer.WriteSingle(float.NegativeInfinity);

            AssertEx.Equal(new byte[]
            {
                0x44, 0x33, 0x22, 0x11,
                0x66, 0x55,
                0x77,
                0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
                0xff, 0xff, 0xff, 0xff,
                0xfe, 0xff,
                0xfd,
                0x01,
                0x00,
                0x21, 0x43, 0x65, 0x87, 0x09, 0xBA, 0xDC, 0xFE,
                0x56, 0x55, 0x44, 0x34, 0x33, 0x22, 0x12, 0x11,
                0x02, 0xD6, 0xE0, 0x9A, 0x94, 0x47, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
                0x00, 0x00, 0x80, 0xFF
            }, writer.ToArray());
        }
Exemple #7
0
        public void ToImmutableArray()
        {
            var builder = new BlobBuilder(16);

            AssertEx.Equal(new byte[] { }, builder.ToArray(0, 0));

            for (int i = 0; i < 13; i++)
            {
                builder.WriteByte((byte)i);
            }

            builder.WriteUInt32(0xaabbccdd);

            AssertEx.Equal(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0xDD, 0xCC, 0xBB, 0xAA }, builder.ToImmutableArray());
            AssertEx.Equal(new byte[] { }, builder.ToImmutableArray(0, 0));
            AssertEx.Equal(new byte[] { 0 }, builder.ToImmutableArray(0, 1));
            AssertEx.Equal(new byte[] { 1 }, builder.ToImmutableArray(1, 1));

            AssertEx.Equal(new byte[] { }, builder.ToImmutableArray(14, 0));
            AssertEx.Equal(new byte[] { }, builder.ToImmutableArray(15, 0));
            AssertEx.Equal(new byte[] { }, builder.ToImmutableArray(16, 0));
            AssertEx.Equal(new byte[] { }, builder.ToImmutableArray(17, 0));

            AssertEx.Equal(new byte[] { 0xdd }, builder.ToImmutableArray(13, 1));
            AssertEx.Equal(new byte[] { 0xcc }, builder.ToImmutableArray(14, 1));
            AssertEx.Equal(new byte[] { 0xbb }, builder.ToImmutableArray(15, 1));
            AssertEx.Equal(new byte[] { 0xaa }, builder.ToImmutableArray(16, 1));

            AssertEx.Equal(new byte[] { 0xdd, 0xcc }, builder.ToImmutableArray(13, 2));
            AssertEx.Equal(new byte[] { 0xcc, 0xbb }, builder.ToImmutableArray(14, 2));
            AssertEx.Equal(new byte[] { 0xbb, 0xaa }, builder.ToImmutableArray(15, 2));

            AssertEx.Equal(new byte[] { 0xdd, 0xcc, 0xbb }, builder.ToImmutableArray(13, 3));
            AssertEx.Equal(new byte[] { 0xcc, 0xbb, 0xaa }, builder.ToImmutableArray(14, 3));

            AssertEx.Equal(new byte[] { 0xdd, 0xcc, 0xbb, 0xaa }, builder.ToImmutableArray(13, 4));
        }
        public unsafe void External()
        {
            var array = new byte[] { 1, 2, 3 };

            fixed(byte *arrayPtr = array)
            {
                using (var provider = new ExternalMemoryBlockProvider(arrayPtr, array.Length))
                {
                    using (var block = provider.GetMemoryBlock())
                    {
                        Assert.Equal(3, block.Size);
                        AssertEx.Equal(provider.Pointer, block.Pointer);
                        AssertEx.Equal(array, block.GetContent());
                    }

                    using (var block = provider.GetMemoryBlock(1, 2))
                    {
                        AssertEx.Equal(provider.Pointer + 1, block.Pointer);
                        Assert.Equal(2, block.Size);
                        AssertEx.Equal(new byte[] { 2, 3 }, block.GetContent());
                    }
                }
            }
        }
Exemple #9
0
        public void WriteUTF8_Substring()
        {
            var writer = new BlobBuilder(4);

            writer.WriteUTF8("abc", 0, 0, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new byte[0], writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 0, 1, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new[] { (byte)'a' }, writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 0, 2, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new[] { (byte)'a', (byte)'b' }, writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 0, 3, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new[] { (byte)'a', (byte)'b', (byte)'c' }, writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 1, 0, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new byte[0], writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 1, 1, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new[] { (byte)'b' }, writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 1, 2, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new[] { (byte)'b', (byte)'c' }, writer.ToArray());
            writer.Clear();

            writer.WriteUTF8("abc", 2, 1, allowUnpairedSurrogates: true, prependSize: false);
            AssertEx.Equal(new[] { (byte)'c' }, writer.ToArray());
            writer.Clear();
        }
Exemple #10
0
        public unsafe void ReadFromMemoryReader()
        {
            byte[] buffer = new byte[4] {
                0, 1, 0, 2
            };
            fixed(byte *bufferPtr = buffer)
            {
                var reader = new BlobReader(new MemoryBlock(bufferPtr, buffer.Length));

                Assert.False(reader.SeekOffset(-1));
                Assert.False(reader.SeekOffset(Int32.MaxValue));
                Assert.False(reader.SeekOffset(Int32.MinValue));
                Assert.False(reader.SeekOffset(buffer.Length));
                Assert.True(reader.SeekOffset(buffer.Length - 1));

                Assert.True(reader.SeekOffset(0));
                Assert.Equal(0, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadUInt64());
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(1));
                Assert.Equal(1, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadDouble());
                Assert.Equal(1, reader.Offset);

                Assert.True(reader.SeekOffset(2));
                Assert.Equal(2, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadUInt32());
                Assert.Equal((ushort)0x0200, reader.ReadUInt16());
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(2));
                Assert.Equal(2, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadSingle());
                Assert.Equal(2, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Equal(9.404242E-38F, reader.ReadSingle());
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(3));
                Assert.Equal(3, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadUInt16());
                Assert.Equal((byte)0x02, reader.ReadByte());
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Equal("\u0000\u0001\u0000\u0002", reader.ReadUTF8(4));
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadUTF8(5));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadUTF8(-1));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Equal("\u0100\u0200", reader.ReadUTF16(4));
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadUTF16(5));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadUTF16(-1));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadUTF16(6));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                AssertEx.Equal(buffer, reader.ReadBytes(4));
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Same(String.Empty, reader.ReadUtf8NullTerminated());
                Assert.Equal(1, reader.Offset);

                Assert.True(reader.SeekOffset(1));
                Assert.Equal("\u0001", reader.ReadUtf8NullTerminated());
                Assert.Equal(3, reader.Offset);

                Assert.True(reader.SeekOffset(3));
                Assert.Equal("\u0002", reader.ReadUtf8NullTerminated());
                Assert.Equal(4, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Same(String.Empty, reader.ReadUtf8NullTerminated());
                Assert.Equal(1, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadBytes(5));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.ReadBytes(Int32.MinValue));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.GetMemoryBlockAt(-1, 1));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Throws <BadImageFormatException>(() => reader.GetMemoryBlockAt(1, -1));
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(0));
                Assert.Equal(3, reader.GetMemoryBlockAt(1, 3).Length);
                Assert.Equal(0, reader.Offset);

                Assert.True(reader.SeekOffset(3));
                reader.ReadByte();
                Assert.Equal(4, reader.Offset);

                Assert.Equal(4, reader.Offset);
                Assert.Equal(0, reader.ReadBytes(0).Length);

                Assert.Equal(4, reader.Offset);
                int value;

                Assert.False(reader.TryReadCompressedInteger(out value));
                Assert.Equal(BlobReader.InvalidCompressedInteger, value);

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadCompressedInteger());

                Assert.Equal(4, reader.Offset);
                Assert.Equal(SerializationTypeCode.Invalid, reader.ReadSerializationTypeCode());

                Assert.Equal(4, reader.Offset);
                Assert.Equal(SignatureTypeCode.Invalid, reader.ReadSignatureTypeCode());

                Assert.Equal(4, reader.Offset);
                Assert.Equal(default(Handle), reader.ReadTypeHandle());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadBoolean());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadByte());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadSByte());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadUInt32());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadInt32());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadUInt64());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadInt64());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadSingle());

                Assert.Equal(4, reader.Offset);
                Assert.Throws <BadImageFormatException>(() => reader.ReadDouble());

                Assert.Equal(4, reader.Offset);
            }

            byte[] buffer2 = new byte[8] {
                1, 2, 3, 4, 5, 6, 7, 8
            };
            fixed(byte *bufferPtr2 = buffer2)
            {
                var reader = new BlobReader(new MemoryBlock(bufferPtr2, buffer2.Length));

                Assert.Equal(reader.Offset, 0);
                Assert.Equal(0x0807060504030201UL, reader.ReadUInt64());
                Assert.Equal(reader.Offset, 8);

                reader.Reset();
                Assert.Equal(reader.Offset, 0);
                Assert.Equal(0x0807060504030201L, reader.ReadInt64());

                reader.Reset();
                Assert.Equal(reader.Offset, 0);
                Assert.Equal(BitConverter.ToDouble(buffer2, 0), reader.ReadDouble());
            }
        }