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_string_noalloc.c 1.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Tests if binary strings are supported.
  3. */
  4. #include "config.h"
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "json_object.h"
  10. #include "json_object_private.h"
  11. int main(void)
  12. {
  13. /* this test has a space after the null character. check that it's still included */
  14. char *str = strdup("This string should be longer than 32 characters");
  15. struct json_object *jso_str = NULL;
  16. int retval = 0;
  17. if (!str)
  18. {
  19. puts("FAIL: strdup() returned NULL");
  20. retval = 1;
  21. goto exit;
  22. }
  23. jso_str = json_object_new_string_noalloc(str);
  24. if (!jso_str)
  25. {
  26. puts("FAIL: json_object_new_string_noalloc(str) returned NULL");
  27. retval = 2;
  28. goto exit;
  29. }
  30. if (str != json_object_get_string(jso_str))
  31. {
  32. puts("FAIL: json_object_new_string_noalloc(str) did not own the buffer.");
  33. retval = 3;
  34. goto exit;
  35. }
  36. json_object_put(jso_str);
  37. str = strdup("this string is short");
  38. if (!str)
  39. {
  40. puts("FAIL: strdup() returned NULL");
  41. retval = 4;
  42. goto exit;
  43. }
  44. jso_str = json_object_new_string_noalloc(str);
  45. if (!jso_str)
  46. {
  47. puts("FAIL: json_object_new_string_noalloc(str) returned NULL");
  48. retval = 5;
  49. goto exit;
  50. }
  51. if (str == json_object_get_string(jso_str))
  52. {
  53. puts("FAIL: json_object_new_string_noalloc(str) owns the pointer instead using the internal buffer.");
  54. retval = 6;
  55. goto exit;
  56. }
  57. if (json_object_get_string(jso_str) != jso_str->o.c_string.str.data)
  58. {
  59. puts("FAIL: json_object_new_string_noalloc(str) does not use the internal buffer.");
  60. free(str);
  61. retval = 7;
  62. goto exit;
  63. }
  64. puts("PASS");
  65. exit:
  66. if (jso_str)
  67. json_object_put(jso_str);
  68. return retval;
  69. }