Debian Codegraph @

internal/encoder/compact_fast_test.go

raw
1 package encoder
2
3 import (
4 "bytes"
5 stdjson "encoding/json"
6 "strconv"
7 "strings"
8 "testing"
9 )
10
11 // appendCompactOutput must accept an output only if compact leaves it as it is: what it accepts is copied.
12 // Whatever it rejects is compacted, so a rejection is never wrong, but the valid and compact outputs must be
13 // accepted for the speed.
14 func TestAppendCompactOutput(t *testing.T) {
15 accepted := []string{
16 `"hello"`, `""`, `"hello world"`, `0`, `-0`, `123`, `-12.5e+3`, `1E-2`, `true`, `false`, `null`,
17 `{}`, `[]`, `{"a":1}`, `{"a":{"b":[1,2,{"c":null}]},"d":"e"}`, `[[[[]]]]`, `[1,"2",true,null,{"x":[]}]`,
18 `{"a b":"c d"}`, strings.Repeat("[", 64) + strings.Repeat("]", 64),
19 // escapes, and characters which are not ASCII, which compact leaves as they are.
20 `"a\"b"`, `"a\\b"`, `"a\nb"`, `"\"\\\/\b\f\n\r\t\u00e9\uD83D\uDE00\uABCD"`, `{"\"k\"":"\\"}`, `["\\","\""]`,
21 `"日本語"`, `"a — b"`, `{"é":"😀"}`, "\"\xff\xfe\"", `"\u2028"`,
22 // several quotes in the eight bytes read at once, of which the first one in memory ends the string.
23 `["x","y","z"]`, `["ab","cd","e"]`, `["a\\bcdefg","ijklmnop","q"]`, `{"k":"ab","l":"cdefghij"}`,
24 }
25 rejected := []string{
26 ``, ` `, `{ }`, `{"a": 1}`, `[1, 2]`, "[1,\n2]", `{"a":1} `, ` 1`, "\"a\tb\"",
27 `{`, `[`, `}`, `]`, `{"a"}`, `{"a":}`, `{"a":1,}`, `[1,]`,
28 `[1 2]`, `{"a":1 "b":2}`, `tru`, `nul`, `truex`, `01`, `1.`, `.5`, `1e`, `1e+`, `-`, `--1`, `+1`, `"abc`,
29 `{"a":1}}`, `[1]]`, `{a:1}`, `{1:2}`, `[1]x`, "\x00", "\"\x01\"", strings.Repeat("[", 65) + strings.Repeat("]", 65),
30 `{"a":1}{"b":2}`, `1 2`, `"a" "b"`,
31 // several quotes in the eight bytes read at once: a string ended at a later one would make these valid.
32 `""}a,:,{{`, `"\\]"[b]a}"`, `["ab" "cd"]`, `["ab" "c\\"]`, `{"k":"ab" "cd"}`,
33 // escapes which are not ones of JSON, or which end the string.
34 `"\"`, `"\`, `"\\"x`, `"\u00"`, `"\u00g0"`, `"\x41"`, `"\U0041"`, `"\'"`, `"\u12`, `["\"]`, `{"\":1}`,
35 // commas and colons which are not in their places.
36 `[,]`, `[1,,2]`, `{"a":1,,"b":2}`, `{,}`, `{"a"::1}`, `{"a":1:2}`, `[1:2]`, `,`, `:`, `{"a",1}`, `["a":1]`,
37 // brackets which don't match.
38 `[}`, `{]`, `[{]}`, `{"a":[}`,
39 // values which are not values.
40 `[undefined]`, `[NaN]`, `[Infinity]`, `[.1]`, `[1.]`, `[1e5.0]`, `[0x1]`, `[1_000]`, `['a']`, `[True]`,
41 }
42 for _, escape := range []bool{false, true} {
43 for _, s := range accepted {
44 // what compact does must be the same.
45 compacted, err := compact([]byte("x"), append([]byte(s), nul), escape)
46 if err != nil {
47 t.Fatalf("escape=%v: compact of %q: %v", escape, s, err)
48 }
49 got, ok := appendCompactOutput([]byte("x"), []byte(s), escape, false)
50 if !ok || string(got) != string(compacted) {
51 t.Fatalf("escape=%v: %q must be accepted as %q, got %q, %v", escape, s, compacted, got, ok)
52 }
53 }
54 for _, s := range rejected {
55 if got, ok := appendCompactOutput([]byte("x"), []byte(s), escape, false); ok {
56 t.Fatalf("escape=%v: %q must be rejected, got %q", escape, s, got)
57 }
58 }
59 }
60 // the characters which compact escapes for HTML are escaped as it escapes them, anywhere in a long output too.
61 for _, s := range []string{`"<html>"`, `"a&b"`, "\"a\u2028b\"", "{\"\u2029\":1}", "\"\xe2\x80\xa8\"", "\"\xe2\x80\xa7\xe2\"",
62 `["` + strings.Repeat("a<b>c&d — “e” \u2028 \\n", 20) + `"]`} {
63 for _, escape := range []bool{false, true} {
64 want, err := compact(nil, append([]byte(s), nul), escape)
65 if err != nil {
66 t.Fatal(err)
67 }
68 got, ok := appendCompactOutput(nil, []byte(s), escape, false)
69 if !ok || string(got) != string(want) {
70 t.Fatalf("escape=%v: %q must be accepted as %q, got %q, %v", escape, s, want, got, ok)
71 }
72 }
73 }
74 }
75
76 // Every byte in every position of a valid document must give the same answer as compact: accepted only if the
77 // document is compact, and then appended as compact appends it.
78 func TestAppendCompactOutputAgainstCompact(t *testing.T) {
79 docs := []string{
80 `{"ab":[1,-2.5e3,"cd",true,null,{}],"e":{"f":[[]]}}`, `"abc def"`, `12345`, `[true,false]`, `-0.5E+10`,
81 `{"":""}`, `[[[{"a":[]}]]]`, `{"a":1,"b":2}`, `[1,2,3]`, `null`,
82 }
83 for _, doc := range docs {
84 for pos := 0; pos < len(doc); pos++ {
85 for c := 0; c < 256; c++ {
86 b := []byte(doc)
87 b[pos] = byte(c)
88 for _, escape := range []bool{false, true} {
89 compacted, err := compact(nil, append(append([]byte(nil), b...), nul), escape)
90 unescaped, _ := compact(nil, append(append([]byte(nil), b...), nul), false)
91 same := err == nil && bytes.Equal(unescaped, b)
92 out, ok := appendCompactOutput(nil, b, escape, false)
93 if ok && (!same || !bytes.Equal(out, compacted)) {
94 t.Fatalf("escape=%v: %q is accepted as %q but compact gives %q, %v", escape, b, out, compacted, err)
95 }
96 // what is accepted must be valid by encoding/json: the check must be as strict as it.
97 if ok && !stdjson.Valid(b) {
98 t.Fatalf("escape=%v: %q is accepted but not valid", escape, b)
99 }
100 // compact accepts a number which is not one of JSON, such as 02: the check doesn't.
101 if !ok && same && stdjson.Valid(b) && c >= 0x20 {
102 t.Fatalf("escape=%v: %q is compact but rejected", escape, b)
103 }
104 }
105 }
106 }
107 }
108 }
109
110 func BenchmarkCompactOutput(b *testing.B) {
111 obj := `{"id":123456,"name":"alice","tags":["a","b","c"],"score":12.5,"active":true,"nested":{"x":1,"y":null}}`
112 for _, s := range []string{`"hello"`, obj, "[" + strings.Repeat(obj+",", 20) + obj + "]"} {
113 src := []byte(s)
114 dst := make([]byte, 0, len(s)+16)
115 b.Run("check/"+strconv.Itoa(len(s)), func(b *testing.B) {
116 b.SetBytes(int64(len(s)))
117 for i := 0; i < b.N; i++ {
118 if _, ok := appendCompactOutput(dst[:0], src, true, false); !ok {
119 b.Fatal("rejected")
120 }
121 }
122 })
123 withNul := append([]byte(s), nul)
124 b.Run("compact/"+strconv.Itoa(len(s)), func(b *testing.B) {
125 b.SetBytes(int64(len(s)))
126 for i := 0; i < b.N; i++ {
127 if _, err := compact(dst[:0], withNul, true); err != nil {
128 b.Fatal(err)
129 }
130 }
131 })
132 }
133 }
134
135 // FuzzAppendCompactOutput checks that whatever is accepted is valid by encoding/json and is appended as compact
136 // appends it, so that the check is as strict as both.
137 func FuzzAppendCompactOutput(f *testing.F) {
138 for _, s := range []string{`{"a":[1,"b",true,null,{"c":-1.5e3}]}`, `"x"`, `[]`, `0`, `{"k":"v","n":[1,2]}`, ` `, `[1,]`} {
139 f.Add([]byte(s), true)
140 f.Add([]byte(s), false)
141 }
142 f.Fuzz(func(t *testing.T, src []byte, escape bool) {
143 out, ok := appendCompactOutput([]byte("x"), src, escape, false)
144 if !ok {
145 return
146 }
147 if !stdjson.Valid(src) {
148 t.Fatalf("escape=%v: %q is accepted but not valid", escape, src)
149 }
150 compacted, err := compact([]byte("x"), append(append([]byte(nil), src...), nul), escape)
151 if err != nil || !bytes.Equal(compacted, out) {
152 t.Fatalf("escape=%v: %q is accepted as %q but compact gives %q, %v", escape, src, out, compacted, err)
153 }
154 // what encoding/json compacts must not be accepted either.
155 var buf bytes.Buffer
156 if stdjson.Compact(&buf, src) == nil && !bytes.Equal(buf.Bytes(), src) {
157 t.Fatalf("escape=%v: %q is accepted but encoding/json compacts it to %q", escape, src, buf.Bytes())
158 }
159 })
160 }