7e9ada8c13813edb18cf5db18e229b3ec667fd85
[jansson.git] / test / suites / api / test_object.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
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.
6  */
7
8 #include <jansson.h>
9 #include <string.h>
10 #include "util.h"
11
12 static void test_clear()
13 {
14     json_t *object, *ten;
15
16     object = json_object();
17     ten = json_integer(10);
18
19     if(!object)
20         fail("unable to create object");
21     if(!ten)
22         fail("unable to create integer");
23
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");
30
31     if(json_object_size(object) != 5)
32         fail("invalid size");
33
34     json_object_clear(object);
35
36     if(json_object_size(object) != 0)
37         fail("invalid size after clear");
38
39     json_decref(ten);
40     json_decref(object);
41 }
42
43 static void test_update()
44 {
45     json_t *object, *other, *nine, *ten;
46
47     object = json_object();
48     other = json_object();
49
50     nine = json_integer(9);
51     ten = json_integer(10);
52
53     if(!object || !other)
54         fail("unable to create object");
55     if(!nine || !ten)
56         fail("unable to create integer");
57
58
59     /* update an empty object with an empty object */
60
61     if(json_object_update(object, other))
62         fail("unable to update an emtpy object with an empty object");
63
64     if(json_object_size(object) != 0)
65         fail("invalid size after update");
66
67     if(json_object_size(other) != 0)
68         fail("invalid size for updater after update");
69
70
71     /* update an empty object with a nonempty object */
72
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");
79
80     if(json_object_update(object, other))
81         fail("unable to update an empty object");
82
83     if(json_object_size(object) != 5)
84         fail("invalid size after update");
85
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");
92
93
94     /* perform the same update again */
95
96     if(json_object_update(object, other))
97         fail("unable to update an empty object");
98
99     if(json_object_size(object) != 5)
100         fail("invalid size after update");
101
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");
108
109
110     /* update a nonempty object with a nonempty object with both old
111        and new keys */
112
113     if(json_object_clear(other))
114         fail("clear failed");
115
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");
122
123     if(json_object_update(object, other))
124         fail("unable to update a nonempty object");
125
126     if(json_object_size(object) != 8)
127         fail("invalid size after update");
128
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");
135
136     json_decref(nine);
137     json_decref(ten);
138     json_decref(other);
139     json_decref(object);
140 }
141
142 static void test_circular()
143 {
144     json_t *object1, *object2;
145
146     object1 = json_object();
147     object2 = json_object();
148     if(!object1 || !object2)
149         fail("unable to create object");
150
151     /* the simple case is checked */
152     if(json_object_set(object1, "a", object1) == 0)
153         fail("able to set self");
154
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");
159
160     /* circularity is detected when dumping */
161     if(json_dumps(object1, 0) != NULL)
162         fail("able to dump circulars");
163
164     /* decref twice to deal with the circular references */
165     json_decref(object1);
166     json_decref(object2);
167     json_decref(object1);
168 }
169
170 static void test_set_nocheck()
171 {
172     json_t *object, *string;
173
174     object = json_object();
175     string = json_string("bar");
176
177     if(!object)
178         fail("unable to create object");
179     if(!string)
180         fail("unable to create string");
181
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");
186
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");
192
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");
197
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");
203
204     json_decref(string);
205     json_decref(object);
206 }
207
208 static void test_misc()
209 {
210     json_t *object, *string, *other_string, *value;
211     void *iter;
212
213     object = json_object();
214     string = json_string("test");
215     other_string = json_string("other");
216
217     if(!object)
218         fail("unable to create object");
219     if(!string || !other_string)
220         fail("unable to create string");
221
222     if(json_object_get(object, "a"))
223         fail("value for nonexisting key");
224
225     if(json_object_set(object, "a", string))
226         fail("unable to set value");
227
228     if(!json_object_set(object, NULL, string))
229         fail("able to set NULL key");
230
231     if(!json_object_set(object, "a", NULL))
232         fail("able to set NULL value");
233
234     iter = json_object_iter(object);
235     if(!iter)
236         fail("unable to get iterator");
237
238     if(strcmp(json_object_iter_key(iter), "a"))
239         fail("iterating failed: wrong key");
240     if(json_object_iter_value(iter) != string)
241         fail("iterating failed: wrong value");
242     if(json_object_iter_next(object, iter) != NULL)
243         fail("able to iterate over the end");
244
245     /* invalid UTF-8 in key */
246     if(!json_object_set(object, "a\xefz", string))
247         fail("able to set invalid unicode key");
248
249     value = json_object_get(object, "a");
250     if(!value)
251         fail("no value for existing key");
252     if(value != string)
253         fail("got different value than what was added");
254
255     /* "a", "lp" and "px" collide in a five-bucket hashtable */
256     if(json_object_set(object, "b", string) ||
257        json_object_set(object, "lp", string) ||
258        json_object_set(object, "px", string))
259         fail("unable to set value");
260
261     value = json_object_get(object, "a");
262     if(!value)
263         fail("no value for existing key");
264     if(value != string)
265         fail("got different value than what was added");
266
267     if(json_object_set(object, "a", other_string))
268         fail("unable to replace an existing key");
269
270     value = json_object_get(object, "a");
271     if(!value)
272         fail("no value for existing key");
273     if(value != other_string)
274         fail("got different value than what was set");
275
276     if(!json_object_del(object, "nonexisting"))
277         fail("able to delete a nonexisting key");
278
279     if(json_object_del(object, "px"))
280         fail("unable to delete an existing key");
281
282     if(json_object_del(object, "a"))
283         fail("unable to delete an existing key");
284
285     if(json_object_del(object, "lp"))
286         fail("unable to delete an existing key");
287
288
289     /* add many keys to initiate rehashing */
290
291     if(json_object_set(object, "a", string))
292         fail("unable to set value");
293
294     if(json_object_set(object, "lp", string))
295         fail("unable to set value");
296
297     if(json_object_set(object, "px", string))
298         fail("unable to set value");
299
300     if(json_object_set(object, "c", string))
301         fail("unable to set value");
302
303     if(json_object_set(object, "d", string))
304         fail("unable to set value");
305
306     if(json_object_set(object, "e", string))
307         fail("unable to set value");
308
309
310     if(json_object_set_new(object, "foo", json_integer(123)))
311         fail("unable to set new value");
312
313     value = json_object_get(object, "foo");
314     if(!json_is_integer(value) || json_integer_value(value) != 123)
315         fail("json_object_set_new works incorrectly");
316
317     if(!json_object_set_new(object, NULL, json_integer(432)))
318         fail("able to set_new NULL key");
319
320     if(!json_object_set_new(object, "foo", NULL))
321         fail("able to set_new NULL value");
322
323     json_decref(string);
324     json_decref(other_string);
325     json_decref(object);
326 }
327
328 int main()
329 {
330     test_misc();
331     test_clear();
332     test_update();
333     test_circular();
334     test_set_nocheck();
335
336     return 0;
337 }