github.com/intel-go/fastjson@v0.0.0-20170329170629-f846ae58a1ab/scanner_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fastjson 6 7 import ( 8 "bytes" 9 "math" 10 "math/rand" 11 "reflect" 12 "testing" 13 ) 14 15 // Tests of simple examples. 16 17 type example struct { 18 compact string 19 indent string 20 } 21 22 var examples = []example{ 23 {`1`, `1`}, 24 {`{}`, `{}`}, 25 {`[]`, `[]`}, 26 {`{"":2}`, "{\n\t\"\": 2\n}"}, 27 {`[3]`, "[\n\t3\n]"}, 28 {`[1,2,3]`, "[\n\t1,\n\t2,\n\t3\n]"}, 29 {`{"x":1}`, "{\n\t\"x\": 1\n}"}, 30 {ex1, ex1i}, 31 } 32 33 var ex1 = `[true,false,null,"x",1,1.5,0,-5e+2]` 34 35 var ex1i = `[ 36 true, 37 false, 38 null, 39 "x", 40 1, 41 1.5, 42 0, 43 -5e+2 44 ]` 45 46 func TestCompact(t *testing.T) { 47 var buf bytes.Buffer 48 for _, tt := range examples { 49 buf.Reset() 50 if err := Compact(&buf, []byte(tt.compact)); err != nil { 51 t.Errorf("Compact(%#q): %v", tt.compact, err) 52 } else if s := buf.String(); s != tt.compact { 53 t.Errorf("Compact(%#q) = %#q, want original", tt.compact, s) 54 } 55 56 buf.Reset() 57 if err := Compact(&buf, []byte(tt.indent)); err != nil { 58 t.Errorf("Compact(%#q): %v", tt.indent, err) 59 continue 60 } else if s := buf.String(); s != tt.compact { 61 t.Errorf("Compact(%#q) = %#q, want %#q", tt.indent, s, tt.compact) 62 } 63 } 64 } 65 66 func TestCompactSeparators(t *testing.T) { 67 // U+2028 and U+2029 should be escaped inside strings. 68 // They should not appear outside strings. 69 tests := []struct { 70 in, compact string 71 }{ 72 {"{\"\u2028\": 1}", `{"\u2028":1}`}, 73 {"{\"\u2029\" :2}", `{"\u2029":2}`}, 74 } 75 for _, tt := range tests { 76 var buf bytes.Buffer 77 if err := Compact(&buf, []byte(tt.in)); err != nil { 78 t.Errorf("Compact(%q): %v", tt.in, err) 79 } else if s := buf.String(); s != tt.compact { 80 t.Errorf("Compact(%q) = %q, want %q", tt.in, s, tt.compact) 81 } 82 } 83 } 84 85 func TestIndent(t *testing.T) { 86 var buf bytes.Buffer 87 for _, tt := range examples { 88 buf.Reset() 89 if err := Indent(&buf, []byte(tt.indent), "", "\t"); err != nil { 90 t.Errorf("Indent(%#q): %v", tt.indent, err) 91 } else if s := buf.String(); s != tt.indent { 92 t.Errorf("Indent(%#q) = %#q, want original", tt.indent, s) 93 } 94 95 buf.Reset() 96 if err := Indent(&buf, []byte(tt.compact), "", "\t"); err != nil { 97 t.Errorf("Indent(%#q): %v", tt.compact, err) 98 continue 99 } else if s := buf.String(); s != tt.indent { 100 t.Errorf("Indent(%#q) = %#q, want %#q", tt.compact, s, tt.indent) 101 } 102 } 103 } 104 105 // Tests of a large random structure. 106 107 func TestCompactBig(t *testing.T) { 108 initBig() 109 var buf bytes.Buffer 110 if err := Compact(&buf, jsonBig); err != nil { 111 t.Fatalf("Compact: %v", err) 112 } 113 b := buf.Bytes() 114 if !bytes.Equal(b, jsonBig) { 115 t.Error("Compact(jsonBig) != jsonBig") 116 diff(t, b, jsonBig) 117 return 118 } 119 } 120 121 func TestIndentBig(t *testing.T) { 122 initBig() 123 var buf bytes.Buffer 124 if err := Indent(&buf, jsonBig, "", "\t"); err != nil { 125 t.Fatalf("Indent1: %v", err) 126 } 127 b := buf.Bytes() 128 if len(b) == len(jsonBig) { 129 // jsonBig is compact (no unnecessary spaces); 130 // indenting should make it bigger 131 t.Fatalf("Indent(jsonBig) did not get bigger") 132 } 133 134 // should be idempotent 135 var buf1 bytes.Buffer 136 if err := Indent(&buf1, b, "", "\t"); err != nil { 137 t.Fatalf("Indent2: %v", err) 138 } 139 b1 := buf1.Bytes() 140 if !bytes.Equal(b1, b) { 141 t.Error("Indent(Indent(jsonBig)) != Indent(jsonBig)") 142 diff(t, b1, b) 143 return 144 } 145 146 // should get back to original 147 buf1.Reset() 148 if err := Compact(&buf1, b); err != nil { 149 t.Fatalf("Compact: %v", err) 150 } 151 b1 = buf1.Bytes() 152 if !bytes.Equal(b1, jsonBig) { 153 t.Error("Compact(Indent(jsonBig)) != jsonBig") 154 diff(t, b1, jsonBig) 155 return 156 } 157 } 158 159 type indentErrorTest struct { 160 in string 161 err error 162 } 163 164 var indentErrorTests = []indentErrorTest{ 165 {`{"X": "foo", "Y"}`, &SyntaxError{"invalid character '}' after object key", 17}}, 166 {`{"X": "foo" "Y": "bar"}`, &SyntaxError{"invalid character '\"' after object key:value pair", 13}}, 167 } 168 169 func TestIndentErrors(t *testing.T) { 170 for i, tt := range indentErrorTests { 171 slice := make([]uint8, 0) 172 buf := bytes.NewBuffer(slice) 173 if err := Indent(buf, []uint8(tt.in), "", ""); err != nil { 174 if !reflect.DeepEqual(err, tt.err) { 175 t.Errorf("#%d: Indent: %#v", i, err) 176 continue 177 } 178 } 179 } 180 } 181 182 183 func diff(t *testing.T, a, b []byte) { 184 for i := 0; ; i++ { 185 if i >= len(a) || i >= len(b) || a[i] != b[i] { 186 j := i - 10 187 if j < 0 { 188 j = 0 189 } 190 t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:])) 191 return 192 } 193 } 194 } 195 196 func trim(b []byte) []byte { 197 if len(b) > 20 { 198 return b[0:20] 199 } 200 return b 201 } 202 203 // Generate a random JSON object. 204 205 var jsonBig []byte 206 207 func initBig() { 208 n := 10000 209 if testing.Short() { 210 n = 100 211 } 212 b, err := Marshal(genValue(n)) 213 if err != nil { 214 panic(err) 215 } 216 jsonBig = b 217 } 218 219 func genValue(n int) interface{} { 220 if n > 1 { 221 switch rand.Intn(2) { 222 case 0: 223 return genArray(n) 224 case 1: 225 return genMap(n) 226 } 227 } 228 switch rand.Intn(3) { 229 case 0: 230 return rand.Intn(2) == 0 231 case 1: 232 return rand.NormFloat64() 233 case 2: 234 return genString(30) 235 } 236 panic("unreachable") 237 } 238 239 func genString(stddev float64) string { 240 n := int(math.Abs(rand.NormFloat64()*stddev + stddev/2)) 241 c := make([]rune, n) 242 for i := range c { 243 f := math.Abs(rand.NormFloat64()*64 + 32) 244 if f > 0x10ffff { 245 f = 0x10ffff 246 } 247 c[i] = rune(f) 248 } 249 return string(c) 250 } 251 252 func genArray(n int) []interface{} { 253 f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2))) 254 if f > n { 255 f = n 256 } 257 if f < 1 { 258 f = 1 259 } 260 x := make([]interface{}, f) 261 for i := range x { 262 x[i] = genValue(((i+1)*n)/f - (i*n)/f) 263 } 264 return x 265 } 266 267 func genMap(n int) map[string]interface{} { 268 f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2))) 269 if f > n { 270 f = n 271 } 272 if n > 0 && f == 0 { 273 f = 1 274 } 275 x := make(map[string]interface{}) 276 for i := 0; i < f; i++ { 277 x[genString(10)] = genValue(((i+1)*n)/f - (i*n)/f) 278 } 279 return x 280 }