From f59b3b671984d6a3bf7f7dd08512a89dd916a901 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Sun, 8 May 2011 21:12:32 +0930 Subject: Add NaN/Inf encoding tests, rearrange test order --- tests/test.lua | 145 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 74 insertions(+), 71 deletions(-) diff --git a/tests/test.lua b/tests/test.lua index ab2fe7c..436b20c 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -9,7 +9,62 @@ require "common" local json = require "cjson" -local simple_value_tests = { +local function gen_ascii() + local chars = {} + for i = 0, 255 do chars[i + 1] = string.char(i) end + return table.concat(chars) +end + +-- Generate every UTF-16 codepoint, including supplementary codes +local function gen_utf16_escaped() + -- Create raw table escapes + local utf16_escaped = {} + local count = 0 + + local function append_escape(code) + local esc = string.format('\\u%04X', code) + table.insert(utf16_escaped, esc) + end + + table.insert(utf16_escaped, '"') + for i = 0, 0xD7FF do + append_escape(i) + end + -- Skip 0xD800 - 0xDFFF since they are used to encode supplementary + -- codepoints + for i = 0xE000, 0xFFFF do + append_escape(i) + end + -- Append surrogate pair for each supplementary codepoint + for high = 0xD800, 0xDBFF do + for low = 0xDC00, 0xDFFF do + append_escape(high) + append_escape(low) + end + end + table.insert(utf16_escaped, '"') + + return table.concat(utf16_escaped) +end + +function test_decode_cycle(filename) + local obj1 = json.decode(file_load(filename)) + local obj2 = json.decode(json.encode(obj1)) + return compare_values(obj1, obj2) +end + +local Inf = math.huge; +local NaN = math.huge * 0; +local octets_raw = gen_ascii() +local octets_escaped = file_load("octets-escaped.dat") +local utf8_loaded, utf8_raw = pcall(file_load, "utf8.dat") +if not utf8_loaded then + utf8_raw = "Failed to load utf8.dat" +end +local utf16_escaped = gen_utf16_escaped() +local nested5 = {{{{{ "nested" }}}}} + +local decode_simple_tests = { { json.decode, { '"test string"' }, true, { "test string" } }, { json.decode, { '-5e3' }, true, { -5000 } }, { json.decode, { 'null' }, true, { json.null } }, @@ -21,10 +76,21 @@ local simple_value_tests = { true, { { "one", json.null, "three" } } } } -local Inf = math.huge; -local NaN = math.huge * 0; +local encode_simple_tests = { + { json.encode, { json.null }, true, { 'null' } }, + { json.encode, { true }, true, { 'true' } }, + { json.encode, { false }, true, { 'false' } }, + { json.encode, { { } }, true, { '{ }' } }, + { json.encode, { 10 }, true, { '10' } }, + { json.encode, { NaN }, + false, { "Cannot serialise number: must not be NaN or Inf" } }, + { json.encode, { Inf }, + false, { "Cannot serialise number: must not be NaN or Inf" } }, + { json.encode, { "hello" }, true, { '"hello"' } }, +} + -local numeric_tests = { +local decode_numeric_tests = { { json.decode, { '[ 0.0, -1, 0.3e-3, 1023.2 ]' }, true, { { 0.0, -1, 0.0003, 1023.2 } } }, { json.decode, { '00123' }, true, { 123 } }, @@ -41,13 +107,11 @@ local numeric_tests = { false, { "Expected value but found invalid token at character 1" } }, } -local nested5 = {{{{{ "nested" }}}}} - local encode_table_tests = { function() cjson.encode_sparse_array(true, 2, 3) cjson.encode_max_depth(5) - return "Setting sparse array / max depth" + return "Setting sparse array (true, 2, 3) / max depth (5)" end, { json.encode, { { [3] = "sparse test" } }, true, { '[ null, null, "sparse test" ]' } }, @@ -86,61 +150,6 @@ local decode_error_tests = { false, { "Expected comma or array end but found invalid token at character 6" } }, } -local encode_simple_tests = { - { json.encode, { json.null }, true, { 'null' } }, - { json.encode, { true }, true, { 'true' } }, - { json.encode, { false }, true, { 'false' } }, - { json.encode, { { } }, true, { '{ }' } }, - { json.encode, { 10 }, true, { '10' } }, - { json.encode, { "hello" }, true, { '"hello"' } }, -} - -local function gen_ascii() - local chars = {} - for i = 0, 255 do chars[i + 1] = string.char(i) end - return table.concat(chars) -end - --- Generate every UTF-16 codepoint, including supplementary codes -local function gen_utf16_escaped() - -- Create raw table escapes - local utf16_escaped = {} - local count = 0 - - local function append_escape(code) - local esc = string.format('\\u%04X', code) - table.insert(utf16_escaped, esc) - end - - table.insert(utf16_escaped, '"') - for i = 0, 0xD7FF do - append_escape(i) - end - -- Skip 0xD800 - 0xDFFF since they are used to encode supplementary - -- codepoints - for i = 0xE000, 0xFFFF do - append_escape(i) - end - -- Append surrogate pair for each supplementary codepoint - for high = 0xD800, 0xDBFF do - for low = 0xDC00, 0xDFFF do - append_escape(high) - append_escape(low) - end - end - table.insert(utf16_escaped, '"') - - return table.concat(utf16_escaped) -end - -local octets_raw = gen_ascii() -local octets_escaped = file_load("octets-escaped.dat") -local utf8_loaded, utf8_raw = pcall(file_load, "utf8.dat") -if not utf8_loaded then - utf8_raw = "Failed to load utf8.dat" -end -local utf16_escaped = gen_utf16_escaped() - local escape_tests = { -- Test 8bit clean { json.encode, { octets_raw }, true, { octets_escaped } }, @@ -162,14 +171,9 @@ local escape_tests = { { json.decode, { utf16_escaped }, true, { utf8_raw } } } -function test_decode_cycle(filename) - local obj1 = json.decode(file_load(filename)) - local obj2 = json.decode(json.encode(obj1)) - return compare_values(obj1, obj2) -end - -run_test_group("decode simple value", simple_value_tests) -run_test_group("decode numeric", numeric_tests) +run_test_group("decode simple value", decode_simple_tests) +run_test_group("encode simple value", encode_simple_tests) +run_test_group("decode numeric", decode_numeric_tests) -- INCLUDE: -- - Sparse array exception.. @@ -179,7 +183,6 @@ run_test_group("decode numeric", numeric_tests) run_test_group("encode table", encode_table_tests) run_test_group("decode error", decode_error_tests) -run_test_group("encode simple value", encode_simple_tests) run_test_group("escape", escape_tests) cjson.encode_max_depth(20) -- cgit v1.2.3-55-g6feb