2 * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
11 static void test_equal_simple()
13 json_t *value1, *value2;
15 if(json_equal(NULL, NULL))
16 fail("json_equal fails for two NULLs");
19 if(json_equal(value1, NULL) || json_equal(NULL, value1))
20 fail("json_equal fails for NULL");
22 /* this covers true, false and null as they are singletons */
23 if(!json_equal(value1, value1))
24 fail("identical objects are not equal");
28 value1 = json_integer(1);
29 value2 = json_integer(1);
30 if(!value1 || !value2)
31 fail("unable to create integers");
32 if(!json_equal(value1, value2))
33 fail("json_equal fails for two equal integers");
36 value2 = json_integer(2);
38 fail("unable to create an integer");
39 if(json_equal(value1, value2))
40 fail("json_equal fails for two inequal integers");
46 value1 = json_real(1.2);
47 value2 = json_real(1.2);
48 if(!value1 || !value2)
49 fail("unable to create reals");
50 if(!json_equal(value1, value2))
51 fail("json_equal fails for two equal reals");
54 value2 = json_real(3.141592);
56 fail("unable to create an real");
57 if(json_equal(value1, value2))
58 fail("json_equal fails for two inequal reals");
64 value1 = json_string("foo");
65 value2 = json_string("foo");
66 if(!value1 || !value2)
67 fail("unable to create strings");
68 if(!json_equal(value1, value2))
69 fail("json_equal fails for two equal strings");
72 value2 = json_string("bar");
74 fail("unable to create an string");
75 if(json_equal(value1, value2))
76 fail("json_equal fails for two inequal strings");
82 static void test_equal_array()
84 json_t *array1, *array2;
86 array1 = json_array();
87 array2 = json_array();
88 if(!array1 || !array2)
89 fail("unable to create arrays");
91 if(!json_equal(array1, array2))
92 fail("json_equal fails for two empty arrays");
94 json_array_append_new(array1, json_integer(1));
95 json_array_append_new(array2, json_integer(1));
96 json_array_append_new(array1, json_string("foo"));
97 json_array_append_new(array2, json_string("foo"));
98 json_array_append_new(array1, json_integer(2));
99 json_array_append_new(array2, json_integer(2));
100 if(!json_equal(array1, array2))
101 fail("json_equal fails for two equal arrays");
103 json_array_remove(array2, 2);
104 if(json_equal(array1, array2))
105 fail("json_equal fails for two inequal arrays");
107 json_array_append_new(array2, json_integer(3));
108 if(json_equal(array1, array2))
109 fail("json_equal fails for two inequal arrays");
115 static void test_equal_object()
117 json_t *object1, *object2;
119 object1 = json_object();
120 object2 = json_object();
121 if(!object1 || !object2)
122 fail("unable to create objects");
124 if(!json_equal(object1, object2))
125 fail("json_equal fails for two empty objects");
127 json_object_set_new(object1, "a", json_integer(1));
128 json_object_set_new(object2, "a", json_integer(1));
129 json_object_set_new(object1, "b", json_string("foo"));
130 json_object_set_new(object2, "b", json_string("foo"));
131 json_object_set_new(object1, "c", json_integer(2));
132 json_object_set_new(object2, "c", json_integer(2));
133 if(!json_equal(object1, object2))
134 fail("json_equal fails for two equal objects");
136 json_object_del(object2, "c");
137 if(json_equal(object1, object2))
138 fail("json_equal fails for two inequal objects");
140 json_object_set_new(object2, "c", json_integer(3));
141 if(json_equal(object1, object2))
142 fail("json_equal fails for two inequal objects");
144 json_object_del(object2, "c");
145 json_object_set_new(object2, "d", json_integer(2));
146 if(json_equal(object1, object2))
147 fail("json_equal fails for two inequal objects");
149 json_decref(object1);
150 json_decref(object2);
153 static void test_equal_complex()
155 json_t *value1, *value2;
157 const char *complex_json =
160 " \"real\": 3.141592, "
161 " \"string\": \"foobar\", "
164 " \"array-in-object\": [1,true,\"foo\",{}],"
165 " \"object-in-object\": {\"foo\": \"bar\"}"
167 " \"array\": [\"foo\", false, null, 1.234]"
170 value1 = json_loads(complex_json, 0, NULL);
171 value2 = json_loads(complex_json, 0, NULL);
172 if(!value1 || !value2)
173 fail("unable to parse JSON");
174 if(!json_equal(value1, value2))
175 fail("json_equal fails for two inequal strings");
180 /* TODO: There's no negative test case here */
183 static void run_tests()
188 test_equal_complex();