mirror of
https://github.com/json-iterator/go.git
synced 2025-01-17 18:44:50 +02:00
Add output tests for builtin types
This fuzzes a type, marshals it with stdlib and json-iterator, compares, then unmarshals with stdlib and json-iterator and compares. This is checking for literal, byte-for-byte compatibility. In every case the test is exactly the same. It also include benchmark functions to compare stdlib vs json-iterator. This depends on a couple PRs to be merged in gofuzz.
This commit is contained in:
parent
64e500f3c8
commit
9215b3c508
144
output_tests/builtins/bool/json_test.go
Normal file
144
output_tests/builtins/bool/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/bool/types.go
Normal file
3
output_tests/builtins/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T bool
|
144
output_tests/builtins/bool_alias/json_test.go
Normal file
144
output_tests/builtins/bool_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/bool_alias/types.go
Normal file
3
output_tests/builtins/bool_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T bool
|
144
output_tests/builtins/byte/json_test.go
Normal file
144
output_tests/builtins/byte/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/byte/types.go
Normal file
3
output_tests/builtins/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T byte
|
144
output_tests/builtins/byte_alias/json_test.go
Normal file
144
output_tests/builtins/byte_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/byte_alias/types.go
Normal file
3
output_tests/builtins/byte_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T byte
|
144
output_tests/builtins/float32/json_test.go
Normal file
144
output_tests/builtins/float32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/float32/types.go
Normal file
3
output_tests/builtins/float32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T float32
|
144
output_tests/builtins/float32_alias/json_test.go
Normal file
144
output_tests/builtins/float32_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/float32_alias/types.go
Normal file
3
output_tests/builtins/float32_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T float32
|
144
output_tests/builtins/float64/json_test.go
Normal file
144
output_tests/builtins/float64/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/float64/types.go
Normal file
3
output_tests/builtins/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T float64
|
144
output_tests/builtins/float64_alias/json_test.go
Normal file
144
output_tests/builtins/float64_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/float64_alias/types.go
Normal file
3
output_tests/builtins/float64_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T float64
|
144
output_tests/builtins/int16/json_test.go
Normal file
144
output_tests/builtins/int16/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/int16/types.go
Normal file
3
output_tests/builtins/int16/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T int16
|
144
output_tests/builtins/int16_alias/json_test.go
Normal file
144
output_tests/builtins/int16_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/int16_alias/types.go
Normal file
3
output_tests/builtins/int16_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T int16
|
144
output_tests/builtins/int32/json_test.go
Normal file
144
output_tests/builtins/int32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/int32/types.go
Normal file
3
output_tests/builtins/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T int32
|
144
output_tests/builtins/int32_alias/json_test.go
Normal file
144
output_tests/builtins/int32_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/int32_alias/types.go
Normal file
3
output_tests/builtins/int32_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T int32
|
144
output_tests/builtins/int8/json_test.go
Normal file
144
output_tests/builtins/int8/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/int8/types.go
Normal file
3
output_tests/builtins/int8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T int8
|
144
output_tests/builtins/int8_alias/json_test.go
Normal file
144
output_tests/builtins/int8_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/int8_alias/types.go
Normal file
3
output_tests/builtins/int8_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T int8
|
144
output_tests/builtins/string/json_test.go
Normal file
144
output_tests/builtins/string/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/string/types.go
Normal file
3
output_tests/builtins/string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T string
|
144
output_tests/builtins/string_alias/json_test.go
Normal file
144
output_tests/builtins/string_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/string_alias/types.go
Normal file
3
output_tests/builtins/string_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T string
|
144
output_tests/builtins/uint16/json_test.go
Normal file
144
output_tests/builtins/uint16/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/uint16/types.go
Normal file
3
output_tests/builtins/uint16/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T uint16
|
144
output_tests/builtins/uint16_alias/json_test.go
Normal file
144
output_tests/builtins/uint16_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/uint16_alias/types.go
Normal file
3
output_tests/builtins/uint16_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T uint16
|
144
output_tests/builtins/uint32/json_test.go
Normal file
144
output_tests/builtins/uint32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/uint32/types.go
Normal file
3
output_tests/builtins/uint32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T uint32
|
144
output_tests/builtins/uint32_alias/json_test.go
Normal file
144
output_tests/builtins/uint32_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/uint32_alias/types.go
Normal file
3
output_tests/builtins/uint32_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T uint32
|
144
output_tests/builtins/uint8/json_test.go
Normal file
144
output_tests/builtins/uint8/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/uint8/types.go
Normal file
3
output_tests/builtins/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T uint8
|
144
output_tests/builtins/uint8_alias/json_test.go
Normal file
144
output_tests/builtins/uint8_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 1000; i++ {
|
||||
var before T
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
jbIter, err := jsoniter.Marshal(before)
|
||||
if err != nil {
|
||||
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd T
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
json.Indent(&buf, src, prefix, indentStr)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = json.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := jsoniter.Marshal(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before T
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after T
|
||||
err = jsoniter.Unmarshal(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
3
output_tests/builtins/uint8_alias/types.go
Normal file
3
output_tests/builtins/uint8_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T uint8
|
Loading…
x
Reference in New Issue
Block a user