void writeString32TestHelper(char[] input, byte[] expect) { MemoryStream stream = new MemoryStream(); OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream); String str = new String(input); writer.WriteString32(str); byte[] result = stream.GetBuffer(); Assert.AreEqual(result[0], 0x00); Assert.AreEqual(result[1], 0x00); Assert.AreEqual(result[2], 0x00); Assert.AreEqual(result[3], expect.Length); for (int i = 4; i < expect.Length; ++i) { Assert.AreEqual(result[i], expect[i - 4]); } }
public void testWriteString32() { // Test data with 1-byte UTF8 encoding. { char[] input = { '\u0000', '\u000B', '\u0048', '\u0065', '\u006C', '\u006C', '\u006F', '\u0020', '\u0057', '\u006F', '\u0072', '\u006C', '\u0064' }; byte[] expect = { 0xC0, 0x80, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; writeString32TestHelper(input, expect); } // Test data with 2-byte UT8 encoding. { char[] input = { '\u0000', '\u00C2', '\u00A9', '\u00C3', '\u00A6' }; byte[] expect = { 0xC0, 0x80, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC2, 0xA6 }; writeString32TestHelper(input, expect); } // Test data with 1-byte and 2-byte encoding with embedded NULL's. { char[] input = { '\u0000', '\u0004', '\u00C2', '\u00A9', '\u00C3', '\u0000', '\u00A6' }; byte[] expect = { 0xC0, 0x80, 0x04, 0xC3, 0x82, 0xC2, 0xA9, 0xC3, 0x83, 0xC0, 0x80, 0xC2, 0xA6 }; writeString32TestHelper(input, expect); } // test that a null strings writes a -1 { MemoryStream stream = new MemoryStream(); OpenWireBinaryWriter writer = new OpenWireBinaryWriter(stream); writer.WriteString32(null); stream.Seek(0, SeekOrigin.Begin); OpenWireBinaryReader reader = new OpenWireBinaryReader(stream); Assert.AreEqual(-1, reader.ReadInt32()); } }