Browse Source

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.
pull/620/head
Mehmet Akif TASOVA Björn Esser 5 years ago
parent
commit
0e43aab025
4 changed files with 84 additions and 0 deletions
  1. +1
    -0
      tests/CMakeLists.txt
  2. +81
    -0
      tests/test_string_noalloc.c
  3. +1
    -0
      tests/test_string_noalloc.expected
  4. +1
    -0
      tests/test_string_noalloc.test

+ 1
- 0
tests/CMakeLists.txt View File

@@ -32,6 +32,7 @@ foreach(TESTNAME
test_printbuf test_printbuf
test_set_serializer test_set_serializer
test_set_value test_set_value
test_string_noalloc
test_util_file test_util_file
test_visit test_visit
test_object_iterator) test_object_iterator)


+ 81
- 0
tests/test_string_noalloc.c View File

@@ -0,0 +1,81 @@
/*
* Tests if binary strings are supported.
*/

#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;
}

+ 1
- 0
tests/test_string_noalloc.expected View File

@@ -0,0 +1 @@
PASS

+ 1
- 0
tests/test_string_noalloc.test View File

@@ -0,0 +1 @@
test_basic.test

Loading…
Cancel
Save