list-test.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2017 University of North Carolina at Chapel Hill and
  4. Fortanix, Inc.
  5. This file is part of Graphene Library OS.
  6. Graphene Library OS is free software: you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public License
  8. as published by the Free Software Foundation, either version 3 of the
  9. License, or (at your option) any later version.
  10. Graphene Library OS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Unit test for the new list implementation */
  17. #include "list.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <assert.h>
  21. DEFINE_LIST(simple);
  22. struct simple {
  23. int idx;
  24. LIST_TYPE(simple) list;
  25. };
  26. DEFINE_LISTP(simple);
  27. static LISTP_TYPE(simple) list_in_the_sky = LISTP_INIT;
  28. static LISTP_TYPE(simple) list_in_the_basement = LISTP_INIT;
  29. /* Use some static arrays to assert expected list contents */
  30. int sol1[7] = {1, 2, 3, 4, 5, 6, 7};
  31. int sol2[10] = {1, 2, 25, 3, 4, 45, 5, 6, 65, 7};
  32. int sol3[17] = {1, 2, 25, 3, 4, 45, 5, 6, 65, 7, 8, 9, 10, 11, 12, 13, 14};
  33. int sol4[20] = {1, 2, 25, 3, 4, 45, 5, 6, 65, 7, 8, 85, 9, 10, 105, 11, 12, 125, 13, 14};
  34. int sol5[7] = {7, 6, 5, 4, 3, 2, 1};
  35. int sol6[4] = {7, 5, 3, 1};
  36. int sol7[10] = {7, 5, 3, 1, 13, 12, 11, 10, 9, 8};
  37. int sol8[17] = {7, 5, 3, 1, 13, 12, 11, 10, 9, 8, 20, 19, 18, 17, 16, 15, 14};
  38. void print_list(LISTP_TYPE(simple) *listp) {
  39. struct simple *tmp;
  40. printf("Beginning of list\n");
  41. listp_for_each_entry(tmp, listp, list) {
  42. printf("List element %d\n", tmp->idx);
  43. }
  44. printf("End of list\n\n");
  45. }
  46. void assert_list(LISTP_TYPE(simple) *listp, int len, int *array, int stop_early_ok) {
  47. int j = 0;
  48. struct simple *tmp;
  49. int stop_early = 0;
  50. check_list_head(struct simple *, listp, list);
  51. listp_for_each_entry(tmp, listp, list) {
  52. if (j >= len) {
  53. stop_early = 1;
  54. break;
  55. }
  56. assert(tmp->idx == array[j]);
  57. j++;
  58. }
  59. assert(j >= len);
  60. if (!stop_early)
  61. assert(tmp == listp->first);
  62. else
  63. assert(stop_early_ok);
  64. }
  65. void print_list_reverse(LISTP_TYPE(simple) *listp) {
  66. struct simple *tmp;
  67. printf("Beginning of list\n");
  68. listp_for_each_entry_reverse(tmp, listp, list) {
  69. printf("List element %d\n", tmp->idx);
  70. }
  71. printf("End of list\n\n");
  72. }
  73. int main() {
  74. int i;
  75. struct simple *tmp, *tmp2, *n;
  76. assert(listp_empty(&list_in_the_sky));
  77. /* Try printing an empty list */
  78. print_list(&list_in_the_sky);
  79. /* Test adding things to the listp */
  80. for (i = 0; i < 7; i++) {
  81. tmp = malloc(sizeof(struct simple));
  82. tmp->idx = 7 - i;
  83. INIT_LIST_HEAD(tmp, list);
  84. assert(list_empty(tmp, list));
  85. listp_add(tmp, &list_in_the_sky, list);
  86. assert(!list_empty(tmp, list));
  87. assert_list(&list_in_the_sky, i, &sol1[6-i], 1);
  88. }
  89. assert(!listp_empty(&list_in_the_sky));
  90. assert_list(&list_in_the_sky, 7, sol1, 0);
  91. /* Test list_add - i.e., adding things in the middle of the list*/
  92. listp_for_each_entry_safe(tmp, n, &list_in_the_sky, list) {
  93. if ((tmp->idx % 2) == 0) {
  94. tmp2 = malloc(sizeof(struct simple));
  95. tmp2->idx = (tmp->idx * 10) + 5;
  96. INIT_LIST_HEAD(tmp2, list);
  97. assert(list_empty(tmp2, list));
  98. list_add(tmp2, tmp, list);
  99. assert(!list_empty(tmp2, list));
  100. }
  101. }
  102. //print_list(&list_in_the_sky);
  103. //print_list_reverse(&list_in_the_sky);
  104. assert_list(&list_in_the_sky, 10, sol2, 0);
  105. /* Try adding some integers to the tail of the list */
  106. for (i = 0; i < 7; i++) {
  107. tmp = malloc(sizeof(struct simple));
  108. tmp->idx = 8 + i;
  109. INIT_LIST_HEAD(tmp, list);
  110. assert(list_empty(tmp, list));
  111. listp_add_tail(tmp, &list_in_the_sky, list);
  112. assert(!list_empty(tmp, list));
  113. }
  114. assert(!listp_empty(&list_in_the_sky));
  115. assert_list(&list_in_the_sky, 17, sol3, 0);
  116. /* Test list_add_tail by adding ints from end */
  117. listp_for_each_entry(tmp, &list_in_the_sky, list) {
  118. if (tmp->idx <= 7 || tmp->idx > 20)
  119. continue;
  120. if ((tmp->idx % 2) == 1) {
  121. tmp2 = malloc(sizeof(struct simple));
  122. tmp2->idx = ((tmp->idx - 1) * 10) + 5;
  123. INIT_LIST_HEAD(tmp2, list);
  124. assert(list_empty(tmp2, list));
  125. list_add_tail(tmp2, tmp, list);
  126. assert(!list_empty(tmp2, list));
  127. }
  128. }
  129. //print_list(&list_in_the_sky);
  130. //print_list_reverse(&list_in_the_sky);
  131. assert_list(&list_in_the_sky, 20, sol4, 0);
  132. printf("Deletion test starting\n\n");
  133. /* Test list deletion and safe iteration by destroying the list*/
  134. listp_for_each_entry_safe(tmp, n, &list_in_the_sky, list) {
  135. listp_del(tmp, &list_in_the_sky, list);
  136. free(tmp);
  137. //print_list(&list_in_the_sky);
  138. }
  139. assert(listp_empty(&list_in_the_sky));
  140. printf("Deletion test Ending\n\n");
  141. /* Rebuild the list */
  142. for (i = 0; i < 7; i++) {
  143. tmp = malloc(sizeof(struct simple));
  144. tmp->idx = 7 - i;
  145. INIT_LIST_HEAD(tmp, list);
  146. assert(list_empty(tmp, list));
  147. listp_add(tmp, &list_in_the_sky, list);
  148. assert(!list_empty(tmp, list));
  149. }
  150. assert(!listp_empty(&list_in_the_sky));
  151. printf("Deletion test 2 starting\n\n");
  152. /* Test listp_del_init by migrating to another list */
  153. listp_for_each_entry_safe(tmp, n, &list_in_the_sky, list) {
  154. listp_del(tmp, &list_in_the_sky, list);
  155. listp_add(tmp, &list_in_the_basement, list);
  156. //print_list(&list_in_the_sky);
  157. //print_list(&list_in_the_basement);
  158. }
  159. //print_list(&list_in_the_sky);
  160. //print_list(&list_in_the_basement);
  161. assert(listp_empty(&list_in_the_sky));
  162. assert_list(&list_in_the_basement, 7, sol5, 0);
  163. /* Test listp_first_entry, for funzies */
  164. assert(listp_first_entry(&list_in_the_basement, simple, list)->idx == 7);
  165. /*
  166. printf("List in the sky:\n");
  167. print_list(&list_in_the_sky);
  168. printf("\nList in the basement:\n");
  169. print_list(&list_in_the_basement);
  170. printf("\nfin\n");
  171. printf("\nList in the basement, but backward:\n");
  172. print_list_reverse(&list_in_the_basement);
  173. printf("\nfin\n");
  174. printf("\nList in the sky, but backward:\n");
  175. print_list_reverse(&list_in_the_sky);
  176. printf("\nfin\n");
  177. */
  178. printf("Deletion test 2 Ending\n\n");
  179. /* Test listp_for_each_entry_safe_continue; stop on 4
  180. * after deleting 6 and 4, break, and continue.
  181. * */
  182. listp_for_each_entry_safe(tmp, n, &list_in_the_basement, list) {
  183. if (0 == (tmp->idx % 2)) {
  184. int idx = tmp->idx;
  185. listp_del(tmp, &list_in_the_basement, list);
  186. // NB: The continue pointer needs to be valid (will probably work
  187. // by accident even if 4 isn't freed, so better to leak one node
  188. if (idx == 4)
  189. break;
  190. else
  191. free(tmp);
  192. }
  193. }
  194. //printf("Continuing\n");
  195. listp_for_each_entry_safe_continue(tmp, n, &list_in_the_basement, list) {
  196. if (0 == (tmp->idx % 2)) {
  197. listp_del(tmp, &list_in_the_basement, list);
  198. free(tmp);
  199. }
  200. }
  201. //print_list(&list_in_the_sky);
  202. //print_list(&list_in_the_basement);
  203. assert(listp_empty(&list_in_the_sky));
  204. assert_list(&list_in_the_basement, 4, sol6, 0);
  205. /* Test list_splice variants. Rebuild sky list again */
  206. /* Rebuild the list */
  207. for (i = 8; i < 14; i++) {
  208. tmp = malloc(sizeof(struct simple));
  209. tmp->idx = i;
  210. INIT_LIST_HEAD(tmp, list);
  211. assert(list_empty(tmp, list));
  212. listp_add(tmp, &list_in_the_sky, list);
  213. assert(!list_empty(tmp, list));
  214. }
  215. assert(!listp_empty(&list_in_the_sky));
  216. printf("Begin splice tests \n\n");
  217. /* Test listp splice */
  218. listp_splice_init(&list_in_the_basement, &list_in_the_sky, list, simple);
  219. assert(listp_empty(&list_in_the_basement));
  220. assert_list(&list_in_the_sky, 10, sol7, 0);
  221. listp_splice(&list_in_the_sky, &list_in_the_basement, list, simple);
  222. INIT_LISTP(&list_in_the_sky);
  223. assert(listp_empty(&list_in_the_sky));
  224. assert_list(&list_in_the_basement, 10, sol7, 0);
  225. /* Test splicing onto the tail */
  226. /* Rebuild the list */
  227. for (i = 14; i < 21; i++) {
  228. tmp = malloc(sizeof(struct simple));
  229. tmp->idx = i;
  230. INIT_LIST_HEAD(tmp, list);
  231. assert(list_empty(tmp, list));
  232. listp_add(tmp, &list_in_the_sky, list);
  233. assert(!list_empty(tmp, list));
  234. }
  235. assert(!listp_empty(&list_in_the_sky));
  236. listp_splice_tail(&list_in_the_sky, &list_in_the_basement, list, simple);
  237. INIT_LISTP(&list_in_the_sky);
  238. /*
  239. printf("\nList in the basement:\n");
  240. print_list(&list_in_the_basement);
  241. printf("\nfin\n");
  242. printf("\nList in the sky:\n");
  243. print_list(&list_in_the_sky);
  244. printf("\nfin\n");
  245. */
  246. printf("Before list move test \n\n");
  247. /* Test listp_move_tail */
  248. listp_for_each_entry_safe(tmp, n, &list_in_the_basement, list) {
  249. listp_move_tail(tmp, &list_in_the_sky, &list_in_the_basement, list);
  250. }
  251. assert(listp_empty(&list_in_the_basement));
  252. assert_list(&list_in_the_sky, 17, sol8, 0);
  253. printf("After list move test \n\n");
  254. /*
  255. printf("\nList in the basement:\n");
  256. print_list(&list_in_the_basement);
  257. printf("\nfin\n");
  258. printf("\nList in the sky:\n");
  259. print_list(&list_in_the_sky);
  260. printf("\nfin\n");
  261. */
  262. printf("All tests passed!\n");
  263. return 0;
  264. }