2
* Copyright (c) 2009-2011 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_set_nocheck()
172
json_t *object, *string;
174
object = json_object();
175
string = json_string("bar");
178
fail("unable to create object");
180
fail("unable to create string");
182
if(json_object_set_nocheck(object, "foo", string))
183
fail("json_object_set_nocheck failed");
184
if(json_object_get(object, "foo") != string)
185
fail("json_object_get after json_object_set_nocheck failed");
187
/* invalid UTF-8 in key */
188
if(json_object_set_nocheck(object, "a\xefz", string))
189
fail("json_object_set_nocheck failed for invalid UTF-8");
190
if(json_object_get(object, "a\xefz") != string)
191
fail("json_object_get after json_object_set_nocheck failed");
193
if(json_object_set_new_nocheck(object, "bax", json_integer(123)))
194
fail("json_object_set_new_nocheck failed");
195
if(json_integer_value(json_object_get(object, "bax")) != 123)
196
fail("json_object_get after json_object_set_new_nocheck failed");
198
/* invalid UTF-8 in key */
199
if(json_object_set_new_nocheck(object, "asdf\xfe", json_integer(321)))
200
fail("json_object_set_new_nocheck failed for invalid UTF-8");
201
if(json_integer_value(json_object_get(object, "asdf\xfe")) != 321)
202
fail("json_object_get after json_object_set_new_nocheck failed");
208
static void test_iterators()
210
json_t *object, *foo, *bar, *baz;
213
if(json_object_iter(NULL))
214
fail("able to iterate over NULL");
216
if(json_object_iter_next(NULL, NULL))
217
fail("able to increment an iterator on a NULL object");
219
object = json_object();
220
foo = json_string("foo");
221
bar = json_string("bar");
222
baz = json_string("baz");
223
if(!object || !foo || !bar || !bar)
224
fail("unable to create values");
226
if(json_object_iter_next(object, NULL))
227
fail("able to increment a NULL iterator");
229
if(json_object_set(object, "a", foo) ||
230
json_object_set(object, "b", bar) ||
231
json_object_set(object, "c", baz))
232
fail("unable to populate object");
234
iter = json_object_iter(object);
236
fail("unable to get iterator");
237
if(strcmp(json_object_iter_key(iter), "a"))
238
fail("iterating failed: wrong key");
239
if(json_object_iter_value(iter) != foo)
240
fail("iterating failed: wrong value");
242
iter = json_object_iter_next(object, iter);
244
fail("unable to increment iterator");
245
if(strcmp(json_object_iter_key(iter), "b"))
246
fail("iterating failed: wrong key");
247
if(json_object_iter_value(iter) != bar)
248
fail("iterating failed: wrong value");
250
iter = json_object_iter_next(object, iter);
252
fail("unable to increment iterator");
253
if(strcmp(json_object_iter_key(iter), "c"))
254
fail("iterating failed: wrong key");
255
if(json_object_iter_value(iter) != baz)
256
fail("iterating failed: wrong value");
258
if(json_object_iter_next(object, iter) != NULL)
259
fail("able to iterate over the end");
261
if(json_object_iter_at(object, "foo"))
262
fail("json_object_iter_at() succeeds for non-existent key");
264
iter = json_object_iter_at(object, "b");
266
fail("json_object_iter_at() fails for an existing key");
268
if(strcmp(json_object_iter_key(iter), "b"))
269
fail("iterating failed: wrong key");
270
if(json_object_iter_value(iter) != bar)
271
fail("iterating failed: wrong value");
273
iter = json_object_iter_next(object, iter);
275
fail("unable to increment iterator");
276
if(strcmp(json_object_iter_key(iter), "c"))
277
fail("iterating failed: wrong key");
278
if(json_object_iter_value(iter) != baz)
279
fail("iterating failed: wrong value");
281
if(json_object_iter_set(object, iter, bar))
282
fail("unable to set value at iterator");
284
if(strcmp(json_object_iter_key(iter), "c"))
285
fail("json_object_iter_key() fails after json_object_iter_set()");
286
if(json_object_iter_value(iter) != bar)
287
fail("json_object_iter_value() fails after json_object_iter_set()");
288
if(json_object_get(object, "c") != bar)
289
fail("json_object_get() fails after json_object_iter_set()");
297
static void test_misc()
299
json_t *object, *string, *other_string, *value;
301
object = json_object();
302
string = json_string("test");
303
other_string = json_string("other");
306
fail("unable to create object");
307
if(!string || !other_string)
308
fail("unable to create string");
310
if(json_object_get(object, "a"))
311
fail("value for nonexisting key");
313
if(json_object_set(object, "a", string))
314
fail("unable to set value");
316
if(!json_object_set(object, NULL, string))
317
fail("able to set NULL key");
319
if(!json_object_set(object, "a", NULL))
320
fail("able to set NULL value");
322
/* invalid UTF-8 in key */
323
if(!json_object_set(object, "a\xefz", string))
324
fail("able to set invalid unicode key");
326
value = json_object_get(object, "a");
328
fail("no value for existing key");
330
fail("got different value than what was added");
332
/* "a", "lp" and "px" collide in a five-bucket hashtable */
333
if(json_object_set(object, "b", string) ||
334
json_object_set(object, "lp", string) ||
335
json_object_set(object, "px", string))
336
fail("unable to set value");
338
value = json_object_get(object, "a");
340
fail("no value for existing key");
342
fail("got different value than what was added");
344
if(json_object_set(object, "a", other_string))
345
fail("unable to replace an existing key");
347
value = json_object_get(object, "a");
349
fail("no value for existing key");
350
if(value != other_string)
351
fail("got different value than what was set");
353
if(!json_object_del(object, "nonexisting"))
354
fail("able to delete a nonexisting key");
356
if(json_object_del(object, "px"))
357
fail("unable to delete an existing key");
359
if(json_object_del(object, "a"))
360
fail("unable to delete an existing key");
362
if(json_object_del(object, "lp"))
363
fail("unable to delete an existing key");
366
/* add many keys to initiate rehashing */
368
if(json_object_set(object, "a", string))
369
fail("unable to set value");
371
if(json_object_set(object, "lp", string))
372
fail("unable to set value");
374
if(json_object_set(object, "px", string))
375
fail("unable to set value");
377
if(json_object_set(object, "c", string))
378
fail("unable to set value");
380
if(json_object_set(object, "d", string))
381
fail("unable to set value");
383
if(json_object_set(object, "e", string))
384
fail("unable to set value");
387
if(json_object_set_new(object, "foo", json_integer(123)))
388
fail("unable to set new value");
390
value = json_object_get(object, "foo");
391
if(!json_is_integer(value) || json_integer_value(value) != 123)
392
fail("json_object_set_new works incorrectly");
394
if(!json_object_set_new(object, NULL, json_integer(432)))
395
fail("able to set_new NULL key");
397
if(!json_object_set_new(object, "foo", NULL))
398
fail("able to set_new NULL value");
401
json_decref(other_string);
405
static void test_preserve_order()
410
const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": 3, \"sit amet\": 5, \"helicopter\": 7}";
412
object = json_object();
414
json_object_set_new(object, "foobar", json_integer(1));
415
json_object_set_new(object, "bazquux", json_integer(2));
416
json_object_set_new(object, "lorem ipsum", json_integer(3));
417
json_object_set_new(object, "dolor", json_integer(4));
418
json_object_set_new(object, "sit amet", json_integer(5));
420
/* changing a value should preserve the order */
421
json_object_set_new(object, "bazquux", json_integer(6));
423
/* deletion shouldn't change the order of others */
424
json_object_del(object, "dolor");
426
/* add a new item just to make sure */
427
json_object_set_new(object, "helicopter", json_integer(7));
429
result = json_dumps(object, JSON_PRESERVE_ORDER);
431
if(strcmp(expected, result) != 0) {
432
fprintf(stderr, "%s != %s", expected, result);
433
fail("JSON_PRESERVE_ORDER doesn't work");
448
test_preserve_order();