test-list.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Doubly-linked list - test program
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/os.h"
  10. #include "utils/list.h"
  11. struct test {
  12. struct dl_list list;
  13. int value;
  14. };
  15. static void dump_list(struct dl_list *head)
  16. {
  17. struct test *t;
  18. printf("dump:");
  19. dl_list_for_each(t, head, struct test, list)
  20. printf(" %d", t->value);
  21. printf(" (len=%d%s)\n", dl_list_len(head),
  22. dl_list_empty(head) ? " empty" : "");
  23. }
  24. int main(int argc, char *argv[])
  25. {
  26. struct dl_list head;
  27. struct test *t, *tmp;
  28. int i;
  29. dl_list_init(&head);
  30. dump_list(&head);
  31. for (i = 0; i < 5; i++) {
  32. t = os_zalloc(sizeof(*t));
  33. if (t == NULL)
  34. return -1;
  35. t->value = i;
  36. dl_list_add(&head, &t->list);
  37. dump_list(&head);
  38. }
  39. for (i = 10; i > 5; i--) {
  40. t = os_zalloc(sizeof(*t));
  41. if (t == NULL)
  42. return -1;
  43. t->value = i;
  44. dl_list_add_tail(&head, &t->list);
  45. dump_list(&head);
  46. }
  47. i = 0;
  48. dl_list_for_each(t, &head, struct test, list)
  49. if (++i == 5)
  50. break;
  51. printf("move: %d\n", t->value);
  52. dl_list_del(&t->list);
  53. dl_list_add(&head, &t->list);
  54. dump_list(&head);
  55. dl_list_for_each_safe(t, tmp, &head, struct test, list) {
  56. printf("delete: %d\n", t->value);
  57. dl_list_del(&t->list);
  58. os_free(t);
  59. dump_list(&head);
  60. }
  61. return 0;
  62. }