aboutsummaryrefslogtreecommitdiff
path: root/tests/test.lua
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-03 23:04:22 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-03 23:04:22 +0930
commit4146070e69475d5eedcf7a9e3d990e511f46ec7a (patch)
tree5e6bfa66ee9a1fc4ac4125f349c55a3d7c551e35 /tests/test.lua
parentce3a769dbdd13571b5b761d933e17e8fe5771739 (diff)
downloadlua-cjson-4146070e69475d5eedcf7a9e3d990e511f46ec7a.tar.gz
lua-cjson-4146070e69475d5eedcf7a9e3d990e511f46ec7a.tar.bz2
lua-cjson-4146070e69475d5eedcf7a9e3d990e511f46ec7a.zip
Add basic JSON tests and benchmark
Diffstat (limited to 'tests/test.lua')
-rwxr-xr-xtests/test.lua132
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/test.lua b/tests/test.lua
new file mode 100755
index 0000000..9424e8f
--- /dev/null
+++ b/tests/test.lua
@@ -0,0 +1,132 @@
1#!/usr/bin/env lua
2
3require "common"
4local json = require "cjson"
5
6local cjson_test_non_default = true
7
8function run_tests(tests, func)
9 for k, v in ipairs(tests) do
10 local success, msg = pcall(func, v)
11 if not success then
12 print("Error: " .. msg)
13 end
14 print()
15 end
16end
17
18local simple_value_tests = {
19 [[ "test string" ]],
20 [[ -5e3 ]],
21 [[ null ]],
22 [[ true ]],
23 [[ false ]],
24 [[ { "1": "one", "3": "three" } ]]
25}
26
27local numeric_tests = {
28 "[ 0.0, -1, 0.3e-3, 1023.2 ]",
29 "[ 00123 ]",
30 "[ 05.2 ]",
31 "[ 0e10 ]",
32 "[ 0x6 ]",
33 "[ +Inf ]",
34 "[ Inf ]",
35 "[ -Inf ]",
36 "[ +Infinity ]",
37 "[ Infinity ]",
38 "[ -Infinity ]",
39 "[ +NaN ]",
40 "[ NaN ]",
41 "[ -NaN ]",
42 "[ Infrared ]",
43 "[ Noodle ]"
44}
45
46local object_tests = {
47 { [5] = "sparse test" },
48 { [6] = "sparse test" },
49 {{{{{{{{{{{{{{{{{{{{{ "nested" }}}}}}}}}}}}}}}}}}}}}
50}
51
52local decode_error_tests = {
53 '{ "unexpected eof": ',
54 '{ "extra data": true }, false',
55 [[ { "bad escape \q code" } ]],
56 [[ { "bad unicode \u0f6 escape" } ]],
57 [[ [ "bad barewood", test ] ]],
58 "[ -+12 ]",
59 "-v",
60 "[ 0.4eg10 ]",
61}
62
63local simple_encode_tests = {
64 json.null, true, false, { }, 10, "hello"
65}
66
67
68local function gen_ascii(max)
69 local chars = {}
70 for i = 0, max do
71 chars[i] = string.char(i)
72 end
73 return table.concat(chars)
74end
75
76local function decode_encode(text)
77 print("==JSON=> " .. text)
78 local obj_data = json.decode(text)
79 dump_value(obj_data)
80 local obj_json = json.encode(obj_data)
81 print(obj_json)
82end
83local function encode_decode(obj)
84 print("==OBJ==> ")
85 dump_value(obj)
86 local obj_json = json.encode(obj)
87 print(obj_json)
88 local obj_data = json.decode(obj_json)
89 dump_value(obj_data)
90end
91
92run_tests(simple_value_tests, decode_encode)
93run_tests(decode_error_tests, decode_encode)
94
95run_tests(numeric_tests, decode_encode)
96
97if cjson_test_non_default then
98 print("=== Disabling strict numbers ===")
99 json.strict_numbers(false)
100 run_tests(numeric_tests, decode_encode)
101 json.strict_numbers(true)
102end
103
104run_tests(object_tests, encode_decode)
105print ("Encode tests..")
106
107run_tests(simple_encode_tests, encode_decode)
108
109if cjson_test_non_default then
110 print("=== Setting max_depth to 21, sparse_ratio to 5 ===")
111 json.max_depth(21)
112 json.sparse_ratio(5)
113 run_tests(object_tests, encode_decode)
114end
115
116local ascii1 = gen_ascii(255)
117print("Unprintable escapes:")
118print(json.encode(ascii1))
119print("===")
120local ascii2 = json.decode(json.encode(ascii))
121
122print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2))
123
124for i = 1, #arg do
125 print(arg[i] .. " enc->dec..")
126 local obj1 = json.decode(file_load(arg[i]))
127 local obj2 = json.decode(json.encode(obj1))
128 -- obj_compare(obj_json1, obj_json2)
129 print(".. unimplemented")
130end
131
132-- vi:ai et sw=4 ts=4: