mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Simplified the serialisation format. Other task #39.
The new format is simpler but less efficient for small integers, short strings and short byte arrays.
This commit is contained in:
@@ -7,7 +7,6 @@ import java.io.ByteArrayInputStream;
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ReaderImplTest extends BriarTestCase {
|
||||
@@ -17,7 +16,7 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadBoolean() throws Exception {
|
||||
setContents("FF" + "FE");
|
||||
setContents("00" + "01");
|
||||
assertFalse(r.readBoolean());
|
||||
assertTrue(r.readBoolean());
|
||||
assertTrue(r.eof());
|
||||
@@ -25,160 +24,60 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipBoolean() throws Exception {
|
||||
setContents("FF" + "FE");
|
||||
setContents("00" + "01");
|
||||
r.skipBoolean();
|
||||
r.skipBoolean();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInt8() throws Exception {
|
||||
setContents("FD00" + "FDFF" + "FD7F" + "FD80");
|
||||
assertEquals((byte) 0, r.readInt8());
|
||||
assertEquals((byte) -1, r.readInt8());
|
||||
assertEquals(Byte.MAX_VALUE, r.readInt8());
|
||||
assertEquals(Byte.MIN_VALUE, r.readInt8());
|
||||
public void testReadInteger() throws Exception {
|
||||
setContents("02" + "0000000000000000" + "02" + "FFFFFFFFFFFFFFFF"
|
||||
+ "02" + "7FFFFFFFFFFFFFFF" + "02" + "8000000000000000");
|
||||
assertEquals(0, r.readInteger());
|
||||
assertEquals(-1, r.readInteger());
|
||||
assertEquals(Long.MAX_VALUE, r.readInteger());
|
||||
assertEquals(Long.MIN_VALUE, r.readInteger());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipInt8() throws Exception {
|
||||
setContents("FD00");
|
||||
r.skipInt8();
|
||||
public void testSkipInteger() throws Exception {
|
||||
setContents("02" + "0000000000000000");
|
||||
r.skipInteger();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInt16() throws Exception {
|
||||
setContents("FC0000" + "FCFFFF" + "FC7FFF" + "FC8000");
|
||||
assertEquals((short) 0, r.readInt16());
|
||||
assertEquals((short) -1, r.readInt16());
|
||||
assertEquals(Short.MAX_VALUE, r.readInt16());
|
||||
assertEquals(Short.MIN_VALUE, r.readInt16());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipInt16() throws Exception {
|
||||
setContents("FC0000");
|
||||
r.skipInt16();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInt32() throws Exception {
|
||||
setContents("FB00000000" + "FBFFFFFFFF" + "FB7FFFFFFF" + "FB80000000");
|
||||
assertEquals(0, r.readInt32());
|
||||
assertEquals(-1, r.readInt32());
|
||||
assertEquals(Integer.MAX_VALUE, r.readInt32());
|
||||
assertEquals(Integer.MIN_VALUE, r.readInt32());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipInt32() throws Exception {
|
||||
setContents("FB00000000");
|
||||
r.skipInt32();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInt64() throws Exception {
|
||||
setContents("FA0000000000000000" + "FAFFFFFFFFFFFFFFFF"
|
||||
+ "FA7FFFFFFFFFFFFFFF" + "FA8000000000000000");
|
||||
assertEquals(0, r.readInt64());
|
||||
assertEquals(-1, r.readInt64());
|
||||
assertEquals(Long.MAX_VALUE, r.readInt64());
|
||||
assertEquals(Long.MIN_VALUE, r.readInt64());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipInt64() throws Exception {
|
||||
setContents("FA0000000000000000");
|
||||
r.skipInt64();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadIntAny() throws Exception {
|
||||
setContents("00" + "7F" + "FD80" + "FDFF" + "FC0080" + "FC7FFF"
|
||||
+ "FB00008000" + "FB7FFFFFFF" + "FA0000000080000000");
|
||||
assertEquals(0, r.readIntAny());
|
||||
assertEquals(127, r.readIntAny());
|
||||
assertEquals(-128, r.readIntAny());
|
||||
assertEquals(-1, r.readIntAny());
|
||||
assertEquals(128, r.readIntAny());
|
||||
assertEquals(32767, r.readIntAny());
|
||||
assertEquals(32768, r.readIntAny());
|
||||
assertEquals(2147483647, r.readIntAny());
|
||||
assertEquals(2147483648L, r.readIntAny());
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipIntAny() throws Exception {
|
||||
setContents("00" + "FD00" + "FC0000" + "FB00000000"
|
||||
+ "FA0000000000000000");
|
||||
r.skipIntAny();
|
||||
r.skipIntAny();
|
||||
r.skipIntAny();
|
||||
r.skipIntAny();
|
||||
r.skipIntAny();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFloat32() throws Exception {
|
||||
public void testReadFloat() throws Exception {
|
||||
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html
|
||||
// http://steve.hollasch.net/cgindex/coding/ieeefloat.html
|
||||
setContents("F900000000" + "F93F800000" + "F940000000" + "F9BF800000"
|
||||
+ "F980000000" + "F9FF800000" + "F97F800000" + "F97FC00000");
|
||||
assertEquals(0F, r.readFloat32());
|
||||
assertEquals(1F, r.readFloat32());
|
||||
assertEquals(2F, r.readFloat32());
|
||||
assertEquals(-1F, r.readFloat32());
|
||||
assertEquals(-0F, r.readFloat32());
|
||||
assertEquals(Float.NEGATIVE_INFINITY, r.readFloat32());
|
||||
assertEquals(Float.POSITIVE_INFINITY, r.readFloat32());
|
||||
assertTrue(Float.isNaN(r.readFloat32()));
|
||||
setContents("03" + "0000000000000000" + "03" + "3FF0000000000000"
|
||||
+ "03" + "4000000000000000" + "03" + "BFF0000000000000"
|
||||
+ "03" + "8000000000000000" + "03" + "FFF0000000000000"
|
||||
+ "03" + "7FF0000000000000" + "03" + "7FF8000000000000");
|
||||
assertEquals(0.0, r.readFloat());
|
||||
assertEquals(1.0, r.readFloat());
|
||||
assertEquals(2.0, r.readFloat());
|
||||
assertEquals(-1.0, r.readFloat());
|
||||
assertEquals(-0.0, r.readFloat());
|
||||
assertEquals(Double.NEGATIVE_INFINITY, r.readFloat());
|
||||
assertEquals(Double.POSITIVE_INFINITY, r.readFloat());
|
||||
assertTrue(Double.isNaN(r.readFloat()));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipFloat32() throws Exception {
|
||||
setContents("F900000000");
|
||||
r.skipFloat32();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFloat64() throws Exception {
|
||||
setContents("F80000000000000000" + "F83FF0000000000000"
|
||||
+ "F84000000000000000" + "F8BFF0000000000000"
|
||||
+ "F88000000000000000" + "F8FFF0000000000000"
|
||||
+ "F87FF0000000000000" + "F87FF8000000000000");
|
||||
assertEquals(0.0, r.readFloat64());
|
||||
assertEquals(1.0, r.readFloat64());
|
||||
assertEquals(2.0, r.readFloat64());
|
||||
assertEquals(-1.0, r.readFloat64());
|
||||
assertEquals(-0.0, r.readFloat64());
|
||||
assertEquals(Double.NEGATIVE_INFINITY, r.readFloat64());
|
||||
assertEquals(Double.POSITIVE_INFINITY, r.readFloat64());
|
||||
assertTrue(Double.isNaN(r.readFloat64()));
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipFloat64() throws Exception {
|
||||
setContents("F80000000000000000");
|
||||
r.skipFloat64();
|
||||
public void testSkipFloat() throws Exception {
|
||||
setContents("03" + "0000000000000000");
|
||||
r.skipFloat();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadString() throws Exception {
|
||||
setContents("F703666F6F" + "F700");
|
||||
// "foo" and the empty string
|
||||
setContents("04" + "00000003" + "666F6F" + "04" + "00000000");
|
||||
assertEquals("foo", r.readString(Integer.MAX_VALUE));
|
||||
assertEquals("", r.readString(Integer.MAX_VALUE));
|
||||
assertTrue(r.eof());
|
||||
@@ -186,8 +85,11 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadStringMaxLength() throws Exception {
|
||||
setContents("F703666F6F" + "F703666F6F");
|
||||
// "foo" twice
|
||||
setContents("04" + "00000003" + "666F6F" +
|
||||
"04" + "00000003" + "666F6F");
|
||||
assertEquals("foo", r.readString(3));
|
||||
assertTrue(r.hasString());
|
||||
try {
|
||||
r.readString(2);
|
||||
fail();
|
||||
@@ -196,7 +98,8 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipString() throws Exception {
|
||||
setContents("F703666F6F" + "F700");
|
||||
// "foo" and the empty string
|
||||
setContents("04" + "00000003" + "666F6F" + "04" + "00000000");
|
||||
r.skipString(Integer.MAX_VALUE);
|
||||
r.skipString(Integer.MAX_VALUE);
|
||||
assertTrue(r.eof());
|
||||
@@ -204,8 +107,11 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipStringMaxLength() throws Exception {
|
||||
setContents("F703666F6F" + "F703666F6F");
|
||||
// "foo" twice
|
||||
setContents("04" + "00000003" + "666F6F" +
|
||||
"04" + "00000003" + "666F6F");
|
||||
r.skipString(3);
|
||||
assertTrue(r.hasString());
|
||||
try {
|
||||
r.skipString(2);
|
||||
fail();
|
||||
@@ -214,7 +120,8 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadBytes() throws Exception {
|
||||
setContents("F603010203" + "F600");
|
||||
// {1, 2, 3} and {}
|
||||
setContents("05" + "00000003" + "010203" + "05" + "00000000");
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(Integer.MAX_VALUE));
|
||||
assertArrayEquals(new byte[] {}, r.readBytes(Integer.MAX_VALUE));
|
||||
assertTrue(r.eof());
|
||||
@@ -222,8 +129,11 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadBytesMaxLength() throws Exception {
|
||||
setContents("F603010203" + "F603010203");
|
||||
// {1, 2, 3} twice
|
||||
setContents("05" + "00000003" + "010203" +
|
||||
"05" + "00000003" + "010203");
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, r.readBytes(3));
|
||||
assertTrue(r.hasBytes());
|
||||
try {
|
||||
r.readBytes(2);
|
||||
fail();
|
||||
@@ -232,7 +142,8 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipBytes() throws Exception {
|
||||
setContents("F603010203" + "F600");
|
||||
// {1, 2, 3} and {}
|
||||
setContents("05" + "00000003" + "010203" + "05" + "00000000");
|
||||
r.skipBytes(Integer.MAX_VALUE);
|
||||
r.skipBytes(Integer.MAX_VALUE);
|
||||
assertTrue(r.eof());
|
||||
@@ -240,8 +151,11 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipBytesMaxLength() throws Exception {
|
||||
setContents("F603010203" + "F603010203");
|
||||
// {1, 2, 3} twice
|
||||
setContents("05" + "00000003" + "010203" +
|
||||
"05" + "00000003" + "010203");
|
||||
r.skipBytes(3);
|
||||
assertTrue(r.hasBytes());
|
||||
try {
|
||||
r.skipBytes(2);
|
||||
fail();
|
||||
@@ -250,14 +164,17 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadList() throws Exception {
|
||||
setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2");
|
||||
// A list containing 2, "foo", and 128
|
||||
setContents("06" + "02" + "0000000000000001" +
|
||||
"04" + "00000003" + "666F6F" +
|
||||
"02" + "0000000000000080" + "09");
|
||||
r.readListStart();
|
||||
assertFalse(r.hasListEnd());
|
||||
assertEquals((byte) 1, r.readIntAny());
|
||||
assertEquals(1, r.readInteger());
|
||||
assertFalse(r.hasListEnd());
|
||||
assertEquals("foo", r.readString(1000));
|
||||
assertFalse(r.hasListEnd());
|
||||
assertEquals((short) 128, r.readIntAny());
|
||||
assertEquals(128, r.readInteger());
|
||||
assertTrue(r.hasListEnd());
|
||||
r.readListEnd();
|
||||
assertTrue(r.eof());
|
||||
@@ -265,19 +182,24 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipList() throws Exception {
|
||||
setContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2");
|
||||
// A list containing 2, "foo", and 128
|
||||
setContents("06" + "02" + "0000000000000001" +
|
||||
"04" + "00000003" + "666F6F" +
|
||||
"02" + "0000000000000080" + "09");
|
||||
r.skipList();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadMap() throws Exception {
|
||||
setContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "F2");
|
||||
// A map containing "foo" -> 123 and {} -> null
|
||||
setContents("07" + "04" + "00000003" + "666F6F" +
|
||||
"02" + "000000000000007B" + "05" + "00000000" + "0A" + "09");
|
||||
r.readMapStart();
|
||||
assertFalse(r.hasMapEnd());
|
||||
assertEquals("foo", r.readString(1000));
|
||||
assertFalse(r.hasMapEnd());
|
||||
assertEquals((byte) 123, r.readIntAny());
|
||||
assertEquals(123, r.readInteger());
|
||||
assertFalse(r.hasMapEnd());
|
||||
assertArrayEquals(new byte[] {}, r.readBytes(1000));
|
||||
assertFalse(r.hasMapEnd());
|
||||
@@ -290,7 +212,9 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testSkipMap() throws Exception {
|
||||
setContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "F2");
|
||||
// A map containing "foo" -> 123 and {} -> null
|
||||
setContents("07" + "04" + "00000003" + "666F6F" +
|
||||
"02" + "000000000000007B" + "05" + "00000000" + "0A" + "09");
|
||||
r.skipMap();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
@@ -298,7 +222,7 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testReadStruct() throws Exception {
|
||||
// Two empty structs with IDs 0 and 255
|
||||
setContents("F300" + "F2" + "F3FF" + "F2");
|
||||
setContents("0800" + "09" + "08FF" + "09");
|
||||
r.readStructStart(0);
|
||||
r.readStructEnd();
|
||||
r.readStructStart(255);
|
||||
@@ -309,7 +233,7 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testSkipStruct() throws Exception {
|
||||
// Two empty structs with IDs 0 and 255
|
||||
setContents("F300" + "F2" + "F3FF" + "F2");
|
||||
setContents("0800" + "09" + "08FF" + "09");
|
||||
r.skipStruct();
|
||||
r.skipStruct();
|
||||
assertTrue(r.eof());
|
||||
@@ -318,21 +242,21 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testSkipNestedStructMapAndList() throws Exception {
|
||||
// A struct containing a map containing two empty lists
|
||||
setContents("F300" + "F4" + "F5" + "F2" + "F5" + "F2" + "F2" + "F2");
|
||||
setContents("0800" + "07" + "06" + "09" + "06" + "09" + "09" + "09");
|
||||
r.skipStruct();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadNull() throws Exception {
|
||||
setContents("F1");
|
||||
setContents("0A");
|
||||
r.readNull();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipNull() throws Exception {
|
||||
setContents("F1");
|
||||
setContents("0A");
|
||||
r.skipNull();
|
||||
assertTrue(r.eof());
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.util.Map;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -30,114 +29,43 @@ public class WriterImplTest extends BriarTestCase {
|
||||
w.writeBoolean(true);
|
||||
w.writeBoolean(false);
|
||||
// TRUE tag, FALSE tag
|
||||
checkContents("FE" + "FF");
|
||||
checkContents("01" + "00");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteUint7() throws IOException {
|
||||
w.writeUint7((byte) 0);
|
||||
w.writeUint7(Byte.MAX_VALUE);
|
||||
// 0, 127
|
||||
checkContents("00" + "7F");
|
||||
public void testWriteInteger() throws IOException {
|
||||
w.writeInteger(0);
|
||||
w.writeInteger(-1);
|
||||
w.writeInteger(Long.MIN_VALUE);
|
||||
w.writeInteger(Long.MAX_VALUE);
|
||||
// INTEGER tag, 0, INTEGER tag, -1, etc
|
||||
checkContents("02" + "0000000000000000" + "02" + "FFFFFFFFFFFFFFFF"
|
||||
+ "02" + "8000000000000000" + "02" + "7FFFFFFFFFFFFFFF");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteInt8() throws IOException {
|
||||
w.writeInt8((byte) 0);
|
||||
w.writeInt8((byte) -1);
|
||||
w.writeInt8(Byte.MIN_VALUE);
|
||||
w.writeInt8(Byte.MAX_VALUE);
|
||||
// INT8 tag, 0, INT8 tag, -1, INT8 tag, -128, INT8 tag, 127
|
||||
checkContents("FD" + "00" + "FD" + "FF" + "FD" + "80" + "FD" + "7F");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteInt16() throws IOException {
|
||||
w.writeInt16((short) 0);
|
||||
w.writeInt16((short) -1);
|
||||
w.writeInt16(Short.MIN_VALUE);
|
||||
w.writeInt16(Short.MAX_VALUE);
|
||||
// INT16 tag, 0, INT16 tag, -1, INT16 tag, -32768, INT16 tag, 32767
|
||||
checkContents("FC" + "0000" + "FC" + "FFFF" + "FC" + "8000"
|
||||
+ "FC" + "7FFF");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteInt32() throws IOException {
|
||||
w.writeInt32(0);
|
||||
w.writeInt32(-1);
|
||||
w.writeInt32(Integer.MIN_VALUE);
|
||||
w.writeInt32(Integer.MAX_VALUE);
|
||||
// INT32 tag, 0, INT32 tag, -1, etc
|
||||
checkContents("FB" + "00000000" + "FB" + "FFFFFFFF" + "FB" + "80000000"
|
||||
+ "FB" + "7FFFFFFF");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteInt64() throws IOException {
|
||||
w.writeInt64(0);
|
||||
w.writeInt64(-1);
|
||||
w.writeInt64(Long.MIN_VALUE);
|
||||
w.writeInt64(Long.MAX_VALUE);
|
||||
// INT64 tag, 0, INT64 tag, -1, etc
|
||||
checkContents("FA" + "0000000000000000" + "FA" + "FFFFFFFFFFFFFFFF"
|
||||
+ "FA" + "8000000000000000" + "FA" + "7FFFFFFFFFFFFFFF");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteIntAny() throws IOException {
|
||||
w.writeIntAny(0); // uint7
|
||||
w.writeIntAny(-1); // int8
|
||||
w.writeIntAny(Byte.MAX_VALUE); // uint7
|
||||
w.writeIntAny(Byte.MAX_VALUE + 1); // int16
|
||||
w.writeIntAny(Short.MAX_VALUE); // int16
|
||||
w.writeIntAny(Short.MAX_VALUE + 1); // int32
|
||||
w.writeIntAny(Integer.MAX_VALUE); // int32
|
||||
w.writeIntAny(Integer.MAX_VALUE + 1L); // int64
|
||||
checkContents("00" + "FDFF" + "7F" + "FC0080" + "FC7FFF"
|
||||
+ "FB00008000" + "FB7FFFFFFF" + "FA0000000080000000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteFloat32() throws IOException {
|
||||
public void testWriteFloat() throws IOException {
|
||||
// http://babbage.cs.qc.edu/IEEE-754/Decimal.html
|
||||
// 1 bit for sign, 8 for exponent, 23 for significand
|
||||
w.writeFloat32(0F); // 0 0 0 -> 0x00000000
|
||||
w.writeFloat32(1F); // 0 127 1 -> 0x3F800000
|
||||
w.writeFloat32(2F); // 0 128 1 -> 0x40000000
|
||||
w.writeFloat32(-1F); // 1 127 1 -> 0xBF800000
|
||||
w.writeFloat32(-0F); // 1 0 0 -> 0x80000000
|
||||
// http://steve.hollasch.net/cgindex/coding/ieeefloat.html
|
||||
w.writeFloat32(Float.NEGATIVE_INFINITY); // 1 255 0 -> 0xFF800000
|
||||
w.writeFloat32(Float.POSITIVE_INFINITY); // 0 255 0 -> 0x7F800000
|
||||
w.writeFloat32(Float.NaN); // 0 255 1 -> 0x7FC00000
|
||||
checkContents("F9" + "00000000" + "F9" + "3F800000" + "F9" + "40000000"
|
||||
+ "F9" + "BF800000" + "F9" + "80000000" + "F9" + "FF800000"
|
||||
+ "F9" + "7F800000" + "F9" + "7FC00000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteFloat64() throws IOException {
|
||||
// 1 bit for sign, 11 for exponent, 52 for significand
|
||||
w.writeFloat64(0.0); // 0 0 0 -> 0x0000000000000000
|
||||
w.writeFloat64(1.0); // 0 1023 1 -> 0x3FF0000000000000
|
||||
w.writeFloat64(2.0); // 0 1024 1 -> 0x4000000000000000
|
||||
w.writeFloat64(-1.0); // 1 1023 1 -> 0xBFF0000000000000
|
||||
w.writeFloat64(-0.0); // 1 0 0 -> 0x8000000000000000
|
||||
w.writeFloat64(Double.NEGATIVE_INFINITY); // 1 2047 0 -> 0xFFF00000...
|
||||
w.writeFloat64(Double.POSITIVE_INFINITY); // 0 2047 0 -> 0x7FF00000...
|
||||
w.writeFloat64(Double.NaN); // 0 2047 1 -> 0x7FF8000000000000
|
||||
checkContents("F8" + "0000000000000000" + "F8" + "3FF0000000000000"
|
||||
+ "F8" + "4000000000000000" + "F8" + "BFF0000000000000"
|
||||
+ "F8" + "8000000000000000" + "F8" + "FFF0000000000000"
|
||||
+ "F8" + "7FF0000000000000" + "F8" + "7FF8000000000000");
|
||||
w.writeFloat(0.0); // 0 0 0 -> 0x0000000000000000
|
||||
w.writeFloat(1.0); // 0 1023 1 -> 0x3FF0000000000000
|
||||
w.writeFloat(2.0); // 0 1024 1 -> 0x4000000000000000
|
||||
w.writeFloat(-1.0); // 1 1023 1 -> 0xBFF0000000000000
|
||||
w.writeFloat(-0.0); // 1 0 0 -> 0x8000000000000000
|
||||
w.writeFloat(Double.NEGATIVE_INFINITY); // 1 2047 0 -> 0xFFF00000...
|
||||
w.writeFloat(Double.POSITIVE_INFINITY); // 0 2047 0 -> 0x7FF00000...
|
||||
w.writeFloat(Double.NaN); // 0 2047 1 -> 0x7FF8000000000000
|
||||
checkContents("03" + "0000000000000000" + "03" + "3FF0000000000000"
|
||||
+ "03" + "4000000000000000" + "03" + "BFF0000000000000"
|
||||
+ "03" + "8000000000000000" + "03" + "FFF0000000000000"
|
||||
+ "03" + "7FF0000000000000" + "03" + "7FF8000000000000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteString() throws IOException {
|
||||
w.writeString("foo bar baz bam ");
|
||||
// STRING tag, length 16 as uint7, UTF-8 bytes
|
||||
checkContents("F7" + "10" + "666F6F206261722062617A2062616D20");
|
||||
// STRING tag, length 16, UTF-8 bytes
|
||||
checkContents("04" + "00000010" + "666F6F206261722062617A2062616D20");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,17 +73,18 @@ public class WriterImplTest extends BriarTestCase {
|
||||
w.writeBytes(new byte[] {
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
|
||||
});
|
||||
// BYTES tag, length 16 as uint7, bytes
|
||||
checkContents("F6" + "10" + "000102030405060708090A0B0C0D0E0F");
|
||||
// BYTES tag, length 16, bytes
|
||||
checkContents("05" + "00000010" + "000102030405060708090A0B0C0D0E0F");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteList() throws IOException {
|
||||
List<Object> l = new ArrayList<Object>();
|
||||
for(int i = 0; i < 16; i++) l.add(i);
|
||||
for(int i = 0; i < 3; i++) l.add(i);
|
||||
w.writeList(l);
|
||||
// LIST tag, elements as uint7, END tag
|
||||
checkContents("F5" + "000102030405060708090A0B0C0D0E0F" + "F2");
|
||||
// LIST tag, elements as integers, END tag
|
||||
checkContents("06" + "02" + "0000000000000000" +
|
||||
"02" + "0000000000000001" + "02" + "0000000000000002" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -165,59 +94,66 @@ public class WriterImplTest extends BriarTestCase {
|
||||
l.add(null);
|
||||
l.add(2);
|
||||
w.writeList(l);
|
||||
// LIST tag, 1 as uint7, null, 2 as uint7, END tag
|
||||
checkContents("F5" + "01" + "F1" + "02" + "F2");
|
||||
// LIST tag, 1 as integer, NULL tag, 2 as integer, END tag
|
||||
checkContents("06" + "02" + "0000000000000001" + "0A" +
|
||||
"02" + "0000000000000002" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteMap() throws IOException {
|
||||
// Use LinkedHashMap to get predictable iteration order
|
||||
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
|
||||
for(int i = 0; i < 16; i++) m.put(i, i + 1);
|
||||
for(int i = 0; i < 4; i++) m.put(i, i + 1);
|
||||
w.writeMap(m);
|
||||
// MAP tag, entries as uint7, END tag
|
||||
checkContents("F4" + "0001" + "0102" + "0203" + "0304" + "0405"
|
||||
+ "0506" + "0607" + "0708" + "0809" + "090A" + "0A0B" + "0B0C"
|
||||
+ "0C0D" + "0D0E" + "0E0F" + "0F10" + "F2");
|
||||
// MAP tag, entries as integers, END tag
|
||||
checkContents("07" + "02" + "0000000000000000" +
|
||||
"02" + "0000000000000001" + "02" + "0000000000000001" +
|
||||
"02" + "0000000000000002" + "02" + "0000000000000002" +
|
||||
"02" + "0000000000000003" + "02" + "0000000000000003" +
|
||||
"02" + "0000000000000004" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteDelimitedList() throws IOException {
|
||||
w.writeListStart();
|
||||
w.writeIntAny((byte) 1); // Written as uint7
|
||||
w.writeString("foo"); // Written as string
|
||||
w.writeIntAny(128L); // Written as int16
|
||||
w.writeInteger(1);
|
||||
w.writeString("foo");
|
||||
w.writeInteger(128);
|
||||
w.writeListEnd();
|
||||
// LIST tag, 1 as uint7, "foo" as string, 128 as int16, END tag
|
||||
checkContents("F5" + "01" + "F703666F6F" + "FC0080" + "F2");
|
||||
// LIST tag, 1 as integer, "foo" as string, 128 as integer, END tag
|
||||
checkContents("06" + "02" + "0000000000000001" +
|
||||
"04" + "00000003" + "666F6F" +
|
||||
"02" + "0000000000000080" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteDelimitedMap() throws IOException {
|
||||
w.writeMapStart();
|
||||
w.writeString("foo"); // Written as string
|
||||
w.writeIntAny(123); // Written as uint7
|
||||
w.writeBytes(new byte[0]); // Written as bytes
|
||||
w.writeString("foo");
|
||||
w.writeInteger(123);
|
||||
w.writeBytes(new byte[0]);
|
||||
w.writeNull();
|
||||
w.writeMapEnd();
|
||||
// MAP tag, "foo" as string, 123 as uint7, byte[0] as bytes,
|
||||
// NULL tag, END tag
|
||||
checkContents("F4" + "F703666F6F" + "7B" + "F600" + "F1" + "F2");
|
||||
// MAP tag, "foo" as string, 123 as integer, {} as bytes, NULL tag,
|
||||
// END tag
|
||||
checkContents("07" + "04" + "00000003" + "666F6F" +
|
||||
"02" + "000000000000007B" + "05" + "00000000" + "0A" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteNestedMapsAndLists() throws IOException {
|
||||
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
|
||||
m.put("foo", Integer.valueOf(123));
|
||||
m.put("foo", 123);
|
||||
List<Object> l = new ArrayList<Object>();
|
||||
l.add(Byte.valueOf((byte) 1));
|
||||
l.add((byte) 1);
|
||||
Map<Object, Object> m1 = new LinkedHashMap<Object, Object>();
|
||||
m1.put(m, l);
|
||||
w.writeMap(m1);
|
||||
// MAP tag, MAP tag, "foo" as string, 123 as uint7, END tag,
|
||||
// LIST tag, 1 as uint7, END tag, END tag
|
||||
checkContents("F4" + "F4" + "F703666F6F" + "7B" + "F2"
|
||||
+ "F5" + "01" + "F2" + "F2");
|
||||
// MAP tag, MAP tag, "foo" as string, 123 as integer, END tag,
|
||||
// LIST tag, 1 as integer, END tag, END tag
|
||||
checkContents("07" + "07" + "04" + "00000003" + "666F6F" +
|
||||
"02" + "000000000000007B" + "09" + "06" +
|
||||
"02" + "0000000000000001" + "09" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -225,13 +161,13 @@ public class WriterImplTest extends BriarTestCase {
|
||||
w.writeStructStart(123);
|
||||
w.writeStructEnd();
|
||||
// STRUCT tag, 123 as struct ID, END tag
|
||||
checkContents("F3" + "7B" + "F2");
|
||||
checkContents("08" + "7B" + "09");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteNull() throws IOException {
|
||||
w.writeNull();
|
||||
checkContents("F1");
|
||||
checkContents("0A");
|
||||
}
|
||||
|
||||
private void checkContents(String hex) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user