Debian Codegraph @

json/parse_test.go

raw
1 package json
2
3 import (
4 "bytes"
5 "strings"
6 "testing"
7 )
8
9 func TestParseString(t *testing.T) {
10 tests := []struct {
11 in string
12 out string
13 ext string
14 err string
15 }{
16 {`""`, `""`, ``, ``},
17 {`"1234567890"`, `"1234567890"`, ``, ``},
18 {`"Hello World!"`, `"Hello World!"`, ``, ``},
19 {`"Hello\"World!"`, `"Hello\"World!"`, ``, ``},
20 {`"\\"`, `"\\"`, ``, ``},
21 {`"\u0061\u0062\u0063"`, `"\u0061\u0062\u0063"`, ``, ``},
22 {`"\u0"`, ``, ``, `json: unicode code point must have at least 4 characters: 0"`},
23 }
24
25 d := decoder{}
26 for _, test := range tests {
27 t.Run(test.in, func(t *testing.T) {
28 out, ext, _, err := d.parseString([]byte(test.in))
29
30 if test.err == "" {
31 if err != nil {
32 t.Errorf("%s => %s", test.in, err)
33 return
34 }
35 } else {
36 if s := err.Error(); s != test.err {
37 t.Error("invalid error")
38 t.Logf("expected: %s", test.err)
39 t.Logf("found: %s", s)
40 }
41 }
42
43 if s := string(out); s != test.out {
44 t.Error("invalid output")
45 t.Logf("expected: %s", test.out)
46 t.Logf("found: %s", s)
47 }
48
49 if s := string(ext); s != test.ext {
50 t.Error("invalid extra bytes")
51 t.Logf("expected: %s", test.ext)
52 t.Logf("found: %s", s)
53 }
54 })
55 }
56 }
57
58 func TestParseStringUnquote(t *testing.T) {
59 tests := []struct {
60 in string
61 out string
62 ext string
63 }{
64 {`""`, ``, ``},
65 {`"1234567890"`, `1234567890`, ``},
66 {`"Hello World!"`, `Hello World!`, ``},
67 {`"Hello\"World!"`, `Hello"World!`, ``},
68 {`"\\"`, `\`, ``},
69 {`"\u0061\u0062\u0063"`, `abc`, ``},
70 }
71
72 d := decoder{}
73 for _, test := range tests {
74 t.Run(test.in, func(t *testing.T) {
75 out, ext, _, err := d.parseStringUnquote([]byte(test.in), nil)
76 if err != nil {
77 t.Errorf("%s => %s", test.in, err)
78 return
79 }
80
81 if s := string(out); s != test.out {
82 t.Error("invalid output")
83 t.Logf("expected: %s", test.out)
84 t.Logf("found: %s", s)
85 }
86
87 if s := string(ext); s != test.ext {
88 t.Error("invalid extra bytes")
89 t.Logf("expected: %s", test.ext)
90 t.Logf("found: %s", s)
91 }
92 })
93 }
94 }
95
96 func TestAppendToLower(t *testing.T) {
97 tests := []string{
98 "",
99 "A",
100 "a",
101 "__segment_internal",
102 "someFieldWithALongBName",
103 "Hello World!",
104 "Hello\"World!",
105 "Hello\\World!",
106 "Hello\nWorld!",
107 "Hello\rWorld!",
108 "Hello\tWorld!",
109 "Hello\bWorld!",
110 "Hello\fWorld!",
111 "你好",
112 "<",
113 ">",
114 "&",
115 "\u001944",
116 "\u00c2e>",
117 "\u00c2V?",
118 "\u000e=8",
119 "\u001944\u00c2e>\u00c2V?\u000e=8",
120 "ir\u001bQJ\u007f\u0007y\u0015)",
121 }
122
123 for _, test := range tests {
124 s1 := strings.ToLower(test)
125 s2 := string(appendToLower(nil, []byte(test)))
126
127 if s1 != s2 {
128 t.Error("lowercase values mismatch")
129 t.Log("expected:", s1)
130 t.Log("found: ", s2)
131 }
132 }
133 }
134
135 func BenchmarkParseString(b *testing.B) {
136 s := []byte(`"__segment_internal"`)
137
138 d := decoder{}
139 for range b.N {
140 d.parseString(s)
141 }
142 }
143
144 func BenchmarkToLower(b *testing.B) {
145 s := []byte("someFieldWithALongName")
146
147 for range b.N {
148 bytes.ToLower(s)
149 }
150 }
151
152 func BenchmarkAppendToLower(b *testing.B) {
153 a := []byte(nil)
154 s := []byte("someFieldWithALongName")
155
156 for range b.N {
157 a = appendToLower(a[:0], s)
158 }
159 }
160
161 var (
162 benchmarkHasPrefixString = []byte("some random string")
163 benchmarkHasPrefixResult = false
164 )
165
166 func BenchmarkHasPrefix(b *testing.B) {
167 for range b.N {
168 benchmarkHasPrefixResult = hasPrefix(benchmarkHasPrefixString, "null")
169 }
170 }
171
172 func BenchmarkHasNullPrefix(b *testing.B) {
173 for range b.N {
174 benchmarkHasPrefixResult = hasNullPrefix(benchmarkHasPrefixString)
175 }
176 }
177
178 func BenchmarkHasTruePrefix(b *testing.B) {
179 for range b.N {
180 benchmarkHasPrefixResult = hasTruePrefix(benchmarkHasPrefixString)
181 }
182 }
183
184 func BenchmarkHasFalsePrefix(b *testing.B) {
185 for range b.N {
186 benchmarkHasPrefixResult = hasFalsePrefix(benchmarkHasPrefixString)
187 }
188 }
189
190 func BenchmarkParseStringEscapeNone(b *testing.B) {
191 j := []byte(`"` + strings.Repeat(`a`, 1000) + `"`)
192 var s string
193 b.SetBytes(int64(len(j)))
194
195 for range b.N {
196 if err := Unmarshal(j, &s); err != nil {
197 b.Fatal(err)
198 }
199 s = ""
200 }
201 }
202
203 func BenchmarkParseStringEscapeOne(b *testing.B) {
204 j := []byte(`"` + strings.Repeat(`a`, 998) + `\n"`)
205 var s string
206 b.SetBytes(int64(len(j)))
207
208 for range b.N {
209 if err := Unmarshal(j, &s); err != nil {
210 b.Fatal(err)
211 }
212 s = ""
213 }
214 }
215
216 func BenchmarkParseStringEscapeAll(b *testing.B) {
217 j := []byte(`"` + strings.Repeat(`\`, 1000) + `"`)
218 var s string
219 b.SetBytes(int64(len(j)))
220
221 for range b.N {
222 if err := Unmarshal(j, &s); err != nil {
223 b.Fatal(err)
224 }
225 s = ""
226 }
227 }