2009-12-31 21:56:28 +08:00
|
|
|
/*
|
2016-09-18 19:17:03 +08:00
|
|
|
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
|
2009-12-31 21:56:28 +08:00
|
|
|
*
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util.h"
|
2019-10-17 14:08:51 +08:00
|
|
|
#include <jansson.h>
|
|
|
|
#include <string.h>
|
2009-12-31 21:56:28 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_copy_simple(void) {
|
2009-12-31 21:56:28 +08:00
|
|
|
json_t *value, *copy;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_copy(NULL))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying NULL doesn't return NULL");
|
|
|
|
|
|
|
|
/* true */
|
|
|
|
value = json_true();
|
|
|
|
copy = json_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying true failed");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* false */
|
|
|
|
value = json_false();
|
|
|
|
copy = json_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying false failed");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* null */
|
|
|
|
value = json_null();
|
|
|
|
copy = json_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying null failed");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* string */
|
|
|
|
value = json_string("foo");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to create a string");
|
|
|
|
copy = json_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to copy a string");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying a string doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, value))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying a string produces an inequal copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value->refcount != 1 || copy->refcount != 1)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("invalid refcounts");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* integer */
|
|
|
|
value = json_integer(543);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to create an integer");
|
|
|
|
copy = json_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to copy an integer");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an integer doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, value))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an integer produces an inequal copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value->refcount != 1 || copy->refcount != 1)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("invalid refcounts");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* real */
|
|
|
|
value = json_real(123e9);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to create a real");
|
|
|
|
copy = json_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to copy a real");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying a real doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, value))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying a real produces an inequal copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value->refcount != 1 || copy->refcount != 1)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("invalid refcounts");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_deep_copy_simple(void) {
|
2009-12-31 21:56:28 +08:00
|
|
|
json_t *value, *copy;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_deep_copy(NULL))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying NULL doesn't return NULL");
|
|
|
|
|
|
|
|
/* true */
|
|
|
|
value = json_true();
|
|
|
|
copy = json_deep_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying true failed");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* false */
|
|
|
|
value = json_false();
|
|
|
|
copy = json_deep_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying false failed");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* null */
|
|
|
|
value = json_null();
|
|
|
|
copy = json_deep_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying null failed");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* string */
|
|
|
|
value = json_string("foo");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to create a string");
|
|
|
|
copy = json_deep_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to deep copy a string");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying a string doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, value))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying a string produces an inequal copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value->refcount != 1 || copy->refcount != 1)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("invalid refcounts");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* integer */
|
|
|
|
value = json_integer(543);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to create an integer");
|
|
|
|
copy = json_deep_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to deep copy an integer");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an integer doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, value))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an integer produces an inequal copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value->refcount != 1 || copy->refcount != 1)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("invalid refcounts");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
|
|
|
|
/* real */
|
|
|
|
value = json_real(123e9);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to create a real");
|
|
|
|
copy = json_deep_copy(value);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to deep copy a real");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == value)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying a real doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, value))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying a real produces an inequal copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value->refcount != 1 || copy->refcount != 1)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("invalid refcounts");
|
|
|
|
json_decref(value);
|
|
|
|
json_decref(copy);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_copy_array(void) {
|
2009-12-31 21:56:28 +08:00
|
|
|
const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
|
|
|
|
|
|
|
|
json_t *array, *copy;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t i;
|
2009-12-31 21:56:28 +08:00
|
|
|
|
2010-08-14 03:19:20 +08:00
|
|
|
array = json_loads(json_array_text, 0, NULL);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!array)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to parse an array");
|
|
|
|
|
|
|
|
copy = json_copy(array);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to copy an array");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == array)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an array doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, array))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an array produces an inequal copy");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
for (i = 0; i < json_array_size(copy); i++) {
|
|
|
|
if (json_array_get(array, i) != json_array_get(copy, i))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an array modifies its elements");
|
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(array);
|
|
|
|
json_decref(copy);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_deep_copy_array(void) {
|
2009-12-31 21:56:28 +08:00
|
|
|
const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]";
|
|
|
|
|
|
|
|
json_t *array, *copy;
|
2010-06-15 20:27:35 +08:00
|
|
|
size_t i;
|
2009-12-31 21:56:28 +08:00
|
|
|
|
2010-08-14 03:19:20 +08:00
|
|
|
array = json_loads(json_array_text, 0, NULL);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!array)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to parse an array");
|
|
|
|
|
|
|
|
copy = json_deep_copy(array);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to deep copy an array");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == array)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an array doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, array))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an array produces an inequal copy");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
for (i = 0; i < json_array_size(copy); i++) {
|
|
|
|
if (json_array_get(array, i) == json_array_get(copy, i))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an array doesn't copy its elements");
|
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(array);
|
|
|
|
json_decref(copy);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_copy_object(void) {
|
2009-12-31 21:56:28 +08:00
|
|
|
const char *json_object_text =
|
|
|
|
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
const char *keys[] = {"foo", "a", "b", "c"};
|
|
|
|
int i;
|
|
|
|
|
2009-12-31 21:56:28 +08:00
|
|
|
json_t *object, *copy;
|
|
|
|
void *iter;
|
|
|
|
|
2010-08-14 03:19:20 +08:00
|
|
|
object = json_loads(json_object_text, 0, NULL);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to parse an object");
|
|
|
|
|
|
|
|
copy = json_copy(object);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to copy an object");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == object)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an object doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, object))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("copying an object produces an inequal copy");
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
i = 0;
|
2009-12-31 21:56:28 +08:00
|
|
|
iter = json_object_iter(object);
|
2019-10-17 14:08:51 +08:00
|
|
|
while (iter) {
|
2009-12-31 21:56:28 +08:00
|
|
|
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);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value1 != value2)
|
2016-06-21 02:03:02 +08:00
|
|
|
fail("copying an object modifies its items");
|
|
|
|
|
|
|
|
if (strcmp(key, keys[i]) != 0)
|
|
|
|
fail("copying an object doesn't preserve key order");
|
2009-12-31 21:56:28 +08:00
|
|
|
|
|
|
|
iter = json_object_iter_next(object, iter);
|
2016-06-21 02:03:02 +08:00
|
|
|
i++;
|
2009-12-31 21:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
json_decref(copy);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_deep_copy_object(void) {
|
2009-12-31 21:56:28 +08:00
|
|
|
const char *json_object_text =
|
|
|
|
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}";
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
const char *keys[] = {"foo", "a", "b", "c"};
|
|
|
|
int i;
|
|
|
|
|
2009-12-31 21:56:28 +08:00
|
|
|
json_t *object, *copy;
|
|
|
|
void *iter;
|
|
|
|
|
2010-08-14 03:19:20 +08:00
|
|
|
object = json_loads(json_object_text, 0, NULL);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to parse an object");
|
|
|
|
|
|
|
|
copy = json_deep_copy(object);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("unable to deep copy an object");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy == object)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an object doesn't copy");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(copy, object))
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an object produces an inequal copy");
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
i = 0;
|
2009-12-31 21:56:28 +08:00
|
|
|
iter = json_object_iter(object);
|
2019-10-17 14:08:51 +08:00
|
|
|
while (iter) {
|
2009-12-31 21:56:28 +08:00
|
|
|
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);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value1 == value2)
|
2009-12-31 21:56:28 +08:00
|
|
|
fail("deep copying an object doesn't copy its items");
|
|
|
|
|
2016-06-21 02:03:02 +08:00
|
|
|
if (strcmp(key, keys[i]) != 0)
|
|
|
|
fail("deep copying an object doesn't preserve key order");
|
|
|
|
|
2009-12-31 21:56:28 +08:00
|
|
|
iter = json_object_iter_next(object, iter);
|
2016-06-21 02:03:02 +08:00
|
|
|
i++;
|
2009-12-31 21:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
json_decref(copy);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_deep_copy_circular_references(void) {
|
|
|
|
/* Construct a JSON object/array with a circular reference:
|
2019-08-12 11:17:25 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
object: {"a": {"b": {"c": <circular reference to $.a>}}}
|
|
|
|
array: [[[<circular reference to the $[0] array>]]]
|
2019-08-12 11:17:25 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
Deep copy it, remove the circular reference and deep copy again.
|
|
|
|
*/
|
2019-08-12 11:17:25 +08:00
|
|
|
|
|
|
|
json_t *json;
|
|
|
|
json_t *copy;
|
|
|
|
|
|
|
|
json = json_object();
|
|
|
|
json_object_set_new(json, "a", json_object());
|
|
|
|
json_object_set_new(json_object_get(json, "a"), "b", json_object());
|
|
|
|
json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c",
|
|
|
|
json_object_get(json, "a"));
|
|
|
|
|
|
|
|
copy = json_deep_copy(json);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy)
|
2019-08-12 11:17:25 +08:00
|
|
|
fail("json_deep_copy copied a circular reference!");
|
|
|
|
|
|
|
|
json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c");
|
|
|
|
|
|
|
|
copy = json_deep_copy(json);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2019-08-12 11:17:25 +08:00
|
|
|
fail("json_deep_copy failed!");
|
|
|
|
|
|
|
|
json_decref(copy);
|
|
|
|
json_decref(json);
|
|
|
|
|
|
|
|
json = json_array();
|
|
|
|
json_array_append_new(json, json_array());
|
|
|
|
json_array_append_new(json_array_get(json, 0), json_array());
|
|
|
|
json_array_append(json_array_get(json_array_get(json, 0), 0),
|
|
|
|
json_array_get(json, 0));
|
|
|
|
|
|
|
|
copy = json_deep_copy(json);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (copy)
|
2019-08-12 11:17:25 +08:00
|
|
|
fail("json_deep_copy copied a circular reference!");
|
|
|
|
|
|
|
|
json_array_remove(json_array_get(json_array_get(json, 0), 0), 0);
|
|
|
|
|
|
|
|
copy = json_deep_copy(json);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!copy)
|
2019-08-12 11:17:25 +08:00
|
|
|
fail("json_deep_copy failed!");
|
|
|
|
|
|
|
|
json_decref(copy);
|
|
|
|
json_decref(json);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void run_tests() {
|
2009-12-31 21:56:28 +08:00
|
|
|
test_copy_simple();
|
|
|
|
test_deep_copy_simple();
|
|
|
|
test_copy_array();
|
|
|
|
test_deep_copy_array();
|
|
|
|
test_copy_object();
|
|
|
|
test_deep_copy_object();
|
2019-08-12 11:17:25 +08:00
|
|
|
test_deep_copy_circular_references();
|
2009-12-31 21:56:28 +08:00
|
|
|
}
|