Skip to content

Commit e16ee7f

Browse files
committed
Add output tests for structs of slices and maps
1 parent cf63675 commit e16ee7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+4376
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/davecgh/go-spew/spew"
9+
fuzz "github.com/google/gofuzz"
10+
jsoniter "github.com/json-iterator/go"
11+
)
12+
13+
func Test_Roundtrip(t *testing.T) {
14+
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
15+
for i := 0; i < 1000; i++ {
16+
var before T
17+
fz.Fuzz(&before)
18+
19+
jbStd, err := json.Marshal(before)
20+
if err != nil {
21+
t.Errorf("failed to marshal with stdlib: %v", err)
22+
}
23+
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
24+
if err != nil {
25+
t.Errorf("failed to marshal with jsoniter: %v", err)
26+
}
27+
if string(jbStd) != string(jbIter) {
28+
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
29+
indent(jbStd, " "), indent(jbIter, " "), dump(before))
30+
}
31+
32+
var afterStd T
33+
err = json.Unmarshal(jbIter, &afterStd)
34+
if err != nil {
35+
t.Errorf("failed to unmarshal with stdlib: %v", err)
36+
}
37+
var afterIter T
38+
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
39+
if err != nil {
40+
t.Errorf("failed to unmarshal with jsoniter: %v", err)
41+
}
42+
if fingerprint(afterStd) != fingerprint(afterIter) {
43+
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
44+
dump(afterStd), dump(afterIter), indent(jbIter, " "))
45+
}
46+
}
47+
}
48+
49+
const indentStr = "> "
50+
51+
func fingerprint(obj interface{}) string {
52+
c := spew.ConfigState{
53+
SortKeys: true,
54+
SpewKeys: true,
55+
}
56+
return c.Sprintf("%v", obj)
57+
}
58+
59+
func dump(obj interface{}) string {
60+
cfg := spew.ConfigState{
61+
Indent: indentStr,
62+
}
63+
return cfg.Sdump(obj)
64+
}
65+
66+
func indent(src []byte, prefix string) string {
67+
var buf bytes.Buffer
68+
json.Indent(&buf, src, prefix, indentStr)
69+
return buf.String()
70+
}
71+
72+
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
73+
t.ReportAllocs()
74+
t.ResetTimer()
75+
76+
var obj T
77+
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
78+
fz.Fuzz(&obj)
79+
for i := 0; i < t.N; i++ {
80+
jb, err := fn(obj)
81+
if err != nil {
82+
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
83+
}
84+
_ = jb
85+
}
86+
}
87+
88+
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
89+
t.ReportAllocs()
90+
t.ResetTimer()
91+
92+
var before T
93+
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
94+
fz.Fuzz(&before)
95+
jb, err := json.Marshal(before)
96+
if err != nil {
97+
t.Fatalf("failed to marshal: %v", err)
98+
}
99+
100+
for i := 0; i < t.N; i++ {
101+
var after T
102+
err = fn(jb, &after)
103+
if err != nil {
104+
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
105+
}
106+
}
107+
}
108+
109+
func BenchmarkStandardMarshal(t *testing.B) {
110+
benchmarkMarshal(t, "stdlib", json.Marshal)
111+
}
112+
113+
func BenchmarkStandardUnmarshal(t *testing.B) {
114+
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
115+
}
116+
117+
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
118+
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
119+
}
120+
121+
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
122+
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
123+
}
124+
125+
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
126+
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
127+
}
128+
129+
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
130+
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
131+
}
132+
133+
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
134+
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
135+
}
136+
137+
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
138+
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
139+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test
2+
3+
type T struct {
4+
F map[string]float32
5+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/davecgh/go-spew/spew"
9+
fuzz "github.com/google/gofuzz"
10+
jsoniter "github.com/json-iterator/go"
11+
)
12+
13+
func Test_Roundtrip(t *testing.T) {
14+
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
15+
for i := 0; i < 1000; i++ {
16+
var before T
17+
fz.Fuzz(&before)
18+
19+
jbStd, err := json.Marshal(before)
20+
if err != nil {
21+
t.Errorf("failed to marshal with stdlib: %v", err)
22+
}
23+
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
24+
if err != nil {
25+
t.Errorf("failed to marshal with jsoniter: %v", err)
26+
}
27+
if string(jbStd) != string(jbIter) {
28+
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
29+
indent(jbStd, " "), indent(jbIter, " "), dump(before))
30+
}
31+
32+
var afterStd T
33+
err = json.Unmarshal(jbIter, &afterStd)
34+
if err != nil {
35+
t.Errorf("failed to unmarshal with stdlib: %v", err)
36+
}
37+
var afterIter T
38+
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
39+
if err != nil {
40+
t.Errorf("failed to unmarshal with jsoniter: %v", err)
41+
}
42+
if fingerprint(afterStd) != fingerprint(afterIter) {
43+
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
44+
dump(afterStd), dump(afterIter), indent(jbIter, " "))
45+
}
46+
}
47+
}
48+
49+
const indentStr = "> "
50+
51+
func fingerprint(obj interface{}) string {
52+
c := spew.ConfigState{
53+
SortKeys: true,
54+
SpewKeys: true,
55+
}
56+
return c.Sprintf("%v", obj)
57+
}
58+
59+
func dump(obj interface{}) string {
60+
cfg := spew.ConfigState{
61+
Indent: indentStr,
62+
}
63+
return cfg.Sdump(obj)
64+
}
65+
66+
func indent(src []byte, prefix string) string {
67+
var buf bytes.Buffer
68+
json.Indent(&buf, src, prefix, indentStr)
69+
return buf.String()
70+
}
71+
72+
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
73+
t.ReportAllocs()
74+
t.ResetTimer()
75+
76+
var obj T
77+
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
78+
fz.Fuzz(&obj)
79+
for i := 0; i < t.N; i++ {
80+
jb, err := fn(obj)
81+
if err != nil {
82+
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
83+
}
84+
_ = jb
85+
}
86+
}
87+
88+
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
89+
t.ReportAllocs()
90+
t.ResetTimer()
91+
92+
var before T
93+
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
94+
fz.Fuzz(&before)
95+
jb, err := json.Marshal(before)
96+
if err != nil {
97+
t.Fatalf("failed to marshal: %v", err)
98+
}
99+
100+
for i := 0; i < t.N; i++ {
101+
var after T
102+
err = fn(jb, &after)
103+
if err != nil {
104+
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
105+
}
106+
}
107+
}
108+
109+
func BenchmarkStandardMarshal(t *testing.B) {
110+
benchmarkMarshal(t, "stdlib", json.Marshal)
111+
}
112+
113+
func BenchmarkStandardUnmarshal(t *testing.B) {
114+
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
115+
}
116+
117+
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
118+
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
119+
}
120+
121+
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
122+
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
123+
}
124+
125+
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
126+
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
127+
}
128+
129+
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
130+
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
131+
}
132+
133+
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
134+
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
135+
}
136+
137+
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
138+
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
139+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test
2+
3+
type T struct {
4+
F map[string]int32
5+
}

0 commit comments

Comments
 (0)