test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "buf.h"
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. static Buf *B;
  7. static FILE *in_f;
  8. static const char *in_p = "in.txt", *in_buf = "hello world";
  9. static FILE *out_f;
  10. static const char *out_p = "out.txt", *out_buf = "Hey buddy!";
  11. static FILE *tmp_f;
  12. static const char *tmp_p = "tmp.txt";
  13. static void setup();
  14. static void setdown();
  15. static void test_bufinit();
  16. static void test_bufidx();
  17. static void test_bufins();
  18. static void test_bufdel();
  19. static void test_bufins1();
  20. static void test_bufdel1();
  21. static void test_bufins2();
  22. static void test_bufout();
  23. int main()
  24. {
  25. printf("test: ");
  26. setup();
  27. signal(SIGABRT, setdown);
  28. test_bufinit();
  29. test_bufidx();
  30. test_bufins();
  31. test_bufdel();
  32. test_bufins1();
  33. test_bufdel1();
  34. test_bufins2();
  35. test_bufout();
  36. setdown(0);
  37. puts("success");
  38. }
  39. static void debugprint()
  40. {
  41. Piece *p = B->tail;
  42. printf("\nBuf: %p\n", (void *)B);
  43. printf("size: %d, index: %d\n", (int)B->size, (int)B->idx);
  44. do {
  45. printf("%p", (void *)p);
  46. if (p == B->tail) printf("<-tail");
  47. if (p == B->head) printf("<-head");
  48. if (p == B->pos) printf("<-pos");
  49. printf("\n\toff: %d, len %d, f: %s\n", (int)p->off, (int)p->len,
  50. (!p->f) ? "0" : (p->f == B->read) ? "read" : "append");
  51. } while ((p = p->next));
  52. printf("\n");
  53. }
  54. static void setup()
  55. {
  56. in_f = fopen(in_p, "w+b");
  57. if (ferror(in_f)) perror("fopen in");
  58. fputs(in_buf, in_f);
  59. out_f = fopen(out_p, "w+b");
  60. if (ferror(out_f)) perror("fopen out");
  61. tmp_f = fopen(tmp_p, "w+b");
  62. if (ferror(tmp_f)) perror("fopen tmp");
  63. }
  64. static void setdown(int n)
  65. {
  66. buffree(B);
  67. fclose(out_f);
  68. fclose(in_f);
  69. fclose(tmp_f);
  70. if (n != SIGABRT) {
  71. remove(in_p);
  72. remove(out_p);
  73. remove(tmp_p);
  74. }
  75. }
  76. static void test_bufinit()
  77. {
  78. Piece *p;
  79. B = bufinit(in_f, tmp_f);
  80. assert(B->read);
  81. assert(B->append);
  82. assert(B->pos == B->tail);
  83. assert(B->pos == B->head);
  84. assert((p = B->pos));
  85. assert(p->f == B->read);
  86. assert(p->off == 0);
  87. assert(p->len == strlen(in_buf));
  88. assert(p->prev == NULL);
  89. assert(p->next == NULL);
  90. }
  91. static void test_bufidx()
  92. {
  93. Piece *p; size_t idx = 5;
  94. p = bufidx(B, idx);
  95. assert(p == B->pos);
  96. assert(p == B->head);
  97. assert(p->prev == B->tail);
  98. assert(p->f == B->read);
  99. assert(p->off == idx);
  100. assert(p->len == strlen(in_buf) - idx);
  101. }
  102. static void test_bufins()
  103. {
  104. const char *buf = "y"; size_t idx = 2;
  105. const size_t len = strlen(buf);
  106. bufins(B, idx, buf);
  107. assert(B->size == strlen(in_buf) + len);
  108. assert(B->idx == idx + len);
  109. assert(B->pos == B->tail->next->next);
  110. assert(B->pos->f == B->read);
  111. assert(B->pos->off == 2);
  112. assert(B->pos->len == 3);
  113. assert(B->pos->prev == B->tail->next);
  114. assert(B->pos->next == B->head);
  115. }
  116. static void test_bufdel()
  117. {
  118. size_t idx = 3; int len = B->size+1;
  119. const size_t bsiz = B->size;
  120. bufdel(B, idx, len);
  121. assert(B->size == bsiz-(bsiz-idx));
  122. assert(B->idx == idx);
  123. assert(B->pos == B->head);
  124. assert(B->tail->next->next == B->pos);
  125. assert(B->pos->f == B->read);
  126. assert(B->pos->off == 11);
  127. assert(B->pos->len == 0);
  128. assert(B->pos->prev == B->tail->next);
  129. assert(B->pos->next == NULL);
  130. }
  131. static void test_bufins1()
  132. {
  133. size_t idx = 3; const char *buf = " buddy!";
  134. const size_t len = strlen(buf), bsiz = B->size;
  135. bufins(B, idx, buf);
  136. assert(B->size == bsiz + len);
  137. assert(B->idx == idx + len);
  138. assert(B->pos == B->head);
  139. assert(B->pos == B->tail->next->next->next);
  140. assert(B->pos->prev->f == B->append);
  141. assert(B->pos->prev->off == 1);
  142. assert(B->pos->prev->len == len);
  143. assert(B->pos->prev == B->tail->next->next);
  144. assert(B->pos->next == NULL);
  145. }
  146. static void test_bufdel1()
  147. {
  148. size_t idx = 0; int len = 1;
  149. const size_t bsiz = B->size;
  150. bufdel(B, idx, len);
  151. assert(B->size == bsiz - len);
  152. assert(B->idx == idx);
  153. assert(B->pos->prev == B->tail);
  154. assert(B->pos->prev->f == 0);
  155. assert(B->pos->prev->off == 0);
  156. assert(B->pos->prev->len == 0);
  157. assert(B->pos->prev->prev == 0);
  158. assert(B->pos->prev->next == B->pos);
  159. assert(B->pos->f == B->read);
  160. assert(B->pos->off == 1);
  161. assert(B->pos->len == (size_t)len);
  162. assert(B->pos->prev == B->tail);
  163. assert(B->pos->next->next->next == B->head);
  164. }
  165. static void test_bufins2()
  166. {
  167. const char *buf = "H"; const size_t idx = 0;
  168. size_t len = strlen(buf), bsiz = B->size;
  169. bufins(B, idx, buf);
  170. assert(B->size == bsiz + len);
  171. assert(B->idx == idx + len);
  172. assert(B->pos == B->tail->next->next);
  173. assert(B->pos->prev == B->tail->next);
  174. assert(B->pos->prev->f == B->append);
  175. assert(B->pos->prev->off == 8);
  176. assert(B->pos->prev->len == 1);
  177. assert(B->pos->prev->prev == B->tail);
  178. assert(B->pos->prev->next == B->pos);
  179. }
  180. static void test_bufout()
  181. {
  182. size_t n = 0;
  183. char buf[BUFSIZ] = {0};
  184. bufout(B, out_f);
  185. rewind(out_f);
  186. n = fread(buf, 1, BUFSIZ, out_f);
  187. assert(n > 0);
  188. assert(strcmp(buf, out_buf) == 0);
  189. }