forked from BigfootDev/flatbuffers
Java: Added access object for vector of struct and vector of tables. (#5233)
* Java: Added access object for vector of struct and vector of tables. * Java: Workarounds removed when accessing the union vector.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
97f3aa9174
commit
e365c502ff
@@ -26,8 +26,11 @@ import NamespaceA.NamespaceB.*;
|
||||
import com.google.flatbuffers.ByteBufferUtil;
|
||||
import static com.google.flatbuffers.Constants.*;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import com.google.flatbuffers.ByteVector;
|
||||
import com.google.flatbuffers.FlexBuffersBuilder;
|
||||
import com.google.flatbuffers.FlexBuffers;
|
||||
import com.google.flatbuffers.StringVector;
|
||||
import com.google.flatbuffers.UnionVector;
|
||||
import MyGame.MonsterExtra;
|
||||
|
||||
class JavaTest {
|
||||
@@ -126,6 +129,14 @@ class JavaTest {
|
||||
invsum += monster.inventory(i);
|
||||
TestEq(invsum, 10);
|
||||
|
||||
// Method using a vector access object:
|
||||
ByteVector inventoryVector = monster.inventoryVector();
|
||||
TestEq(inventoryVector.length(), 5);
|
||||
invsum = 0;
|
||||
for (int i = 0; i < inventoryVector.length(); i++)
|
||||
invsum += inventoryVector.getAsUnsigned(i);
|
||||
TestEq(invsum, 10);
|
||||
|
||||
// Alternative way of accessing a vector:
|
||||
ByteBuffer ibb = monster.inventoryAsByteBuffer();
|
||||
invsum = 0;
|
||||
@@ -138,10 +149,22 @@ class JavaTest {
|
||||
TestEq(monster.test4Length(), 2);
|
||||
TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100);
|
||||
|
||||
Test.Vector test4Vector = monster.test4Vector();
|
||||
test_0 = test4Vector.get(0);
|
||||
test_1 = test4Vector.get(1);
|
||||
TestEq(test4Vector.length(), 2);
|
||||
TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100);
|
||||
|
||||
TestEq(monster.testarrayofstringLength(), 2);
|
||||
TestEq(monster.testarrayofstring(0),"test1");
|
||||
TestEq(monster.testarrayofstring(1),"test2");
|
||||
|
||||
// Method using a vector access object:
|
||||
StringVector testarrayofstringVector = monster.testarrayofstringVector();
|
||||
TestEq(testarrayofstringVector.length(), 2);
|
||||
TestEq(testarrayofstringVector.get(0),"test1");
|
||||
TestEq(testarrayofstringVector.get(1),"test2");
|
||||
|
||||
TestEq(monster.testbool(), true);
|
||||
}
|
||||
|
||||
@@ -218,6 +241,10 @@ class JavaTest {
|
||||
|
||||
TestEq(monsterObject.inventory(1), (int)inventory[1]);
|
||||
TestEq(monsterObject.inventoryLength(), inventory.length);
|
||||
ByteVector inventoryVector = monsterObject.inventoryVector();
|
||||
TestEq(inventoryVector.getAsUnsigned(1), (int)inventory[1]);
|
||||
TestEq(inventoryVector.length(), inventory.length);
|
||||
|
||||
TestEq(ByteBuffer.wrap(inventory), monsterObject.inventoryAsByteBuffer());
|
||||
}
|
||||
|
||||
@@ -239,6 +266,9 @@ class JavaTest {
|
||||
|
||||
TestEq(monsterObject.inventory(1), (int)inventory[1]);
|
||||
TestEq(monsterObject.inventoryLength(), inventory.length);
|
||||
ByteVector inventoryVector = monsterObject.inventoryVector();
|
||||
TestEq(inventoryVector.getAsUnsigned(1), (int)inventory[1]);
|
||||
TestEq(inventoryVector.length(), inventory.length);
|
||||
TestEq(ByteBuffer.wrap(inventory), monsterObject.inventoryAsByteBuffer());
|
||||
}
|
||||
|
||||
@@ -388,11 +418,18 @@ class JavaTest {
|
||||
TestEq(monster.testarrayoftables(0).name(), "Barney");
|
||||
TestEq(monster.testarrayoftables(1).name(), "Frodo");
|
||||
TestEq(monster.testarrayoftables(2).name(), "Wilma");
|
||||
Monster.Vector testarrayoftablesVector = monster.testarrayoftablesVector();
|
||||
TestEq(testarrayoftablesVector.get(0).name(), "Barney");
|
||||
TestEq(testarrayoftablesVector.get(1).name(), "Frodo");
|
||||
TestEq(testarrayoftablesVector.get(2).name(), "Wilma");
|
||||
|
||||
// Example of searching for a table by the key
|
||||
TestEq(monster.testarrayoftablesByKey("Frodo").name(), "Frodo");
|
||||
TestEq(monster.testarrayoftablesByKey("Barney").name(), "Barney");
|
||||
TestEq(monster.testarrayoftablesByKey("Wilma").name(), "Wilma");
|
||||
TestEq(testarrayoftablesVector.getByKey("Frodo").name(), "Frodo");
|
||||
TestEq(testarrayoftablesVector.getByKey("Barney").name(), "Barney");
|
||||
TestEq(testarrayoftablesVector.getByKey("Wilma").name(), "Wilma");
|
||||
|
||||
// testType is an existing field and mutating it should succeed
|
||||
TestEq(monster.testType(), (byte)Any.Monster);
|
||||
@@ -411,6 +448,10 @@ class JavaTest {
|
||||
for (int i = 0; i < monster.inventoryLength(); i++) {
|
||||
TestEq(monster.inventory(i), i + 1);
|
||||
}
|
||||
ByteVector inventoryVector = monster.inventoryVector();
|
||||
for (int i = 0; i < inventoryVector.length(); i++) {
|
||||
TestEq((int)inventoryVector.get(i), i + 1);
|
||||
}
|
||||
|
||||
//reverse mutation
|
||||
TestEq(monster.mutateInventory(0, 0), true);
|
||||
@@ -453,11 +494,16 @@ class JavaTest {
|
||||
);
|
||||
|
||||
final Movie movie = Movie.getRootAsMovie(fbb.dataBuffer());
|
||||
ByteVector charactersTypeByteVector = movie.charactersTypeVector();
|
||||
UnionVector charactersVector = movie.charactersVector();
|
||||
|
||||
TestEq(movie.charactersTypeLength(), characterTypeVector.length);
|
||||
TestEq(charactersTypeByteVector.length(), characterTypeVector.length);
|
||||
TestEq(movie.charactersLength(), characterVector.length);
|
||||
TestEq(charactersVector.length(), characterVector.length);
|
||||
|
||||
TestEq(movie.charactersType(0), characterTypeVector[0]);
|
||||
TestEq(charactersTypeByteVector.get(0), characterTypeVector[0]);
|
||||
|
||||
TestEq(((Attacker)movie.characters(new Attacker(), 0)).swordAttackDamage(), swordAttackDamage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user