public void CountClear() { var builder = new BlobBuilder(); Assert.Equal(0, builder.Count); builder.WriteByte(1); Assert.Equal(1, builder.Count); builder.WriteInt32(4); Assert.Equal(5, builder.Count); builder.Clear(); Assert.Equal(0, builder.Count); builder.WriteInt64(1); Assert.Equal(8, builder.Count); AssertEx.Equal(new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, builder.ToArray()); }
public void WriteUTF16() { var writer = new BlobBuilder(4); writer.WriteUTF16(""); writer.WriteUTF16("a"); writer.WriteUTF16(""); writer.WriteUTF16(new char[0]); writer.WriteUTF16(new char[] { '\ud800' }); // hi surrogate writer.WriteUTF16("\udc00"); // lo surrogate writer.WriteUTF16("\ud800\udc00"); // pair writer.WriteUTF16(new char[] { '\udc00', '\ud800' }); // lo + hi writer.WriteUTF16("\u1234"); AssertEx.Equal(new byte[] { 0x61, 0x00, 0x00, 0xD8, 0x00, 0xDC, 0x00, 0xD8, 0x00, 0xDC, 0x00, 0xDC, 0x00, 0xD8, 0x34, 0x12 }, writer.ToArray()); }
public void WriteAlignPad() { var writer = new BlobBuilder(4); writer.WriteByte(0x01); writer.PadTo(2); writer.WriteByte(0x02); writer.Align(4); writer.Align(4); writer.WriteByte(0x03); writer.Align(4); writer.WriteByte(0x04); writer.WriteByte(0x05); writer.Align(8); writer.WriteByte(0x06); writer.Align(2); writer.Align(1); AssertEx.Equal(new byte[] { 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00 }, writer.ToArray()); }
public void WriteBytes2() { var writer = new BlobBuilder(4); writer.WriteBytes(0xff, 0); writer.WriteBytes(1, 4); writer.WriteBytes(0xff, 0); writer.WriteBytes(2, 10); writer.WriteBytes(0xff, 0); writer.WriteBytes(3, 1); AssertEx.Equal(new byte[] { 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03 }, writer.ToArray()); }
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()); }
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()); }
private static void TestCompressedSignedInteger(byte[] expected, int value) { var writer = new BlobWriter(4); writer.WriteCompressedSignedInteger(value); AssertEx.Equal(expected, writer.ToArray()); var builder = new BlobBuilder(); builder.WriteCompressedSignedInteger(value); AssertEx.Equal(expected, builder.ToArray()); }
private static byte[] CompressSignedInteger(int value) { var writer = new BlobBuilder(); writer.WriteCompressedSignedInteger(value); return writer.ToArray(); }
public void Link() { var builder1 = new BlobBuilder(16); builder1.WriteByte(1); var builder2 = new BlobBuilder(16); builder2.WriteByte(2); var builder3 = new BlobBuilder(16); builder3.WriteByte(3); var builder4 = new BlobBuilder(16); builder4.WriteByte(4); var builder5 = new BlobBuilder(16); builder5.WriteByte(5); builder2.LinkPrefix(builder1); AssertEx.Equal(new byte[] { 1, 2 }, builder2.ToArray()); Assert.Throws<InvalidOperationException>(() => builder1.ToArray()); Assert.Throws<InvalidOperationException>(() => builder2.LinkPrefix(builder1)); Assert.Throws<InvalidOperationException>(() => builder1.WriteByte(0xff)); Assert.Throws<InvalidOperationException>(() => builder1.WriteBytes(1, 10)); Assert.Throws<InvalidOperationException>(() => builder1.WriteBytes(new byte[] { 1 })); Assert.Throws<InvalidOperationException>(() => builder1.ReserveBytes(1)); Assert.Throws<InvalidOperationException>(() => builder1.GetBlobs()); Assert.Throws<InvalidOperationException>(() => builder1.ContentEquals(builder1)); Assert.Throws<InvalidOperationException>(() => builder1.WriteUTF16("str")); Assert.Throws<InvalidOperationException>(() => builder1.WriteUTF8("str", allowUnpairedSurrogates: false)); builder2.LinkSuffix(builder3); AssertEx.Equal(new byte[] { 1, 2, 3 }, builder2.ToArray()); Assert.Throws<InvalidOperationException>(() => builder3.LinkPrefix(builder5)); builder2.LinkPrefix(builder4); AssertEx.Equal(new byte[] { 4, 1, 2, 3 }, builder2.ToArray()); Assert.Throws<InvalidOperationException>(() => builder4.LinkPrefix(builder5)); builder2.LinkSuffix(builder5); AssertEx.Equal(new byte[] { 4, 1, 2, 3, 5 }, builder2.ToArray()); }
public void LinkPrefix1() { var builder1 = new BlobBuilder(16); builder1.WriteByte(1); builder1.WriteByte(2); builder1.WriteByte(3); var builder2 = new BlobBuilder(16); builder2.WriteByte(4); builder1.LinkPrefix(builder2); AssertEx.Equal(new byte[] { 4, 1, 2, 3 }, builder1.ToArray()); }
public void WriteContentToBlobBuilder() { var builder1 = new BlobBuilder(16); for (int i = 0; i < 20; i++) { builder1.WriteByte((byte)i); } var builder2 = new BlobBuilder(256); builder1.WriteContentTo(builder2); AssertEx.Equal(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 }, builder2.ToArray()); builder1.WriteByte(0xff); builder1.WriteContentTo(builder2); AssertEx.Equal(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0xff, }, builder2.ToArray()); }
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 void ToArray_Errors() { var builder = new BlobBuilder(16); builder.WriteByte(1); Assert.Throws<ArgumentOutOfRangeException>(() => builder.ToArray(-1, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.ToArray(0, -1)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.ToArray(0, 2)); Assert.Throws<ArgumentOutOfRangeException>(() => builder.ToArray(1, 1)); }
public void ToArray2() { var builder = new BlobBuilder(16); AssertEx.Equal(new byte[] { }, builder.ToArray(0, 0)); for (int i = 0; i < 34; i++) { builder.WriteByte((byte)i); } AssertEx.Equal(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21 }, builder.ToArray()); AssertEx.Equal(new byte[] { 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21 }, builder.ToArray(0x0e, 20)); AssertEx.Equal(new byte[] { 0x0E }, builder.ToArray(0x0e, 1)); AssertEx.Equal(new byte[] { 0x0E, 0x0F }, builder.ToArray(0x0e, 2)); AssertEx.Equal(new byte[] { 0x0E, 0x0F, 0x10 }, builder.ToArray(0x0e, 3)); AssertEx.Equal(new byte[] { 0x0E, 0x0F, 0x10, 0x11 }, builder.ToArray(0x0e, 4)); AssertEx.Equal(new byte[] { 0x1E }, builder.ToArray(0x1e, 1)); AssertEx.Equal(new byte[] { 0x1E, 0x1F }, builder.ToArray(0x1e, 2)); AssertEx.Equal(new byte[] { 0x1E, 0x1F, 0x20 }, builder.ToArray(0x1e, 3)); AssertEx.Equal(new byte[] { 0x1E, 0x1F, 0x20, 0x21 }, builder.ToArray(0x1e, 4)); }
public void WriteSerializedString() { var writer = new BlobBuilder(4); writer.WriteSerializedString(""); writer.WriteSerializedString("a"); writer.WriteSerializedString(null); writer.WriteSerializedString(""); writer.WriteSerializedString("\ud800"); // hi surrogate writer.WriteSerializedString("\udc00"); // lo surrogate writer.WriteSerializedString("\ud800\udc00"); // pair writer.WriteSerializedString("\udc00\ud800"); // lo + hi writer.WriteSerializedString("\u1234"); AssertEx.Equal(new byte[] { 0x00, 0x01, 0x61, 0xff, 0x00, 0x03, 0xED, 0xA0, 0x80, 0x03, 0xED, 0xB0, 0x80, 0x04, 0xF0, 0x90, 0x80, 0x80, 0x06, 0xED, 0xB0, 0x80, 0xED, 0xA0, 0x80, 0x03, 0xE1, 0x88, 0xB4 }, writer.ToArray()); }
public void ReserveBytes1() { var builder = new BlobBuilder(16); var writer0 = builder.ReserveBytes(0); var writer1 = builder.ReserveBytes(1); var writer2 = 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); }
public void WriteUTF8_ReplaceUnpairedSurrogates() { var writer = new BlobBuilder(4); writer.WriteUTF8("a", allowUnpairedSurrogates: false); writer.WriteUTF8("", allowUnpairedSurrogates: false); writer.WriteUTF8("bc", allowUnpairedSurrogates: false); writer.WriteUTF8("d", allowUnpairedSurrogates: false); writer.WriteUTF8("", allowUnpairedSurrogates: false); writer.WriteUTF8(Encoding.UTF8.GetString(new byte[] { 0x00, 0xC2, 0x80, 0xE1, 0x88, 0xB4 }), allowUnpairedSurrogates: false); writer.WriteUTF8("\0\ud800", allowUnpairedSurrogates: false); // hi surrogate writer.WriteUTF8("\0\udc00", allowUnpairedSurrogates: false); // lo surrogate writer.WriteUTF8("\0\ud800\udc00", allowUnpairedSurrogates: false); // pair writer.WriteUTF8("\0\udc00\ud800", allowUnpairedSurrogates: false); // lo + hi AssertEx.Equal(new byte[] { (byte)'a', (byte)'b', (byte)'c', (byte)'d', 0x00, 0xC2, 0x80, 0xE1, 0x88, 0xB4, 0x00, 0xEF, 0xBF, 0xBD, 0x00, 0xEF, 0xBF, 0xBD, 0x00, 0xF0, 0x90, 0x80, 0x80, 0x00, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD }, writer.ToArray()); }
public void WriteUTF8() { var writer = new BlobBuilder(4); writer.WriteUTF8("a"); writer.WriteUTF8(""); writer.WriteUTF8("bc"); writer.WriteUTF8("d"); writer.WriteUTF8(""); writer.WriteUTF8(Encoding.UTF8.GetString(new byte[] { 0x00, 0xC2, 0x80, 0xE1, 0x88, 0xB4 })); writer.WriteUTF8("\0\ud800"); // hi surrogate writer.WriteUTF8("\0\udc00"); // lo surrogate writer.WriteUTF8("\0\ud800\udc00"); // pair writer.WriteUTF8("\0\udc00\ud800"); // lo + hi AssertEx.Equal(new byte[] { (byte)'a', (byte)'b', (byte)'c', (byte)'d', 0x00, 0xC2, 0x80, 0xE1, 0x88, 0xB4, 0x00, 0xED, 0xA0, 0x80, 0x00, 0xED, 0xB0, 0x80, 0x00, 0xF0, 0x90, 0x80, 0x80, 0x00, 0xED, 0xB0, 0x80, 0xED, 0xA0, 0x80 }, writer.ToArray()); }