d017c7a13a3b9a4a04053d50fe56f7635430f595
[platform/upstream/json-c.git] / tests / test_null.c
1 /*
2 * Tests if binary strings are supported.
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include "config.h"
8
9 #include "json_inttypes.h"
10 #include "json_object.h"
11 #include "json_tokener.h"
12
13 int main()
14 {
15         // this test has a space after the null character. check that it's still included
16         const char *input = " \0 ";
17         const char *expected = "\" \\u0000 \"";
18         struct json_object *string = json_object_new_string_len(input, 3);
19         const char *json = json_object_to_json_string(string);
20
21         int strings_match =  !strcmp( expected, json);
22         int retval = 0;
23         if (strings_match)
24         {
25                 printf("JSON write result is correct: %s\n", json);
26                 printf("PASS\n");
27         } else {
28                 printf("JSON write result doesn't match expected string\n");
29                 printf("expected string: ");
30                 printf("%s\n", expected);
31                 printf("parsed string:   ");
32                 printf("%s\n", json);
33                 printf("FAIL\n");
34                 retval=1;
35         }
36         json_object_put(string);
37
38         struct json_object *parsed_str = json_tokener_parse(expected);
39         if (parsed_str)
40         {
41                 int parsed_len = json_object_get_string_len(parsed_str);
42                 const char *parsed_cstr = json_object_get_string(parsed_str);
43                 int ii;
44                 fprintf( stderr, "Re-parsed object string len=%d, chars=[", parsed_len);
45                 for (ii = 0; ii < parsed_len ; ii++)
46                 {
47                         fprintf( stderr, "%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
48                 }
49                 fprintf( stderr, "]\n");
50                 json_object_put(parsed_str);
51         }
52         else
53         {
54                 printf("ERROR: failed to parse\n");
55         }
56         return retval;
57 }