You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
5.7 KiB

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