run.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "list/list.h"
  2. #include "tap/tap.h"
  3. #include "list/list.c"
  4. struct parent {
  5. const char *name;
  6. struct list_head children;
  7. unsigned int num_children;
  8. };
  9. struct child {
  10. const char *name;
  11. struct list_node list;
  12. };
  13. static LIST_HEAD(static_list);
  14. int main(int argc, char *argv[])
  15. {
  16. struct parent parent;
  17. struct child c1, c2, c3, *c, *n;
  18. unsigned int i;
  19. plan_tests(41);
  20. /* Test LIST_HEAD, list_empty and check_list */
  21. ok1(list_empty(&static_list));
  22. ok1(list_check(&static_list, NULL));
  23. parent.num_children = 0;
  24. list_head_init(&parent.children);
  25. /* Test list_head_init */
  26. ok1(list_empty(&parent.children));
  27. ok1(list_check(&parent.children, NULL));
  28. c2.name = "c2";
  29. list_add(&parent.children, &c2.list);
  30. /* Test list_add and !list_empty. */
  31. ok1(!list_empty(&parent.children));
  32. ok1(c2.list.next == &parent.children.n);
  33. ok1(c2.list.prev == &parent.children.n);
  34. ok1(parent.children.n.next == &c2.list);
  35. ok1(parent.children.n.prev == &c2.list);
  36. /* Test list_check */
  37. ok1(list_check(&parent.children, NULL));
  38. c1.name = "c1";
  39. list_add(&parent.children, &c1.list);
  40. /* Test list_add and !list_empty. */
  41. ok1(!list_empty(&parent.children));
  42. ok1(c2.list.next == &parent.children.n);
  43. ok1(c2.list.prev == &c1.list);
  44. ok1(parent.children.n.next == &c1.list);
  45. ok1(parent.children.n.prev == &c2.list);
  46. ok1(c1.list.next == &c2.list);
  47. ok1(c1.list.prev == &parent.children.n);
  48. /* Test list_check */
  49. ok1(list_check(&parent.children, NULL));
  50. c3.name = "c3";
  51. list_add_tail(&parent.children, &c3.list);
  52. /* Test list_add_tail and !list_empty. */
  53. ok1(!list_empty(&parent.children));
  54. ok1(parent.children.n.next == &c1.list);
  55. ok1(parent.children.n.prev == &c3.list);
  56. ok1(c1.list.next == &c2.list);
  57. ok1(c1.list.prev == &parent.children.n);
  58. ok1(c2.list.next == &c3.list);
  59. ok1(c2.list.prev == &c1.list);
  60. ok1(c3.list.next == &parent.children.n);
  61. ok1(c3.list.prev == &c2.list);
  62. /* Test list_check */
  63. ok1(list_check(&parent.children, NULL));
  64. /* Test list_top */
  65. ok1(list_top(&parent.children, struct child, list) == &c1);
  66. /* Test list_for_each. */
  67. i = 0;
  68. list_for_each(&parent.children, c, list) {
  69. switch (i++) {
  70. case 0:
  71. ok1(c == &c1);
  72. break;
  73. case 1:
  74. ok1(c == &c2);
  75. break;
  76. case 2:
  77. ok1(c == &c3);
  78. break;
  79. }
  80. if (i > 2)
  81. break;
  82. }
  83. ok1(i == 3);
  84. /* Test list_for_each_safe and list_del. */
  85. i = 0;
  86. list_for_each_safe(&parent.children, c, n, list) {
  87. switch (i++) {
  88. case 0:
  89. ok1(c == &c1);
  90. break;
  91. case 1:
  92. ok1(c == &c2);
  93. break;
  94. case 2:
  95. ok1(c == &c3);
  96. break;
  97. }
  98. list_del(&c->list);
  99. ok1(list_check(&parent.children, NULL));
  100. if (i > 2)
  101. break;
  102. }
  103. ok1(i == 3);
  104. ok1(list_empty(&parent.children));
  105. return exit_status();
  106. }