You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test4.c 1.9 kB

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