示例#1
0
// CheckDocExample checks that the code given in FlatBuffers documentation
// is syntactically correct.
func CheckDocExample(buf []byte, off flatbuffers.UOffsetT, fail func(string, ...interface{})) {
	monster := example.GetRootAsMonster(buf, off)
	_ = monster.Hp()
	_ = monster.Pos(nil)
	for i := 0; i < monster.InventoryLength(); i++ {
		_ = monster.Inventory(i) // do something here
	}

	builder := flatbuffers.NewBuilder(0)

	example.MonsterStartInventoryVector(builder, 5)
	for i := 4; i >= 0; i-- {
		builder.PrependByte(byte(i))
	}
	inv := builder.EndVector(5)

	str := builder.CreateString("MyMonster")
	example.MonsterStart(builder)
	example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6))
	example.MonsterAddHp(builder, 80)
	example.MonsterAddName(builder, str)
	example.MonsterAddInventory(builder, inv)
	example.MonsterAddTestType(builder, 1)
	// example.MonsterAddTest(builder, mon2)
	// example.MonsterAddTest4(builder, test4s)
	_ = example.MonsterEnd(builder)
}
示例#2
0
// BenchmarkParseGold measures the speed of parsing the 'gold' data
// used throughout this test suite.
func BenchmarkParseGold(b *testing.B) {
	buf, offset := CheckGeneratedBuild(b.Fatalf)
	monster := example.GetRootAsMonster(buf, offset)

	// use these to prevent allocations:
	reuse_pos := example.Vec3{}
	reuse_test3 := example.Test{}
	reuse_table2 := flatbuffers.Table{}
	reuse_monster2 := example.Monster{}
	reuse_test4_0 := example.Test{}
	reuse_test4_1 := example.Test{}

	b.SetBytes(int64(len(buf[offset:])))
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		monster.Hp()
		monster.Mana()
		name := monster.Name()
		_ = name[0]
		_ = name[len(name)-1]

		monster.Pos(&reuse_pos)
		reuse_pos.X()
		reuse_pos.Y()
		reuse_pos.Z()
		reuse_pos.Test1()
		reuse_pos.Test2()
		reuse_pos.Test3(&reuse_test3)
		reuse_test3.A()
		reuse_test3.B()
		monster.TestType()
		monster.Test(&reuse_table2)
		reuse_monster2.Init(reuse_table2.Bytes, reuse_table2.Pos)
		name2 := reuse_monster2.Name()
		_ = name2[0]
		_ = name2[len(name2)-1]
		monster.InventoryLength()
		l := monster.InventoryLength()
		for i := 0; i < l; i++ {
			monster.Inventory(i)
		}
		monster.Test4Length()
		monster.Test4(&reuse_test4_0, 0)
		monster.Test4(&reuse_test4_1, 1)

		reuse_test4_0.A()
		reuse_test4_0.B()
		reuse_test4_1.A()
		reuse_test4_1.B()

		monster.TestarrayofstringLength()
		str0 := monster.Testarrayofstring(0)
		_ = str0[0]
		_ = str0[len(str0)-1]
		str1 := monster.Testarrayofstring(1)
		_ = str1[0]
		_ = str1[len(str1)-1]
	}
}
示例#3
0
// CheckReadBuffer checks that the given buffer is evaluated correctly
// as the example Monster.
func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, ...interface{})) {
	monster := example.GetRootAsMonster(buf, offset)

	if got := monster.Hp(); 80 != got {
		fail(FailString("hp", 80, got))
	}

	// default
	if got := monster.Mana(); 150 != got {
		fail(FailString("mana", 150, got))
	}

	if got := monster.Name(); !bytes.Equal([]byte("MyMonster"), got) {
		fail(FailString("name", "MyMonster", got))
	}

	// initialize a Vec3 from Pos()
	vec := new(example.Vec3)
	vec = monster.Pos(vec)
	if vec == nil {
		fail("vec3 initialization failed")
	}

	// check that new allocs equal given ones:
	vec2 := monster.Pos(nil)
	if !reflect.DeepEqual(vec, vec2) {
		fail("fresh allocation failed")
	}

	// verify the properties of the Vec3
	if got := vec.X(); float32(1.0) != got {
		fail(FailString("Pos.X", float32(1.0), got))
	}

	if got := vec.Y(); float32(2.0) != got {
		fail(FailString("Pos.Y", float32(2.0), got))
	}

	if got := vec.Z(); float32(3.0) != got {
		fail(FailString("Pos.Z", float32(3.0), got))
	}

	if got := vec.Test1(); float64(3.0) != got {
		fail(FailString("Pos.Test1", float64(3.0), got))
	}

	if got := vec.Test2(); int8(2) != got {
		fail(FailString("Pos.Test2", int8(2), got))
	}

	// initialize a Test from Test3(...)
	t := new(example.Test)
	t = vec.Test3(t)
	if t == nil {
		fail("vec.Test3(&t) failed")
	}

	// check that new allocs equal given ones:
	t2 := vec.Test3(nil)
	if !reflect.DeepEqual(t, t2) {
		fail("fresh allocation failed")
	}

	// verify the properties of the Test
	if got := t.A(); int16(5) != got {
		fail(FailString("t.A()", int16(5), got))
	}

	if got := t.B(); int8(6) != got {
		fail(FailString("t.B()", int8(6), got))
	}

	if got := monster.TestType(); example.AnyMonster != got {
		fail(FailString("monster.TestType()", example.AnyMonster, got))
	}

	if unionType := monster.TestType(); unionType != example.AnyMonster {
		fail("monster.TestType()")
	}

	// initialize a Table from a union field Test(...)
	var table2 flatbuffers.Table
	if ok := monster.Test(&table2); !ok {
		fail("monster.Test(&monster2) failed")
	}

	// initialize a Monster from the Table from the union
	var monster2 example.Monster
	monster2.Init(table2.Bytes, table2.Pos)

	if got := monster2.Name(); !bytes.Equal([]byte("Fred"), got) {
		fail(FailString("monster2.Name()", "Fred", got))
	}

	inventorySlice := monster.InventoryBytes()
	if len(inventorySlice) != monster.InventoryLength() {
		fail(FailString("len(monster.InventoryBytes) != monster.InventoryLength", len(inventorySlice), monster.InventoryLength()))
	}

	if got := monster.InventoryLength(); 5 != got {
		fail(FailString("monster.InventoryLength", 5, got))
	}

	invsum := 0
	l := monster.InventoryLength()
	for i := 0; i < l; i++ {
		v := monster.Inventory(i)
		if v != inventorySlice[i] {
			fail(FailString("monster inventory slice[i] != Inventory(i)", v, inventorySlice[i]))
		}
		invsum += int(v)
	}
	if invsum != 10 {
		fail(FailString("monster inventory sum", 10, invsum))
	}

	if got := monster.Test4Length(); 2 != got {
		fail(FailString("monster.Test4Length()", 2, got))
	}

	var test0 example.Test
	ok := monster.Test4(&test0, 0)
	if !ok {
		fail(FailString("monster.Test4(&test0, 0)", true, ok))
	}

	var test1 example.Test
	ok = monster.Test4(&test1, 1)
	if !ok {
		fail(FailString("monster.Test4(&test1, 1)", true, ok))
	}

	// the position of test0 and test1 are swapped in monsterdata_java_wire
	// and monsterdata_test_wire, so ignore ordering
	v0 := test0.A()
	v1 := test0.B()
	v2 := test1.A()
	v3 := test1.B()
	sum := int(v0) + int(v1) + int(v2) + int(v3)

	if 100 != sum {
		fail(FailString("test0 and test1 sum", 100, sum))
	}

	if got := monster.TestarrayofstringLength(); 2 != got {
		fail(FailString("Testarrayofstring length", 2, got))
	}

	if got := monster.Testarrayofstring(0); !bytes.Equal([]byte("test1"), got) {
		fail(FailString("Testarrayofstring(0)", "test1", got))
	}

	if got := monster.Testarrayofstring(1); !bytes.Equal([]byte("test2"), got) {
		fail(FailString("Testarrayofstring(1)", "test2", got))
	}
}
示例#4
0
// CheckMutateBuffer checks that the given buffer can be mutated correctly
// as the example Monster. Only available scalar values are mutated.
func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string, ...interface{})) {
	// make a copy to mutate
	buf := make([]byte, len(org))
	copy(buf, org)

	// load monster data from the buffer
	monster := example.GetRootAsMonster(buf, offset)

	// test case struct
	type testcase struct {
		field  string
		testfn func() bool
	}

	testForOriginalValues := []testcase{
		testcase{"Hp", func() bool { return monster.Hp() == 80 }},
		testcase{"Mana", func() bool { return monster.Mana() == 150 }},
		testcase{"Pos.X'", func() bool { return monster.Pos(nil).X() == float32(1.0) }},
		testcase{"Pos.Y'", func() bool { return monster.Pos(nil).Y() == float32(2.0) }},
		testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(3.0) }},
		testcase{"Pos.Test1'", func() bool { return monster.Pos(nil).Test1() == float64(3.0) }},
		testcase{"Pos.Test2'", func() bool { return monster.Pos(nil).Test2() == int8(2) }},
		testcase{"Pos.Test3.A", func() bool { return monster.Pos(nil).Test3(nil).A() == int16(5) }},
		testcase{"Pos.Test3.B", func() bool { return monster.Pos(nil).Test3(nil).B() == int8(6) }},
	}

	testMutability := []testcase{
		testcase{"Hp", func() bool { return monster.MutateHp(70) }},
		testcase{"Mana", func() bool { return !monster.MutateMana(140) }},
		testcase{"Pos.X", func() bool { return monster.Pos(nil).MutateX(10.0) }},
		testcase{"Pos.Y", func() bool { return monster.Pos(nil).MutateY(20.0) }},
		testcase{"Pos.Z", func() bool { return monster.Pos(nil).MutateZ(30.0) }},
		testcase{"Pos.Test1", func() bool { return monster.Pos(nil).MutateTest1(30.0) }},
		testcase{"Pos.Test2", func() bool { return monster.Pos(nil).MutateTest2(20) }},
		testcase{"Pos.Test3.A", func() bool { return monster.Pos(nil).Test3(nil).MutateA(50) }},
		testcase{"Pos.Test3.B", func() bool { return monster.Pos(nil).Test3(nil).MutateB(60) }},
	}

	testForMutatedValues := []testcase{
		testcase{"Hp", func() bool { return monster.Hp() == 70 }},
		testcase{"Mana", func() bool { return monster.Mana() == 150 }},
		testcase{"Pos.X'", func() bool { return monster.Pos(nil).X() == float32(10.0) }},
		testcase{"Pos.Y'", func() bool { return monster.Pos(nil).Y() == float32(20.0) }},
		testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(30.0) }},
		testcase{"Pos.Test1'", func() bool { return monster.Pos(nil).Test1() == float64(30.0) }},
		testcase{"Pos.Test2'", func() bool { return monster.Pos(nil).Test2() == int8(20) }},
		testcase{"Pos.Test3.A", func() bool { return monster.Pos(nil).Test3(nil).A() == int16(50) }},
		testcase{"Pos.Test3.B", func() bool { return monster.Pos(nil).Test3(nil).B() == int8(60) }},
	}

	// make sure original values are okay
	for _, t := range testForOriginalValues {
		if !t.testfn() {
			fail("field '" + t.field + "' doesn't have the expected original value")
		}
	}

	// try to mutate fields and check mutability
	for _, t := range testMutability {
		if !t.testfn() {
			fail(FailString("field '"+t.field+"' failed mutability test", true, false))
		}
	}

	// test whether values have changed
	for _, t := range testForMutatedValues {
		if !t.testfn() {
			fail("field '" + t.field + "' doesn't have the expected mutated value")
		}
	}

	// make sure the buffer has changed
	if reflect.DeepEqual(buf, org) {
		fail("mutate buffer failed")
	}

	// To make sure the buffer has changed accordingly
	// Read data from the buffer and verify all fields
	monster = example.GetRootAsMonster(buf, offset)
	for _, t := range testForMutatedValues {
		if !t.testfn() {
			fail("field '" + t.field + "' doesn't have the expected mutated value")
		}
	}

	// reverting all fields to original values should
	// re-create the original buffer. Mutate all fields
	// back to their original values and compare buffers.
	// This test is done to make sure mutations do not do
	// any unnecessary changes to the buffer.
	monster = example.GetRootAsMonster(buf, offset)
	monster.MutateHp(80)
	monster.Pos(nil).MutateX(1.0)
	monster.Pos(nil).MutateY(2.0)
	monster.Pos(nil).MutateZ(3.0)
	monster.Pos(nil).MutateTest1(3.0)
	monster.Pos(nil).MutateTest2(2)
	monster.Pos(nil).Test3(nil).MutateA(5)
	monster.Pos(nil).Test3(nil).MutateB(6)

	for _, t := range testForOriginalValues {
		if !t.testfn() {
			fail("field '" + t.field + "' doesn't have the expected original value")
		}
	}

	// buffer should have original values
	if !reflect.DeepEqual(buf, org) {
		fail("revert changes failed")
	}
}