mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
Merge pull request #84 from thockin/output_tests
Output tests for embedded marshalers and arrays
This commit is contained in:
commit
7244d730b9
152
output_tests/_json_test.go
Normal file
152
output_tests/_json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/_not_done/TODO
Normal file
3
output_tests/_not_done/TODO
Normal file
@ -0,0 +1,3 @@
|
||||
arrays
|
||||
interfaces
|
||||
complex
|
152
output_tests/_not_done/structs/everything/json_test.go
Normal file
152
output_tests/_not_done/structs/everything/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
204
output_tests/_not_done/structs/everything/types.go
Normal file
204
output_tests/_not_done/structs/everything/types.go
Normal file
@ -0,0 +1,204 @@
|
||||
package test
|
||||
|
||||
type ByteAlias byte
|
||||
|
||||
type BoolAlias bool
|
||||
|
||||
type Int32Alias int32
|
||||
|
||||
type Float32Alias float32
|
||||
|
||||
type StringAlias string
|
||||
|
||||
type SliceStringAlias []string
|
||||
|
||||
type SlicePtrStringAlias []*string
|
||||
|
||||
type MapStringStringAlias map[string]string
|
||||
|
||||
type Inner struct {
|
||||
Byte byte
|
||||
BytePtr *byte
|
||||
ByteAlias ByteAlias
|
||||
ByteAliasPtr *ByteAlias
|
||||
Bool bool
|
||||
BoolPtr *bool
|
||||
BoolAlias BoolAlias
|
||||
BoolAliasPtr *BoolAlias
|
||||
Int8 int8
|
||||
Int8Ptr *int8
|
||||
Int16 int16
|
||||
Int16Ptr *int16
|
||||
Int32 int32
|
||||
Int32Ptr *int32
|
||||
Int32Alias Int32Alias
|
||||
Int32AliasPtr *Int32Alias
|
||||
Uint8 uint8
|
||||
Uint8Ptr *uint8
|
||||
Uint16 uint16
|
||||
Uint16Ptr *uint16
|
||||
Uint32 uint32
|
||||
Uint32Ptr *uint32
|
||||
Float32 float32
|
||||
Float32Ptr *float32
|
||||
Float32Alias Float32Alias
|
||||
Float32AliasPtr *Float32Alias
|
||||
Float64 float64
|
||||
Float64Ptr *float64
|
||||
String string
|
||||
StringPtr *string
|
||||
StringAlias StringAlias
|
||||
StringAliasPtr *StringAlias
|
||||
Struct struct {
|
||||
Byte byte
|
||||
BytePtr *byte
|
||||
ByteAlias ByteAlias
|
||||
ByteAliasPtr *ByteAlias
|
||||
Bool bool
|
||||
BoolPtr *bool
|
||||
BoolAlias BoolAlias
|
||||
BoolAliasPtr *BoolAlias
|
||||
Int8 int8
|
||||
Int8Ptr *int8
|
||||
Int16 int16
|
||||
Int16Ptr *int16
|
||||
Int32 int32
|
||||
Int32Ptr *int32
|
||||
Int32Alias Int32Alias
|
||||
Int32AliasPtr *Int32Alias
|
||||
Uint8 uint8
|
||||
Uint8Ptr *uint8
|
||||
Uint16 uint16
|
||||
Uint16Ptr *uint16
|
||||
Uint32 uint32
|
||||
Uint32Ptr *uint32
|
||||
Float32 float32
|
||||
Float32Ptr *float32
|
||||
Float32Alias Float32Alias
|
||||
Float32AliasPtr *Float32Alias
|
||||
Float64 float64
|
||||
Float64Ptr *float64
|
||||
String string
|
||||
StringPtr *string
|
||||
StringAlias StringAlias
|
||||
StringAliasPtr *StringAlias
|
||||
Struct struct{}
|
||||
StructPtr *Inner
|
||||
SliceString []string
|
||||
SliceStringAlias SliceStringAlias
|
||||
SlicePtrString []*string
|
||||
SliceStringPtrAlias SlicePtrStringAlias
|
||||
SliceStringPtr *[]string
|
||||
SliceByte []byte
|
||||
}
|
||||
StructPtr *struct {
|
||||
Byte byte
|
||||
BytePtr *byte
|
||||
ByteAlias ByteAlias
|
||||
ByteAliasPtr *ByteAlias
|
||||
Bool bool
|
||||
BoolPtr *bool
|
||||
BoolAlias BoolAlias
|
||||
BoolAliasPtr *BoolAlias
|
||||
Int8 int8
|
||||
Int8Ptr *int8
|
||||
Int16 int16
|
||||
Int16Ptr *int16
|
||||
Int32 int32
|
||||
Int32Ptr *int32
|
||||
Int32Alias Int32Alias
|
||||
Int32AliasPtr *Int32Alias
|
||||
Uint8 uint8
|
||||
Uint8Ptr *uint8
|
||||
Uint16 uint16
|
||||
Uint16Ptr *uint16
|
||||
Uint32 uint32
|
||||
Uint32Ptr *uint32
|
||||
Float32 float32
|
||||
Float32Ptr *float32
|
||||
Float32Alias Float32Alias
|
||||
Float32AliasPtr *Float32Alias
|
||||
Float64 float64
|
||||
Float64Ptr *float64
|
||||
String string
|
||||
StringPtr *string
|
||||
StringAlias StringAlias
|
||||
StringAliasPtr *StringAlias
|
||||
Struct struct{}
|
||||
StructPtr *Inner
|
||||
SliceString []string
|
||||
SliceStringAlias SliceStringAlias
|
||||
SlicePtrString []*string
|
||||
SliceStringPtrAlias SlicePtrStringAlias
|
||||
SliceStringPtr *[]string
|
||||
SliceByte []byte
|
||||
MapStringString map[string]string
|
||||
MapStringStringPtr *map[string]string
|
||||
MapStringStringAlias MapStringStringAlias
|
||||
MapStringStringAliasPtr *MapStringStringAlias
|
||||
}
|
||||
SliceString []string
|
||||
SliceStringAlias SliceStringAlias
|
||||
SlicePtrString []*string
|
||||
SliceStringPtrAlias SlicePtrStringAlias
|
||||
SliceStringPtr *[]string
|
||||
SliceByte []byte
|
||||
MapStringString map[string]string
|
||||
MapStringStringPtr *map[string]string
|
||||
MapStringStringAlias MapStringStringAlias
|
||||
MapStringStringAliasPtr *MapStringStringAlias
|
||||
}
|
||||
|
||||
type T struct {
|
||||
Byte byte
|
||||
BytePtr *byte
|
||||
ByteAlias ByteAlias
|
||||
ByteAliasPtr *ByteAlias
|
||||
Bool bool
|
||||
BoolPtr *bool
|
||||
BoolAlias BoolAlias
|
||||
BoolAliasPtr *BoolAlias
|
||||
Int8 int8
|
||||
Int8Ptr *int8
|
||||
Int16 int16
|
||||
Int16Ptr *int16
|
||||
Int32 int32
|
||||
Int32Ptr *int32
|
||||
Int32Alias Int32Alias
|
||||
Int32AliasPtr *Int32Alias
|
||||
Uint8 uint8
|
||||
Uint8Ptr *uint8
|
||||
Uint16 uint16
|
||||
Uint16Ptr *uint16
|
||||
Uint32 uint32
|
||||
Uint32Ptr *uint32
|
||||
Float32 float32
|
||||
Float32Ptr *float32
|
||||
Float32Alias Float32Alias
|
||||
Float32AliasPtr *Float32Alias
|
||||
Float64 float64
|
||||
Float64Ptr *float64
|
||||
String string
|
||||
StringPtr *string
|
||||
StringAlias StringAlias
|
||||
StringAliasPtr *StringAlias
|
||||
StructPtr *Inner
|
||||
Struct struct {
|
||||
Struct struct {
|
||||
Struct struct {
|
||||
Struct struct {
|
||||
String string
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SliceString []string
|
||||
SliceStringAlias SliceStringAlias
|
||||
SlicePtrString []*string
|
||||
SliceStringPtrAlias SlicePtrStringAlias
|
||||
SliceStringPtr *[]string
|
||||
MapStringString map[string]string
|
||||
MapStringStringPtr *map[string]string
|
||||
MapStringStringAlias MapStringStringAlias
|
||||
MapStringStringAliasPtr *MapStringStringAlias
|
||||
}
|
152
output_tests/array/array/bool/json_test.go
Normal file
152
output_tests/array/array/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/bool/types.go
Normal file
3
output_tests/array/array/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]bool
|
152
output_tests/array/array/byte/json_test.go
Normal file
152
output_tests/array/array/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/byte/types.go
Normal file
3
output_tests/array/array/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]byte
|
152
output_tests/array/array/float64/json_test.go
Normal file
152
output_tests/array/array/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/float64/types.go
Normal file
3
output_tests/array/array/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]float64
|
152
output_tests/array/array/int32/json_test.go
Normal file
152
output_tests/array/array/int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/int32/types.go
Normal file
3
output_tests/array/array/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]int32
|
152
output_tests/array/array/ptr_string/json_test.go
Normal file
152
output_tests/array/array/ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/ptr_string/types.go
Normal file
3
output_tests/array/array/ptr_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]*string
|
152
output_tests/array/array/string/json_test.go
Normal file
152
output_tests/array/array/string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/string/types.go
Normal file
3
output_tests/array/array/string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]string
|
152
output_tests/array/array/uint8/json_test.go
Normal file
152
output_tests/array/array/uint8/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/array/uint8/types.go
Normal file
3
output_tests/array/array/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]uint8
|
152
output_tests/array/bool/json_test.go
Normal file
152
output_tests/array/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/bool/types.go
Normal file
3
output_tests/array/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]bool
|
152
output_tests/array/byte/json_test.go
Normal file
152
output_tests/array/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/byte/types.go
Normal file
3
output_tests/array/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]byte
|
152
output_tests/array/float64/json_test.go
Normal file
152
output_tests/array/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/float64/types.go
Normal file
3
output_tests/array/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]float64
|
152
output_tests/array/int32/json_test.go
Normal file
152
output_tests/array/int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/int32/types.go
Normal file
3
output_tests/array/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]int32
|
152
output_tests/array/map/int32_string/json_test.go
Normal file
152
output_tests/array/map/int32_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/map/int32_string/types.go
Normal file
3
output_tests/array/map/int32_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]map[int32]string
|
152
output_tests/array/map/string_string/json_test.go
Normal file
152
output_tests/array/map/string_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/map/string_string/types.go
Normal file
3
output_tests/array/map/string_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]map[string]string
|
152
output_tests/array/ptr_bool/json_test.go
Normal file
152
output_tests/array/ptr_bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_bool/types.go
Normal file
3
output_tests/array/ptr_bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*bool
|
152
output_tests/array/ptr_float64/json_test.go
Normal file
152
output_tests/array/ptr_float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_float64/types.go
Normal file
3
output_tests/array/ptr_float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*float64
|
152
output_tests/array/ptr_int32/json_test.go
Normal file
152
output_tests/array/ptr_int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_int32/types.go
Normal file
3
output_tests/array/ptr_int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*int32
|
152
output_tests/array/ptr_map/int32_string/json_test.go
Normal file
152
output_tests/array/ptr_map/int32_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_map/int32_string/types.go
Normal file
3
output_tests/array/ptr_map/int32_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*map[int32]string
|
152
output_tests/array/ptr_map/string_string/json_test.go
Normal file
152
output_tests/array/ptr_map/string_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_map/string_string/types.go
Normal file
3
output_tests/array/ptr_map/string_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*map[string]string
|
152
output_tests/array/ptr_slice/bool/json_test.go
Normal file
152
output_tests/array/ptr_slice/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/bool/types.go
Normal file
3
output_tests/array/ptr_slice/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]bool
|
152
output_tests/array/ptr_slice/byte/json_test.go
Normal file
152
output_tests/array/ptr_slice/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/byte/types.go
Normal file
3
output_tests/array/ptr_slice/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]byte
|
152
output_tests/array/ptr_slice/float64/json_test.go
Normal file
152
output_tests/array/ptr_slice/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/float64/types.go
Normal file
3
output_tests/array/ptr_slice/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]float64
|
152
output_tests/array/ptr_slice/int32/json_test.go
Normal file
152
output_tests/array/ptr_slice/int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/int32/types.go
Normal file
3
output_tests/array/ptr_slice/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]int32
|
152
output_tests/array/ptr_slice/ptr_string/json_test.go
Normal file
152
output_tests/array/ptr_slice/ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/ptr_string/types.go
Normal file
3
output_tests/array/ptr_slice/ptr_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]*string
|
152
output_tests/array/ptr_slice/string/json_test.go
Normal file
152
output_tests/array/ptr_slice/string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/string/types.go
Normal file
3
output_tests/array/ptr_slice/string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]string
|
152
output_tests/array/ptr_slice/uint8/json_test.go
Normal file
152
output_tests/array/ptr_slice/uint8/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_slice/uint8/types.go
Normal file
3
output_tests/array/ptr_slice/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*[4]uint8
|
152
output_tests/array/ptr_string/json_test.go
Normal file
152
output_tests/array/ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_string/types.go
Normal file
3
output_tests/array/ptr_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*string
|
152
output_tests/array/ptr_struct_various/json_test.go
Normal file
152
output_tests/array/ptr_struct_various/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
12
output_tests/array/ptr_struct_various/types.go
Normal file
12
output_tests/array/ptr_struct_various/types.go
Normal file
@ -0,0 +1,12 @@
|
||||
package test
|
||||
|
||||
type T [4]*struct {
|
||||
String string
|
||||
Int int32
|
||||
Float float64
|
||||
Struct struct {
|
||||
X string
|
||||
}
|
||||
Slice [4]string
|
||||
Map map[string]string
|
||||
}
|
152
output_tests/array/ptr_uint8/json_test.go
Normal file
152
output_tests/array/ptr_uint8/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/ptr_uint8/types.go
Normal file
3
output_tests/array/ptr_uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]*uint8
|
152
output_tests/array/slice/bool/json_test.go
Normal file
152
output_tests/array/slice/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/bool/types.go
Normal file
3
output_tests/array/slice/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]bool
|
152
output_tests/array/slice/byte/json_test.go
Normal file
152
output_tests/array/slice/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/byte/types.go
Normal file
3
output_tests/array/slice/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]byte
|
152
output_tests/array/slice/float64/json_test.go
Normal file
152
output_tests/array/slice/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/float64/types.go
Normal file
3
output_tests/array/slice/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]float64
|
152
output_tests/array/slice/int32/json_test.go
Normal file
152
output_tests/array/slice/int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/int32/types.go
Normal file
3
output_tests/array/slice/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]int32
|
152
output_tests/array/slice/ptr_string/json_test.go
Normal file
152
output_tests/array/slice/ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/ptr_string/types.go
Normal file
3
output_tests/array/slice/ptr_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]*string
|
152
output_tests/array/slice/string/json_test.go
Normal file
152
output_tests/array/slice/string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/string/types.go
Normal file
3
output_tests/array/slice/string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]string
|
152
output_tests/array/slice/uint8/json_test.go
Normal file
152
output_tests/array/slice/uint8/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/slice/uint8/types.go
Normal file
3
output_tests/array/slice/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]uint8
|
152
output_tests/array/string/json_test.go
Normal file
152
output_tests/array/string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/string/types.go
Normal file
3
output_tests/array/string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]string
|
152
output_tests/array/struct_empty/json_test.go
Normal file
152
output_tests/array/struct_empty/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/struct_empty/types.go
Normal file
3
output_tests/array/struct_empty/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]struct{}
|
152
output_tests/array/struct_empty_alias/json_test.go
Normal file
152
output_tests/array/struct_empty_alias/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
5
output_tests/array/struct_empty_alias/types.go
Normal file
5
output_tests/array/struct_empty_alias/types.go
Normal file
@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
type A struct{}
|
||||
|
||||
type T [4]A
|
152
output_tests/array/struct_ptr_string/json_test.go
Normal file
152
output_tests/array/struct_ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
5
output_tests/array/struct_ptr_string/types.go
Normal file
5
output_tests/array/struct_ptr_string/types.go
Normal file
@ -0,0 +1,5 @@
|
||||
package test
|
||||
|
||||
type T [4]struct {
|
||||
F *string
|
||||
}
|
152
output_tests/array/struct_various/json_test.go
Normal file
152
output_tests/array/struct_various/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
12
output_tests/array/struct_various/types.go
Normal file
12
output_tests/array/struct_various/types.go
Normal file
@ -0,0 +1,12 @@
|
||||
package test
|
||||
|
||||
type T [4]struct {
|
||||
String string
|
||||
Int int32
|
||||
Float float64
|
||||
Struct struct {
|
||||
X string
|
||||
}
|
||||
Slice [4]string
|
||||
Map map[string]string
|
||||
}
|
152
output_tests/array/uint8/json_test.go
Normal file
152
output_tests/array/uint8/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/array/uint8/types.go
Normal file
3
output_tests/array/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4]uint8
|
152
output_tests/map/string/array_string/json_test.go
Normal file
152
output_tests/map/string/array_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/map/string/array_string/types.go
Normal file
3
output_tests/map/string/array_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T map[string][4]string
|
152
output_tests/map/string/ptr_array_string/json_test.go
Normal file
152
output_tests/map/string/ptr_array_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/map/string/ptr_array_string/types.go
Normal file
3
output_tests/map/string/ptr_array_string/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T map[string]*[4]string
|
23
output_tests/marshal_fail_case.go
Normal file
23
output_tests/marshal_fail_case.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
F *float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
var obj T
|
||||
|
||||
jb1, _ := json.Marshal(obj)
|
||||
jb2, _ := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
|
||||
if !reflect.DeepEqual(jb1, jb2) {
|
||||
fmt.Printf("results differ:\n expected: `%s`\n got: `%s`\n", string(jb1), string(jb2))
|
||||
}
|
||||
}
|
152
output_tests/slice/array/bool/json_test.go
Normal file
152
output_tests/slice/array/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/slice/array/bool/types.go
Normal file
3
output_tests/slice/array/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]bool
|
152
output_tests/slice/array/byte/json_test.go
Normal file
152
output_tests/slice/array/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/slice/array/byte/types.go
Normal file
3
output_tests/slice/array/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]byte
|
152
output_tests/slice/array/float64/json_test.go
Normal file
152
output_tests/slice/array/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/slice/array/float64/types.go
Normal file
3
output_tests/slice/array/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]float64
|
152
output_tests/slice/array/int32/json_test.go
Normal file
152
output_tests/slice/array/int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
3
output_tests/slice/array/int32/types.go
Normal file
3
output_tests/slice/array/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
||||
package test
|
||||
|
||||
type T [4][4]int32
|
152
output_tests/slice/array/ptr_string/json_test.go
Normal file
152
output_tests/slice/array/ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"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.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("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.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter T
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("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
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
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 := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
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 = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user