test_miao.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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", 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", 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", 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", 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. int main(void) {
  93. TEST_INIT();
  94. TEST_RUN(miao);
  95. TEST_REPORT();
  96. }