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_copy_simple(void)
17 fail("copying NULL doesn't return NULL");
21 copy = json_copy(value);
23 fail("copying true failed");
29 copy = json_copy(value);
31 fail("copying false failed");
37 copy = json_copy(value);
39 fail("copying null failed");
44 value = json_string("foo");
46 fail("unable to create a string");
47 copy = json_copy(value);
49 fail("unable to copy a string");
51 fail("copying a string doesn't copy");
52 if(!json_equal(copy, value))
53 fail("copying a string produces an inequal copy");
54 if(value->refcount != 1 || copy->refcount != 1)
55 fail("invalid refcounts");
60 value = json_integer(543);
62 fail("unable to create an integer");
63 copy = json_copy(value);
65 fail("unable to copy an integer");
67 fail("copying an integer doesn't copy");
68 if(!json_equal(copy, value))
69 fail("copying an integer produces an inequal copy");
70 if(value->refcount != 1 || copy->refcount != 1)
71 fail("invalid refcounts");
76 value = json_real(123e9);
78 fail("unable to create a real");
79 copy = json_copy(value);
81 fail("unable to copy a real");
83 fail("copying a real doesn't copy");
84 if(!json_equal(copy, value))
85 fail("copying a real produces an inequal copy");
86 if(value->refcount != 1 || copy->refcount != 1)
87 fail("invalid refcounts");
92 static void test_deep_copy_simple(void)
96 if(json_deep_copy(NULL))
97 fail("deep copying NULL doesn't return NULL");
101 copy = json_deep_copy(value);
103 fail("deep copying true failed");
108 value = json_false();
109 copy = json_deep_copy(value);
111 fail("deep copying false failed");
117 copy = json_deep_copy(value);
119 fail("deep copying null failed");
124 value = json_string("foo");
126 fail("unable to create a string");
127 copy = json_deep_copy(value);
129 fail("unable to deep copy a string");
131 fail("deep copying a string doesn't copy");
132 if(!json_equal(copy, value))
133 fail("deep copying a string produces an inequal copy");
134 if(value->refcount != 1 || copy->refcount != 1)
135 fail("invalid refcounts");
140 value = json_integer(543);
142 fail("unable to create an integer");
143 copy = json_deep_copy(value);
145 fail("unable to deep copy an integer");
147 fail("deep copying an integer doesn't copy");
148 if(!json_equal(copy, value))
149 fail("deep copying an integer produces an inequal copy");
150 if(value->refcount != 1 || copy->refcount != 1)
151 fail("invalid refcounts");
156 value = json_real(123e9);
158 fail("unable to create a real");
159 copy = json_deep_copy(value);
161 fail("unable to deep copy a real");
163 fail("deep copying a real doesn't copy");
164 if(!json_equal(copy, value))
165 fail("deep copying a real produces an inequal copy");
166 if(value->refcount != 1 || copy->refcount != 1)
167 fail("invalid refcounts");
172 static void test_copy_array(void)
174 const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
176 json_t *array, *copy;
179 array = json_loads(json_array_text, 0, NULL);
181 fail("unable to parse an array");
183 copy = json_copy(array);
185 fail("unable to copy an array");
187 fail("copying an array doesn't copy");
188 if(!json_equal(copy, array))
189 fail("copying an array produces an inequal copy");
191 for(i = 0; i < json_array_size(copy); i++)
193 if(json_array_get(array, i) != json_array_get(copy, i))
194 fail("copying an array modifies its elements");
201 static void test_deep_copy_array(void)
203 const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
205 json_t *array, *copy;
208 array = json_loads(json_array_text, 0, NULL);
210 fail("unable to parse an array");
212 copy = json_deep_copy(array);
214 fail("unable to deep copy an array");
216 fail("deep copying an array doesn't copy");
217 if(!json_equal(copy, array))
218 fail("deep copying an array produces an inequal copy");
220 for(i = 0; i < json_array_size(copy); i++)
222 if(json_array_get(array, i) == json_array_get(copy, i))
223 fail("deep copying an array doesn't copy its elements");
230 static void test_copy_object(void)
232 const char *json_object_text =
233 "{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
235 json_t *object, *copy;
238 object = json_loads(json_object_text, 0, NULL);
240 fail("unable to parse an object");
242 copy = json_copy(object);
244 fail("unable to copy an object");
246 fail("copying an object doesn't copy");
247 if(!json_equal(copy, object))
248 fail("copying an object produces an inequal copy");
250 iter = json_object_iter(object);
254 json_t *value1, *value2;
256 key = json_object_iter_key(iter);
257 value1 = json_object_iter_value(iter);
258 value2 = json_object_get(copy, key);
261 fail("deep copying an object modifies its items");
263 iter = json_object_iter_next(object, iter);
270 static void test_deep_copy_object(void)
272 const char *json_object_text =
273 "{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
275 json_t *object, *copy;
278 object = json_loads(json_object_text, 0, NULL);
280 fail("unable to parse an object");
282 copy = json_deep_copy(object);
284 fail("unable to deep copy an object");
286 fail("deep copying an object doesn't copy");
287 if(!json_equal(copy, object))
288 fail("deep copying an object produces an inequal copy");
290 iter = json_object_iter(object);
294 json_t *value1, *value2;
296 key = json_object_iter_key(iter);
297 value1 = json_object_iter_value(iter);
298 value2 = json_object_get(copy, key);
301 fail("deep copying an object doesn't copy its items");
303 iter = json_object_iter_next(object, iter);
310 static void run_tests()
313 test_deep_copy_simple();
315 test_deep_copy_array();
317 test_deep_copy_object();