Imported Upstream version 0.17
[platform/upstream/json-c.git] / tests / test4.c
1 /*
2  * gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
3  */
4
5 #ifdef NDEBUG
6 #undef NDEBUG
7 #endif
8 #include "config.h"
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "json_inttypes.h"
15 #include "json_object.h"
16 #include "json_tokener.h"
17 #include "snprintf_compat.h"
18
19 void print_hex(const char *s)
20 {
21         const char *iter = s;
22         unsigned char ch;
23         while ((ch = *iter++) != 0)
24         {
25                 if (',' != ch)
26                         printf("%x ", ch);
27                 else
28                         printf(",");
29         }
30         putchar('\n');
31 }
32
33 static void test_lot_of_adds(void);
34 static void test_lot_of_adds(void)
35 {
36         int ii;
37         char key[50];
38         json_object *jobj = json_object_new_object();
39         assert(jobj != NULL);
40         for (ii = 0; ii < 500; ii++)
41         {
42                 snprintf(key, sizeof(key), "k%d", ii);
43                 json_object *iobj = json_object_new_int(ii);
44                 assert(iobj != NULL);
45                 if (json_object_object_add(jobj, key, iobj))
46                 {
47                         fprintf(stderr, "FAILED to add object #%d\n", ii);
48                         abort();
49                 }
50         }
51         printf("%s\n", json_object_to_json_string(jobj));
52         assert(json_object_object_length(jobj) == 500);
53         json_object_put(jobj);
54 }
55
56 int main(void)
57 {
58         const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
59         const char *expected =
60             "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
61         struct json_object *parse_result = json_tokener_parse(input);
62         const char *unjson = json_object_get_string(parse_result);
63
64         printf("input: %s\n", input);
65
66         int strings_match = !strcmp(expected, unjson);
67         int retval = 0;
68         if (strings_match)
69         {
70                 printf("JSON parse result is correct: %s\n", unjson);
71                 puts("PASS");
72         }
73         else
74         {
75                 printf("JSON parse result doesn't match expected string\n");
76                 printf("expected string bytes: ");
77                 print_hex(expected);
78                 printf("parsed string bytes:   ");
79                 print_hex(unjson);
80                 puts("FAIL");
81                 retval = 1;
82         }
83         json_object_put(parse_result);
84
85         test_lot_of_adds();
86
87         return retval;
88 }