mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-12 07:50:59 +00:00
Merge branch 'master' of https://github.com/google/flatbuffers
This commit is contained in:
@@ -215,5 +215,44 @@ namespace FlatBuffers.Test
|
||||
Assert.AreEqual("NONE", Any.NONE.ToString());
|
||||
Assert.AreEqual("Monster", Any.Monster.ToString());
|
||||
}
|
||||
|
||||
[FlatBuffersTestMethod]
|
||||
public void TestNestedFlatBuffer()
|
||||
{
|
||||
const string nestedMonsterName = "NestedMonsterName";
|
||||
const short nestedMonsterHp = 600;
|
||||
const short nestedMonsterMana = 1024;
|
||||
// Create nested buffer as a Monster type
|
||||
var fbb1 = new FlatBufferBuilder(16);
|
||||
var str1 = fbb1.CreateString(nestedMonsterName);
|
||||
Monster.StartMonster(fbb1);
|
||||
Monster.AddName(fbb1, str1);
|
||||
Monster.AddHp(fbb1, nestedMonsterHp);
|
||||
Monster.AddMana(fbb1, nestedMonsterMana);
|
||||
var monster1 = Monster.EndMonster(fbb1);
|
||||
Monster.FinishMonsterBuffer(fbb1, monster1);
|
||||
var fbb1Bytes = fbb1.SizedByteArray();
|
||||
fbb1 = null;
|
||||
|
||||
// Create a Monster which has the first buffer as a nested buffer
|
||||
var fbb2 = new FlatBufferBuilder(16);
|
||||
var str2 = fbb2.CreateString("My Monster");
|
||||
var nestedBuffer = Monster.CreateTestnestedflatbufferVector(fbb2, fbb1Bytes);
|
||||
Monster.StartMonster(fbb2);
|
||||
Monster.AddName(fbb2, str2);
|
||||
Monster.AddHp(fbb2, 50);
|
||||
Monster.AddMana(fbb2, 32);
|
||||
Monster.AddTestnestedflatbuffer(fbb2, nestedBuffer);
|
||||
var monster = Monster.EndMonster(fbb2);
|
||||
Monster.FinishMonsterBuffer(fbb2, monster);
|
||||
|
||||
// Now test the data extracted from the nested buffer
|
||||
var mons = Monster.GetRootAsMonster(fbb2.DataBuffer);
|
||||
var nestedMonster = mons.TestnestedflatbufferAsMonster();
|
||||
|
||||
Assert.AreEqual(nestedMonsterMana, nestedMonster.Mana);
|
||||
Assert.AreEqual(nestedMonsterHp, nestedMonster.Hp);
|
||||
Assert.AreEqual(nestedMonsterName, nestedMonster.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,6 +159,8 @@ class JavaTest {
|
||||
|
||||
TestNamespaceNesting();
|
||||
|
||||
TestNestedFlatBuffer();
|
||||
|
||||
System.out.println("FlatBuffers test: completed successfully");
|
||||
}
|
||||
|
||||
@@ -242,6 +244,42 @@ class JavaTest {
|
||||
TableInFirstNS.addFooTable(fbb, nestedTableOff);
|
||||
int off = TableInFirstNS.endTableInFirstNS(fbb);
|
||||
}
|
||||
|
||||
static void TestNestedFlatBuffer() {
|
||||
final String nestedMonsterName = "NestedMonsterName";
|
||||
final short nestedMonsterHp = 600;
|
||||
final short nestedMonsterMana = 1024;
|
||||
|
||||
FlatBufferBuilder fbb1 = new FlatBufferBuilder(16);
|
||||
int str1 = fbb1.createString(nestedMonsterName);
|
||||
Monster.startMonster(fbb1);
|
||||
Monster.addName(fbb1, str1);
|
||||
Monster.addHp(fbb1, nestedMonsterHp);
|
||||
Monster.addMana(fbb1, nestedMonsterMana);
|
||||
int monster1 = Monster.endMonster(fbb1);
|
||||
Monster.finishMonsterBuffer(fbb1, monster1);
|
||||
byte[] fbb1Bytes = fbb1.sizedByteArray();
|
||||
fbb1 = null;
|
||||
|
||||
FlatBufferBuilder fbb2 = new FlatBufferBuilder(16);
|
||||
int str2 = fbb2.createString("My Monster");
|
||||
int nestedBuffer = Monster.createTestnestedflatbufferVector(fbb2, fbb1Bytes);
|
||||
Monster.startMonster(fbb2);
|
||||
Monster.addName(fbb2, str2);
|
||||
Monster.addHp(fbb2, (short)50);
|
||||
Monster.addMana(fbb2, (short)32);
|
||||
Monster.addTestnestedflatbuffer(fbb2, nestedBuffer);
|
||||
int monster = Monster.endMonster(fbb2);
|
||||
Monster.finishMonsterBuffer(fbb2, monster);
|
||||
|
||||
// Now test the data extracted from the nested buffer
|
||||
Monster mons = Monster.getRootAsMonster(fbb2.dataBuffer());
|
||||
Monster nestedMonster = mons.testnestedflatbufferAsMonster();
|
||||
|
||||
TestEq(nestedMonsterMana, nestedMonster.mana());
|
||||
TestEq(nestedMonsterHp, nestedMonster.hp());
|
||||
TestEq(nestedMonsterName, nestedMonster.name());
|
||||
}
|
||||
|
||||
static <T> void TestEq(T a, T b) {
|
||||
if (!a.equals(b)) {
|
||||
|
||||
@@ -45,6 +45,8 @@ public sealed class Monster : Table {
|
||||
public byte GetTestnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.Get(__vector(o) + j * 1) : (byte)0; }
|
||||
public int TestnestedflatbufferLength { get { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } }
|
||||
public ArraySegment<byte>? GetTestnestedflatbufferBytes() { return __vector_as_arraysegment(30); }
|
||||
public Monster TestnestedflatbufferAsMonster() { return GetTestnestedflatbufferAsMonster(new Monster()); }
|
||||
public Monster GetTestnestedflatbufferAsMonster(Monster obj) { int o = __offset(30); return o != 0 ? obj.__init(__indirect(__vector(o)), bb) : null; }
|
||||
public bool MutateTestnestedflatbuffer(int j, byte testnestedflatbuffer) { int o = __offset(30); if (o != 0) { bb.Put(__vector(o) + j * 1, testnestedflatbuffer); return true; } else { return false; } }
|
||||
public Stat Testempty { get { return GetTestempty(new Stat()); } }
|
||||
public Stat GetTestempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
|
||||
@@ -51,6 +51,8 @@ public final class Monster extends Table {
|
||||
public int testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
|
||||
public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); }
|
||||
public Monster testnestedflatbufferAsMonster() { return testnestedflatbufferAsMonster(new Monster()); }
|
||||
public Monster testnestedflatbufferAsMonster(Monster obj) { int o = __offset(30); return o != 0 ? obj.__init(__indirect(__vector(o)), bb) : null; }
|
||||
public boolean mutateTestnestedflatbuffer(int j, int testnestedflatbuffer) { int o = __offset(30); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)testnestedflatbuffer); return true; } else { return false; } }
|
||||
public Stat testempty() { return testempty(new Stat()); }
|
||||
public Stat testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
|
||||
@@ -102,7 +102,7 @@ STRUCT_END(Vec3, 32);
|
||||
|
||||
struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
enum {
|
||||
VT_COLOR = 4,
|
||||
VT_COLOR = 4
|
||||
};
|
||||
Color color() const { return static_cast<Color>(GetField<int8_t>(VT_COLOR, 2)); }
|
||||
bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast<int8_t>(_color)); }
|
||||
@@ -136,7 +136,7 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
enum {
|
||||
VT_ID = 4,
|
||||
VT_VAL = 6,
|
||||
VT_COUNT = 8,
|
||||
VT_COUNT = 8
|
||||
};
|
||||
const flatbuffers::String *id() const { return GetPointer<const flatbuffers::String *>(VT_ID); }
|
||||
flatbuffers::String *mutable_id() { return GetPointer<flatbuffers::String *>(VT_ID); }
|
||||
@@ -205,7 +205,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
VT_TESTHASHU32_FNV1A = 46,
|
||||
VT_TESTHASHS64_FNV1A = 48,
|
||||
VT_TESTHASHU64_FNV1A = 50,
|
||||
VT_TESTARRAYOFBOOLS = 52,
|
||||
VT_TESTARRAYOFBOOLS = 52
|
||||
};
|
||||
const Vec3 *pos() const { return GetStruct<const Vec3 *>(VT_POS); }
|
||||
Vec3 *mutable_pos() { return GetStruct<Vec3 *>(VT_POS); }
|
||||
|
||||
@@ -45,7 +45,7 @@ STRUCT_END(StructInNestedNS, 8);
|
||||
|
||||
struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
enum {
|
||||
VT_FOO = 4,
|
||||
VT_FOO = 4
|
||||
};
|
||||
int32_t foo() const { return GetField<int32_t>(VT_FOO, 0); }
|
||||
bool mutate_foo(int32_t _foo) { return SetField(VT_FOO, _foo); }
|
||||
|
||||
@@ -22,7 +22,7 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
enum {
|
||||
VT_FOO_TABLE = 4,
|
||||
VT_FOO_ENUM = 6,
|
||||
VT_FOO_STRUCT = 8,
|
||||
VT_FOO_STRUCT = 8
|
||||
};
|
||||
const NamespaceA::NamespaceB::TableInNestedNS *foo_table() const { return GetPointer<const NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE); }
|
||||
NamespaceA::NamespaceB::TableInNestedNS *mutable_foo_table() { return GetPointer<NamespaceA::NamespaceB::TableInNestedNS *>(VT_FOO_TABLE); }
|
||||
|
||||
@@ -478,8 +478,8 @@ void FuzzTest1() {
|
||||
const uint16_t ushort_val = 0xFEEE;
|
||||
const int32_t int_val = 0x83333333;
|
||||
const uint32_t uint_val = 0xFDDDDDDD;
|
||||
const int64_t long_val = 0x8444444444444444;
|
||||
const uint64_t ulong_val = 0xFCCCCCCCCCCCCCCC;
|
||||
const int64_t long_val = 0x8444444444444444LL;
|
||||
const uint64_t ulong_val = 0xFCCCCCCCCCCCCCCCULL;
|
||||
const float float_val = 3.14159f;
|
||||
const double double_val = 3.14159265359;
|
||||
|
||||
@@ -564,8 +564,28 @@ void FuzzTest2() {
|
||||
|
||||
struct RndDef {
|
||||
std::string instances[instances_per_definition];
|
||||
|
||||
// Since we're generating schema and corresponding data in tandem,
|
||||
// this convenience function adds strings to both at once.
|
||||
static void Add(RndDef (&definitions_l)[num_definitions],
|
||||
std::string &schema_l,
|
||||
const int instances_per_definition_l,
|
||||
const char *schema_add, const char *instance_add,
|
||||
int definition) {
|
||||
schema_l += schema_add;
|
||||
for (int i = 0; i < instances_per_definition_l; i++)
|
||||
definitions_l[definition].instances[i] += instance_add;
|
||||
}
|
||||
};
|
||||
|
||||
#define AddToSchemaAndInstances(schema_add, instance_add) \
|
||||
RndDef::Add(definitions, schema, instances_per_definition, \
|
||||
schema_add, instance_add, definition)
|
||||
|
||||
#define Dummy() \
|
||||
RndDef::Add(definitions, schema, instances_per_definition, \
|
||||
"byte", "1", definition)
|
||||
|
||||
RndDef definitions[num_definitions];
|
||||
|
||||
// We are going to generate num_definitions, the first
|
||||
@@ -577,17 +597,6 @@ void FuzzTest2() {
|
||||
// being generated. We generate multiple instances such that when creating
|
||||
// hierarchy, we get some variety by picking one randomly.
|
||||
for (int definition = 0; definition < num_definitions; definition++) {
|
||||
// Since we're generating schema & and corresponding data in tandem,
|
||||
// this convenience function adds strings to both at once.
|
||||
auto AddToSchemaAndInstances = [&](const char *schema_add,
|
||||
const char *instance_add) {
|
||||
schema += schema_add;
|
||||
for (int i = 0; i < instances_per_definition; i++)
|
||||
definitions[definition].instances[i] += instance_add;
|
||||
};
|
||||
// Generate a default type if we can't generate something else.
|
||||
auto Dummy = [&]() { AddToSchemaAndInstances("byte", "1"); };
|
||||
|
||||
std::string definition_name = "D" + flatbuffers::NumToString(definition);
|
||||
|
||||
bool is_struct = definition < num_struct_definitions;
|
||||
|
||||
Reference in New Issue
Block a user