Branch
Hash :
6f01253b
Author :
Date :
2017-01-26T13:30:45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/*
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include <jansson.h>
#include "util.h"
static void test_copy_simple(void)
{
json_t *value, *copy;
if(json_copy(NULL))
fail("copying NULL doesn't return NULL");
/* true */
value = json_true();
copy = json_copy(value);
if(value != copy)
fail("copying true failed");
json_decref(value);
json_decref(copy);
/* false */
value = json_false();
copy = json_copy(value);
if(value != copy)
fail("copying false failed");
json_decref(value);
json_decref(copy);
/* null */
value = json_null();
copy = json_copy(value);
if(value != copy)
fail("copying null failed");
json_decref(value);
json_decref(copy);
/* string */
value = json_string("foo");
if(!value)
fail("unable to create a string");
copy = json_copy(value);
if(!copy)
fail("unable to copy a string");
if(copy == value)
fail("copying a string doesn't copy");
if(!json_equal(copy, value))
fail("copying a string produces an inequal copy");
if(value->refcount != 1 || copy->refcount != 1)
fail("invalid refcounts");
json_decref(value);
json_decref(copy);
/* integer */
value = json_integer(543);
if(!value)
fail("unable to create an integer");
copy = json_copy(value);
if(!copy)
fail("unable to copy an integer");
if(copy == value)
fail("copying an integer doesn't copy");
if(!json_equal(copy, value))
fail("copying an integer produces an inequal copy");
if(value->refcount != 1 || copy->refcount != 1)
fail("invalid refcounts");
json_decref(value);
json_decref(copy);
/* real */
value = json_real(123e9);
if(!value)
fail("unable to create a real");
copy = json_copy(value);
if(!copy)
fail("unable to copy a real");
if(copy == value)
fail("copying a real doesn't copy");
if(!json_equal(copy, value))
fail("copying a real produces an inequal copy");
if(value->refcount != 1 || copy->refcount != 1)
fail("invalid refcounts");
json_decref(value);
json_decref(copy);
}
static void test_deep_copy_simple(void)
{
json_t *value, *copy;
if(json_deep_copy(NULL))
fail("deep copying NULL doesn't return NULL");
/* true */
value = json_true();
copy = json_deep_copy(value);
if(value != copy)
fail("deep copying true failed");
json_decref(value);
json_decref(copy);
/* false */
value = json_false();
copy = json_deep_copy(value);
if(value != copy)
fail("deep copying false failed");
json_decref(value);
json_decref(copy);
/* null */
value = json_null();
copy = json_deep_copy(value);
if(value != copy)
fail("deep copying null failed");
json_decref(value);
json_decref(copy);
/* string */
value = json_string("foo");
if(!value)
fail("unable to create a string");
copy = json_deep_copy(value);
if(!copy)
fail("unable to deep copy a string");
if(copy == value)
fail("deep copying a string doesn't copy");
if(!json_equal(copy, value))
fail("deep copying a string produces an inequal copy");
if(value->refcount != 1 || copy->refcount != 1)
fail("invalid refcounts");
json_decref(value);
json_decref(copy);
/* integer */
value = json_integer(543);
if(!value)
fail("unable to create an integer");
copy = json_deep_copy(value);
if(!copy)
fail("unable to deep copy an integer");
if(copy == value)
fail("deep copying an integer doesn't copy");
if(!json_equal(copy, value))
fail("deep copying an integer produces an inequal copy");
if(value->refcount != 1 || copy->refcount != 1)
fail("invalid refcounts");
json_decref(value);
json_decref(copy);
/* real */
value = json_real(123e9);
if(!value)
fail("unable to create a real");
copy = json_deep_copy(value);
if(!copy)
fail("unable to deep copy a real");
if(copy == value)
fail("deep copying a real doesn't copy");
if(!json_equal(copy, value))
fail("deep copying a real produces an inequal copy");
if(value->refcount != 1 || copy->refcount != 1)
fail("invalid refcounts");
json_decref(value);
json_decref(copy);
}
static void test_copy_array(void)
{
const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
json_t *array, *copy;
size_t i;
array = json_loads(json_array_text, 0, NULL);
if(!array)
fail("unable to parse an array");
copy = json_copy(array);
if(!copy)
fail("unable to copy an array");
if(copy == array)
fail("copying an array doesn't copy");
if(!json_equal(copy, array))
fail("copying an array produces an inequal copy");
for(i = 0; i < json_array_size(copy); i++)
{
if(json_array_get(array, i) != json_array_get(copy, i))
fail("copying an array modifies its elements");
}
json_decref(array);
json_decref(copy);
}
static void test_deep_copy_array(void)
{
const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
json_t *array, *copy;
size_t i;
array = json_loads(json_array_text, 0, NULL);
if(!array)
fail("unable to parse an array");
copy = json_deep_copy(array);
if(!copy)
fail("unable to deep copy an array");
if(copy == array)
fail("deep copying an array doesn't copy");
if(!json_equal(copy, array))
fail("deep copying an array produces an inequal copy");
for(i = 0; i < json_array_size(copy); i++)
{
if(json_array_get(array, i) == json_array_get(copy, i))
fail("deep copying an array doesn't copy its elements");
}
json_decref(array);
json_decref(copy);
}
static void test_copy_object(void)
{
const char *json_object_text =
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
const char *keys[] = {"foo", "a", "b", "c"};
int i;
json_t *object, *copy;
void *iter;
object = json_loads(json_object_text, 0, NULL);
if(!object)
fail("unable to parse an object");
copy = json_copy(object);
if(!copy)
fail("unable to copy an object");
if(copy == object)
fail("copying an object doesn't copy");
if(!json_equal(copy, object))
fail("copying an object produces an inequal copy");
i = 0;
iter = json_object_iter(object);
while(iter)
{
const char *key;
json_t *value1, *value2;
key = json_object_iter_key(iter);
value1 = json_object_iter_value(iter);
value2 = json_object_get(copy, key);
if(value1 != value2)
fail("copying an object modifies its items");
if (strcmp(key, keys[i]) != 0)
fail("copying an object doesn't preserve key order");
iter = json_object_iter_next(object, iter);
i++;
}
json_decref(object);
json_decref(copy);
}
static void test_deep_copy_object(void)
{
const char *json_object_text =
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
const char *keys[] = {"foo", "a", "b", "c"};
int i;
json_t *object, *copy;
void *iter;
object = json_loads(json_object_text, 0, NULL);
if(!object)
fail("unable to parse an object");
copy = json_deep_copy(object);
if(!copy)
fail("unable to deep copy an object");
if(copy == object)
fail("deep copying an object doesn't copy");
if(!json_equal(copy, object))
fail("deep copying an object produces an inequal copy");
i = 0;
iter = json_object_iter(object);
while(iter)
{
const char *key;
json_t *value1, *value2;
key = json_object_iter_key(iter);
value1 = json_object_iter_value(iter);
value2 = json_object_get(copy, key);
if(value1 == value2)
fail("deep copying an object doesn't copy its items");
if (strcmp(key, keys[i]) != 0)
fail("deep copying an object doesn't preserve key order");
iter = json_object_iter_next(object, iter);
i++;
}
json_decref(object);
json_decref(copy);
}
static void run_tests()
{
test_copy_simple();
test_deep_copy_simple();
test_copy_array();
test_deep_copy_array();
test_copy_object();
test_deep_copy_object();
}