2009-09-06 19:06:02 +08:00
|
|
|
/*
|
2016-09-18 19:17:03 +08:00
|
|
|
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
|
2009-09-06 19:06:02 +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 <string.h>
|
|
|
|
#include <jansson.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
/* Call the simple functions not covered by other tests of the public API */
|
2011-10-03 02:27:53 +08:00
|
|
|
static void run_tests()
|
2009-09-06 19:06:02 +08:00
|
|
|
{
|
|
|
|
json_t *value;
|
|
|
|
|
2012-07-30 12:20:36 +08:00
|
|
|
value = json_boolean(1);
|
|
|
|
if(!json_is_true(value))
|
|
|
|
fail("json_boolean(1) failed");
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
value = json_boolean(-123);
|
|
|
|
if(!json_is_true(value))
|
|
|
|
fail("json_boolean(-123) failed");
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
value = json_boolean(0);
|
|
|
|
if(!json_is_false(value))
|
|
|
|
fail("json_boolean(0) failed");
|
2013-12-04 15:14:17 +08:00
|
|
|
if(json_boolean_value(value) != 0)
|
|
|
|
fail("json_boolean_value failed");
|
2012-07-30 12:20:36 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
|
2009-09-06 19:06:02 +08:00
|
|
|
value = json_integer(1);
|
|
|
|
if(json_typeof(value) != JSON_INTEGER)
|
|
|
|
fail("json_typeof failed");
|
|
|
|
|
|
|
|
if(json_is_object(value))
|
|
|
|
fail("json_is_object failed");
|
|
|
|
|
|
|
|
if(json_is_array(value))
|
|
|
|
fail("json_is_array failed");
|
|
|
|
|
|
|
|
if(json_is_string(value))
|
|
|
|
fail("json_is_string failed");
|
|
|
|
|
|
|
|
if(!json_is_integer(value))
|
|
|
|
fail("json_is_integer failed");
|
|
|
|
|
|
|
|
if(json_is_real(value))
|
|
|
|
fail("json_is_real failed");
|
|
|
|
|
|
|
|
if(!json_is_number(value))
|
|
|
|
fail("json_is_number failed");
|
|
|
|
|
|
|
|
if(json_is_true(value))
|
|
|
|
fail("json_is_true failed");
|
|
|
|
|
|
|
|
if(json_is_false(value))
|
|
|
|
fail("json_is_false failed");
|
|
|
|
|
|
|
|
if(json_is_boolean(value))
|
|
|
|
fail("json_is_boolean failed");
|
|
|
|
|
|
|
|
if(json_is_null(value))
|
|
|
|
fail("json_is_null failed");
|
|
|
|
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
|
|
|
|
value = json_string("foo");
|
|
|
|
if(!value)
|
|
|
|
fail("json_string failed");
|
|
|
|
if(strcmp(json_string_value(value), "foo"))
|
|
|
|
fail("invalid string value");
|
2013-10-04 10:07:50 +08:00
|
|
|
if (json_string_length(value) != 3)
|
|
|
|
fail("invalid string length");
|
2009-10-14 03:51:04 +08:00
|
|
|
|
2013-10-04 10:07:50 +08:00
|
|
|
if(json_string_set(value, "barr"))
|
2009-10-14 03:51:04 +08:00
|
|
|
fail("json_string_set failed");
|
2013-10-04 10:07:50 +08:00
|
|
|
if(strcmp(json_string_value(value), "barr"))
|
2009-10-14 03:51:04 +08:00
|
|
|
fail("invalid string value");
|
2013-10-04 10:07:50 +08:00
|
|
|
if (json_string_length(value) != 4)
|
|
|
|
fail("invalid string length");
|
|
|
|
|
|
|
|
if(json_string_setn(value, "hi\0ho", 5))
|
|
|
|
fail("json_string_set failed");
|
|
|
|
if(memcmp(json_string_value(value), "hi\0ho\0", 6))
|
|
|
|
fail("invalid string value");
|
|
|
|
if (json_string_length(value) != 5)
|
|
|
|
fail("invalid string length");
|
2009-10-14 03:51:04 +08:00
|
|
|
|
2009-09-06 19:06:02 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
2009-09-07 03:24:55 +08:00
|
|
|
value = json_string(NULL);
|
|
|
|
if(value)
|
|
|
|
fail("json_string(NULL) failed");
|
|
|
|
|
2009-10-14 03:51:04 +08:00
|
|
|
/* invalid UTF-8 */
|
|
|
|
value = json_string("a\xefz");
|
|
|
|
if(value)
|
|
|
|
fail("json_string(<invalid utf-8>) failed");
|
|
|
|
|
2009-12-21 20:00:40 +08:00
|
|
|
value = json_string_nocheck("foo");
|
|
|
|
if(!value)
|
|
|
|
fail("json_string_nocheck failed");
|
|
|
|
if(strcmp(json_string_value(value), "foo"))
|
|
|
|
fail("invalid string value");
|
2013-10-04 10:07:50 +08:00
|
|
|
if (json_string_length(value) != 3)
|
|
|
|
fail("invalid string length");
|
2009-12-21 20:00:40 +08:00
|
|
|
|
2013-10-04 10:07:50 +08:00
|
|
|
if(json_string_set_nocheck(value, "barr"))
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("json_string_set_nocheck failed");
|
2013-10-04 10:07:50 +08:00
|
|
|
if(strcmp(json_string_value(value), "barr"))
|
|
|
|
fail("invalid string value");
|
|
|
|
if (json_string_length(value) != 4)
|
|
|
|
fail("invalid string length");
|
|
|
|
|
|
|
|
if(json_string_setn_nocheck(value, "hi\0ho", 5))
|
|
|
|
fail("json_string_set failed");
|
|
|
|
if(memcmp(json_string_value(value), "hi\0ho\0", 6))
|
2009-12-21 20:00:40 +08:00
|
|
|
fail("invalid string value");
|
2013-10-04 10:07:50 +08:00
|
|
|
if (json_string_length(value) != 5)
|
|
|
|
fail("invalid string length");
|
2009-12-21 20:00:40 +08:00
|
|
|
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
/* invalid UTF-8 */
|
|
|
|
value = json_string_nocheck("qu\xff");
|
|
|
|
if(!value)
|
|
|
|
fail("json_string_nocheck failed");
|
|
|
|
if(strcmp(json_string_value(value), "qu\xff"))
|
|
|
|
fail("invalid string value");
|
2013-10-04 10:07:50 +08:00
|
|
|
if (json_string_length(value) != 3)
|
|
|
|
fail("invalid string length");
|
2009-12-21 20:00:40 +08:00
|
|
|
|
|
|
|
if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
|
|
|
|
fail("json_string_set_nocheck failed");
|
|
|
|
if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
|
|
|
|
fail("invalid string value");
|
2013-10-04 10:07:50 +08:00
|
|
|
if (json_string_length(value) != 3)
|
|
|
|
fail("invalid string length");
|
2009-12-21 20:00:40 +08:00
|
|
|
|
|
|
|
json_decref(value);
|
|
|
|
|
2009-10-14 03:51:04 +08:00
|
|
|
|
2009-09-06 19:06:02 +08:00
|
|
|
value = json_integer(123);
|
|
|
|
if(!value)
|
|
|
|
fail("json_integer failed");
|
|
|
|
if(json_integer_value(value) != 123)
|
|
|
|
fail("invalid integer value");
|
|
|
|
if(json_number_value(value) != 123.0)
|
|
|
|
fail("invalid number value");
|
2009-10-14 03:51:04 +08:00
|
|
|
|
|
|
|
if(json_integer_set(value, 321))
|
|
|
|
fail("json_integer_set failed");
|
|
|
|
if(json_integer_value(value) != 321)
|
|
|
|
fail("invalid integer value");
|
|
|
|
if(json_number_value(value) != 321.0)
|
|
|
|
fail("invalid number value");
|
|
|
|
|
2009-09-06 19:06:02 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
value = json_real(123.123);
|
|
|
|
if(!value)
|
|
|
|
fail("json_real failed");
|
|
|
|
if(json_real_value(value) != 123.123)
|
|
|
|
fail("invalid integer value");
|
|
|
|
if(json_number_value(value) != 123.123)
|
|
|
|
fail("invalid number value");
|
2009-10-14 03:51:04 +08:00
|
|
|
|
|
|
|
if(json_real_set(value, 321.321))
|
|
|
|
fail("json_real_set failed");
|
|
|
|
if(json_real_value(value) != 321.321)
|
|
|
|
fail("invalid real value");
|
|
|
|
if(json_number_value(value) != 321.321)
|
|
|
|
fail("invalid number value");
|
|
|
|
|
2009-09-06 19:06:02 +08:00
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
value = json_true();
|
|
|
|
if(!value)
|
|
|
|
fail("json_true failed");
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
value = json_false();
|
|
|
|
if(!value)
|
|
|
|
fail("json_false failed");
|
|
|
|
json_decref(value);
|
|
|
|
|
|
|
|
value = json_null();
|
|
|
|
if(!value)
|
|
|
|
fail("json_null failed");
|
|
|
|
json_decref(value);
|
|
|
|
|
2010-03-23 14:08:57 +08:00
|
|
|
/* Test reference counting on singletons (true, false, null) */
|
|
|
|
value = json_true();
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting true works incorrectly");
|
|
|
|
json_decref(value);
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting true works incorrectly");
|
|
|
|
json_incref(value);
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting true works incorrectly");
|
|
|
|
|
|
|
|
value = json_false();
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting false works incorrectly");
|
|
|
|
json_decref(value);
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting false works incorrectly");
|
|
|
|
json_incref(value);
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting false works incorrectly");
|
|
|
|
|
|
|
|
value = json_null();
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting null works incorrectly");
|
|
|
|
json_decref(value);
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting null works incorrectly");
|
|
|
|
json_incref(value);
|
2010-06-15 20:27:35 +08:00
|
|
|
if(value->refcount != (size_t)-1)
|
2010-03-23 14:08:57 +08:00
|
|
|
fail("refcounting null works incorrectly");
|
2016-08-31 04:12:22 +08:00
|
|
|
|
|
|
|
#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. */
|
|
|
|
if(!json_is_string(test))
|
|
|
|
fail("value type check failed");
|
|
|
|
}
|
|
|
|
if(value->refcount != 1)
|
|
|
|
fail("automatic decrement failed");
|
|
|
|
json_decref(value);
|
|
|
|
#endif
|
2009-09-06 19:06:02 +08:00
|
|
|
}
|