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 2.2 kB

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