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.

test_null.c 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Tests if binary strings are supported.
  3. */
  4. #ifdef NDEBUG
  5. #undef NDEBUG
  6. #endif
  7. #include "config.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "json_inttypes.h"
  11. #include "json_object.h"
  12. #include "json_tokener.h"
  13. int main(void)
  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. int strings_match = !strcmp(expected, json);
  21. int retval = 0;
  22. if (strings_match)
  23. {
  24. printf("JSON write result is correct: %s\n", json);
  25. puts("PASS");
  26. }
  27. else
  28. {
  29. puts("JSON write result doesn't match expected string");
  30. printf("expected string: ");
  31. puts(expected);
  32. printf("parsed string: ");
  33. puts(json);
  34. puts("FAIL");
  35. retval = 1;
  36. }
  37. json_object_put(string);
  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. printf("Re-parsed object string len=%d, chars=[", parsed_len);
  45. for (ii = 0; ii < parsed_len; ii++)
  46. {
  47. printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
  48. }
  49. puts("]");
  50. json_object_put(parsed_str);
  51. }
  52. else
  53. {
  54. puts("ERROR: failed to parse");
  55. }
  56. return retval;
  57. }