opkg_active_list_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* opkg_active_list.c - the opkg package management system
  2. Tick Chen <tick@openmoko.com>
  3. Copyright (C) 2008 Openmoko
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include <stdlib.h>
  14. #include <libopkg/active_list.h>
  15. #include <active_list.h>
  16. #include <stdio.h>
  17. struct active_test {
  18. char *str;
  19. struct active_list list;
  20. };
  21. static struct active_test *active_test_new(char *str)
  22. {
  23. struct active_test *ans =
  24. (struct active_test *)calloc(1, sizeof(struct active_test));
  25. ans->str = str;
  26. active_list_init(&ans->list);
  27. return ans;
  28. }
  29. static void active_test_add(struct active_list *head, struct active_test *node)
  30. {
  31. active_list_add(head, &node->list);
  32. }
  33. /*
  34. .--A---B----C----D-----E----F
  35. | |__k---L
  36. | |_ N
  37. |__ G ---H ---I---J
  38. |_M |_O
  39. Then the sequence will be
  40. +: G M H I O J A B K N L C D E F
  41. -: F E D C L N K B A J O I H M G
  42. */
  43. static void make_list(struct active_list *head)
  44. {
  45. struct active_test *A = active_test_new("A");
  46. struct active_test *B = active_test_new("B");
  47. struct active_test *C = active_test_new("C");
  48. struct active_test *D = active_test_new("D");
  49. struct active_test *E = active_test_new("E");
  50. struct active_test *F = active_test_new("F");
  51. struct active_test *G = active_test_new("G");
  52. struct active_test *H = active_test_new("H");
  53. struct active_test *I = active_test_new("I");
  54. struct active_test *J = active_test_new("J");
  55. struct active_test *K = active_test_new("K");
  56. struct active_test *L = active_test_new("L");
  57. struct active_test *M = active_test_new("M");
  58. struct active_test *N = active_test_new("N");
  59. struct active_test *O = active_test_new("O");
  60. active_test_add(head, A);
  61. active_test_add(head, B);
  62. active_test_add(head, C);
  63. active_test_add(head, D);
  64. active_test_add(head, E);
  65. active_test_add(head, F);
  66. active_test_add(head, G);
  67. active_test_add(head, H);
  68. active_test_add(head, I);
  69. active_test_add(head, J);
  70. active_test_add(head, K);
  71. active_test_add(head, L);
  72. active_test_add(head, M);
  73. active_test_add(head, N);
  74. active_test_add(head, O);
  75. }
  76. static void show_list(struct active_list *head)
  77. {
  78. struct active_list *ptr;
  79. struct active_test *test;
  80. for (ptr = active_list_next(head, NULL); ptr;
  81. ptr = active_list_next(head, ptr)) {
  82. test = list_entry(ptr, struct active_test, list);
  83. printf("%s ", test->str);
  84. }
  85. printf("\n");
  86. }
  87. int main(void)
  88. {
  89. struct active_list head;
  90. struct active_list *ptr;
  91. struct active_test *test;
  92. active_list_init(&head);
  93. make_list(&head);
  94. printf("pos order: ");
  95. show_list(&head);
  96. /* for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
  97. test = list_entry(ptr, struct active_test, list);
  98. printf ("%s ",test->str);
  99. }*/
  100. printf("neg order: ");
  101. for (ptr = active_list_prev(&head, &head); ptr;
  102. ptr = active_list_prev(&head, ptr)) {
  103. test = list_entry(ptr, struct active_test, list);
  104. printf("%s ", test->str);
  105. }
  106. printf("after clear: ");
  107. active_list_clear(&head);
  108. for (ptr = active_list_next(&head, NULL); ptr;
  109. ptr = active_list_next(&head, ptr)) {
  110. test = list_entry(ptr, struct active_test, list);
  111. printf("%s ", test->str);
  112. }
  113. printf("\n");
  114. }