Add support for fixed-size arrays (#5313)

This commit is contained in:
svenk177
2019-06-18 00:15:13 +02:00
committed by Wouter van Oortmerssen
parent 0d2cebccfe
commit e635141d5b
40 changed files with 2113 additions and 220 deletions

View File

@@ -75,6 +75,8 @@ class JavaTest {
TestVectorOfUnions();
TestFixedLengthArrays();
System.out.println("FlatBuffers test: completed successfully");
}
@@ -452,6 +454,58 @@ class JavaTest {
TestEq(((Attacker)movie.characters(new Attacker(), 0)).swordAttackDamage(), swordAttackDamage);
}
static void TestFixedLengthArrays() {
FlatBufferBuilder builder = new FlatBufferBuilder(0);
float a;
int[] b = new int[15];
byte c;
int[][] d_a = new int[2][2];
byte[] d_b = new byte[2];
byte[][] d_c = new byte[2][2];
a = 0.5f;
for (int i = 0; i < 15; i++) b[i] = i;
c = 1;
d_a[0][0] = 1;
d_a[0][1] = 2;
d_a[1][0] = 3;
d_a[1][1] = 4;
d_b[0] = TestEnum.B;
d_b[1] = TestEnum.C;
d_c[0][0] = TestEnum.A;
d_c[0][1] = TestEnum.B;
d_c[1][0] = TestEnum.C;
d_c[1][1] = TestEnum.B;
int arrayOffset = ArrayStruct.createArrayStruct(builder,
a, b, c, d_a, d_b, d_c);
// Create a table with the ArrayStruct.
ArrayTable.startArrayTable(builder);
ArrayTable.addA(builder, arrayOffset);
int tableOffset = ArrayTable.endArrayTable(builder);
ArrayTable.finishArrayTableBuffer(builder, tableOffset);
ArrayTable table = ArrayTable.getRootAsArrayTable(builder.dataBuffer());
NestedStruct nested = new NestedStruct();
TestEq(table.a().a(), 0.5f);
for (int i = 0; i < 15; i++) TestEq(table.a().b(i), i);
TestEq(table.a().c(), (byte)1);
TestEq(table.a().d(nested, 0).a(0), 1);
TestEq(table.a().d(nested, 0).a(1), 2);
TestEq(table.a().d(nested, 1).a(0), 3);
TestEq(table.a().d(nested, 1).a(1), 4);
TestEq(table.a().d(nested, 0).b(), TestEnum.B);
TestEq(table.a().d(nested, 1).b(), TestEnum.C);
TestEq(table.a().d(nested, 0).c(0), TestEnum.A);
TestEq(table.a().d(nested, 0).c(1), TestEnum.B);
TestEq(table.a().d(nested, 1).c(0), TestEnum.C);
TestEq(table.a().d(nested, 1).c(1), TestEnum.B);
}
static <T> void TestEq(T a, T b) {
if (!a.equals(b)) {
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());