2
0

test-list.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "list.h"
  5. #include "utils.h"
  6. struct item {
  7. const char *name;
  8. struct list_head list;
  9. };
  10. #define OUT(fmt, ...) do { \
  11. fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
  12. } while (0);
  13. static void init_list(struct list_head *list)
  14. {
  15. const char *vals[] = {
  16. "zero", "one", "two", "three", "four", "five", "six",
  17. "seven", "eight", "nine", "ten", "eleven", "twelve"
  18. };
  19. OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
  20. OUT("list_add_tail: ");
  21. for (size_t i=0; i<ARRAY_SIZE(vals); i++) {
  22. struct item *e = malloc(sizeof(struct item));
  23. e->name = vals[i];
  24. list_add_tail(&e->list, list);
  25. fprintf(stdout, "%s ", vals[i]);
  26. }
  27. fprintf(stdout, "\n");
  28. OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
  29. }
  30. static void test_basics()
  31. {
  32. struct item *tmp;
  33. struct item *item;
  34. struct item *last;
  35. struct item *first;
  36. struct list_head test_list = LIST_HEAD_INIT(test_list);
  37. init_list(&test_list);
  38. first = list_first_entry(&test_list, struct item, list);
  39. last = list_last_entry(&test_list, struct item, list);
  40. OUT("first=%s last=%s\n", first->name, last->name);
  41. OUT("'zero' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
  42. OUT("'twelve' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
  43. OUT("removing 'twelve' and 'zero'\n");
  44. list_del(&first->list);
  45. list_del(&last->list);
  46. free(first);
  47. free(last);
  48. first = list_first_entry(&test_list, struct item, list);
  49. last = list_last_entry(&test_list, struct item, list);
  50. if (!first || !last)
  51. return;
  52. OUT("first=%s last=%s\n", first->name, last->name);
  53. OUT("'one' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
  54. OUT("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
  55. OUT("moving 'one' to the tail\n");
  56. list_move_tail(&first->list, &test_list);
  57. first = list_first_entry(&test_list, struct item, list);
  58. last = list_last_entry(&test_list, struct item, list);
  59. OUT("first=%s last=%s\n", first->name, last->name);
  60. OUT("'two' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
  61. OUT("'one' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
  62. OUT("list_for_each_entry: ");
  63. list_for_each_entry(item, &test_list, list) {
  64. fprintf(stdout, "%s ", item->name);
  65. }
  66. fprintf(stdout, "\n");
  67. OUT("list_for_each_entry_reverse: ");
  68. list_for_each_entry_reverse(item, &test_list, list) {
  69. fprintf(stdout, "%s ", item->name);
  70. }
  71. fprintf(stdout, "\n");
  72. OUT("delete all entries\n");
  73. list_for_each_entry_safe(item, tmp, &test_list, list) {
  74. list_del(&item->list);
  75. free(item);
  76. }
  77. OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
  78. }
  79. static void test_while_list_empty()
  80. {
  81. struct item *first;
  82. struct list_head test_list = LIST_HEAD_INIT(test_list);
  83. init_list(&test_list);
  84. OUT("delete all entries\n");
  85. while (!list_empty(&test_list)) {
  86. first = list_first_entry(&test_list, struct item, list);
  87. list_del(&first->list);
  88. free(first);
  89. }
  90. OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
  91. }
  92. int main()
  93. {
  94. test_basics();
  95. test_while_list_empty();
  96. return 0;
  97. }