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.
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_conditional_updates()
144 json_t *object, *other;
146 object = json_pack("{sisi}", "foo", 1, "bar", 2);
147 other = json_pack("{sisi}", "foo", 3, "baz", 4);
149 if(json_object_update_existing(object, other))
150 fail("json_object_update_existing failed");
152 if(json_object_size(object) != 2)
153 fail("json_object_update_existing added new items");
155 if(json_integer_value(json_object_get(object, "foo")) != 3)
156 fail("json_object_update_existing failed to update existing key");
158 if(json_integer_value(json_object_get(object, "bar")) != 2)
159 fail("json_object_update_existing updated wrong key");
163 object = json_pack("{sisi}", "foo", 1, "bar", 2);
165 if(json_object_update_missing(object, other))
166 fail("json_object_update_missing failed");
168 if(json_object_size(object) != 3)
169 fail("json_object_update_missing didn't add new items");
171 if(json_integer_value(json_object_get(object, "foo")) != 1)
172 fail("json_object_update_missing updated existing key");
174 if(json_integer_value(json_object_get(object, "bar")) != 2)
175 fail("json_object_update_missing updated wrong key");
177 if(json_integer_value(json_object_get(object, "baz")) != 4)
178 fail("json_object_update_missing didn't add new items");
184 static void test_circular()
186 json_t *object1, *object2;
188 object1 = json_object();
189 object2 = json_object();
190 if(!object1 || !object2)
191 fail("unable to create object");
193 /* the simple case is checked */
194 if(json_object_set(object1, "a", object1) == 0)
195 fail("able to set self");
197 /* create circular references */
198 if(json_object_set(object1, "a", object2) ||
199 json_object_set(object2, "a", object1))
200 fail("unable to set value");
202 /* circularity is detected when dumping */
203 if(json_dumps(object1, 0) != NULL)
204 fail("able to dump circulars");
206 /* decref twice to deal with the circular references */
207 json_decref(object1);
208 json_decref(object2);
209 json_decref(object1);
212 static void test_set_nocheck()
214 json_t *object, *string;
216 object = json_object();
217 string = json_string("bar");
220 fail("unable to create object");
222 fail("unable to create string");
224 if(json_object_set_nocheck(object, "foo", string))
225 fail("json_object_set_nocheck failed");
226 if(json_object_get(object, "foo") != string)
227 fail("json_object_get after json_object_set_nocheck failed");
229 /* invalid UTF-8 in key */
230 if(json_object_set_nocheck(object, "a\xefz", string))
231 fail("json_object_set_nocheck failed for invalid UTF-8");
232 if(json_object_get(object, "a\xefz") != string)
233 fail("json_object_get after json_object_set_nocheck failed");
235 if(json_object_set_new_nocheck(object, "bax", json_integer(123)))
236 fail("json_object_set_new_nocheck failed");
237 if(json_integer_value(json_object_get(object, "bax")) != 123)
238 fail("json_object_get after json_object_set_new_nocheck failed");
240 /* invalid UTF-8 in key */
241 if(json_object_set_new_nocheck(object, "asdf\xfe", json_integer(321)))
242 fail("json_object_set_new_nocheck failed for invalid UTF-8");
243 if(json_integer_value(json_object_get(object, "asdf\xfe")) != 321)
244 fail("json_object_get after json_object_set_new_nocheck failed");
250 static void test_iterators()
252 json_t *object, *foo, *bar, *baz;
255 if(json_object_iter(NULL))
256 fail("able to iterate over NULL");
258 if(json_object_iter_next(NULL, NULL))
259 fail("able to increment an iterator on a NULL object");
261 object = json_object();
262 foo = json_string("foo");
263 bar = json_string("bar");
264 baz = json_string("baz");
265 if(!object || !foo || !bar || !bar)
266 fail("unable to create values");
268 if(json_object_iter_next(object, NULL))
269 fail("able to increment a NULL iterator");
271 if(json_object_set(object, "a", foo) ||
272 json_object_set(object, "b", bar) ||
273 json_object_set(object, "c", baz))
274 fail("unable to populate object");
276 iter = json_object_iter(object);
278 fail("unable to get iterator");
279 if(strcmp(json_object_iter_key(iter), "a"))
280 fail("iterating failed: wrong key");
281 if(json_object_iter_value(iter) != foo)
282 fail("iterating failed: wrong value");
284 iter = json_object_iter_next(object, iter);
286 fail("unable to increment iterator");
287 if(strcmp(json_object_iter_key(iter), "b"))
288 fail("iterating failed: wrong key");
289 if(json_object_iter_value(iter) != bar)
290 fail("iterating failed: wrong value");
292 iter = json_object_iter_next(object, iter);
294 fail("unable to increment iterator");
295 if(strcmp(json_object_iter_key(iter), "c"))
296 fail("iterating failed: wrong key");
297 if(json_object_iter_value(iter) != baz)
298 fail("iterating failed: wrong value");
300 if(json_object_iter_next(object, iter) != NULL)
301 fail("able to iterate over the end");
303 if(json_object_iter_at(object, "foo"))
304 fail("json_object_iter_at() succeeds for non-existent key");
306 iter = json_object_iter_at(object, "b");
308 fail("json_object_iter_at() fails for an existing key");
310 if(strcmp(json_object_iter_key(iter), "b"))
311 fail("iterating failed: wrong key");
312 if(json_object_iter_value(iter) != bar)
313 fail("iterating failed: wrong value");
315 iter = json_object_iter_next(object, iter);
317 fail("unable to increment iterator");
318 if(strcmp(json_object_iter_key(iter), "c"))
319 fail("iterating failed: wrong key");
320 if(json_object_iter_value(iter) != baz)
321 fail("iterating failed: wrong value");
323 if(json_object_iter_set(object, iter, bar))
324 fail("unable to set value at iterator");
326 if(strcmp(json_object_iter_key(iter), "c"))
327 fail("json_object_iter_key() fails after json_object_iter_set()");
328 if(json_object_iter_value(iter) != bar)
329 fail("json_object_iter_value() fails after json_object_iter_set()");
330 if(json_object_get(object, "c") != bar)
331 fail("json_object_get() fails after json_object_iter_set()");
339 static void test_misc()
341 json_t *object, *string, *other_string, *value;
343 object = json_object();
344 string = json_string("test");
345 other_string = json_string("other");
348 fail("unable to create object");
349 if(!string || !other_string)
350 fail("unable to create string");
352 if(json_object_get(object, "a"))
353 fail("value for nonexisting key");
355 if(json_object_set(object, "a", string))
356 fail("unable to set value");
358 if(!json_object_set(object, NULL, string))
359 fail("able to set NULL key");
361 if(!json_object_set(object, "a", NULL))
362 fail("able to set NULL value");
364 /* invalid UTF-8 in key */
365 if(!json_object_set(object, "a\xefz", string))
366 fail("able to set invalid unicode key");
368 value = json_object_get(object, "a");
370 fail("no value for existing key");
372 fail("got different value than what was added");
374 /* "a", "lp" and "px" collide in a five-bucket hashtable */
375 if(json_object_set(object, "b", string) ||
376 json_object_set(object, "lp", string) ||
377 json_object_set(object, "px", string))
378 fail("unable to set value");
380 value = json_object_get(object, "a");
382 fail("no value for existing key");
384 fail("got different value than what was added");
386 if(json_object_set(object, "a", other_string))
387 fail("unable to replace an existing key");
389 value = json_object_get(object, "a");
391 fail("no value for existing key");
392 if(value != other_string)
393 fail("got different value than what was set");
395 if(!json_object_del(object, "nonexisting"))
396 fail("able to delete a nonexisting key");
398 if(json_object_del(object, "px"))
399 fail("unable to delete an existing key");
401 if(json_object_del(object, "a"))
402 fail("unable to delete an existing key");
404 if(json_object_del(object, "lp"))
405 fail("unable to delete an existing key");
408 /* add many keys to initiate rehashing */
410 if(json_object_set(object, "a", string))
411 fail("unable to set value");
413 if(json_object_set(object, "lp", string))
414 fail("unable to set value");
416 if(json_object_set(object, "px", string))
417 fail("unable to set value");
419 if(json_object_set(object, "c", string))
420 fail("unable to set value");
422 if(json_object_set(object, "d", string))
423 fail("unable to set value");
425 if(json_object_set(object, "e", string))
426 fail("unable to set value");
429 if(json_object_set_new(object, "foo", json_integer(123)))
430 fail("unable to set new value");
432 value = json_object_get(object, "foo");
433 if(!json_is_integer(value) || json_integer_value(value) != 123)
434 fail("json_object_set_new works incorrectly");
436 if(!json_object_set_new(object, NULL, json_integer(432)))
437 fail("able to set_new NULL key");
439 if(!json_object_set_new(object, "foo", NULL))
440 fail("able to set_new NULL value");
443 json_decref(other_string);
447 static void test_preserve_order()
452 const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": 3, \"sit amet\": 5, \"helicopter\": 7}";
454 object = json_object();
456 json_object_set_new(object, "foobar", json_integer(1));
457 json_object_set_new(object, "bazquux", json_integer(2));
458 json_object_set_new(object, "lorem ipsum", json_integer(3));
459 json_object_set_new(object, "dolor", json_integer(4));
460 json_object_set_new(object, "sit amet", json_integer(5));
462 /* changing a value should preserve the order */
463 json_object_set_new(object, "bazquux", json_integer(6));
465 /* deletion shouldn't change the order of others */
466 json_object_del(object, "dolor");
468 /* add a new item just to make sure */
469 json_object_set_new(object, "helicopter", json_integer(7));
471 result = json_dumps(object, JSON_PRESERVE_ORDER);
473 if(strcmp(expected, result) != 0) {
474 fprintf(stderr, "%s != %s", expected, result);
475 fail("JSON_PRESERVE_ORDER doesn't work");
482 static void test_foreach()
485 json_t *object1, *object2, *value;
487 object1 = json_pack("{sisisi}", "foo", 1, "bar", 2, "baz", 3);
488 object2 = json_object();
490 json_object_foreach(object1, key, value)
491 json_object_set(object2, key, value);
493 if(!json_equal(object1, object2))
494 fail("json_object_foreach failed to iterate all key-value pairs");
496 json_decref(object1);
497 json_decref(object2);
500 static void run_tests()
505 test_conditional_updates();
509 test_preserve_order();