tests: updates to Lua 5.2/5.3 requirements
[luajson.git] / tests / lunit-numbers.lua
blob20637a1a75a1b8c422e045cb30cd61adec44b010
1 local json = require("json")
2 local lunit = require("lunit")
3 local math = require("math")
4 local testutil = require("testutil")
5 local string = require("string")
7 local encode = json.encode
8 -- DECODE NOT 'local' due to requirement for testutil to access it
9 decode = json.decode.getDecoder(false)
11 if not module then
12 _ENV = lunit.module("lunit-numbers", 'seeall')
13 else
14 module("lunit-numbers", lunit.testcase, package.seeall)
15 end
17 function setup()
18 -- Ensure that the decoder is reset
19 _G["decode"] = json.decode.getDecoder(false)
20 end
22 local function assert_near(expect, received)
23 local pctDiff
24 if expect == received then
25 pctDiff = 0
26 else
27 pctDiff = math.abs(1 - expect / received)
28 end
29 local msg = ("expected '%s' but was '%s' .. '%s'%% apart"):format(expect, received, pctDiff * 100)
30 assert(pctDiff < 0.000001, msg)
31 end
32 local function test_simple(num)
33 assert_near(num, decode(tostring(num)))
34 end
35 local function test_simple_w_encode(num)
36 assert_near(num, decode(encode(num)))
37 end
38 local function test_scientific(num)
39 assert_near(num, decode(string.format('%e', num)))
40 assert_near(num, decode(string.format('%E', num)))
41 end
42 local numbers = {
43 0, 1, -1, math.pi, -math.pi
45 math.randomseed(0xDEADBEEF)
46 -- Add sequence of numbers at low/high end of value-set
47 for i = -300,300,60 do
48 numbers[#numbers + 1] = math.random() * math.pow(10, i)
49 numbers[#numbers + 1] = -math.random() * math.pow(10, i)
50 end
52 local function get_number_tester(f)
53 return function ()
54 for _, v in ipairs(numbers) do
55 f(v)
56 end
57 end
58 end
60 test_simple_numbers = get_number_tester(test_simple)
61 test_simple_numbers_w_encode = get_number_tester(test_simple_w_encode)
62 test_simple_numbers_scientific = get_number_tester(test_scientific)
64 function test_infinite_nostrict()
65 assert_equal(math.huge, decode("Infinity"))
66 assert_equal(math.huge, decode("infinity"))
67 assert_equal(-math.huge, decode("-Infinity"))
68 assert_equal(-math.huge, decode("-infinity"))
69 end
71 function test_nan_nostrict()
72 local value = decode("nan")
73 assert_true(value ~= value)
74 local value = decode("NaN")
75 assert_true(value ~= value)
76 end
78 function test_expression()
79 assert_error(function()
80 decode("1 + 2")
81 end)
82 end
84 -- For strict tests, small concession must be made to allow non-array/objects as root
85 local strict = json.util.merge({}, json.decode.strict, {initialObject = false})
86 local strictDecoder = json.decode.getDecoder(strict)
88 local numberValue = {hex = true}
90 local hex = {number = numberValue}
91 local hexDecoder = json.decode.getDecoder(hex)
93 function test_hex()
94 if decode == hexDecoder then -- MUST SKIP FAIL UNTIL BETTER METHOD SETUP
95 return
96 end
97 assert_error(function()
98 decode("0x20")
99 end)
102 local hexNumbers = {
103 0xDEADBEEF,
104 0xCAFEBABE,
105 0x00000000,
106 0xFFFFFFFF,
107 0xCE,
108 0x01
111 function test_hex_only()
112 _G["decode"] = hexDecoder
113 for _, v in ipairs(hexNumbers) do
114 assert_equal(v, decode(("0x%x"):format(v)))
115 assert_equal(v, decode(("0X%X"):format(v)))
116 assert_equal(v, decode(("0x%X"):format(v)))
117 assert_equal(v, decode(("0X%x"):format(v)))
121 local decimal_hexes = {
122 "0x0.1",
123 "0x.1",
124 "0x0e+1",
125 "0x0E-1"
127 function test_no_decimal_hex_only()
128 for _, str in ipairs(decimal_hexes) do
129 assert_error(function()
130 hexDecoder(str)
131 end)
135 function test_nearly_scientific_hex_only()
136 assert_equal(0x00E1, hexDecoder("0x00e1"))
139 local function buildStrictDecoder(f)
140 return testutil.buildPatchedDecoder(f, strictDecoder)
142 local function buildFailedStrictDecoder(f)
143 return testutil.buildFailedPatchedDecoder(f, strictDecoder)
145 -- SETUP CHECKS FOR SEQUENCE OF DECODERS
146 for k, v in pairs(_M) do
147 if k:match("^test_") and not k:match("_gen$") and not k:match("_only$") then
148 if k:match("_nostrict") then
149 _M[k .. "_strict_gen"] = buildFailedStrictDecoder(v)
150 else
151 _M[k .. "_strict_gen"] = buildStrictDecoder(v)
153 _M[k .. "_hex_gen"] = testutil.buildPatchedDecoder(v, hexDecoder)