// The below is a test set for the Lost Souls (http://lostsouls.org/) unit // testing rig. It won't be usable directly outside of that system, but is // provided as an example in principle of testing this module. #include <test.h> inherit "/mod/algorithm/json"; inherit "/std/test"; void test_simple_mapping() { mapping value = ([ "test" : 65536, ]); string expect = "{\"test\":65536}"; string fwd = json_encode(value); AssertEquals(expect, fwd); mixed rev = json_decode(fwd); AssertEquivalent(value, rev); } void test_simple_array() { mixed array value = ({ 100, "test", "another", -85 }); string expect = "[100,\"test\",\"another\",-85]"; string fwd = json_encode(value); AssertEquals(expect, fwd); mixed rev = json_decode(fwd); AssertEquivalent(value, rev); } void test_complex_structure() { mapping value = ([ "test" : 65536, "more" : ([ "data" : ({ 1, 27, -0.7, ({ 8, "embedded" })}), "also" : ({ "item with\nnewlines\nand\ttabs\r\nand a CRLF in it", ([ "thing" : "other thing", ]), }), ]), ]); string fwd = json_encode(value); mixed rev = json_decode(fwd); AssertEquivalent(value, rev); } void test_nonnative_json() { string json; mixed expect; mixed proc; json = " { \"user\" :\t{ \"name\": \"Bob\" , \"validated\":true,\"profiles\":[{\"url\":\"http://facespace.com/bob\",\"useful\":false},\n{\"url\":\"http://bugroff.com/bob\",\"useful\":null}\n],\n\"sequence\":[4,8,15,16,23,42]},\r\n\t\"login\":\"2014-10-01 18:35:12\"}\n "; expect = ([ "user" : ([ "name" : "Bob", "validated" : 1, "profiles" : ({ ([ "url" : "http://facespace.com/bob", "useful" : 0, ]), ([ "url" : "http://bugroff.com/bob", "useful" : 0, ]), }), "sequence" : ({ 4, 8, 15, 16, 23, 42 }), ]), "login" : "2014-10-01 18:35:12", ]); proc = json_decode(json); AssertEquivalent(expect, proc); json = " [ \n8e-3, 7.5E6, -2e-2 \t]"; expect = ({ 0.008, 7500000, -0.02 }); proc = json_decode(json); AssertEquivalent(expect, proc); } void test_floats() { float array values = ({ 0.5, 27.3, 0.001, 0.000001, 1000000.0, 0.0000000000002, 2000000000000.0 }); foreach(float value : values) { string pos_fwd = json_encode(value); mixed pos_rev = json_decode(pos_fwd); AssertEquivalent(value, pos_rev); float neg_value = -value; string neg_fwd = json_encode(neg_value); mixed neg_rev = json_decode(neg_fwd); AssertEquivalent(neg_value, neg_rev); } for(int ix = 0; ix < 200000; ix += ((ix ^ 0xdead) & 0xff) + 1) { float value = to_float(ix); string pos_fwd = json_encode(value); mixed pos_rev = json_decode(pos_fwd); AssertEquivalent(value, pos_rev); float neg_value = -value; string neg_fwd = json_encode(neg_value); mixed neg_rev = json_decode(neg_fwd); AssertEquivalent(neg_value, neg_rev); } } void test_ints() { int array values = ({ 0, 1, 27, 1000, 27638, 8381237, __INT_MAX__ - 1 }); foreach(int value : values) { string pos_fwd = json_encode(value); mixed pos_rev = json_decode(pos_fwd); AssertEquivalent(value, pos_rev); int neg_value = -value; string neg_fwd = json_encode(neg_value); mixed neg_rev = json_decode(neg_fwd); AssertEquivalent(neg_value, neg_rev); } for(int value = 0; value < 200000; value += ((value ^ 0xdead) & 0xff) + 1) { string pos_fwd = json_encode(value); mixed pos_rev = json_decode(pos_fwd); AssertEquivalent(value, pos_rev); int neg_value = -value; string neg_fwd = json_encode(neg_value); mixed neg_rev = json_decode(neg_fwd); AssertEquivalent(neg_value, neg_rev); } } void test_object() { mixed value = this_object(); string expect = "null"; string fwd = json_encode(value); AssertEquals(expect, fwd); } void test_closure() { mixed value = #'test_closure; string expect = "null"; string fwd = json_encode(value); AssertEquals(expect, fwd); } void test_quoted_array() { mixed value = '({ 273 }); string expect = "null"; string fwd = json_encode(value); AssertEquals(expect, fwd); } void test_symbol() { mixed value = 'test; string expect = "null"; string fwd = json_encode(value); AssertEquals(expect, fwd); }