testc_buf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include "faux/str.h"
  8. #include "faux/buf.h"
  9. #include "faux/testc_helpers.h"
  10. #define CHUNK 100
  11. int testc_faux_buf(void)
  12. {
  13. char *src_fn = NULL;
  14. char *dst_fn = NULL;
  15. ssize_t len = 0;
  16. char *rnd = NULL;
  17. char *dst = NULL;
  18. faux_buf_t *buf = NULL;
  19. // Prepare files
  20. len = CHUNK * 3 + 15;
  21. rnd = faux_testc_rnd_buf(len);
  22. src_fn = faux_testc_tmpfile_deploy(rnd, len);
  23. // Create buf
  24. buf = faux_buf_new(CHUNK);
  25. if (!buf) {
  26. fprintf(stderr, "faux_buf_new() error\n");
  27. return -1;
  28. }
  29. // Write to buffer
  30. if (faux_buf_write(buf, rnd, len - 5) != (len - 5)) {
  31. fprintf(stderr, "faux_buf_write() error\n");
  32. return -1;
  33. }
  34. if (faux_buf_write(buf, rnd + len - 5, 5) != 5) {
  35. fprintf(stderr, "faux_buf_write() the rest error\n");
  36. return -1;
  37. }
  38. // Buf length
  39. if (faux_buf_len(buf) != len) {
  40. fprintf(stderr, "faux_buf_len() error\n");
  41. return -1;
  42. }
  43. // Buf read
  44. dst = faux_malloc(len);
  45. if (!dst) {
  46. fprintf(stderr, "faux_malloc() error\n");
  47. return -1;
  48. }
  49. if (faux_buf_read(buf, dst, len) != len) {
  50. fprintf(stderr, "faux_buf_read() error\n");
  51. return -1;
  52. }
  53. dst_fn = faux_testc_tmpfile_deploy(dst, len);
  54. // Buf length == 0
  55. if (faux_buf_len(buf) != 0) {
  56. fprintf(stderr, "faux_buf_len() is not 0: error\n");
  57. return -1;
  58. }
  59. // Compare files
  60. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  61. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  62. dst_fn, src_fn);
  63. return -1;
  64. }
  65. faux_free(dst);
  66. faux_buf_free(buf);
  67. return 0;
  68. }
  69. int testc_faux_buf_boundaries(void)
  70. {
  71. char *src_fn = NULL;
  72. char *dst_fn = NULL;
  73. ssize_t len = 0;
  74. char *rnd = NULL;
  75. char *dst = NULL;
  76. faux_buf_t *buf = NULL;
  77. // Prepare files
  78. len = CHUNK * 3;
  79. rnd = faux_testc_rnd_buf(len);
  80. src_fn = faux_testc_tmpfile_deploy(rnd, len);
  81. // Create buf
  82. buf = faux_buf_new(CHUNK);
  83. if (!buf) {
  84. fprintf(stderr, "faux_buf_new() error\n");
  85. return -1;
  86. }
  87. // Write to buffer
  88. if (faux_buf_write(buf, rnd, len) != len) {
  89. fprintf(stderr, "faux_buf_write() error\n");
  90. return -1;
  91. }
  92. // Buf length
  93. if (faux_buf_len(buf) != len) {
  94. fprintf(stderr, "faux_buf_len() error\n");
  95. return -1;
  96. }
  97. // Buf read
  98. dst = faux_malloc(len);
  99. if (!dst) {
  100. fprintf(stderr, "faux_malloc() error\n");
  101. return -1;
  102. }
  103. if (faux_buf_read(buf, dst, len) != len) {
  104. fprintf(stderr, "faux_buf_read() error\n");
  105. return -1;
  106. }
  107. dst_fn = faux_testc_tmpfile_deploy(dst, len);
  108. // Buf length == 0
  109. if (faux_buf_len(buf) != 0) {
  110. fprintf(stderr, "faux_buf_len() is not 0: error\n");
  111. return -1;
  112. }
  113. // Compare files
  114. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  115. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  116. dst_fn, src_fn);
  117. return -1;
  118. }
  119. // Write to buffer anoter time
  120. if (faux_buf_write(buf, rnd, len) != len) {
  121. fprintf(stderr, "another faux_buf_write() error\n");
  122. return -1;
  123. }
  124. if (faux_buf_read(buf, dst, len) != len) {
  125. fprintf(stderr, "another faux_buf_read() error\n");
  126. return -1;
  127. }
  128. dst_fn = faux_testc_tmpfile_deploy(dst, len);
  129. // Compare files another time
  130. if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
  131. fprintf(stderr, "Destination file %s is not equal to source %s\n",
  132. dst_fn, src_fn);
  133. return -1;
  134. }
  135. faux_free(dst);
  136. faux_buf_free(buf);
  137. return 0;
  138. }