mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 08:08:05 +00:00
extend JavaTest to test the size prefixed binary as well
This commit is contained in:
@@ -29,23 +29,28 @@ class JavaTest {
|
|||||||
// First, let's test reading a FlatBuffer generated by C++ code:
|
// First, let's test reading a FlatBuffer generated by C++ code:
|
||||||
// This file was generated from monsterdata_test.json
|
// This file was generated from monsterdata_test.json
|
||||||
|
|
||||||
byte[] data = null;
|
String[] filenames = new String[] { "monsterdata_test.mon", "monsterdata_test.mops" };
|
||||||
File file = new File("monsterdata_test.mon");
|
byte[][] data = { null, null };
|
||||||
RandomAccessFile f = null;
|
for (int i = 0; i < filenames.length; ++i) {
|
||||||
try {
|
File file = new File(filenames[i]);
|
||||||
f = new RandomAccessFile(file, "r");
|
RandomAccessFile f = null;
|
||||||
data = new byte[(int)f.length()];
|
try {
|
||||||
f.readFully(data);
|
f = new RandomAccessFile(file, "r");
|
||||||
f.close();
|
data[i] = new byte[(int)f.length()];
|
||||||
} catch(java.io.IOException e) {
|
f.readFully(data[i]);
|
||||||
System.out.println("FlatBuffers test: couldn't read file");
|
f.close();
|
||||||
return;
|
} catch(java.io.IOException e) {
|
||||||
|
System.out.println("FlatBuffers test: couldn't read file " + filenames[i]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now test it:
|
// Now test it:
|
||||||
|
|
||||||
ByteBuffer bb = ByteBuffer.wrap(data);
|
ByteBuffer bb = ByteBuffer.wrap(data[0]);
|
||||||
TestBuffer(bb);
|
TestBuffer(bb, false);
|
||||||
|
bb = ByteBuffer.wrap(data[1]);
|
||||||
|
TestBuffer(bb, true);
|
||||||
|
|
||||||
// Second, let's create a FlatBuffer from scratch in Java, and test it also.
|
// Second, let's create a FlatBuffer from scratch in Java, and test it also.
|
||||||
// We use an initial size of 1 to exercise the reallocation algorithm,
|
// We use an initial size of 1 to exercise the reallocation algorithm,
|
||||||
@@ -79,10 +84,25 @@ class JavaTest {
|
|||||||
TestEq(Any.name(Any.Monster), "Monster");
|
TestEq(Any.name(Any.Monster), "Monster");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestBuffer(ByteBuffer bb) {
|
static void TestBuffer(ByteBuffer bb, boolean sizePrefix) {
|
||||||
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
|
if (sizePrefix) {
|
||||||
|
// the first 4 bytes hold the length of the remaining buffer
|
||||||
|
TestEq(Monster.getSizePrefix(bb), bb.remaining() - 4);
|
||||||
|
|
||||||
Monster monster = Monster.getRootAsMonster(bb);
|
// advance buffer by these 4 bytes to check the identifier
|
||||||
|
ByteBuffer _bb = bb.slice();
|
||||||
|
_bb.position(4);
|
||||||
|
TestEq(Monster.MonsterBufferHasIdentifier(_bb), true);
|
||||||
|
} else {
|
||||||
|
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Monster monster;
|
||||||
|
if (sizePrefix) {
|
||||||
|
monster = Monster.getSizePrefixedRootAsMonster(bb);
|
||||||
|
} else {
|
||||||
|
monster = Monster.getRootAsMonster(bb);
|
||||||
|
}
|
||||||
|
|
||||||
TestEq(monster.hp(), (short)80);
|
TestEq(monster.hp(), (short)80);
|
||||||
TestEq(monster.mana(), (short)150); // default
|
TestEq(monster.mana(), (short)150); // default
|
||||||
@@ -133,7 +153,7 @@ class JavaTest {
|
|||||||
// this method checks additional fields not present in the binary buffer read from file
|
// this method checks additional fields not present in the binary buffer read from file
|
||||||
// these new tests are performed on top of the regular tests
|
// these new tests are performed on top of the regular tests
|
||||||
static void TestExtendedBuffer(ByteBuffer bb) {
|
static void TestExtendedBuffer(ByteBuffer bb) {
|
||||||
TestBuffer(bb);
|
TestBuffer(bb, false);
|
||||||
|
|
||||||
Monster monster = Monster.getRootAsMonster(bb);
|
Monster monster = Monster.getRootAsMonster(bb);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user