test_miao.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * This is a test for miao in $package$
  3. */
  4. #include "si_test.h"
  5. #include "miao.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. struct foo {
  10. char * s;
  11. };
  12. struct foo_array miao_of_type(struct foo);
  13. int foo_compare(const void * v1, const void * v2)
  14. {
  15. const struct foo * f1, * f2;
  16. f1 = v1;
  17. f2 = v2;
  18. return strcmp(f1->s, f2->s);
  19. }
  20. int foo_each_ptr(size_t i, const struct foo * f, FILE * extra) {
  21. fprintf(extra, "%u => %s\n", (unsigned int)i, f->s);
  22. return 0;
  23. }
  24. int foo_each(size_t i, struct foo f, FILE * extra) {
  25. fprintf(extra, "%u => %s\n", (unsigned int)i, f.s);
  26. return 0;
  27. }
  28. struct foo * foo_each_ptr_with_result(size_t i, struct foo * f, char * extra) {
  29. printf("%u => %s\n", (unsigned int)i, f->s);
  30. return f;
  31. }
  32. int foo_check_ptr(const struct foo * f, char * extra) {
  33. return (strcmp(f->s, extra) == 0);
  34. }
  35. struct foo foo_each_with_result(size_t i, struct foo f, char * extra) {
  36. printf("%u => %s\n", (unsigned int)i, f.s);
  37. return f;
  38. }
  39. int foo_check(const struct foo f, char * extra) {
  40. return (strcmp(f.s, extra) == 0);
  41. }
  42. TEST_FUNC(miao) {
  43. struct foo f1, f2 , f3;
  44. struct foo * e1, * e2, *e3;
  45. struct foo_array a[1];
  46. struct foo_array d[1];
  47. miao_init(a);
  48. miao_init(d);
  49. e1 = miao_push_ptr(a);
  50. TEST_NOTNULL(e1);
  51. e1->s = "world";
  52. e1 = miao_push_ptr(a);
  53. TEST_NOTNULL(e1);
  54. e1->s = "hello";
  55. miao_qsort(a, foo_compare);
  56. TEST_INTEQ(2, miao_size(a));
  57. TEST_PTREQ(e1, miao_get_ptr(a, 1));
  58. TEST_PTREQ(e1, miao_unsafe_get_ptr(a, 1));
  59. TEST_NULL(miao_get_ptr(a, 77));
  60. TEST_ASSERT(miao_out_of_bounds(a, 2));
  61. TEST_ASSERT(miao_cap(a) >= 2);
  62. TEST_ASSERT(miao_size(a) == 2);
  63. TEST_NOTNULL(miao_resize(a, 64));
  64. TEST_ASSERT(miao_cap(a) >= 64);
  65. TEST_ASSERT(miao_size(a) == 2);
  66. e1 = miao_get_ptr(a, 1);
  67. TEST_STREQ("world", e1->s);
  68. TEST_PTREQ(e1, miao_unsafe_get_ptr(a, 1));
  69. TEST_NULL(miao_get_ptr(a, 77));
  70. TEST_ASSERT(miao_out_of_bounds(a, 77));
  71. TEST_INTEQ(sizeof(struct foo), miao_elsize(a));
  72. TEST_NOTNULL(miao_copy(d, a));
  73. TEST_MEMEQ(a->a, miao_size(a) * miao_elsize(a), d->a);
  74. miao_push_ptr(a)->s = "foo";
  75. miao_push_ptr(a)->s = "bar";
  76. TEST_NOTNULL(miao_qsort(a, foo_compare));
  77. miao_each(a, foo_each , stdout);
  78. miao_each_ptr(a, foo_each_ptr, stdout);
  79. miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
  80. TEST_STREQ("foo", f3.s);
  81. miao_each_ptr_with_result(a, foo_each_ptr_with_result, e3, foo_check_ptr, "foo");
  82. TEST_STREQ("foo", e3->s);
  83. f1.s = "hello";
  84. e3 = miao_bsearch(a, foo_compare, &f1);
  85. TEST_STREQ(f1.s, e3->s);
  86. e2 = miao_pop_ptr(a);
  87. TEST_STREQ("world", e2->s);
  88. miao_done(a);
  89. TEST_ZERO(miao_size(a));
  90. TEST_DONE();
  91. }
  92. TEST_FUNC(miao_delete) {
  93. struct foo f1, f2 , f3;
  94. struct foo * e1, * e2, *e3;
  95. struct foo_array a[1];
  96. struct foo_array d[1];
  97. miao_init(a);
  98. e1 = miao_push_ptr(a);
  99. e1->s = "0 zero";
  100. e1 = miao_push_ptr(a);
  101. e1->s = "1 one";
  102. e1 = miao_push_ptr(a);
  103. e1->s = "2 two";
  104. e1 = miao_push_ptr(a);
  105. e1->s = "3 three";
  106. e1 = miao_push_ptr(a);
  107. e1->s = "4 four";
  108. e1 = miao_push_ptr(a);
  109. e1->s = "5 five";
  110. miao_qsort(a, foo_compare);
  111. miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
  112. f1.s = "3 three";
  113. e1 = miao_bsearch(a, foo_compare, &f1);
  114. TEST_NOTNULL(e1);
  115. TEST_INTNEQ(0, miao_delete_entry(a, e1));
  116. TEST_INTEQ(5, miao_size(a));
  117. miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
  118. f1.s = "2 two";
  119. TEST_INTNEQ(0, miao_delete_bsearch(a, foo_compare, &f1));
  120. TEST_INTEQ(4, miao_size(a));
  121. miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
  122. miao_done(a);
  123. TEST_DONE();
  124. }
  125. TEST_FUNC(miao_push) {
  126. struct foo f1, f2 , f3;
  127. struct foo * e1, * e2, *e3;
  128. struct foo_array a[1];
  129. struct foo_array d[1];
  130. miao_init(a);
  131. TEST_NOTNULL(miao_grow(a, 1));
  132. f1.s = "0 zero";
  133. miao_unsafe_push(a, f1);
  134. f1.s = "2 two";
  135. miao_push(a, f1);
  136. f1.s = "1 one";
  137. miao_push(a, f1);
  138. miao_qsort(a, foo_compare);
  139. miao_each_with_result(a, foo_each_with_result, f3, foo_check, "foo");
  140. miao_done(a);
  141. TEST_DONE();
  142. }
  143. int main(void) {
  144. TEST_INIT();
  145. TEST_RUN(miao_push);
  146. TEST_RUN(miao_delete);
  147. TEST_RUN(miao);
  148. TEST_REPORT();
  149. }