From 0e43aab025f4c941ab9c62cd6ed6957aa475ad6d Mon Sep 17 00:00:00 2001 From: Mehmet Akif TASOVA Date: Mon, 18 May 2020 17:17:20 +0200 Subject: [PATCH] test_string_noalloc: New test for json_object_new_string_noalloc(). This is a short test to make sure the json_object_new_string_noalloc() function works as expected. --- tests/CMakeLists.txt | 1 + tests/test_string_noalloc.c | 81 ++++++++++++++++++++++++++++++ tests/test_string_noalloc.expected | 1 + tests/test_string_noalloc.test | 1 + 4 files changed, 84 insertions(+) create mode 100644 tests/test_string_noalloc.c create mode 100644 tests/test_string_noalloc.expected create mode 120000 tests/test_string_noalloc.test diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a871573..25c00b8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,6 +32,7 @@ foreach(TESTNAME test_printbuf test_set_serializer test_set_value + test_string_noalloc test_util_file test_visit test_object_iterator) diff --git a/tests/test_string_noalloc.c b/tests/test_string_noalloc.c new file mode 100644 index 0000000..8049998 --- /dev/null +++ b/tests/test_string_noalloc.c @@ -0,0 +1,81 @@ +/* + * Tests if binary strings are supported. + */ + +#include "config.h" +#include +#include +#include +#include + +#include "json_object.h" +#include "json_object_private.h" + +int main(void) +{ + /* this test has a space after the null character. check that it's still included */ + char *str = strdup("This string should be longer than 32 characters"); + struct json_object *jso_str = NULL; + int retval = 0; + + if (!str) + { + puts("FAIL: strdup() returned NULL"); + retval = 1; + goto exit; + } + + jso_str = json_object_new_string_noalloc(str); + + if (!jso_str) + { + puts("FAIL: json_object_new_string_noalloc(str) returned NULL"); + retval = 2; + goto exit; + } + if (str != json_object_get_string(jso_str)) + { + puts("FAIL: json_object_new_string_noalloc(str) did not own the buffer."); + retval = 3; + goto exit; + } + + json_object_put(jso_str); + + str = strdup("this string is short"); + if (!str) + { + puts("FAIL: strdup() returned NULL"); + retval = 4; + goto exit; + } + + jso_str = json_object_new_string_noalloc(str); + + if (!jso_str) + { + puts("FAIL: json_object_new_string_noalloc(str) returned NULL"); + retval = 5; + goto exit; + } + if (str == json_object_get_string(jso_str)) + { + puts("FAIL: json_object_new_string_noalloc(str) owns the pointer instead using the internal buffer."); + retval = 6; + goto exit; + } + if (json_object_get_string(jso_str) != jso_str->o.c_string.str.data) + { + puts("FAIL: json_object_new_string_noalloc(str) does not use the internal buffer."); + free(str); + retval = 7; + goto exit; + } + + puts("PASS"); + +exit: + if (jso_str) + json_object_put(jso_str); + return retval; +} diff --git a/tests/test_string_noalloc.expected b/tests/test_string_noalloc.expected new file mode 100644 index 0000000..7ef22e9 --- /dev/null +++ b/tests/test_string_noalloc.expected @@ -0,0 +1 @@ +PASS diff --git a/tests/test_string_noalloc.test b/tests/test_string_noalloc.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_string_noalloc.test @@ -0,0 +1 @@ +test_basic.test \ No newline at end of file