public void testStrings () throws IOException { runStringTest(new UnsafeMemoryOutput(4096)); runStringTest(new UnsafeMemoryOutput(897)); runStringTest(new UnsafeMemoryOutput(new ByteArrayOutputStream())); UnsafeMemoryOutput write = new UnsafeMemoryOutput(21); String value = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234"; write.writeString(value); Input read = new UnsafeMemoryInput(write.toBytes()); assertEquals(value, read.readString()); runStringTest(127); runStringTest(256); runStringTest(1024 * 1023); runStringTest(1024 * 1024); runStringTest(1024 * 1025); runStringTest(1024 * 1026); runStringTest(1024 * 1024 * 2); }
private void runCharTest (UnsafeMemoryOutput write) throws IOException { write.writeChar((char)0); write.writeChar((char)63); write.writeChar((char)64); write.writeChar((char)127); write.writeChar((char)128); write.writeChar((char)8192); write.writeChar((char)16384); write.writeChar((char)32767); write.writeChar((char)65535); Input read = new UnsafeMemoryInput(write.toBytes()); assertEquals(0, read.readChar()); assertEquals(63, read.readChar()); assertEquals(64, read.readChar()); assertEquals(127, read.readChar()); assertEquals(128, read.readChar()); assertEquals(8192, read.readChar()); assertEquals(16384, read.readChar()); assertEquals(32767, read.readChar()); assertEquals(65535, read.readChar()); }
private void runShortTest (UnsafeMemoryOutput write) throws IOException { write.writeShort(0); write.writeShort(63); write.writeShort(64); write.writeShort(127); write.writeShort(128); write.writeShort(8192); write.writeShort(16384); write.writeShort(32767); write.writeShort(-63); write.writeShort(-64); write.writeShort(-127); write.writeShort(-128); write.writeShort(-8192); write.writeShort(-16384); write.writeShort(-32768); Input read = new UnsafeMemoryInput(write.toBytes()); assertEquals(0, read.readShort()); assertEquals(63, read.readShort()); assertEquals(64, read.readShort()); assertEquals(127, read.readShort()); assertEquals(128, read.readShort()); assertEquals(8192, read.readShort()); assertEquals(16384, read.readShort()); assertEquals(32767, read.readShort()); assertEquals(-63, read.readShort()); assertEquals(-64, read.readShort()); assertEquals(-127, read.readShort()); assertEquals(-128, read.readShort()); assertEquals(-8192, read.readShort()); assertEquals(-16384, read.readShort()); assertEquals(-32768, read.readShort()); }
private void runBooleanTest (UnsafeMemoryOutput write) throws IOException { for (int i = 0; i < 100; i++) { write.writeBoolean(true); write.writeBoolean(false); } Input read = new UnsafeMemoryInput(write.toBytes()); for (int i = 0; i < 100; i++) { assertEquals(true, read.readBoolean()); assertEquals(false, read.readBoolean()); } }
public void runStringTest (UnsafeMemoryOutput write) throws IOException { String value1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\rabcdefghijklmnopqrstuvwxyz\n1234567890\t\"!`?'.,;:()[]{}<>|/@\\^$-%+=#_&~*"; String value2 = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234"; write.writeString(""); write.writeString("1"); write.writeString("22"); write.writeString("uno"); write.writeString("dos"); write.writeString("tres"); write.writeString(null); write.writeString(value1); write.writeString(value2); for (int i = 0; i < 127; i++) write.writeString(String.valueOf((char)i)); for (int i = 0; i < 127; i++) write.writeString(String.valueOf((char)i) + "abc"); Input read = new UnsafeMemoryInput(write.toBytes()); assertEquals("", read.readString()); assertEquals("1", read.readString()); assertEquals("22", read.readString()); assertEquals("uno", read.readString()); assertEquals("dos", read.readString()); assertEquals("tres", read.readString()); assertEquals(null, read.readString()); assertEquals(value1, read.readString()); assertEquals(value2, read.readString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i), read.readString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i) + "abc", read.readString()); read.rewind(); assertEquals("", read.readStringBuilder().toString()); assertEquals("1", read.readStringBuilder().toString()); assertEquals("22", read.readStringBuilder().toString()); assertEquals("uno", read.readStringBuilder().toString()); assertEquals("dos", read.readStringBuilder().toString()); assertEquals("tres", read.readStringBuilder().toString()); assertEquals(null, read.readStringBuilder()); assertEquals(value1, read.readStringBuilder().toString()); assertEquals(value2, read.readStringBuilder().toString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i), read.readStringBuilder().toString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i) + "abc", read.readStringBuilder().toString()); }