run-all.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <ccan/rbuf/rbuf.h>
  2. /* Include the C files directly. */
  3. #include <ccan/rbuf/rbuf.c>
  4. #include <ccan/tap/tap.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. int main(void)
  10. {
  11. struct rbuf in;
  12. char buf[4096];
  13. int i, size, fd = open("run-all-file", O_WRONLY|O_CREAT, 0600);
  14. /* This is how many tests you plan to run */
  15. plan_tests(8);
  16. /* Make sure we're bigger than a single buffer! */
  17. size = rbuf_good_size(fd)*2;
  18. for (i = 0; i * sizeof(buf) < size; i++) {
  19. memset(buf, 0x42 + i, sizeof(buf));
  20. write(fd, buf, sizeof(buf));
  21. }
  22. close(fd);
  23. ok1(rbuf_open(&in, "run-all-file", NULL, 0));
  24. /* Can't fill without realloc. */
  25. ok1(!rbuf_fill(&in, NULL));
  26. ok1(errno == ENOMEM);
  27. ok1(rbuf_fill(&in, realloc));
  28. /* But can't load in whole file. */
  29. ok1(!rbuf_fill_all(&in, NULL));
  30. ok1(errno == ENOMEM);
  31. ok1(rbuf_fill_all(&in, realloc));
  32. ok1(in.len == size);
  33. for (i = 0; i * sizeof(buf) < size; i++) {
  34. memset(buf, 0x42 + i, sizeof(buf));
  35. if (memcmp(buf, in.start, sizeof(buf)) != 0) {
  36. fail("Bad buffer contents");
  37. break;
  38. }
  39. rbuf_consume(&in, sizeof(buf));
  40. }
  41. free(in.buf);
  42. /* This exits depending on whether all tests passed */
  43. return exit_status();
  44. }