jansson/test/suites/api/test_simple.c

288 lines
8.8 KiB
C
Raw Normal View History

/*
2016-09-18 19:17:03 +08:00
* 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 "util.h"
2019-10-17 14:08:51 +08:00
#include <jansson.h>
#include <string.h>
2019-10-17 14:08:51 +08:00
static void test_bad_args(void) {
json_t *num = json_integer(1);
json_t *txt = json_string("test");
if (!num || !txt)
fail("failed to allocate test objects");
2019-10-17 14:08:51 +08:00
if (json_string_nocheck(NULL) != NULL)
fail("json_string_nocheck with NULL argument did not return NULL");
2019-10-17 14:08:51 +08:00
if (json_stringn_nocheck(NULL, 0) != NULL)
fail("json_stringn_nocheck with NULL argument did not return NULL");
2019-10-17 14:08:51 +08:00
if (json_string(NULL) != NULL)
fail("json_string with NULL argument did not return NULL");
2019-10-17 14:08:51 +08:00
if (json_stringn(NULL, 0) != NULL)
fail("json_stringn with NULL argument did not return NULL");
2019-10-17 14:08:51 +08:00
if (json_string_length(NULL) != 0)
fail("json_string_length with non-string argument did not return 0");
2019-10-17 14:08:51 +08:00
if (json_string_length(num) != 0)
fail("json_string_length with non-string argument did not return 0");
2019-10-17 14:08:51 +08:00
if (json_string_value(NULL) != NULL)
fail("json_string_value with non-string argument did not return NULL");
2019-10-17 14:08:51 +08:00
if (json_string_value(num) != NULL)
fail("json_string_value with non-string argument did not return NULL");
2019-10-17 14:08:51 +08:00
if (!json_string_setn_nocheck(NULL, "", 0))
fail("json_string_setn with non-string argument did not return error");
2019-10-17 14:08:51 +08:00
if (!json_string_setn_nocheck(num, "", 0))
fail("json_string_setn with non-string argument did not return error");
2019-10-17 14:08:51 +08:00
if (!json_string_setn_nocheck(txt, NULL, 0))
fail("json_string_setn_nocheck with NULL value did not return error");
2019-10-17 14:08:51 +08:00
if (!json_string_set_nocheck(txt, NULL))
fail("json_string_set_nocheck with NULL value did not return error");
2019-10-17 14:08:51 +08:00
if (!json_string_set(txt, NULL))
fail("json_string_set with NULL value did not return error");
2019-10-17 14:08:51 +08:00
if (!json_string_setn(txt, NULL, 0))
fail("json_string_setn with NULL value did not return error");
2019-10-17 14:08:51 +08:00
if (num->refcount != 1)
fail("unexpected reference count for num");
2019-10-17 14:08:51 +08:00
if (txt->refcount != 1)
fail("unexpected reference count for txt");
json_decref(num);
json_decref(txt);
}
/* Call the simple functions not covered by other tests of the public API */
2019-10-17 14:08:51 +08:00
static void run_tests() {
json_t *value;
value = json_boolean(1);
2019-10-17 14:08:51 +08:00
if (!json_is_true(value))
fail("json_boolean(1) failed");
json_decref(value);
value = json_boolean(-123);
2019-10-17 14:08:51 +08:00
if (!json_is_true(value))
fail("json_boolean(-123) failed");
json_decref(value);
value = json_boolean(0);
2019-10-17 14:08:51 +08:00
if (!json_is_false(value))
fail("json_boolean(0) failed");
2019-10-17 14:08:51 +08:00
if (json_boolean_value(value) != 0)
fail("json_boolean_value failed");
json_decref(value);
value = json_integer(1);
2019-10-17 14:08:51 +08:00
if (json_typeof(value) != JSON_INTEGER)
fail("json_typeof failed");
2019-10-17 14:08:51 +08:00
if (json_is_object(value))
fail("json_is_object failed");
2019-10-17 14:08:51 +08:00
if (json_is_array(value))
fail("json_is_array failed");
2019-10-17 14:08:51 +08:00
if (json_is_string(value))
fail("json_is_string failed");
2019-10-17 14:08:51 +08:00
if (!json_is_integer(value))
fail("json_is_integer failed");
2019-10-17 14:08:51 +08:00
if (json_is_real(value))
fail("json_is_real failed");
2019-10-17 14:08:51 +08:00
if (!json_is_number(value))
fail("json_is_number failed");
2019-10-17 14:08:51 +08:00
if (json_is_true(value))
fail("json_is_true failed");
2019-10-17 14:08:51 +08:00
if (json_is_false(value))
fail("json_is_false failed");
2019-10-17 14:08:51 +08:00
if (json_is_boolean(value))
fail("json_is_boolean failed");
2019-10-17 14:08:51 +08:00
if (json_is_null(value))
fail("json_is_null failed");
json_decref(value);
value = json_string("foo");
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_string failed");
2019-10-17 14:08:51 +08:00
if (strcmp(json_string_value(value), "foo"))
fail("invalid string value");
if (json_string_length(value) != 3)
fail("invalid string length");
2019-10-17 14:08:51 +08:00
if (json_string_set(value, "barr"))
fail("json_string_set failed");
2019-10-17 14:08:51 +08:00
if (strcmp(json_string_value(value), "barr"))
fail("invalid string value");
if (json_string_length(value) != 4)
fail("invalid string length");
2019-10-17 14:08:51 +08:00
if (json_string_setn(value, "hi\0ho", 5))
fail("json_string_set failed");
2019-10-17 14:08:51 +08:00
if (memcmp(json_string_value(value), "hi\0ho\0", 6))
fail("invalid string value");
if (json_string_length(value) != 5)
fail("invalid string length");
json_decref(value);
value = json_string(NULL);
2019-10-17 14:08:51 +08:00
if (value)
fail("json_string(NULL) failed");
/* invalid UTF-8 */
value = json_string("a\xefz");
2019-10-17 14:08:51 +08:00
if (value)
fail("json_string(<invalid utf-8>) failed");
value = json_string_nocheck("foo");
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_string_nocheck failed");
2019-10-17 14:08:51 +08:00
if (strcmp(json_string_value(value), "foo"))
fail("invalid string value");
if (json_string_length(value) != 3)
fail("invalid string length");
2019-10-17 14:08:51 +08:00
if (json_string_set_nocheck(value, "barr"))
fail("json_string_set_nocheck failed");
2019-10-17 14:08:51 +08:00
if (strcmp(json_string_value(value), "barr"))
fail("invalid string value");
if (json_string_length(value) != 4)
fail("invalid string length");
2019-10-17 14:08:51 +08:00
if (json_string_setn_nocheck(value, "hi\0ho", 5))
fail("json_string_set failed");
2019-10-17 14:08:51 +08:00
if (memcmp(json_string_value(value), "hi\0ho\0", 6))
fail("invalid string value");
if (json_string_length(value) != 5)
fail("invalid string length");
json_decref(value);
/* invalid UTF-8 */
value = json_string_nocheck("qu\xff");
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_string_nocheck failed");
2019-10-17 14:08:51 +08:00
if (strcmp(json_string_value(value), "qu\xff"))
fail("invalid string value");
if (json_string_length(value) != 3)
fail("invalid string length");
2019-10-17 14:08:51 +08:00
if (json_string_set_nocheck(value, "\xfd\xfe\xff"))
fail("json_string_set_nocheck failed");
2019-10-17 14:08:51 +08:00
if (strcmp(json_string_value(value), "\xfd\xfe\xff"))
fail("invalid string value");
if (json_string_length(value) != 3)
fail("invalid string length");
json_decref(value);
value = json_integer(123);
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_integer failed");
2019-10-17 14:08:51 +08:00
if (json_integer_value(value) != 123)
fail("invalid integer value");
2019-10-17 14:08:51 +08:00
if (json_number_value(value) != 123.0)
fail("invalid number value");
2019-10-17 14:08:51 +08:00
if (json_integer_set(value, 321))
fail("json_integer_set failed");
2019-10-17 14:08:51 +08:00
if (json_integer_value(value) != 321)
fail("invalid integer value");
2019-10-17 14:08:51 +08:00
if (json_number_value(value) != 321.0)
fail("invalid number value");
json_decref(value);
value = json_real(123.123);
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_real failed");
2019-10-17 14:08:51 +08:00
if (json_real_value(value) != 123.123)
fail("invalid integer value");
2019-10-17 14:08:51 +08:00
if (json_number_value(value) != 123.123)
fail("invalid number value");
2019-10-17 14:08:51 +08:00
if (json_real_set(value, 321.321))
fail("json_real_set failed");
2019-10-17 14:08:51 +08:00
if (json_real_value(value) != 321.321)
fail("invalid real value");
2019-10-17 14:08:51 +08:00
if (json_number_value(value) != 321.321)
fail("invalid number value");
json_decref(value);
value = json_true();
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_true failed");
json_decref(value);
value = json_false();
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_false failed");
json_decref(value);
value = json_null();
2019-10-17 14:08:51 +08:00
if (!value)
fail("json_null failed");
json_decref(value);
/* Test reference counting on singletons (true, false, null) */
value = json_true();
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting true works incorrectly");
json_decref(value);
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting true works incorrectly");
json_incref(value);
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting true works incorrectly");
value = json_false();
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting false works incorrectly");
json_decref(value);
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting false works incorrectly");
json_incref(value);
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting false works incorrectly");
value = json_null();
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting null works incorrectly");
json_decref(value);
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting null works incorrectly");
json_incref(value);
2019-10-17 14:08:51 +08:00
if (value->refcount != (size_t)-1)
fail("refcounting null works incorrectly");
#ifdef json_auto_t
value = json_string("foo");
{
json_auto_t *test = json_incref(value);
/* Use test so GCC doesn't complain it is unused. */
2019-10-17 14:08:51 +08:00
if (!json_is_string(test))
fail("value type check failed");
}
2019-10-17 14:08:51 +08:00
if (value->refcount != 1)
fail("automatic decrement failed");
json_decref(value);
#endif
test_bad_args();
}