From 0ed9de94f22b338d3e08adfe6bd58a7e88ae890c Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Tue, 13 Feb 2018 20:25:27 +0800 Subject: [PATCH] support asymmetric tests --- .../caseless_unmarshal/caseless_test.go | 84 ---------- output_tests/caseless_unmarshal/types.go | 9 -- .../string_alias/json_test.go | 152 ------------------ .../string_alias/types.go | 22 --- .../map_key_text_marshal/struct/json_test.go | 152 ------------------ .../map_key_text_marshal/struct/types.go | 24 --- output_tests/marshal_fail_case.go | 23 --- .../partial_unmarshal/partial_test.go | 84 ---------- output_tests/partial_unmarshal/types.go | 11 -- .../text_marshal/string_alias/json_test.go | 152 ------------------ .../text_marshal/string_alias/types.go | 50 ------ output_tests/text_marshal/struct/json_test.go | 152 ------------------ output_tests/text_marshal/struct/types.go | 52 ------ .../text_marshal/struct_alias/json_test.go | 152 ------------------ .../text_marshal/struct_alias/types.go | 54 ------- .../text_marshal/struct_field/json_test.go | 152 ------------------ .../text_marshal/struct_field/types.go | 56 ------- .../struct_field_alias/json_test.go | 152 ------------------ .../text_marshal/struct_field_alias/types.go | 58 ------- type_tests/map_key_test.go | 44 +++++ type_tests/struct_field_case_test.go | 24 +++ type_tests/text_marshaler_string_test.go | 8 +- type_tests/text_marshaler_struct_test.go | 69 ++++++++ type_tests/type_test.go | 57 ++++++- 24 files changed, 200 insertions(+), 1593 deletions(-) delete mode 100644 output_tests/caseless_unmarshal/caseless_test.go delete mode 100644 output_tests/caseless_unmarshal/types.go delete mode 100644 output_tests/map_key_text_marshal/string_alias/json_test.go delete mode 100644 output_tests/map_key_text_marshal/string_alias/types.go delete mode 100644 output_tests/map_key_text_marshal/struct/json_test.go delete mode 100644 output_tests/map_key_text_marshal/struct/types.go delete mode 100644 output_tests/marshal_fail_case.go delete mode 100644 output_tests/partial_unmarshal/partial_test.go delete mode 100644 output_tests/partial_unmarshal/types.go delete mode 100644 output_tests/text_marshal/string_alias/json_test.go delete mode 100644 output_tests/text_marshal/string_alias/types.go delete mode 100644 output_tests/text_marshal/struct/json_test.go delete mode 100644 output_tests/text_marshal/struct/types.go delete mode 100644 output_tests/text_marshal/struct_alias/json_test.go delete mode 100644 output_tests/text_marshal/struct_alias/types.go delete mode 100644 output_tests/text_marshal/struct_field/json_test.go delete mode 100644 output_tests/text_marshal/struct_field/types.go delete mode 100644 output_tests/text_marshal/struct_field_alias/json_test.go delete mode 100644 output_tests/text_marshal/struct_field_alias/types.go create mode 100644 type_tests/map_key_test.go create mode 100644 type_tests/struct_field_case_test.go create mode 100644 type_tests/text_marshaler_struct_test.go diff --git a/output_tests/caseless_unmarshal/caseless_test.go b/output_tests/caseless_unmarshal/caseless_test.go deleted file mode 100644 index 9343b68..0000000 --- a/output_tests/caseless_unmarshal/caseless_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// NOTE: This test is different than most of the other JSON roundtrip tests. -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_CaselessUnmarshal(t *testing.T) { - fz := fuzz.New().MaxDepth(10).NilChance(0.3) - for i := 0; i < 100; i++ { - var before typeForTest1 - 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 typeForTest2 - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest2 - 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() -} diff --git a/output_tests/caseless_unmarshal/types.go b/output_tests/caseless_unmarshal/types.go deleted file mode 100644 index beb817e..0000000 --- a/output_tests/caseless_unmarshal/types.go +++ /dev/null @@ -1,9 +0,0 @@ -package test - -type typeForTest1 struct { - Field string -} - -type typeForTest2 struct { - FIELD string -} diff --git a/output_tests/map_key_text_marshal/string_alias/json_test.go b/output_tests/map_key_text_marshal/string_alias/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/map_key_text_marshal/string_alias/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/map_key_text_marshal/string_alias/types.go b/output_tests/map_key_text_marshal/string_alias/types.go deleted file mode 100644 index f12da39..0000000 --- a/output_tests/map_key_text_marshal/string_alias/types.go +++ /dev/null @@ -1,22 +0,0 @@ -package test - -import ( - "encoding" - "strings" -) - -type keyType string - -func (k keyType) MarshalText() ([]byte, error) { - return []byte("MANUAL__" + k), nil -} - -func (k *keyType) UnmarshalText(text []byte) error { - *k = keyType(strings.TrimPrefix(string(text), "MANUAL__")) - return nil -} - -var _ encoding.TextMarshaler = keyType("") -var _ encoding.TextUnmarshaler = new(keyType) - -type typeForTest map[keyType]string diff --git a/output_tests/map_key_text_marshal/struct/json_test.go b/output_tests/map_key_text_marshal/struct/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/map_key_text_marshal/struct/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/map_key_text_marshal/struct/types.go b/output_tests/map_key_text_marshal/struct/types.go deleted file mode 100644 index 1944c1e..0000000 --- a/output_tests/map_key_text_marshal/struct/types.go +++ /dev/null @@ -1,24 +0,0 @@ -package test - -import ( - "encoding" - "strings" -) - -type keyType struct { - X string -} - -func (k keyType) MarshalText() ([]byte, error) { - return []byte("MANUAL__" + k.X), nil -} - -func (k *keyType) UnmarshalText(text []byte) error { - k.X = strings.TrimPrefix(string(text), "MANUAL__") - return nil -} - -var _ encoding.TextMarshaler = keyType{} -var _ encoding.TextUnmarshaler = &keyType{} - -type typeForTest map[keyType]string diff --git a/output_tests/marshal_fail_case.go b/output_tests/marshal_fail_case.go deleted file mode 100644 index 2e20041..0000000 --- a/output_tests/marshal_fail_case.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "reflect" - - jsoniter "github.com/json-iterator/go" -) - -type typeForTest struct { - F *float64 -} - -func main() { - var obj typeForTest - - 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)) - } -} diff --git a/output_tests/partial_unmarshal/partial_test.go b/output_tests/partial_unmarshal/partial_test.go deleted file mode 100644 index a30b5c4..0000000 --- a/output_tests/partial_unmarshal/partial_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// NOTE: This test is different than most of the other JSON roundtrip tests. -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_PartialUnmarshal(t *testing.T) { - fz := fuzz.New().MaxDepth(10).NilChance(0.3) - for i := 0; i < 100; i++ { - var before typeForTest1 - 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 typeForTest2 - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest2 - 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() -} diff --git a/output_tests/partial_unmarshal/types.go b/output_tests/partial_unmarshal/types.go deleted file mode 100644 index 00282d4..0000000 --- a/output_tests/partial_unmarshal/types.go +++ /dev/null @@ -1,11 +0,0 @@ -package test - -type typeForTest1 struct { - F1 string - F2 string - F3 string -} - -type typeForTest2 struct { - F1 string -} diff --git a/output_tests/text_marshal/string_alias/json_test.go b/output_tests/text_marshal/string_alias/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/text_marshal/string_alias/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/text_marshal/string_alias/types.go b/output_tests/text_marshal/string_alias/types.go deleted file mode 100644 index 0dd9ba9..0000000 --- a/output_tests/text_marshal/string_alias/types.go +++ /dev/null @@ -1,50 +0,0 @@ -package test - -import ( - "bytes" - "encoding" - "encoding/base64" - "strings" -) - -type marshalerForTest string - -func encode(str string) string { - buf := bytes.Buffer{} - b64 := base64.NewEncoder(base64.StdEncoding, &buf) - if _, err := b64.Write([]byte(str)); err != nil { - panic(err) - } - if err := b64.Close(); err != nil { - panic(err) - } - return buf.String() -} - -func decode(str string) string { - if len(str) == 0 { - return "" - } - b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str)) - bs := make([]byte, len(str)) - if n, err := b64.Read(bs); err != nil { - panic(err) - } else { - bs = bs[:n] - } - return string(bs) -} - -func (m marshalerForTest) MarshalText() ([]byte, error) { - return []byte(`MANUAL__` + encode(string(m))), nil -} - -func (m *marshalerForTest) UnmarshalText(text []byte) error { - *m = marshalerForTest(decode(strings.TrimPrefix(string(text), "MANUAL__"))) - return nil -} - -var _ encoding.TextMarshaler = *new(marshalerForTest) -var _ encoding.TextUnmarshaler = new(marshalerForTest) - -type typeForTest marshalerForTest diff --git a/output_tests/text_marshal/struct/json_test.go b/output_tests/text_marshal/struct/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/text_marshal/struct/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/text_marshal/struct/types.go b/output_tests/text_marshal/struct/types.go deleted file mode 100644 index 482f41f..0000000 --- a/output_tests/text_marshal/struct/types.go +++ /dev/null @@ -1,52 +0,0 @@ -package test - -import ( - "bytes" - "encoding" - "encoding/base64" - "strings" -) - -type marshalerForTest struct { - X string -} - -func encode(str string) string { - buf := bytes.Buffer{} - b64 := base64.NewEncoder(base64.StdEncoding, &buf) - if _, err := b64.Write([]byte(str)); err != nil { - panic(err) - } - if err := b64.Close(); err != nil { - panic(err) - } - return buf.String() -} - -func decode(str string) string { - if len(str) == 0 { - return "" - } - b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str)) - bs := make([]byte, len(str)) - if n, err := b64.Read(bs); err != nil { - panic(err) - } else { - bs = bs[:n] - } - return string(bs) -} - -func (m marshalerForTest) MarshalText() ([]byte, error) { - return []byte(`MANUAL__` + encode(m.X)), nil -} - -func (m *marshalerForTest) UnmarshalText(text []byte) error { - m.X = decode(strings.TrimPrefix(string(text), "MANUAL__")) - return nil -} - -var _ encoding.TextMarshaler = marshalerForTest{} -var _ encoding.TextUnmarshaler = &marshalerForTest{} - -type typeForTest marshalerForTest diff --git a/output_tests/text_marshal/struct_alias/json_test.go b/output_tests/text_marshal/struct_alias/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/text_marshal/struct_alias/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/text_marshal/struct_alias/types.go b/output_tests/text_marshal/struct_alias/types.go deleted file mode 100644 index 9cf6030..0000000 --- a/output_tests/text_marshal/struct_alias/types.go +++ /dev/null @@ -1,54 +0,0 @@ -package test - -import ( - "bytes" - "encoding" - "encoding/base64" - "strings" -) - -type marshalerForTest struct { - X string -} - -func encode(str string) string { - buf := bytes.Buffer{} - b64 := base64.NewEncoder(base64.StdEncoding, &buf) - if _, err := b64.Write([]byte(str)); err != nil { - panic(err) - } - if err := b64.Close(); err != nil { - panic(err) - } - return buf.String() -} - -func decode(str string) string { - if len(str) == 0 { - return "" - } - b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str)) - bs := make([]byte, len(str)) - if n, err := b64.Read(bs); err != nil { - panic(err) - } else { - bs = bs[:n] - } - return string(bs) -} - -func (m marshalerForTest) MarshalText() ([]byte, error) { - return []byte(`MANUAL__` + encode(m.X)), nil -} - -func (m *marshalerForTest) UnmarshalText(text []byte) error { - m.X = decode(strings.TrimPrefix(string(text), "MANUAL__")) - return nil -} - -var _ encoding.TextMarshaler = marshalerForTest{} -var _ encoding.TextUnmarshaler = &marshalerForTest{} - -type marshalerAlias marshalerForTest - -type typeForTest marshalerAlias diff --git a/output_tests/text_marshal/struct_field/json_test.go b/output_tests/text_marshal/struct_field/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/text_marshal/struct_field/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/text_marshal/struct_field/types.go b/output_tests/text_marshal/struct_field/types.go deleted file mode 100644 index 371aacb..0000000 --- a/output_tests/text_marshal/struct_field/types.go +++ /dev/null @@ -1,56 +0,0 @@ -package test - -import ( - "bytes" - "encoding" - "encoding/base64" - "strings" -) - -type marshalerForTest struct { - X string -} - -func encode(str string) string { - buf := bytes.Buffer{} - b64 := base64.NewEncoder(base64.StdEncoding, &buf) - if _, err := b64.Write([]byte(str)); err != nil { - panic(err) - } - if err := b64.Close(); err != nil { - panic(err) - } - return buf.String() -} - -func decode(str string) string { - if len(str) == 0 { - return "" - } - b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str)) - bs := make([]byte, len(str)) - if n, err := b64.Read(bs); err != nil { - panic(err) - } else { - bs = bs[:n] - } - return string(bs) -} - -func (m marshalerForTest) MarshalText() ([]byte, error) { - return []byte(`MANUAL__` + encode(m.X)), nil -} - -func (m *marshalerForTest) UnmarshalText(text []byte) error { - m.X = decode(strings.TrimPrefix(string(text), "MANUAL__")) - return nil -} - -var _ encoding.TextMarshaler = marshalerForTest{} -var _ encoding.TextUnmarshaler = &marshalerForTest{} - -type typeForTest struct { - S string - M marshalerForTest - I int8 -} diff --git a/output_tests/text_marshal/struct_field_alias/json_test.go b/output_tests/text_marshal/struct_field_alias/json_test.go deleted file mode 100644 index 3c35ffe..0000000 --- a/output_tests/text_marshal/struct_field_alias/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -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 < 100; i++ { - var before typeForTest - 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 typeForTest - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter typeForTest - 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 typeForTest - 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 typeForTest - 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 typeForTest - 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) -} diff --git a/output_tests/text_marshal/struct_field_alias/types.go b/output_tests/text_marshal/struct_field_alias/types.go deleted file mode 100644 index fbfb758..0000000 --- a/output_tests/text_marshal/struct_field_alias/types.go +++ /dev/null @@ -1,58 +0,0 @@ -package test - -import ( - "bytes" - "encoding" - "encoding/base64" - "strings" -) - -type marshalerForTest struct { - X string -} - -func encode(str string) string { - buf := bytes.Buffer{} - b64 := base64.NewEncoder(base64.StdEncoding, &buf) - if _, err := b64.Write([]byte(str)); err != nil { - panic(err) - } - if err := b64.Close(); err != nil { - panic(err) - } - return buf.String() -} - -func decode(str string) string { - if len(str) == 0 { - return "" - } - b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str)) - bs := make([]byte, len(str)) - if n, err := b64.Read(bs); err != nil { - panic(err) - } else { - bs = bs[:n] - } - return string(bs) -} - -func (m marshalerForTest) MarshalText() ([]byte, error) { - return []byte(`MANUAL__` + encode(m.X)), nil -} - -func (m *marshalerForTest) UnmarshalText(text []byte) error { - m.X = decode(strings.TrimPrefix(string(text), "MANUAL__")) - return nil -} - -var _ encoding.TextMarshaler = marshalerForTest{} -var _ encoding.TextUnmarshaler = &marshalerForTest{} - -type marshalerAlias marshalerForTest - -type typeForTest struct { - S string - M marshalerAlias - I int8 -} diff --git a/type_tests/map_key_test.go b/type_tests/map_key_test.go new file mode 100644 index 0000000..6284fb5 --- /dev/null +++ b/type_tests/map_key_test.go @@ -0,0 +1,44 @@ +package test + +import ( + "strings" + "encoding" +) + +func init() { + testCases = append(testCases, + (*map[stringKeyType]string)(nil), + (*map[structKeyType]string)(nil), + ) +} + +type stringKeyType string + +func (k stringKeyType) MarshalText() ([]byte, error) { + return []byte("MANUAL__" + k), nil +} + +func (k *stringKeyType) UnmarshalText(text []byte) error { + *k = stringKeyType(strings.TrimPrefix(string(text), "MANUAL__")) + return nil +} + +var _ encoding.TextMarshaler = stringKeyType("") +var _ encoding.TextUnmarshaler = new(stringKeyType) + + +type structKeyType struct { + X string +} + +func (k structKeyType) MarshalText() ([]byte, error) { + return []byte("MANUAL__" + k.X), nil +} + +func (k *structKeyType) UnmarshalText(text []byte) error { + k.X = strings.TrimPrefix(string(text), "MANUAL__") + return nil +} + +var _ encoding.TextMarshaler = structKeyType{} +var _ encoding.TextUnmarshaler = &structKeyType{} \ No newline at end of file diff --git a/type_tests/struct_field_case_test.go b/type_tests/struct_field_case_test.go new file mode 100644 index 0000000..ad23cbc --- /dev/null +++ b/type_tests/struct_field_case_test.go @@ -0,0 +1,24 @@ +package test + +func init() { + asymmetricTestCases = append(asymmetricTestCases, [][2]interface{}{ + { + (*struct { + Field string + })(nil), + (*struct { + FIELD string + })(nil), + }, + { + (*struct { + F1 string + F2 string + F3 string + })(nil), + (*struct { + F1 string + })(nil), + }, + }...) +} diff --git a/type_tests/text_marshaler_string_test.go b/type_tests/text_marshaler_string_test.go index be68139..747921f 100644 --- a/type_tests/text_marshaler_string_test.go +++ b/type_tests/text_marshaler_string_test.go @@ -7,6 +7,12 @@ import ( "encoding/base64" ) +func init() { + testCases = append(testCases, + (*StringTextMarshaler)(nil), + ) +} + // StringTextMarshaler TEST ONLY type StringTextMarshaler string @@ -48,4 +54,4 @@ func (m *StringTextMarshaler) UnmarshalText(text []byte) error { } var _ encoding.TextMarshaler = *new(StringTextMarshaler) -var _ encoding.TextUnmarshaler = new(StringTextMarshaler) \ No newline at end of file +var _ encoding.TextUnmarshaler = new(StringTextMarshaler) diff --git a/type_tests/text_marshaler_struct_test.go b/type_tests/text_marshaler_struct_test.go new file mode 100644 index 0000000..2d3b65d --- /dev/null +++ b/type_tests/text_marshaler_struct_test.go @@ -0,0 +1,69 @@ +package test + +import ( + "bytes" + "encoding/base64" + "strings" + "encoding" +) + +func init() { + testCases = append(testCases, + (*structTextMarshaler)(nil), + (*structTextMarshalerAlias)(nil), + (*struct { + S string + M structTextMarshaler + I int8 + })(nil), + (*struct { + S string + M structTextMarshalerAlias + I int8 + })(nil), + ) +} + +type structTextMarshaler struct { + X string +} + +func (m structTextMarshaler) encode(str string) string { + buf := bytes.Buffer{} + b64 := base64.NewEncoder(base64.StdEncoding, &buf) + if _, err := b64.Write([]byte(str)); err != nil { + panic(err) + } + if err := b64.Close(); err != nil { + panic(err) + } + return buf.String() +} + +func (m structTextMarshaler) decode(str string) string { + if len(str) == 0 { + return "" + } + b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str)) + bs := make([]byte, len(str)) + if n, err := b64.Read(bs); err != nil { + panic(err) + } else { + bs = bs[:n] + } + return string(bs) +} + +func (m structTextMarshaler) MarshalText() ([]byte, error) { + return []byte(`MANUAL__` + m.encode(m.X)), nil +} + +func (m *structTextMarshaler) UnmarshalText(text []byte) error { + m.X = m.decode(strings.TrimPrefix(string(text), "MANUAL__")) + return nil +} + +var _ encoding.TextMarshaler = structTextMarshaler{} +var _ encoding.TextUnmarshaler = &structTextMarshaler{} + +type structTextMarshalerAlias structTextMarshaler \ No newline at end of file diff --git a/type_tests/type_test.go b/type_tests/type_test.go index c7e36d1..5470b60 100644 --- a/type_tests/type_test.go +++ b/type_tests/type_test.go @@ -13,8 +13,9 @@ import ( ) var testCases []interface{} +var asymmetricTestCases [][2]interface{} -func Test(t *testing.T) { +func Test_symmetric(t *testing.T) { for _, testCase := range testCases { valType := reflect.TypeOf(testCase).Elem() fz := fuzz.New().MaxDepth(10).NilChance(0.3) @@ -67,6 +68,60 @@ func Test(t *testing.T) { } } +func Test_asymmetric(t *testing.T) { + for _, testCase := range asymmetricTestCases { + fromType := reflect.TypeOf(testCase[0]).Elem() + toType := reflect.TypeOf(testCase[1]).Elem() + fz := fuzz.New().MaxDepth(10).NilChance(0.3) + for i := 0; i < 100; i++ { + beforePtrVal := reflect.New(fromType) + beforePtr := beforePtrVal.Interface() + fz.Fuzz(beforePtr) + before := beforePtrVal.Elem().Interface() + + 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)) + } + + afterStdPtrVal := reflect.New(toType) + afterStdPtr := afterStdPtrVal.Interface() + err = json.Unmarshal(jbIter, afterStdPtr) + if err != nil { + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) + } + afterIterPtrVal := reflect.New(toType) + afterIterPtr := afterIterPtrVal.Interface() + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, afterIterPtr) + if err != nil { + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) + } + afterStd := afterStdPtrVal.Elem().Interface() + afterIter := afterIterPtrVal.Elem().Interface() + 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 {