2009-08-19 02:33:45 +08:00
|
|
|
/*
|
2016-09-18 19:17:03 +08:00
|
|
|
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
|
2009-08-19 02:33:45 +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.
|
|
|
|
*/
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
#include "util.h"
|
2009-08-19 02:33:45 +08:00
|
|
|
#include <jansson.h>
|
2009-10-11 03:54:05 +08:00
|
|
|
#include <string.h>
|
2009-08-19 02:33:45 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_clear() {
|
2009-10-12 01:42:43 +08:00
|
|
|
json_t *object, *ten;
|
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
ten = json_integer(10);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to create object");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!ten)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to create integer");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_set(object, "a", ten) || json_object_set(object, "b", ten) ||
|
|
|
|
json_object_set(object, "c", ten) || json_object_set(object, "d", ten) ||
|
|
|
|
json_object_set(object, "e", ten))
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 5)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size");
|
|
|
|
|
|
|
|
json_object_clear(object);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 0)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size after clear");
|
|
|
|
|
|
|
|
json_decref(ten);
|
|
|
|
json_decref(object);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_update() {
|
2009-10-12 01:42:43 +08:00
|
|
|
json_t *object, *other, *nine, *ten;
|
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
other = json_object();
|
|
|
|
|
|
|
|
nine = json_integer(9);
|
|
|
|
ten = json_integer(10);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object || !other)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to create object");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!nine || !ten)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to create integer");
|
|
|
|
|
|
|
|
/* update an empty object with an empty object */
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update(object, other))
|
2019-09-21 01:35:56 +08:00
|
|
|
fail("unable to update an empty object with an empty object");
|
2009-10-12 01:42:43 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 0)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size after update");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(other) != 0)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size for updater after update");
|
|
|
|
|
|
|
|
/* update an empty object with a nonempty object */
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(other, "a", ten) || json_object_set(other, "b", ten) ||
|
|
|
|
json_object_set(other, "c", ten) || json_object_set(other, "d", ten) ||
|
|
|
|
json_object_set(other, "e", ten))
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update(object, other))
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to update an empty object");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 5)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size after update");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_get(object, "a") != ten || json_object_get(object, "b") != ten ||
|
|
|
|
json_object_get(object, "c") != ten || json_object_get(object, "d") != ten ||
|
2019-10-17 14:08:51 +08:00
|
|
|
json_object_get(object, "e") != ten)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("update works incorrectly");
|
|
|
|
|
|
|
|
/* perform the same update again */
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update(object, other))
|
2013-04-27 02:33:47 +08:00
|
|
|
fail("unable to update a non-empty object");
|
2009-10-12 01:42:43 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 5)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size after update");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_get(object, "a") != ten || json_object_get(object, "b") != ten ||
|
|
|
|
json_object_get(object, "c") != ten || json_object_get(object, "d") != ten ||
|
2019-10-17 14:08:51 +08:00
|
|
|
json_object_get(object, "e") != ten)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("update works incorrectly");
|
|
|
|
|
|
|
|
/* update a nonempty object with a nonempty object with both old
|
|
|
|
and new keys */
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_clear(other))
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("clear failed");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_set(other, "a", nine) || json_object_set(other, "b", nine) ||
|
|
|
|
json_object_set(other, "f", nine) || json_object_set(other, "g", nine) ||
|
|
|
|
json_object_set(other, "h", nine))
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update(object, other))
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("unable to update a nonempty object");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 8)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("invalid size after update");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_get(object, "a") != nine || json_object_get(object, "b") != nine ||
|
|
|
|
json_object_get(object, "f") != nine || json_object_get(object, "g") != nine ||
|
2019-10-17 14:08:51 +08:00
|
|
|
json_object_get(object, "h") != nine)
|
2009-10-12 01:42:43 +08:00
|
|
|
fail("update works incorrectly");
|
|
|
|
|
2019-10-08 20:32:27 +08:00
|
|
|
/* update_new check */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_clear(object))
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("clear failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "a", ten) || json_object_set(object, "b", ten) ||
|
|
|
|
json_object_set(object, "c", ten) || json_object_set(object, "d", ten) ||
|
|
|
|
json_object_set(object, "e", ten))
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_new(
|
|
|
|
object, json_pack("{s:O, s:O, s:O}", "b", nine, "f", nine, "g", nine)))
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("unable to update_new a nonempty object");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 7)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("invalid size after update_new");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(object, "a") != ten || json_object_get(object, "b") != nine ||
|
|
|
|
json_object_get(object, "c") != ten || json_object_get(object, "d") != ten ||
|
|
|
|
json_object_get(object, "e") != ten || json_object_get(object, "f") != nine ||
|
|
|
|
json_object_get(object, "g") != nine)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("update_new works incorrectly");
|
|
|
|
|
2009-10-12 01:42:43 +08:00
|
|
|
json_decref(nine);
|
|
|
|
json_decref(ten);
|
|
|
|
json_decref(other);
|
|
|
|
json_decref(object);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_set_many_keys() {
|
2014-12-19 14:35:31 +08:00
|
|
|
json_t *object, *value;
|
|
|
|
const char *keys = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
char buf[2];
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
if (!object)
|
|
|
|
fail("unable to create object");
|
|
|
|
|
|
|
|
value = json_string("a");
|
|
|
|
if (!value)
|
|
|
|
fail("unable to create string");
|
|
|
|
|
|
|
|
buf[1] = '\0';
|
|
|
|
for (i = 0; i < strlen(keys); i++) {
|
|
|
|
buf[0] = keys[i];
|
|
|
|
if (json_object_set(object, buf, value))
|
|
|
|
fail("unable to set object key");
|
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
json_decref(value);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_conditional_updates() {
|
2012-01-25 03:03:36 +08:00
|
|
|
json_t *object, *other;
|
|
|
|
|
|
|
|
object = json_pack("{sisi}", "foo", 1, "bar", 2);
|
|
|
|
other = json_pack("{sisi}", "foo", 3, "baz", 4);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_existing(object, other))
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_existing failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 2)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_existing added new items");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "foo")) != 3)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_existing failed to update existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "bar")) != 2)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_existing updated wrong key");
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
|
2019-10-08 20:32:27 +08:00
|
|
|
/* json_object_update_existing_new check */
|
|
|
|
object = json_pack("{sisi}", "foo", 1, "bar", 2);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_existing_new(object, json_pack("{sisi}", "foo", 3, "baz", 4)))
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_existing_new failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 2)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_existing_new added new items");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "foo")) != 3)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_existing_new failed to update existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "bar")) != 2)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_existing_new updated wrong key");
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
|
2012-01-25 03:03:36 +08:00
|
|
|
object = json_pack("{sisi}", "foo", 1, "bar", 2);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_missing(object, other))
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_missing failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 3)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_missing didn't add new items");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "foo")) != 1)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_missing updated existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "bar")) != 2)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_missing updated wrong key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "baz")) != 4)
|
2012-01-25 03:03:36 +08:00
|
|
|
fail("json_object_update_missing didn't add new items");
|
|
|
|
|
2019-10-08 20:32:27 +08:00
|
|
|
json_decref(object);
|
|
|
|
|
|
|
|
/* json_object_update_missing_new check */
|
|
|
|
object = json_pack("{sisi}", "foo", 1, "bar", 2);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_missing_new(object, json_pack("{sisi}", "foo", 3, "baz", 4)))
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_missing_new failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 3)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_missing_new didn't add new items");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "foo")) != 1)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_missing_new updated existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "bar")) != 2)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_missing_new updated wrong key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "baz")) != 4)
|
2019-10-08 20:32:27 +08:00
|
|
|
fail("json_object_update_missing_new didn't add new items");
|
|
|
|
|
2012-01-25 03:03:36 +08:00
|
|
|
json_decref(object);
|
|
|
|
json_decref(other);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_recursive_updates() {
|
2019-10-12 15:36:05 +08:00
|
|
|
json_t *invalid, *object, *other, *barBefore, *barAfter;
|
2019-01-11 20:50:37 +08:00
|
|
|
|
|
|
|
invalid = json_integer(42);
|
|
|
|
|
|
|
|
object = json_pack("{sis{si}}", "foo", 1, "bar", "baz", 2);
|
|
|
|
other = json_pack("{sisisi}", "foo", 3, "bar", 4, "baz", 5);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_update_recursive(invalid, other))
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive accepted non-object argument");
|
|
|
|
|
|
|
|
json_decref(invalid);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_recursive(object, other))
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 3)
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("invalid size after update");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "foo")) != 3)
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive failed to update existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "bar")) != 4)
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive failed to overwrite object");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "baz")) != 5)
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive didn't add new item");
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
json_decref(other);
|
|
|
|
|
|
|
|
object = json_pack("{sis{si}}", "foo", 1, "bar", "baz", 2);
|
|
|
|
other = json_pack("{s{si}}", "bar", "baz", 3);
|
2019-10-12 15:36:05 +08:00
|
|
|
barBefore = json_object_get(object, "bar");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!barBefore)
|
2019-10-12 15:36:05 +08:00
|
|
|
fail("can't get bar object before json_object_update_recursive");
|
2019-01-11 20:50:37 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_recursive(object, other))
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 2)
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("invalid size after update");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_get(object, "foo"))
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive removed existing key");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_integer_value(json_object_get(json_object_get(object, "bar"), "baz")) != 3)
|
2019-01-11 20:50:37 +08:00
|
|
|
fail("json_object_update_recursive failed to update nested value");
|
|
|
|
|
2019-10-12 15:36:05 +08:00
|
|
|
barAfter = json_object_get(object, "bar");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!barAfter)
|
2019-10-12 15:36:05 +08:00
|
|
|
fail("can't get bar object after json_object_update_recursive");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (barBefore != barAfter)
|
2019-10-12 15:36:05 +08:00
|
|
|
fail("bar object reference changed after json_object_update_recursive");
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
json_decref(other);
|
|
|
|
|
|
|
|
/* check circular reference */
|
2019-10-14 17:32:24 +08:00
|
|
|
object = json_pack("{s{s{s{si}}}}", "foo", "bar", "baz", "xxx", 2);
|
2019-10-12 15:36:05 +08:00
|
|
|
other = json_pack("{s{s{si}}}", "foo", "bar", "baz", 2);
|
2019-10-20 01:35:28 +08:00
|
|
|
json_object_set(json_object_get(json_object_get(other, "foo"), "bar"), "baz",
|
|
|
|
json_object_get(other, "foo"));
|
2019-10-12 15:36:05 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_update_recursive(object, other))
|
2019-10-12 15:36:05 +08:00
|
|
|
fail("json_object_update_recursive update a circular reference!");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
json_object_set_new(json_object_get(json_object_get(other, "foo"), "bar"), "baz",
|
|
|
|
json_integer(1));
|
2019-10-12 15:36:05 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_update_recursive(object, other))
|
2019-10-12 15:36:05 +08:00
|
|
|
fail("json_object_update_recursive failed!");
|
|
|
|
|
2019-01-11 20:50:37 +08:00
|
|
|
json_decref(object);
|
|
|
|
json_decref(other);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_circular() {
|
2009-10-16 02:02:27 +08:00
|
|
|
json_t *object1, *object2;
|
|
|
|
|
|
|
|
object1 = json_object();
|
|
|
|
object2 = json_object();
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object1 || !object2)
|
2009-10-16 02:02:27 +08:00
|
|
|
fail("unable to create object");
|
|
|
|
|
|
|
|
/* the simple case is checked */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object1, "a", object1) == 0)
|
2009-10-16 02:02:27 +08:00
|
|
|
fail("able to set self");
|
|
|
|
|
|
|
|
/* create circular references */
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_set(object1, "a", object2) || json_object_set(object2, "a", object1))
|
2009-10-16 02:02:27 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
|
|
|
/* circularity is detected when dumping */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_dumps(object1, 0) != NULL)
|
2009-10-16 02:02:27 +08:00
|
|
|
fail("able to dump circulars");
|
|
|
|
|
|
|
|
/* decref twice to deal with the circular references */
|
|
|
|
json_decref(object1);
|
|
|
|
json_decref(object2);
|
|
|
|
json_decref(object1);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_set_nocheck() {
|
2009-12-21 20:00:40 +08:00
|
|
|
json_t *object, *string;
|
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
string = json_string("bar");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object)
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("unable to create object");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!string)
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("unable to create string");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set_nocheck(object, "foo", string))
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_set_nocheck failed");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(object, "foo") != string)
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_get after json_object_set_nocheck failed");
|
|
|
|
|
|
|
|
/* invalid UTF-8 in key */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set_nocheck(object, "a\xefz", string))
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_set_nocheck failed for invalid UTF-8");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(object, "a\xefz") != string)
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_get after json_object_set_nocheck failed");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set_new_nocheck(object, "bax", json_integer(123)))
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_set_new_nocheck failed");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "bax")) != 123)
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_get after json_object_set_new_nocheck failed");
|
|
|
|
|
|
|
|
/* invalid UTF-8 in key */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set_new_nocheck(object, "asdf\xfe", json_integer(321)))
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_set_new_nocheck failed for invalid UTF-8");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_integer_value(json_object_get(object, "asdf\xfe")) != 321)
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_object_get after json_object_set_new_nocheck failed");
|
|
|
|
|
|
|
|
json_decref(string);
|
|
|
|
json_decref(object);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_iterators() {
|
2010-01-29 03:04:21 +08:00
|
|
|
json_t *object, *foo, *bar, *baz;
|
|
|
|
void *iter;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter(NULL))
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("able to iterate over NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_next(NULL, NULL))
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("able to increment an iterator on a NULL object");
|
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
foo = json_string("foo");
|
|
|
|
bar = json_string("bar");
|
|
|
|
baz = json_string("baz");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object || !foo || !bar || !baz)
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("unable to create values");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_next(object, NULL))
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("able to increment a NULL iterator");
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_set(object, "a", foo) || json_object_set(object, "b", bar) ||
|
|
|
|
json_object_set(object, "c", baz))
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("unable to populate object");
|
|
|
|
|
|
|
|
iter = json_object_iter(object);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!iter)
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("unable to get iterator");
|
2016-06-21 02:03:02 +08:00
|
|
|
if (strcmp(json_object_iter_key(iter), "a") != 0)
|
|
|
|
fail("iterating doesn't yield keys in order");
|
|
|
|
if (json_object_iter_value(iter) != foo)
|
|
|
|
fail("iterating doesn't yield values in order");
|
2010-01-29 03:04:21 +08:00
|
|
|
|
|
|
|
iter = json_object_iter_next(object, iter);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!iter)
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("unable to increment iterator");
|
2016-06-21 02:03:02 +08:00
|
|
|
if (strcmp(json_object_iter_key(iter), "b") != 0)
|
|
|
|
fail("iterating doesn't yield keys in order");
|
|
|
|
if (json_object_iter_value(iter) != bar)
|
|
|
|
fail("iterating doesn't yield values in order");
|
2010-01-29 03:04:21 +08:00
|
|
|
|
|
|
|
iter = json_object_iter_next(object, iter);
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!iter)
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("unable to increment iterator");
|
2016-06-21 02:03:02 +08:00
|
|
|
if (strcmp(json_object_iter_key(iter), "c") != 0)
|
|
|
|
fail("iterating doesn't yield keys in order");
|
|
|
|
if (json_object_iter_value(iter) != baz)
|
|
|
|
fail("iterating doesn't yield values in order");
|
2010-01-29 03:04:21 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_next(object, iter) != NULL)
|
2010-01-29 03:04:21 +08:00
|
|
|
fail("able to iterate over the end");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_at(object, "foo"))
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("json_object_iter_at() succeeds for non-existent key");
|
|
|
|
|
|
|
|
iter = json_object_iter_at(object, "b");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!iter)
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("json_object_iter_at() fails for an existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (strcmp(json_object_iter_key(iter), "b"))
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("iterating failed: wrong key");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_value(iter) != bar)
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("iterating failed: wrong value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_set(object, iter, baz))
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("unable to set value at iterator");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (strcmp(json_object_iter_key(iter), "b"))
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("json_object_iter_key() fails after json_object_iter_set()");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_value(iter) != baz)
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("json_object_iter_value() fails after json_object_iter_set()");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(object, "b") != baz)
|
2010-02-02 03:01:24 +08:00
|
|
|
fail("json_object_get() fails after json_object_iter_set()");
|
|
|
|
|
2010-01-29 03:04:21 +08:00
|
|
|
json_decref(object);
|
|
|
|
json_decref(foo);
|
|
|
|
json_decref(bar);
|
|
|
|
json_decref(baz);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_misc() {
|
2009-08-19 02:33:45 +08:00
|
|
|
json_t *object, *string, *other_string, *value;
|
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
string = json_string("test");
|
|
|
|
other_string = json_string("other");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!object)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to create object");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!string || !other_string)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to create string");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(object, "a"))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("value for nonexisting key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "a", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_set(object, NULL, string))
|
2009-09-07 03:24:55 +08:00
|
|
|
fail("able to set NULL key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_del(object, "a"))
|
2014-12-19 14:35:31 +08:00
|
|
|
fail("unable to del the only key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "a", string))
|
2014-12-19 14:35:31 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_set(object, "a", NULL))
|
2009-09-07 03:24:55 +08:00
|
|
|
fail("able to set NULL value");
|
|
|
|
|
2009-08-19 02:33:45 +08:00
|
|
|
/* invalid UTF-8 in key */
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_set(object, "a\xefz", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("able to set invalid unicode key");
|
|
|
|
|
|
|
|
value = json_object_get(object, "a");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("no value for existing key");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != string)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("got different value than what was added");
|
|
|
|
|
2009-09-06 19:06:02 +08:00
|
|
|
/* "a", "lp" and "px" collide in a five-bucket hashtable */
|
2019-10-20 01:35:28 +08:00
|
|
|
if (json_object_set(object, "b", string) || json_object_set(object, "lp", string) ||
|
2019-10-17 14:08:51 +08:00
|
|
|
json_object_set(object, "px", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
|
|
|
value = json_object_get(object, "a");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("no value for existing key");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != string)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("got different value than what was added");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "a", other_string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to replace an existing key");
|
|
|
|
|
|
|
|
value = json_object_get(object, "a");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!value)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("no value for existing key");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (value != other_string)
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("got different value than what was set");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_del(object, "nonexisting"))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("able to delete a nonexisting key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_del(object, "px"))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to delete an existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_del(object, "a"))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to delete an existing key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_del(object, "lp"))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to delete an existing key");
|
|
|
|
|
2009-09-03 22:56:00 +08:00
|
|
|
/* add many keys to initiate rehashing */
|
2009-08-19 02:33:45 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "a", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "lp", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "px", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "c", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "d", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set(object, "e", string))
|
2009-08-19 02:33:45 +08:00
|
|
|
fail("unable to set value");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_set_new(object, "foo", json_integer(123)))
|
2009-09-07 03:24:55 +08:00
|
|
|
fail("unable to set new value");
|
|
|
|
|
2009-09-03 22:56:00 +08:00
|
|
|
value = json_object_get(object, "foo");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_is_integer(value) || json_integer_value(value) != 123)
|
2009-10-17 02:20:38 +08:00
|
|
|
fail("json_object_set_new works incorrectly");
|
2009-09-03 22:56:00 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_set_new(object, NULL, json_integer(432)))
|
2009-09-07 03:24:55 +08:00
|
|
|
fail("able to set_new NULL key");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_set_new(object, "foo", NULL))
|
2009-09-07 03:24:55 +08:00
|
|
|
fail("able to set_new NULL value");
|
|
|
|
|
2009-08-19 02:33:45 +08:00
|
|
|
json_decref(string);
|
|
|
|
json_decref(other_string);
|
|
|
|
json_decref(object);
|
2009-10-12 01:42:43 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_preserve_order() {
|
2010-02-10 03:29:33 +08:00
|
|
|
json_t *object;
|
|
|
|
char *result;
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
const char *expected = "{\"foobar\": 1, \"bazquux\": 6, \"lorem ipsum\": "
|
|
|
|
"3, \"sit amet\": 5, \"helicopter\": 7}";
|
2010-02-10 03:29:33 +08:00
|
|
|
|
|
|
|
object = json_object();
|
|
|
|
|
|
|
|
json_object_set_new(object, "foobar", json_integer(1));
|
|
|
|
json_object_set_new(object, "bazquux", json_integer(2));
|
|
|
|
json_object_set_new(object, "lorem ipsum", json_integer(3));
|
|
|
|
json_object_set_new(object, "dolor", json_integer(4));
|
|
|
|
json_object_set_new(object, "sit amet", json_integer(5));
|
|
|
|
|
|
|
|
/* changing a value should preserve the order */
|
|
|
|
json_object_set_new(object, "bazquux", json_integer(6));
|
|
|
|
|
|
|
|
/* deletion shouldn't change the order of others */
|
|
|
|
json_object_del(object, "dolor");
|
|
|
|
|
|
|
|
/* add a new item just to make sure */
|
|
|
|
json_object_set_new(object, "helicopter", json_integer(7));
|
|
|
|
|
|
|
|
result = json_dumps(object, JSON_PRESERVE_ORDER);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (strcmp(expected, result) != 0) {
|
2010-02-10 03:29:33 +08:00
|
|
|
fprintf(stderr, "%s != %s", expected, result);
|
|
|
|
fail("JSON_PRESERVE_ORDER doesn't work");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(result);
|
|
|
|
json_decref(object);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_object_foreach() {
|
2012-01-25 02:37:08 +08:00
|
|
|
const char *key;
|
|
|
|
json_t *object1, *object2, *value;
|
|
|
|
|
|
|
|
object1 = json_pack("{sisisi}", "foo", 1, "bar", 2, "baz", 3);
|
|
|
|
object2 = json_object();
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
json_object_foreach(object1, key, value) json_object_set(object2, key, value);
|
2012-01-25 02:37:08 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_equal(object1, object2))
|
2012-01-25 02:37:08 +08:00
|
|
|
fail("json_object_foreach failed to iterate all key-value pairs");
|
|
|
|
|
|
|
|
json_decref(object1);
|
|
|
|
json_decref(object2);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_object_foreach_safe() {
|
2015-08-26 22:32:55 +08:00
|
|
|
const char *key;
|
|
|
|
void *tmp;
|
|
|
|
json_t *object, *value;
|
|
|
|
|
|
|
|
object = json_pack("{sisisi}", "foo", 1, "bar", 2, "baz", 3);
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
json_object_foreach_safe(object, tmp, key, value) { json_object_del(object, key); }
|
2015-08-26 22:32:55 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(object) != 0)
|
2015-08-26 22:32:55 +08:00
|
|
|
fail("json_object_foreach_safe failed to iterate all key-value pairs");
|
|
|
|
|
|
|
|
json_decref(object);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_bad_args(void) {
|
2018-02-13 17:35:37 +08:00
|
|
|
json_t *obj = json_object();
|
|
|
|
json_t *num = json_integer(1);
|
|
|
|
void *iter;
|
|
|
|
|
|
|
|
if (!obj || !num)
|
|
|
|
fail("failed to allocate test objects");
|
|
|
|
|
|
|
|
if (json_object_set(obj, "testkey", json_null()))
|
|
|
|
fail("failed to set testkey on object");
|
|
|
|
|
|
|
|
iter = json_object_iter(obj);
|
|
|
|
if (!iter)
|
|
|
|
fail("failed to retrieve test iterator");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(NULL) != 0)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_size with non-object argument returned non-zero");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_size(num) != 0)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_size with non-object argument returned non-zero");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(NULL, "test") != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_get with non-object argument returned non-NULL");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(num, "test") != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_get with non-object argument returned non-NULL");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_get(obj, NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_get with NULL key returned non-NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_set_new_nocheck(NULL, "test", json_null()))
|
|
|
|
fail("json_object_set_new_nocheck with non-object argument did not "
|
|
|
|
"return error");
|
|
|
|
if (!json_object_set_new_nocheck(num, "test", json_null()))
|
|
|
|
fail("json_object_set_new_nocheck with non-object argument did not "
|
|
|
|
"return error");
|
|
|
|
if (!json_object_set_new_nocheck(obj, "test", json_incref(obj)))
|
|
|
|
fail("json_object_set_new_nocheck with object == value did not return "
|
|
|
|
"error");
|
|
|
|
if (!json_object_set_new_nocheck(obj, NULL, json_object()))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_set_new_nocheck with NULL key did not return error");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_del(NULL, "test"))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_del with non-object argument did not return error");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_del(num, "test"))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_del with non-object argument did not return error");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_del(obj, NULL))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_del with NULL key did not return error");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_clear(NULL))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_clear with non-object argument did not return error");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_clear(num))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_clear with non-object argument did not return error");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_update(NULL, obj))
|
|
|
|
fail("json_object_update with non-object first argument did not return "
|
|
|
|
"error");
|
|
|
|
if (!json_object_update(num, obj))
|
|
|
|
fail("json_object_update with non-object first argument did not return "
|
|
|
|
"error");
|
|
|
|
if (!json_object_update(obj, NULL))
|
|
|
|
fail("json_object_update with non-object second argument did not "
|
|
|
|
"return error");
|
|
|
|
if (!json_object_update(obj, num))
|
|
|
|
fail("json_object_update with non-object second argument did not "
|
|
|
|
"return error");
|
|
|
|
|
|
|
|
if (!json_object_update_existing(NULL, obj))
|
|
|
|
fail("json_object_update_existing with non-object first argument did "
|
|
|
|
"not return error");
|
|
|
|
if (!json_object_update_existing(num, obj))
|
|
|
|
fail("json_object_update_existing with non-object first argument did "
|
|
|
|
"not return error");
|
|
|
|
if (!json_object_update_existing(obj, NULL))
|
|
|
|
fail("json_object_update_existing with non-object second argument did "
|
|
|
|
"not return error");
|
|
|
|
if (!json_object_update_existing(obj, num))
|
|
|
|
fail("json_object_update_existing with non-object second argument did "
|
|
|
|
"not return error");
|
|
|
|
|
|
|
|
if (!json_object_update_missing(NULL, obj))
|
|
|
|
fail("json_object_update_missing with non-object first argument did "
|
|
|
|
"not return error");
|
|
|
|
if (!json_object_update_missing(num, obj))
|
|
|
|
fail("json_object_update_missing with non-object first argument did "
|
|
|
|
"not return error");
|
|
|
|
if (!json_object_update_missing(obj, NULL))
|
|
|
|
fail("json_object_update_missing with non-object second argument did "
|
|
|
|
"not return error");
|
|
|
|
if (!json_object_update_missing(obj, num))
|
|
|
|
fail("json_object_update_missing with non-object second argument did "
|
|
|
|
"not return error");
|
|
|
|
|
|
|
|
if (json_object_iter(NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter with non-object argument returned non-NULL");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter(num) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter with non-object argument returned non-NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_at(NULL, "test") != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_at with non-object argument returned non-NULL");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_at(num, "test") != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_at with non-object argument returned non-NULL");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_at(obj, NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_at with NULL iter returned non-NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_next(obj, NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_next with NULL iter returned non-NULL");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_next(num, iter) != NULL)
|
2019-10-20 01:35:28 +08:00
|
|
|
fail("json_object_iter_next with non-object argument returned non-NULL");
|
2018-02-13 17:35:37 +08:00
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_key(NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_key with NULL iter returned non-NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_key_to_iter(NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_key_to_iter with NULL iter returned non-NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (json_object_iter_value(NULL) != NULL)
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_value with NULL iter returned non-NULL");
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_iter_set_new(NULL, iter, json_incref(num)))
|
|
|
|
fail("json_object_iter_set_new with non-object argument did not return "
|
|
|
|
"error");
|
|
|
|
if (!json_object_iter_set_new(num, iter, json_incref(num)))
|
|
|
|
fail("json_object_iter_set_new with non-object argument did not return "
|
|
|
|
"error");
|
|
|
|
if (!json_object_iter_set_new(obj, NULL, json_incref(num)))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_set_new with NULL iter did not return error");
|
2019-10-17 14:08:51 +08:00
|
|
|
if (!json_object_iter_set_new(obj, iter, NULL))
|
2018-02-13 17:35:37 +08:00
|
|
|
fail("json_object_iter_set_new with NULL value did not return error");
|
|
|
|
|
|
|
|
if (obj->refcount != 1)
|
|
|
|
fail("unexpected reference count for obj");
|
|
|
|
|
|
|
|
if (num->refcount != 1)
|
|
|
|
fail("unexpected reference count for num");
|
|
|
|
|
|
|
|
json_decref(obj);
|
|
|
|
json_decref(num);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void run_tests() {
|
2009-10-12 01:42:43 +08:00
|
|
|
test_misc();
|
|
|
|
test_clear();
|
|
|
|
test_update();
|
2014-12-19 14:35:31 +08:00
|
|
|
test_set_many_keys();
|
2012-01-25 03:03:36 +08:00
|
|
|
test_conditional_updates();
|
2019-01-11 20:50:37 +08:00
|
|
|
test_recursive_updates();
|
2009-10-16 02:02:27 +08:00
|
|
|
test_circular();
|
2009-12-21 20:00:40 +08:00
|
|
|
test_set_nocheck();
|
2010-01-29 03:04:21 +08:00
|
|
|
test_iterators();
|
2010-02-10 03:29:33 +08:00
|
|
|
test_preserve_order();
|
2013-05-13 03:34:26 +08:00
|
|
|
test_object_foreach();
|
2015-08-26 22:32:55 +08:00
|
|
|
test_object_foreach_safe();
|
2018-02-13 17:35:37 +08:00
|
|
|
test_bad_args();
|
2009-08-19 02:33:45 +08:00
|
|
|
}
|