-
Notifications
You must be signed in to change notification settings - Fork 0
/
getter_test.go
75 lines (63 loc) · 3.02 KB
/
getter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package decoder
import (
"testing"
"github.com/koykov/inspector/testobj"
)
func TestGetter(t *testing.T) {
t.Run("crc32", func(t *testing.T) { testGetter(t, "src", scenarioGetterCrc32) })
t.Run("crc32Static", func(t *testing.T) { testGetter(t, "src", scenarioGetterCrc32) })
t.Run("atoi", func(t *testing.T) { testGetter(t, "src", scenarioGetterAtoi) })
t.Run("atou", func(t *testing.T) { testGetter(t, "src", scenarioGetterAtou) })
t.Run("atof", func(t *testing.T) { testGetter(t, "src", scenarioGetterAtof) })
t.Run("atob", func(t *testing.T) { testGetter(t, "src", scenarioGetterAtob) })
t.Run("itoa-explicit-vector", func(t *testing.T) { testGetter(t, "src", scenarioGetterItoa) })
t.Run("itoa-explicit-var", func(t *testing.T) { testGetter(t, "src", scenarioGetterItoa) })
t.Run("itoa-implicit-vector", func(t *testing.T) { testGetter(t, "src", scenarioGetterItoa) })
t.Run("itoa-implicit-var", func(t *testing.T) { testGetter(t, "src", scenarioGetterItoa) })
}
func testGetter(t *testing.T, jsonKey string, assertFn func(t testing.TB, obj *testobj.TestObject)) {
ctx := NewCtx()
obj := &testobj.TestObject{}
obj = assertDecode(t, ctx, obj, "getter", jsonKey)
assertFn(t, obj)
}
func BenchmarkGetter(b *testing.B) {
b.Run("crc32", func(b *testing.B) { benchGetter(b, "src", scenarioGetterCrc32) })
b.Run("crc32Static", func(b *testing.B) { benchGetter(b, "src", scenarioGetterCrc32) })
b.Run("atoi", func(b *testing.B) { benchGetter(b, "src", scenarioGetterAtoi) })
b.Run("atou", func(b *testing.B) { benchGetter(b, "src", scenarioGetterAtou) })
b.Run("atof", func(b *testing.B) { benchGetter(b, "src", scenarioGetterAtof) })
b.Run("atob", func(b *testing.B) { benchGetter(b, "src", scenarioGetterAtob) })
b.Run("itoa-explicit-vector", func(b *testing.B) { benchGetter(b, "src", scenarioGetterItoa) })
b.Run("itoa-explicit-var", func(b *testing.B) { benchGetter(b, "src", scenarioGetterItoa) })
b.Run("itoa-implicit-vector", func(b *testing.B) { benchGetter(b, "src", scenarioGetterItoa) })
b.Run("itoa-implicit-var", func(b *testing.B) { benchGetter(b, "src", scenarioGetterItoa) })
}
func benchGetter(b *testing.B, jsonKey string, assertFn func(t testing.TB, obj *testobj.TestObject)) {
ctx := NewCtx()
obj := &testobj.TestObject{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
obj = assertDecode(b, ctx, obj, "getter", jsonKey)
assertFn(b, obj)
obj.Clear()
}
}
func scenarioGetterCrc32(t testing.TB, obj *testobj.TestObject) {
assertS(t, "Name", obj.Id, "312073870")
}
func scenarioGetterAtoi(t testing.TB, obj *testobj.TestObject) {
assertI32(t, "Status", obj.Status, 105999)
}
func scenarioGetterAtou(t testing.TB, obj *testobj.TestObject) {
assertU64(t, "Status", obj.Ustate, 67)
}
func scenarioGetterAtof(t testing.TB, obj *testobj.TestObject) {
assertF64(t, "Cost", obj.Cost, 45.90421)
}
func scenarioGetterAtob(t testing.TB, obj *testobj.TestObject) {
assertBl(t, "Finance.AllowBuy", obj.Finance.AllowBuy, true)
}
func scenarioGetterItoa(t testing.TB, obj *testobj.TestObject) {
assertB(t, "Status", obj.Name, []byte("67"))
}