run.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <ccan/asprintf/asprintf.h>
  2. /* Include the C files directly. */
  3. /* Override vasprintf for testing. */
  4. #if HAVE_ASPRINTF
  5. #define vasprintf my_vasprintf
  6. static int my_vasprintf(char **strp, const char *fmt, va_list ap);
  7. #else
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #define vsnprintf my_vsnprintf
  11. static int my_vsnprintf(char *str, size_t size, const char *format, va_list ap);
  12. #endif
  13. #include <ccan/asprintf/asprintf.c>
  14. #include <ccan/tap/tap.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdbool.h>
  18. static bool fail;
  19. #if HAVE_ASPRINTF
  20. #undef vasprintf
  21. static int my_vasprintf(char **strp, const char *fmt, va_list ap)
  22. {
  23. if (fail) {
  24. /* Set strp to crap. */
  25. *strp = (char *)(long)1;
  26. return -1;
  27. }
  28. return vasprintf(strp, fmt, ap);
  29. }
  30. #else
  31. #undef vsnprintf
  32. static int my_vsnprintf(char *str, size_t size, const char *format, va_list ap)
  33. {
  34. if (fail) {
  35. return -1;
  36. }
  37. return vsnprintf(str, size, format, ap);
  38. }
  39. #endif
  40. int main(void)
  41. {
  42. char *p, nul = '\0';
  43. int ret;
  44. /* This is how many tests you plan to run */
  45. plan_tests(8);
  46. fail = false;
  47. p = afmt("Test %u%cafter-nul", 1, nul);
  48. ok1(p);
  49. ok1(strlen(p) == strlen("Test 1"));
  50. ok1(memcmp(p, "Test 1\0after-nul\0", 17) == 0);
  51. free(p);
  52. ret = asprintf(&p, "Test %u%cafter-nul", 1, nul);
  53. ok1(ret == 16);
  54. ok1(p);
  55. ok1(strlen(p) == strlen("Test 1"));
  56. ok1(memcmp(p, "Test 1\0after-nul\0", 17) == 0);
  57. free(p);
  58. fail = true;
  59. p = afmt("Test %u%cafter-nul", 1, nul);
  60. ok1(p == NULL);
  61. /* This exits depending on whether all tests passed */
  62. return exit_status();
  63. }