diff options
author | Wouter van Oortmerssen <wvo@google.com> | 2014-01-27 16:52:49 -0800 |
---|---|---|
committer | Wouter van Oortmerssen <wvo@google.com> | 2014-06-10 13:53:28 -0700 |
commit | 26a30738a4fa4e92300821fd761764ec8df2dcf2 (patch) | |
tree | 4b241e44398a6e216fac5e1d5bedfb3574569429 /tests/JavaTest.java | |
parent | c1b43e22b0dc56cb7a7a7ea8a1bf5f0244a23f5e (diff) | |
download | flatbuffers-26a30738a4fa4e92300821fd761764ec8df2dcf2.tar.gz flatbuffers-26a30738a4fa4e92300821fd761764ec8df2dcf2.tar.bz2 flatbuffers-26a30738a4fa4e92300821fd761764ec8df2dcf2.zip |
Initial commit of the FlatBuffers code.
Change-Id: I4c9f0f722490b374257adb3fec63e44ae93da920
Tested: using VS2010 / Xcode / gcc on Linux.
Diffstat (limited to 'tests/JavaTest.java')
-rwxr-xr-x | tests/JavaTest.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/tests/JavaTest.java b/tests/JavaTest.java new file mode 100755 index 00000000..86424b18 --- /dev/null +++ b/tests/JavaTest.java @@ -0,0 +1,145 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.*; +import java.nio.ByteBuffer; +import MyGame.Example.*; +import flatbuffers.FlatBufferBuilder; + +class JavaTest { + public static void main(String[] args) { + + // First, let's test reading a FlatBuffer generated by C++ code: + // This file was generated from monsterdata_test.json + + byte[] data = null; + File file = new File("monsterdata_test_wire.bin"); + RandomAccessFile f = null; + try { + f = new RandomAccessFile(file, "r"); + data = new byte[(int)f.length()]; + f.readFully(data); + f.close(); + } catch(java.io.IOException e) { + System.out.println("FlatBuffers test: couldn't read file"); + return; + } + + // Now test it: + + ByteBuffer bb = ByteBuffer.wrap(data); + TestBuffer(bb, 0); + + // Second, let's create a FlatBuffer from scratch in Java, and test it also. + // We set up the same values as monsterdata.json: + + FlatBufferBuilder fbb = new FlatBufferBuilder(1024); + + int str = fbb.createString("MyMonster"); + + Monster.startInventoryVector(fbb, 5); + for (byte i = 4; i >=0; i--) fbb.addByte(i); + int inv = fbb.endVector(); + + Monster.startMonster(fbb); + Monster.addHp(fbb, (short)20); + int mon2 = Monster.endMonster(fbb); + + Monster.startTest4Vector(fbb, 2); + Test.createTest(fbb, (short)10, (byte)20); + Test.createTest(fbb, (short)30, (byte)40); + int test4 = fbb.endVector(); + + Monster.startMonster(fbb); + Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0, + (byte)4, (short)5, (byte)6)); + Monster.addHp(fbb, (short)80); + Monster.addName(fbb, str); + Monster.addInventory(fbb, inv); + Monster.addTestType(fbb, (byte)1); + Monster.addTest(fbb, mon2); + Monster.addTest4(fbb, test4); + int mon = Monster.endMonster(fbb); + + fbb.finish(mon); + + // Write the result to a file for debugging purposes: + // Note that the binaries are not necessarily identical, since the JSON + // parser may serialize in a slightly different order than the above + // Java code. They are functionally equivalent though. + + try { + DataOutputStream os = new DataOutputStream(new FileOutputStream( + "monsterdata_java_wire.bin")); + os.write(fbb.dataBuffer().array(), fbb.dataStart(), fbb.offset()); + os.close(); + } catch(java.io.IOException e) { + System.out.println("FlatBuffers test: couldn't write file"); + return; + } + + // Test it: + + TestBuffer(fbb.dataBuffer(), fbb.dataStart()); + + System.out.println("FlatBuffers test: completed successfully"); + } + + static void TestBuffer(ByteBuffer bb, int start) { + Monster monster = Monster.getRootAsMonster(bb, start); + + TestEq(monster.hp(), (short)80); + TestEq(monster.mana(), (short)150); // default + + TestEq(monster.name(), "MyMonster"); + // monster.friendly() // can't access, deprecated + + Vec3 pos = monster.pos(); + TestEq(pos.x(), 1.0f); + TestEq(pos.y(), 2.0f); + TestEq(pos.z(), 3.0f); + TestEq(pos.test1(), 3.0); + TestEq(pos.test2(), (byte)4); + Test t = pos.test3(); + TestEq(t.a(), (short)5); + TestEq(t.b(), (byte)6); + + TestEq(monster.testType(), (byte)Any.Monster); + Monster monster2 = new Monster(); + TestEq(monster.test(monster2) != null, true); + TestEq(monster2.hp(), (short)20); + + TestEq(monster.inventoryLength(), 5); + int invsum = 0; + for (int i = 0; i < monster.inventoryLength(); i++) + invsum += monster.inventory(i); + TestEq(invsum, 10); + + Test test_0 = monster.test4(0); + Test test_1 = monster.test4(1); + TestEq(monster.test4Length(), 2); + TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100); + } + + static <T> void TestEq(T a, T b) { + if (!a.equals(b)) { + System.out.println("" + a.getClass().getName() + " " + b.getClass().getName()); + System.out.println("FlatBuffers test FAILED: \'" + a + "\' != \'" + b + "\'"); + assert false; + System.exit(1); + } + } +} |