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_misc()
144 json_t *object, *string, *other_string, *value;
147 object = json_object();
148 string = json_string("test");
149 other_string = json_string("other");
152 fail("unable to create object");
153 if(!string || !other_string)
154 fail("unable to create string");
156 if(json_object_get(object, "a"))
157 fail("value for nonexisting key");
159 if(json_object_set(object, "a", string))
160 fail("unable to set value");
162 if(!json_object_set(object, NULL, string))
163 fail("able to set NULL key");
165 if(!json_object_set(object, "a", NULL))
166 fail("able to set NULL value");
168 iter = json_object_iter(object);
170 fail("unable to get iterator");
172 if(strcmp(json_object_iter_key(iter), "a"))
173 fail("iterating failed: wrong key");
174 if(json_object_iter_value(iter) != string)
175 fail("iterating failed: wrong value");
176 if(json_object_iter_next(object, iter) != NULL)
177 fail("able to iterate over the end");
179 /* invalid UTF-8 in key */
180 if(!json_object_set(object, "a\xefz", string))
181 fail("able to set invalid unicode key");
183 value = json_object_get(object, "a");
185 fail("no value for existing key");
187 fail("got different value than what was added");
189 /* "a", "lp" and "px" collide in a five-bucket hashtable */
190 if(json_object_set(object, "b", string) ||
191 json_object_set(object, "lp", string) ||
192 json_object_set(object, "px", string))
193 fail("unable to set value");
195 value = json_object_get(object, "a");
197 fail("no value for existing key");
199 fail("got different value than what was added");
201 if(json_object_set(object, "a", other_string))
202 fail("unable to replace an existing key");
204 value = json_object_get(object, "a");
206 fail("no value for existing key");
207 if(value != other_string)
208 fail("got different value than what was set");
210 if(!json_object_del(object, "nonexisting"))
211 fail("able to delete a nonexisting key");
213 if(json_object_del(object, "px"))
214 fail("unable to delete an existing key");
216 if(json_object_del(object, "a"))
217 fail("unable to delete an existing key");
219 if(json_object_del(object, "lp"))
220 fail("unable to delete an existing key");
223 /* add many keys to initiate rehashing */
225 if(json_object_set(object, "a", string))
226 fail("unable to set value");
228 if(json_object_set(object, "lp", string))
229 fail("unable to set value");
231 if(json_object_set(object, "px", string))
232 fail("unable to set value");
234 if(json_object_set(object, "c", string))
235 fail("unable to set value");
237 if(json_object_set(object, "d", string))
238 fail("unable to set value");
240 if(json_object_set(object, "e", string))
241 fail("unable to set value");
244 if(json_object_set_new(object, "foo", json_integer(123)))
245 fail("unable to set new value");
247 value = json_object_get(object, "foo");
248 if(!json_is_integer(value) || json_integer_value(value) != 123)
249 fail("json_object_set_new works incorrectly");
251 if(!json_object_set_new(object, NULL, json_integer(432)))
252 fail("able to set_new NULL key");
254 if(!json_object_set_new(object, "foo", NULL))
255 fail("able to set_new NULL value");
258 json_decref(other_string);