list-test.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Copyright (C) 2017 University of North Carolina at Chapel Hill and
  2. Fortanix, Inc.
  3. This file is part of Graphene Library OS.
  4. Graphene Library OS is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License
  6. as published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. Graphene Library OS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Unit test for the new list implementation */
  15. #include "list.h"
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. DEFINE_LIST(simple);
  20. struct simple {
  21. int idx;
  22. LIST_TYPE(simple) list;
  23. };
  24. DEFINE_LISTP(simple);
  25. static LISTP_TYPE(simple) list_in_the_sky = LISTP_INIT;
  26. static LISTP_TYPE(simple) list_in_the_basement = LISTP_INIT;
  27. /* Use some static arrays to assert expected list contents */
  28. int sol1[7] = {1, 2, 3, 4, 5, 6, 7};
  29. int sol2[10] = {1, 2, 25, 3, 4, 45, 5, 6, 65, 7};
  30. int sol3[17] = {1, 2, 25, 3, 4, 45, 5, 6, 65, 7, 8, 9, 10, 11, 12, 13, 14};
  31. int sol4[20] = {1, 2, 25, 3, 4, 45, 5, 6, 65, 7, 8, 85, 9, 10, 105, 11, 12, 125, 13, 14};
  32. int sol5[7] = {7, 6, 5, 4, 3, 2, 1};
  33. int sol6[4] = {7, 5, 3, 1};
  34. int sol7[10] = {7, 5, 3, 1, 13, 12, 11, 10, 9, 8};
  35. int sol8[17] = {7, 5, 3, 1, 13, 12, 11, 10, 9, 8, 20, 19, 18, 17, 16, 15, 14};
  36. void print_list(LISTP_TYPE(simple)* listp) {
  37. struct simple* tmp;
  38. printf("Beginning of list\n");
  39. LISTP_FOR_EACH_ENTRY(tmp, listp, list) {
  40. printf("List element %d\n", tmp->idx);
  41. }
  42. printf("End of list\n\n");
  43. }
  44. void assert_list(LISTP_TYPE(simple)* listp, int len, int* array, int stop_early_ok) {
  45. int j = 0;
  46. struct simple* tmp;
  47. int stop_early = 0;
  48. CHECK_LIST_HEAD(struct simple*, listp, list);
  49. LISTP_FOR_EACH_ENTRY(tmp, listp, list) {
  50. if (j >= len) {
  51. stop_early = 1;
  52. break;
  53. }
  54. assert(tmp->idx == array[j]);
  55. j++;
  56. }
  57. assert(j >= len);
  58. if (!stop_early)
  59. assert(tmp == listp->first);
  60. else
  61. assert(stop_early_ok);
  62. }
  63. void print_list_reverse(LISTP_TYPE(simple)* listp) {
  64. struct simple* tmp;
  65. printf("Beginning of list\n");
  66. LISTP_FOR_EACH_ENTRY_REVERSE(tmp, listp, list) {
  67. printf("List element %d\n", tmp->idx);
  68. }
  69. printf("End of list\n\n");
  70. }
  71. int main() {
  72. int i;
  73. struct simple* tmp;
  74. struct simple* tmp2;
  75. struct simple* 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. }