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