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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Tests if binary strings are supported.
  3. */
  4. #include "config.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "json_inttypes.h"
  8. #include "json_object.h"
  9. #include "json_tokener.h"
  10. int main(void)
  11. {
  12. /* this test has a space after the null character. check that it's still included */
  13. const char *input = " \0 ";
  14. const char *expected = "\" \\u0000 \"";
  15. struct json_object *string = json_object_new_string_len(input, 3);
  16. const char *json = json_object_to_json_string(string);
  17. int strings_match = !strcmp(expected, json);
  18. int retval = 0;
  19. if (strings_match)
  20. {
  21. printf("JSON write result is correct: %s\n", json);
  22. puts("PASS");
  23. }
  24. else
  25. {
  26. puts("JSON write result doesn't match expected string");
  27. printf("expected string: ");
  28. puts(expected);
  29. printf("parsed string: ");
  30. puts(json);
  31. puts("FAIL");
  32. retval = 1;
  33. }
  34. json_object_put(string);
  35. struct json_object *parsed_str = json_tokener_parse(expected);
  36. if (parsed_str)
  37. {
  38. int parsed_len = json_object_get_string_len(parsed_str);
  39. const char *parsed_cstr = json_object_get_string(parsed_str);
  40. int ii;
  41. printf("Re-parsed object string len=%d, chars=[", parsed_len);
  42. for (ii = 0; ii < parsed_len; ii++)
  43. {
  44. printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
  45. }
  46. puts("]");
  47. json_object_put(parsed_str);
  48. }
  49. else
  50. {
  51. puts("ERROR: failed to parse");
  52. }
  53. return retval;
  54. }