Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,14 @@ inline size_t InlineSize(const Type &type) {
}

inline size_t InlineAlignment(const Type &type) {
return IsStruct(type)
? type.struct_def->minalign
: (SizeOf(IsArray(type) ? type.element : type.base_type));
if (IsStruct(type)) {
return type.struct_def->minalign;
} else if (IsArray(type)) {
return IsStruct(type.VectorType()) ? type.struct_def->minalign
: SizeOf(type.element);
} else {
return SizeOf(type.base_type);
}
}

struct EnumDef;
Expand Down
19 changes: 18 additions & 1 deletion tests/FlatBuffers.Test/FlatBuffersExampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ public void TestFixedLenghtArrays()
int[,] d_a = new int[2, 2];
TestEnum[] d_b = new TestEnum[2];
TestEnum[,] d_c = new TestEnum[2, 2];
long[,] d_d = new long[2, 2];
int e;
long[] f = new long[2];

a = 0.5f;
for (int i = 0; i < 15; i++) b[i] = i;
Expand All @@ -356,9 +359,16 @@ public void TestFixedLenghtArrays()
d_c[0, 1] = TestEnum.B;
d_c[1, 0] = TestEnum.C;
d_c[1, 1] = TestEnum.B;
d_d[0, 0] = -1;
d_d[0, 1] = 1;
d_d[1, 0] = -2;
d_d[1, 1] = 2;
e = 2;
f[0] = -1;
f[1] = 1;

Offset<ArrayStruct> arrayOffset = ArrayStruct.CreateArrayStruct(
builder, a, b, c, d_a, d_b, d_c);
builder, a, b, c, d_a, d_b, d_c, d_d, e, f);

// Create a table with the ArrayStruct.
ArrayTable.StartArrayTable(builder);
Expand All @@ -382,6 +392,13 @@ public void TestFixedLenghtArrays()
Assert.AreEqual(table.A?.D(0).C(1), TestEnum.B);
Assert.AreEqual(table.A?.D(1).C(0), TestEnum.C);
Assert.AreEqual(table.A?.D(1).C(1), TestEnum.B);
Assert.AreEqual(table.A?.D(0).D(0), -1);
Assert.AreEqual(table.A?.D(0).D(1), 1);
Assert.AreEqual(table.A?.D(1).D(0), -2);
Assert.AreEqual(table.A?.D(1).D(1), 2);
Assert.AreEqual(table.A?.E, 2);
Assert.AreEqual(table.A?.F(0), -1);
Assert.AreEqual(table.A?.F(1), 1);
}
}
}
19 changes: 18 additions & 1 deletion tests/JavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ static void TestFixedLengthArrays() {
int[][] d_a = new int[2][2];
byte[] d_b = new byte[2];
byte[][] d_c = new byte[2][2];
long[][] d_d = new long[2][2];
int e;
long[] f = new long[2];

a = 0.5f;
for (int i = 0; i < 15; i++) b[i] = i;
Expand All @@ -477,9 +480,16 @@ static void TestFixedLengthArrays() {
d_c[0][1] = TestEnum.B;
d_c[1][0] = TestEnum.C;
d_c[1][1] = TestEnum.B;
d_d[0][0] = -1;
d_d[0][1] = 1;
d_d[1][0] = -2;
d_d[1][1] = 2;
e = 2;
f[0] = -1;
f[1] = 1;

int arrayOffset = ArrayStruct.createArrayStruct(builder,
a, b, c, d_a, d_b, d_c);
a, b, c, d_a, d_b, d_c, d_d, e, f);

// Create a table with the ArrayStruct.
ArrayTable.startArrayTable(builder);
Expand All @@ -504,6 +514,13 @@ static void TestFixedLengthArrays() {
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);
TestEq(table.a().d(nested, 0).d(0), (long)-1);
TestEq(table.a().d(nested, 0).d(1), (long)1);
TestEq(table.a().d(nested, 1).d(0), (long)-2);
TestEq(table.a().d(nested, 1).d(1), (long)2);
TestEq(table.a().e(), 2);
TestEq(table.a().f(0), (long)-1);
TestEq(table.a().f(1), (long)1);
}

static <T> void TestEq(T a, T b) {
Expand Down
24 changes: 18 additions & 6 deletions tests/MyGame/Example/ArrayStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ public struct ArrayStruct : IFlatbufferObject
public void MutateB(int j, int b) { __p.bb.PutInt(__p.bb_pos + 4 + j * 4, b); }
public sbyte C { get { return __p.bb.GetSbyte(__p.bb_pos + 64); } }
public void MutateC(sbyte c) { __p.bb.PutSbyte(__p.bb_pos + 64, c); }
public MyGame.Example.NestedStruct D(int j) { return (new MyGame.Example.NestedStruct()).__assign(__p.bb_pos + 68 + j * 12, __p.bb); }
public MyGame.Example.NestedStruct D(int j) { return (new MyGame.Example.NestedStruct()).__assign(__p.bb_pos + 72 + j * 32, __p.bb); }
public int E { get { return __p.bb.GetInt(__p.bb_pos + 136); } }
public void MutateE(int e) { __p.bb.PutInt(__p.bb_pos + 136, e); }
public long F(int j) { return __p.bb.GetLong(__p.bb_pos + 144 + j * 8); }
public void MutateF(int j, long f) { __p.bb.PutLong(__p.bb_pos + 144 + j * 8, f); }

public static Offset<MyGame.Example.ArrayStruct> CreateArrayStruct(FlatBufferBuilder builder, float A, int[] B, sbyte C, int[,] d_A, MyGame.Example.TestEnum[] d_B, MyGame.Example.TestEnum[,] d_C) {
builder.Prep(4, 92);
public static Offset<MyGame.Example.ArrayStruct> CreateArrayStruct(FlatBufferBuilder builder, float A, int[] B, sbyte C, int[,] d_A, MyGame.Example.TestEnum[] d_B, MyGame.Example.TestEnum[,] d_C, long[,] d_D, int E, long[] F) {
builder.Prep(8, 160);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.Prep(4, 12);
builder.Pad(1);
builder.PutLong(F[_idx0-1]);
}
builder.Pad(4);
builder.PutInt(E);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.Prep(8, 32);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.PutLong(d_D[_idx0-1,_idx1-1]);
}
builder.Pad(5);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.PutSbyte((sbyte)d_C[_idx0-1,_idx1-1]);
}
Expand All @@ -36,7 +48,7 @@ public struct ArrayStruct : IFlatbufferObject
builder.PutInt(d_A[_idx0-1,_idx1-1]);
}
}
builder.Pad(3);
builder.Pad(7);
builder.PutSbyte(C);
for (int _idx0 = 15; _idx0 > 0; _idx0--) {
builder.PutInt(B[_idx0-1]);
Expand Down
24 changes: 18 additions & 6 deletions tests/MyGame/Example/ArrayStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ public final class ArrayStruct extends Struct {
public void mutateB(int j, int b) { bb.putInt(bb_pos + 4 + j * 4, b); }
public byte c() { return bb.get(bb_pos + 64); }
public void mutateC(byte c) { bb.put(bb_pos + 64, c); }
public MyGame.Example.NestedStruct d(MyGame.Example.NestedStruct obj, int j) { return obj.__assign(bb_pos + 68 + j * 12, bb); }
public MyGame.Example.NestedStruct d(MyGame.Example.NestedStruct obj, int j) { return obj.__assign(bb_pos + 72 + j * 32, bb); }
public int e() { return bb.getInt(bb_pos + 136); }
public void mutateE(int e) { bb.putInt(bb_pos + 136, e); }
public long f(int j) { return bb.getLong(bb_pos + 144 + j * 8); }
public void mutateF(int j, long f) { bb.putLong(bb_pos + 144 + j * 8, f); }

public static int createArrayStruct(FlatBufferBuilder builder, float a, int[] b, byte c, int[][] d_a, byte[] d_b, byte[][] d_c) {
builder.prep(4, 92);
public static int createArrayStruct(FlatBufferBuilder builder, float a, int[] b, byte c, int[][] d_a, byte[] d_b, byte[][] d_c, long[][] d_d, int e, long[] f) {
builder.prep(8, 160);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.prep(4, 12);
builder.pad(1);
builder.putLong(f[_idx0-1]);
}
builder.pad(4);
builder.putInt(e);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.prep(8, 32);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.putLong(d_d[_idx0-1][_idx1-1]);
}
builder.pad(5);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.putByte(d_c[_idx0-1][_idx1-1]);
}
Expand All @@ -33,7 +45,7 @@ public static int createArrayStruct(FlatBufferBuilder builder, float a, int[] b,
builder.putInt(d_a[_idx0-1][_idx1-1]);
}
}
builder.pad(3);
builder.pad(7);
builder.putByte(c);
for (int _idx0 = 15; _idx0 > 0; _idx0--) {
builder.putInt(b[_idx0-1]);
Expand Down
22 changes: 16 additions & 6 deletions tests/MyGame/Example/ArrayStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ def B(self): return [self._tab.Get(flatbuffers.number_types.Int32Flags, self._ta
def C(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(64))
# ArrayStruct
def D(self, obj, i):
obj.Init(self._tab.Bytes, self._tab.Pos + 68 + i * 12)
obj.Init(self._tab.Bytes, self._tab.Pos + 72 + i * 32)
return obj

# ArrayStruct
def E(self): return self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(136))
# ArrayStruct
def F(self): return [self._tab.Get(flatbuffers.number_types.Int64Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(144 + i * 8)) for i in range(2)]

def CreateArrayStruct(builder, a, b, c, d_a, d_b, d_c):
builder.Prep(4, 92)
def CreateArrayStruct(builder, a, b, c, d_a, d_b, d_c, d_d, e, f):
builder.Prep(8, 160)
for _idx0 in range(2 , 0, -1):
builder.Prep(4, 12)
builder.Pad(1)
builder.PrependInt64(f[_idx0-1])
builder.Pad(4)
builder.PrependInt32(e)
for _idx0 in range(2 , 0, -1):
builder.Prep(8, 32)
for _idx1 in range(2 , 0, -1):
builder.PrependInt64(d_d[_idx0-1][_idx1-1])
builder.Pad(5)
for _idx1 in range(2 , 0, -1):
builder.PrependInt8(d_c[_idx0-1][_idx1-1])
builder.PrependInt8(d_b[_idx0-1])
for _idx1 in range(2 , 0, -1):
builder.PrependInt32(d_a[_idx0-1][_idx1-1])
builder.Pad(3)
builder.Pad(7)
builder.PrependInt8(c)
for _idx0 in range(15 , 0, -1):
builder.PrependInt32(b[_idx0-1])
Expand Down
11 changes: 8 additions & 3 deletions tests/MyGame/Example/NestedStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ public struct NestedStruct : IFlatbufferObject
public void MutateB(MyGame.Example.TestEnum b) { __p.bb.PutSbyte(__p.bb_pos + 8, (sbyte)b); }
public MyGame.Example.TestEnum C(int j) { return (MyGame.Example.TestEnum)__p.bb.GetSbyte(__p.bb_pos + 9 + j * 1); }
public void MutateC(int j, MyGame.Example.TestEnum c) { __p.bb.PutSbyte(__p.bb_pos + 9 + j * 1, (sbyte)c); }
public long D(int j) { return __p.bb.GetLong(__p.bb_pos + 16 + j * 8); }
public void MutateD(int j, long d) { __p.bb.PutLong(__p.bb_pos + 16 + j * 8, d); }

public static Offset<MyGame.Example.NestedStruct> CreateNestedStruct(FlatBufferBuilder builder, int[] A, MyGame.Example.TestEnum B, MyGame.Example.TestEnum[] C) {
builder.Prep(4, 12);
builder.Pad(1);
public static Offset<MyGame.Example.NestedStruct> CreateNestedStruct(FlatBufferBuilder builder, int[] A, MyGame.Example.TestEnum B, MyGame.Example.TestEnum[] C, long[] D) {
builder.Prep(8, 32);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.PutLong(D[_idx0-1]);
}
builder.Pad(5);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.PutSbyte((sbyte)C[_idx0-1]);
}
Expand Down
11 changes: 8 additions & 3 deletions tests/MyGame/Example/NestedStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public final class NestedStruct extends Struct {
public void mutateB(byte b) { bb.put(bb_pos + 8, b); }
public byte c(int j) { return bb.get(bb_pos + 9 + j * 1); }
public void mutateC(int j, byte c) { bb.put(bb_pos + 9 + j * 1, c); }
public long d(int j) { return bb.getLong(bb_pos + 16 + j * 8); }
public void mutateD(int j, long d) { bb.putLong(bb_pos + 16 + j * 8, d); }

public static int createNestedStruct(FlatBufferBuilder builder, int[] a, byte b, byte[] c) {
builder.prep(4, 12);
builder.pad(1);
public static int createNestedStruct(FlatBufferBuilder builder, int[] a, byte b, byte[] c, long[] d) {
builder.prep(8, 32);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.putLong(d[_idx0-1]);
}
builder.pad(5);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.putByte(c[_idx0-1]);
}
Expand Down
10 changes: 7 additions & 3 deletions tests/MyGame/Example/NestedStruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ def A(self): return [self._tab.Get(flatbuffers.number_types.Int32Flags, self._ta
def B(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(8))
# NestedStruct
def C(self): return [self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(9 + i * 1)) for i in range(2)]
# NestedStruct
def D(self): return [self._tab.Get(flatbuffers.number_types.Int64Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(16 + i * 8)) for i in range(2)]

def CreateNestedStruct(builder, a, b, c):
builder.Prep(4, 12)
builder.Pad(1)
def CreateNestedStruct(builder, a, b, c, d):
builder.Prep(8, 32)
for _idx0 in range(2 , 0, -1):
builder.PrependInt64(d[_idx0-1])
builder.Pad(5)
for _idx0 in range(2 , 0, -1):
builder.PrependInt8(c[_idx0-1])
builder.PrependInt8(b)
Expand Down
Binary file modified tests/arrays_test.bfbs
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/arrays_test.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ struct NestedStruct{
a:[int:2];
b:TestEnum;
c:[TestEnum:2];
d:[int64:2];
}

struct ArrayStruct{
a:float;
b:[int:0xF];
c:byte;
d:[NestedStruct:2];
e:int32;
f:[int64:2];
}

table ArrayTable{
Expand Down
10 changes: 7 additions & 3 deletions tests/arrays_test.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
{
a : [-1,2],
b : A,
c : [C, B]
c : [C, B],
d : [0x1122334455667788, -0x1122334455667788]
},
{
a : [3,-4],
b : B,
c : [B, A]
c : [B, A],
d : [-0x1122334455667788, 0x1122334455667788]
}
]
],
e: 1,
f: [-0x8000000000000000, 0x7FFFFFFFFFFFFFFF]
}
}
13 changes: 13 additions & 0 deletions tests/arrays_test.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"$ref" : "#/definitions/MyGame_Example_TestEnum",
"minItems": 2,
"maxItems": 2
},
"d" : {
"type" : "array", "items" : { "type" : "number" },
"minItems": 2,
"maxItems": 2
}
},
"additionalProperties" : false
Expand All @@ -42,6 +47,14 @@
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_NestedStruct" },
"minItems": 2,
"maxItems": 2
},
"e" : {
"type" : "number"
},
"f" : {
"type" : "array", "items" : { "type" : "number" },
"minItems": 2,
"maxItems": 2
}
},
"additionalProperties" : false
Expand Down
Loading