mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-12 16:00:59 +00:00
Implement mutators for Go
This commit is contained in:
@@ -43,6 +43,10 @@ func (rcv *Monster) Mana() int16 {
|
||||
return 150
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateMana(n int16) bool {
|
||||
return rcv._tab.MutateInt16Slot(6, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Hp() int16 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
|
||||
if o != 0 {
|
||||
@@ -51,6 +55,10 @@ func (rcv *Monster) Hp() int16 {
|
||||
return 100
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateHp(n int16) bool {
|
||||
return rcv._tab.MutateInt16Slot(8, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Name() []byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(10))
|
||||
if o != 0 {
|
||||
@@ -92,6 +100,10 @@ func (rcv *Monster) Color() int8 {
|
||||
return 8
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateColor(n int8) bool {
|
||||
return rcv._tab.MutateInt8Slot(16, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) TestType() byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(18))
|
||||
if o != 0 {
|
||||
@@ -100,6 +112,10 @@ func (rcv *Monster) TestType() byte {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTestType(n byte) bool {
|
||||
return rcv._tab.MutateByteSlot(18, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Test(obj *flatbuffers.Table) bool {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(20))
|
||||
if o != 0 {
|
||||
@@ -173,6 +189,8 @@ func (rcv *Monster) TestarrayoftablesLength() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
func (rcv *Monster) Enemy(obj *Monster) *Monster {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(28))
|
||||
if o != 0 {
|
||||
@@ -232,6 +250,10 @@ func (rcv *Monster) Testbool() byte {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTestbool(n byte) bool {
|
||||
return rcv._tab.MutateByteSlot(34, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs32Fnv1() int32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(36))
|
||||
if o != 0 {
|
||||
@@ -240,6 +262,10 @@ func (rcv *Monster) Testhashs32Fnv1() int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashs32Fnv1(n int32) bool {
|
||||
return rcv._tab.MutateInt32Slot(36, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu32Fnv1() uint32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(38))
|
||||
if o != 0 {
|
||||
@@ -248,6 +274,10 @@ func (rcv *Monster) Testhashu32Fnv1() uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashu32Fnv1(n uint32) bool {
|
||||
return rcv._tab.MutateUint32Slot(38, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs64Fnv1() int64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(40))
|
||||
if o != 0 {
|
||||
@@ -256,6 +286,10 @@ func (rcv *Monster) Testhashs64Fnv1() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashs64Fnv1(n int64) bool {
|
||||
return rcv._tab.MutateInt64Slot(40, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu64Fnv1() uint64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(42))
|
||||
if o != 0 {
|
||||
@@ -264,6 +298,10 @@ func (rcv *Monster) Testhashu64Fnv1() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashu64Fnv1(n uint64) bool {
|
||||
return rcv._tab.MutateUint64Slot(42, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs32Fnv1a() int32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(44))
|
||||
if o != 0 {
|
||||
@@ -272,6 +310,10 @@ func (rcv *Monster) Testhashs32Fnv1a() int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashs32Fnv1a(n int32) bool {
|
||||
return rcv._tab.MutateInt32Slot(44, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu32Fnv1a() uint32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(46))
|
||||
if o != 0 {
|
||||
@@ -280,6 +322,10 @@ func (rcv *Monster) Testhashu32Fnv1a() uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashu32Fnv1a(n uint32) bool {
|
||||
return rcv._tab.MutateUint32Slot(46, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashs64Fnv1a() int64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(48))
|
||||
if o != 0 {
|
||||
@@ -288,6 +334,10 @@ func (rcv *Monster) Testhashs64Fnv1a() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashs64Fnv1a(n int64) bool {
|
||||
return rcv._tab.MutateInt64Slot(48, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testhashu64Fnv1a() uint64 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(50))
|
||||
if o != 0 {
|
||||
@@ -296,6 +346,10 @@ func (rcv *Monster) Testhashu64Fnv1a() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTesthashu64Fnv1a(n uint64) bool {
|
||||
return rcv._tab.MutateUint64Slot(50, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testarrayofbools(j int) byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(52))
|
||||
if o != 0 {
|
||||
@@ -321,6 +375,10 @@ func (rcv *Monster) Testf() float32 {
|
||||
return 3.14159
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTestf(n float32) bool {
|
||||
return rcv._tab.MutateFloat32Slot(54, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testf2() float32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(56))
|
||||
if o != 0 {
|
||||
@@ -329,6 +387,10 @@ func (rcv *Monster) Testf2() float32 {
|
||||
return 3.0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTestf2(n float32) bool {
|
||||
return rcv._tab.MutateFloat32Slot(56, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testf3() float32 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(58))
|
||||
if o != 0 {
|
||||
@@ -337,6 +399,10 @@ func (rcv *Monster) Testf3() float32 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
func (rcv *Monster) MutateTestf3(n float32) bool {
|
||||
return rcv._tab.MutateFloat32Slot(58, n)
|
||||
}
|
||||
|
||||
func (rcv *Monster) Testarrayofstring2(j int) []byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(60))
|
||||
if o != 0 {
|
||||
|
||||
@@ -30,6 +30,10 @@ func (rcv *Stat) Val() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Stat) MutateVal(n int64) bool {
|
||||
return rcv._tab.MutateInt64Slot(6, n)
|
||||
}
|
||||
|
||||
func (rcv *Stat) Count() uint16 {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
|
||||
if o != 0 {
|
||||
@@ -38,6 +42,10 @@ func (rcv *Stat) Count() uint16 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (rcv *Stat) MutateCount(n uint16) bool {
|
||||
return rcv._tab.MutateUint16Slot(8, n)
|
||||
}
|
||||
|
||||
func StatStart(builder *flatbuffers.Builder) { builder.StartObject(3) }
|
||||
func StatAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0) }
|
||||
func StatAddVal(builder *flatbuffers.Builder, val int64) { builder.PrependInt64Slot(1, val, 0) }
|
||||
|
||||
@@ -15,7 +15,11 @@ func (rcv *Test) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
}
|
||||
|
||||
func (rcv *Test) A() int16 { return rcv._tab.GetInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0)) }
|
||||
func (rcv *Test) MutateA(n int16) bool { return rcv._tab.MutateInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) }
|
||||
|
||||
func (rcv *Test) B() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2)) }
|
||||
func (rcv *Test) MutateB(n int8) bool { return rcv._tab.MutateInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2), n) }
|
||||
|
||||
|
||||
func CreateTest(builder *flatbuffers.Builder, a int16, b int8) flatbuffers.UOffsetT {
|
||||
builder.Prep(2, 4)
|
||||
|
||||
@@ -22,6 +22,10 @@ func (rcv *TestSimpleTableWithEnum) Color() int8 {
|
||||
return 2
|
||||
}
|
||||
|
||||
func (rcv *TestSimpleTableWithEnum) MutateColor(n int8) bool {
|
||||
return rcv._tab.MutateInt8Slot(4, n)
|
||||
}
|
||||
|
||||
func TestSimpleTableWithEnumStart(builder *flatbuffers.Builder) { builder.StartObject(1) }
|
||||
func TestSimpleTableWithEnumAddColor(builder *flatbuffers.Builder, color int8) { builder.PrependInt8Slot(0, color, 2) }
|
||||
func TestSimpleTableWithEnumEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }
|
||||
|
||||
@@ -15,10 +15,20 @@ func (rcv *Vec3) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
}
|
||||
|
||||
func (rcv *Vec3) X() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) }
|
||||
func (rcv *Vec3) MutateX(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) }
|
||||
|
||||
func (rcv *Vec3) Y() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) }
|
||||
func (rcv *Vec3) MutateY(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4), n) }
|
||||
|
||||
func (rcv *Vec3) Z() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8)) }
|
||||
func (rcv *Vec3) MutateZ(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8), n) }
|
||||
|
||||
func (rcv *Vec3) Test1() float64 { return rcv._tab.GetFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16)) }
|
||||
func (rcv *Vec3) MutateTest1(n float64) bool { return rcv._tab.MutateFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16), n) }
|
||||
|
||||
func (rcv *Vec3) Test2() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24)) }
|
||||
func (rcv *Vec3) MutateTest2(n int8) bool { return rcv._tab.MutateInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24), n) }
|
||||
|
||||
func (rcv *Vec3) Test3(obj *Test) *Test {
|
||||
if obj == nil {
|
||||
obj = new(Test)
|
||||
|
||||
262
tests/go_test.go
262
tests/go_test.go
@@ -67,6 +67,7 @@ func TestAll(t *testing.T) {
|
||||
// Verify that the Go FlatBuffers runtime library generates the
|
||||
// expected bytes (does not use any schema):
|
||||
CheckByteLayout(t.Fatalf)
|
||||
CheckMutateMethods(t.Fatalf)
|
||||
|
||||
// Verify that panics are raised during exceptional conditions:
|
||||
CheckNotInObjectError(t.Fatalf)
|
||||
@@ -82,6 +83,7 @@ func TestAll(t *testing.T) {
|
||||
// Verify that the buffer generated by Go code is readable by the
|
||||
// generated Go code:
|
||||
CheckReadBuffer(generated, off, t.Fatalf)
|
||||
CheckMutateBuffer(generated, off, t.Fatalf)
|
||||
|
||||
// Verify that the buffer generated by C++ code is readable by the
|
||||
// generated Go code:
|
||||
@@ -90,6 +92,7 @@ func TestAll(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
CheckReadBuffer(monsterDataCpp, 0, t.Fatalf)
|
||||
CheckMutateBuffer(monsterDataCpp, 0, t.Fatalf)
|
||||
|
||||
// Verify that vtables are deduplicated when written:
|
||||
CheckVtableDeduplication(t.Fatalf)
|
||||
@@ -280,6 +283,120 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
||||
// Low level stress/fuzz test: serialize/deserialize a variety of
|
||||
// different kinds of data in different combinations
|
||||
func checkFuzz(fuzzFields, fuzzObjects int, fail func(string, ...interface{})) {
|
||||
@@ -1248,6 +1365,151 @@ func CheckByteEquality(a, b []byte, fail func(string, ...interface{})) {
|
||||
}
|
||||
}
|
||||
|
||||
// CheckMutateMethods checks all mutate methods one by one
|
||||
func CheckMutateMethods(fail func(string, ...interface{})) {
|
||||
b := flatbuffers.NewBuilder(0)
|
||||
b.StartObject(15)
|
||||
b.PrependBoolSlot(0, true, false)
|
||||
b.PrependByteSlot(1, 1, 0)
|
||||
b.PrependUint8Slot(2, 2, 0)
|
||||
b.PrependUint16Slot(3, 3, 0)
|
||||
b.PrependUint32Slot(4, 4, 0)
|
||||
b.PrependUint64Slot(5, 5, 0)
|
||||
b.PrependInt8Slot(6, 6, 0)
|
||||
b.PrependInt16Slot(7, 7, 0)
|
||||
b.PrependInt32Slot(8, 8, 0)
|
||||
b.PrependInt64Slot(9, 9, 0)
|
||||
b.PrependFloat32Slot(10, 10, 0)
|
||||
b.PrependFloat64Slot(11, 11, 0)
|
||||
|
||||
b.PrependUOffsetTSlot(12, 12, 0)
|
||||
uoVal := b.Offset() - 12
|
||||
|
||||
b.PrependVOffsetT(13)
|
||||
b.Slot(13)
|
||||
|
||||
b.PrependSOffsetT(14)
|
||||
b.Slot(14)
|
||||
soVal := flatbuffers.SOffsetT(b.Offset() - 14)
|
||||
|
||||
offset := b.EndObject()
|
||||
|
||||
t := &flatbuffers.Table{
|
||||
Bytes: b.Bytes,
|
||||
Pos: flatbuffers.UOffsetT(len(b.Bytes)) - offset,
|
||||
}
|
||||
|
||||
calcVOffsetT := func(slot int) (vtableOffset flatbuffers.VOffsetT) {
|
||||
return flatbuffers.VOffsetT((flatbuffers.VtableMetadataFields + slot) * flatbuffers.SizeVOffsetT)
|
||||
}
|
||||
calcUOffsetT := func(vtableOffset flatbuffers.VOffsetT) (valueOffset flatbuffers.UOffsetT) {
|
||||
return t.Pos + flatbuffers.UOffsetT(t.Offset(vtableOffset))
|
||||
}
|
||||
|
||||
type testcase struct {
|
||||
field string
|
||||
testfn func() bool
|
||||
}
|
||||
|
||||
testForOriginalValues := []testcase{
|
||||
testcase{"BoolSlot", func() bool { return t.GetBoolSlot(calcVOffsetT(0), true) == true }},
|
||||
testcase{"ByteSlot", func() bool { return t.GetByteSlot(calcVOffsetT(1), 1) == 1 }},
|
||||
testcase{"Uint8Slot", func() bool { return t.GetUint8Slot(calcVOffsetT(2), 2) == 2 }},
|
||||
testcase{"Uint16Slot", func() bool { return t.GetUint16Slot(calcVOffsetT(3), 3) == 3 }},
|
||||
testcase{"Uint32Slot", func() bool { return t.GetUint32Slot(calcVOffsetT(4), 4) == 4 }},
|
||||
testcase{"Uint64Slot", func() bool { return t.GetUint64Slot(calcVOffsetT(5), 5) == 5 }},
|
||||
testcase{"Int8Slot", func() bool { return t.GetInt8Slot(calcVOffsetT(6), 6) == 6 }},
|
||||
testcase{"Int16Slot", func() bool { return t.GetInt16Slot(calcVOffsetT(7), 7) == 7 }},
|
||||
testcase{"Int32Slot", func() bool { return t.GetInt32Slot(calcVOffsetT(8), 8) == 8 }},
|
||||
testcase{"Int64Slot", func() bool { return t.GetInt64Slot(calcVOffsetT(9), 9) == 9 }},
|
||||
testcase{"Float32Slot", func() bool { return t.GetFloat32Slot(calcVOffsetT(10), 10) == 10 }},
|
||||
testcase{"Float64Slot", func() bool { return t.GetFloat64Slot(calcVOffsetT(11), 11) == 11 }},
|
||||
testcase{"UOffsetTSlot", func() bool { return t.GetUOffsetT(calcUOffsetT(calcVOffsetT(12))) == uoVal }},
|
||||
testcase{"VOffsetTSlot", func() bool { return t.GetVOffsetT(calcUOffsetT(calcVOffsetT(13))) == 13 }},
|
||||
testcase{"SOffsetTSlot", func() bool { return t.GetSOffsetT(calcUOffsetT(calcVOffsetT(14))) == soVal }},
|
||||
}
|
||||
|
||||
testMutability := []testcase{
|
||||
testcase{"BoolSlot", func() bool { return t.MutateBoolSlot(calcVOffsetT(0), false) }},
|
||||
testcase{"ByteSlot", func() bool { return t.MutateByteSlot(calcVOffsetT(1), 2) }},
|
||||
testcase{"Uint8Slot", func() bool { return t.MutateUint8Slot(calcVOffsetT(2), 4) }},
|
||||
testcase{"Uint16Slot", func() bool { return t.MutateUint16Slot(calcVOffsetT(3), 6) }},
|
||||
testcase{"Uint32Slot", func() bool { return t.MutateUint32Slot(calcVOffsetT(4), 8) }},
|
||||
testcase{"Uint64Slot", func() bool { return t.MutateUint64Slot(calcVOffsetT(5), 10) }},
|
||||
testcase{"Int8Slot", func() bool { return t.MutateInt8Slot(calcVOffsetT(6), 12) }},
|
||||
testcase{"Int16Slot", func() bool { return t.MutateInt16Slot(calcVOffsetT(7), 14) }},
|
||||
testcase{"Int32Slot", func() bool { return t.MutateInt32Slot(calcVOffsetT(8), 16) }},
|
||||
testcase{"Int64Slot", func() bool { return t.MutateInt64Slot(calcVOffsetT(9), 18) }},
|
||||
testcase{"Float32Slot", func() bool { return t.MutateFloat32Slot(calcVOffsetT(10), 20) }},
|
||||
testcase{"Float64Slot", func() bool { return t.MutateFloat64Slot(calcVOffsetT(11), 22) }},
|
||||
testcase{"UOffsetTSlot", func() bool { return t.MutateUOffsetT(calcUOffsetT(calcVOffsetT(12)), 24) }},
|
||||
testcase{"VOffsetTSlot", func() bool { return t.MutateVOffsetT(calcUOffsetT(calcVOffsetT(13)), 26) }},
|
||||
testcase{"SOffsetTSlot", func() bool { return t.MutateSOffsetT(calcUOffsetT(calcVOffsetT(14)), 28) }},
|
||||
}
|
||||
|
||||
testMutabilityWithoutSlot := []testcase{
|
||||
testcase{"BoolSlot", func() bool { return t.MutateBoolSlot(calcVOffsetT(16), false) }},
|
||||
testcase{"ByteSlot", func() bool { return t.MutateByteSlot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Uint8Slot", func() bool { return t.MutateUint8Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Uint16Slot", func() bool { return t.MutateUint16Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Uint32Slot", func() bool { return t.MutateUint32Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Uint64Slot", func() bool { return t.MutateUint64Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Int8Slot", func() bool { return t.MutateInt8Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Int16Slot", func() bool { return t.MutateInt16Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Int32Slot", func() bool { return t.MutateInt32Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Int64Slot", func() bool { return t.MutateInt64Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Float32Slot", func() bool { return t.MutateFloat32Slot(calcVOffsetT(16), 2) }},
|
||||
testcase{"Float64Slot", func() bool { return t.MutateFloat64Slot(calcVOffsetT(16), 2) }},
|
||||
}
|
||||
|
||||
testForMutatedValues := []testcase{
|
||||
testcase{"BoolSlot", func() bool { return t.GetBoolSlot(calcVOffsetT(0), true) == false }},
|
||||
testcase{"ByteSlot", func() bool { return t.GetByteSlot(calcVOffsetT(1), 1) == 2 }},
|
||||
testcase{"Uint8Slot", func() bool { return t.GetUint8Slot(calcVOffsetT(2), 1) == 4 }},
|
||||
testcase{"Uint16Slot", func() bool { return t.GetUint16Slot(calcVOffsetT(3), 1) == 6 }},
|
||||
testcase{"Uint32Slot", func() bool { return t.GetUint32Slot(calcVOffsetT(4), 1) == 8 }},
|
||||
testcase{"Uint64Slot", func() bool { return t.GetUint64Slot(calcVOffsetT(5), 1) == 10 }},
|
||||
testcase{"Int8Slot", func() bool { return t.GetInt8Slot(calcVOffsetT(6), 1) == 12 }},
|
||||
testcase{"Int16Slot", func() bool { return t.GetInt16Slot(calcVOffsetT(7), 1) == 14 }},
|
||||
testcase{"Int32Slot", func() bool { return t.GetInt32Slot(calcVOffsetT(8), 1) == 16 }},
|
||||
testcase{"Int64Slot", func() bool { return t.GetInt64Slot(calcVOffsetT(9), 1) == 18 }},
|
||||
testcase{"Float32Slot", func() bool { return t.GetFloat32Slot(calcVOffsetT(10), 1) == 20 }},
|
||||
testcase{"Float64Slot", func() bool { return t.GetFloat64Slot(calcVOffsetT(11), 1) == 22 }},
|
||||
testcase{"UOffsetTSlot", func() bool { return t.GetUOffsetT(calcUOffsetT(calcVOffsetT(12))) == 24 }},
|
||||
testcase{"VOffsetTSlot", func() bool { return t.GetVOffsetT(calcUOffsetT(calcVOffsetT(13))) == 26 }},
|
||||
testcase{"SOffsetTSlot", func() bool { return t.GetSOffsetT(calcUOffsetT(calcVOffsetT(14))) == 28 }},
|
||||
}
|
||||
|
||||
// make sure original values are okay
|
||||
for _, t := range testForOriginalValues {
|
||||
if !t.testfn() {
|
||||
fail(t.field + "' field doesn't have the expected original value")
|
||||
}
|
||||
}
|
||||
|
||||
// try to mutate fields and check mutability
|
||||
for _, t := range testMutability {
|
||||
if !t.testfn() {
|
||||
fail(FailString(t.field+"' field failed mutability test", "passed", "failed"))
|
||||
}
|
||||
}
|
||||
|
||||
// try to mutate fields and check mutability
|
||||
// these have wrong slots so should fail
|
||||
for _, t := range testMutabilityWithoutSlot {
|
||||
if t.testfn() {
|
||||
fail(FailString(t.field+"' field failed no slot mutability test", "failed", "passed"))
|
||||
}
|
||||
}
|
||||
|
||||
// test whether values have changed
|
||||
for _, t := range testForMutatedValues {
|
||||
if !t.testfn() {
|
||||
fail(t.field + "' field doesn't have the expected mutated value")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkVtableDeduplication measures the speed of vtable deduplication
|
||||
// by creating prePop vtables, then populating b.N objects with a
|
||||
// different single vtable.
|
||||
|
||||
Reference in New Issue
Block a user