2 * Copyright (c) 2009 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.
12 static void test_clear()
16 object = json_object();
17 ten = json_integer(10);
20 fail("unable to create object");
22 fail("unable to create integer");
24 if(json_object_set(object, "a", ten) ||
25 json_object_set(object, "b", ten) ||
26 json_object_set(object, "c", ten) ||
27 json_object_set(object, "d", ten) ||
28 json_object_set(object, "e", ten))
29 fail("unable to set value");
31 if(json_object_size(object) != 5)
34 json_object_clear(object);
36 if(json_object_size(object) != 0)
37 fail("invalid size after clear");
43 static void test_update()
45 json_t *object, *other, *nine, *ten;
47 object = json_object();
48 other = json_object();
50 nine = json_integer(9);
51 ten = json_integer(10);
54 fail("unable to create object");
56 fail("unable to create integer");
59 /* update an empty object with an empty object */
61 if(json_object_update(object, other))
62 fail("unable to update an emtpy object with an empty object");
64 if(json_object_size(object) != 0)
65 fail("invalid size after update");
67 if(json_object_size(other) != 0)
68 fail("invalid size for updater after update");
71 /* update an empty object with a nonempty object */
73 if(json_object_set(other, "a", ten) ||
74 json_object_set(other, "b", ten) ||
75 json_object_set(other, "c", ten) ||
76 json_object_set(other, "d", ten) ||
77 json_object_set(other, "e", ten))
78 fail("unable to set value");
80 if(json_object_update(object, other))
81 fail("unable to update an empty object");
83 if(json_object_size(object) != 5)
84 fail("invalid size after update");
86 if(json_object_get(object, "a") != ten ||
87 json_object_get(object, "b") != ten ||
88 json_object_get(object, "c") != ten ||
89 json_object_get(object, "d") != ten ||
90 json_object_get(object, "e") != ten)
91 fail("update works incorrectly");
94 /* perform the same update again */
96 if(json_object_update(object, other))
97 fail("unable to update an empty object");
99 if(json_object_size(object) != 5)
100 fail("invalid size after update");
102 if(json_object_get(object, "a") != ten ||
103 json_object_get(object, "b") != ten ||
104 json_object_get(object, "c") != ten ||
105 json_object_get(object, "d") != ten ||
106 json_object_get(object, "e") != ten)
107 fail("update works incorrectly");
110 /* update a nonempty object with a nonempty object with both old
113 if(json_object_clear(other))
114 fail("clear failed");
116 if(json_object_set(other, "a", nine) ||
117 json_object_set(other, "b", nine) ||
118 json_object_set(other, "f", nine) ||
119 json_object_set(other, "g", nine) ||
120 json_object_set(other, "h", nine))
121 fail("unable to set value");
123 if(json_object_update(object, other))
124 fail("unable to update a nonempty object");
126 if(json_object_size(object) != 8)
127 fail("invalid size after update");
129 if(json_object_get(object, "a") != nine ||
130 json_object_get(object, "b") != nine ||
131 json_object_get(object, "f") != nine ||
132 json_object_get(object, "g") != nine ||
133 json_object_get(object, "h") != nine)
134 fail("update works incorrectly");
142 static void test_circular()
144 json_t *object1, *object2;
146 object1 = json_object();
147 object2 = json_object();
148 if(!object1 || !object2)
149 fail("unable to create object");
151 /* the simple case is checked */
152 if(json_object_set(object1, "a", object1) == 0)
153 fail("able to set self");
155 /* create circular references */
156 if(json_object_set(object1, "a", object2) ||
157 json_object_set(object2, "a", object1))
158 fail("unable to set value");
160 /* circularity is detected when dumping */
161 if(json_dumps(object1, 0) != NULL)
162 fail("able to dump circulars");
164 /* decref twice to deal with the circular references */
165 json_decref(object1);
166 json_decref(object2);
167 json_decref(object1);
170 static void test_misc()
172 json_t *object, *string, *other_string, *value;
175 object = json_object();
176 string = json_string("test");
177 other_string = json_string("other");
180 fail("unable to create object");
181 if(!string || !other_string)
182 fail("unable to create string");
184 if(json_object_get(object, "a"))
185 fail("value for nonexisting key");
187 if(json_object_set(object, "a", string))
188 fail("unable to set value");
190 if(!json_object_set(object, NULL, string))
191 fail("able to set NULL key");
193 if(!json_object_set(object, "a", NULL))
194 fail("able to set NULL value");
196 iter = json_object_iter(object);
198 fail("unable to get iterator");
200 if(strcmp(json_object_iter_key(iter), "a"))
201 fail("iterating failed: wrong key");
202 if(json_object_iter_value(iter) != string)
203 fail("iterating failed: wrong value");
204 if(json_object_iter_next(object, iter) != NULL)
205 fail("able to iterate over the end");
207 /* invalid UTF-8 in key */
208 if(!json_object_set(object, "a\xefz", string))
209 fail("able to set invalid unicode key");
211 value = json_object_get(object, "a");
213 fail("no value for existing key");
215 fail("got different value than what was added");
217 /* "a", "lp" and "px" collide in a five-bucket hashtable */
218 if(json_object_set(object, "b", string) ||
219 json_object_set(object, "lp", string) ||
220 json_object_set(object, "px", string))
221 fail("unable to set value");
223 value = json_object_get(object, "a");
225 fail("no value for existing key");
227 fail("got different value than what was added");
229 if(json_object_set(object, "a", other_string))
230 fail("unable to replace an existing key");
232 value = json_object_get(object, "a");
234 fail("no value for existing key");
235 if(value != other_string)
236 fail("got different value than what was set");
238 if(!json_object_del(object, "nonexisting"))
239 fail("able to delete a nonexisting key");
241 if(json_object_del(object, "px"))
242 fail("unable to delete an existing key");
244 if(json_object_del(object, "a"))
245 fail("unable to delete an existing key");
247 if(json_object_del(object, "lp"))
248 fail("unable to delete an existing key");
251 /* add many keys to initiate rehashing */
253 if(json_object_set(object, "a", string))
254 fail("unable to set value");
256 if(json_object_set(object, "lp", string))
257 fail("unable to set value");
259 if(json_object_set(object, "px", string))
260 fail("unable to set value");
262 if(json_object_set(object, "c", string))
263 fail("unable to set value");
265 if(json_object_set(object, "d", string))
266 fail("unable to set value");
268 if(json_object_set(object, "e", string))
269 fail("unable to set value");
272 if(json_object_set_new(object, "foo", json_integer(123)))
273 fail("unable to set new value");
275 value = json_object_get(object, "foo");
276 if(!json_is_integer(value) || json_integer_value(value) != 123)
277 fail("json_object_set_new works incorrectly");
279 if(!json_object_set_new(object, NULL, json_integer(432)))
280 fail("able to set_new NULL key");
282 if(!json_object_set_new(object, "foo", NULL))
283 fail("able to set_new NULL value");
286 json_decref(other_string);