list-test.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <assert.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, *tmp2, *n;
  74. assert(LISTP_EMPTY(&list_in_the_sky));
  75. /* Try printing an empty list */
  76. print_list(&list_in_the_sky);
  77. /* Test adding things to the listp */
  78. for (i = 0; i < 7; i++) {
  79. tmp = malloc(sizeof(struct simple));
  80. tmp->idx = 7 - i;
  81. INIT_LIST_HEAD(tmp, list);
  82. assert(LIST_EMPTY(tmp, list));
  83. LISTP_ADD(tmp, &list_in_the_sky, list);
  84. assert(!LIST_EMPTY(tmp, list));
  85. assert_list(&list_in_the_sky, i, &sol1[6-i], 1);
  86. }
  87. assert(!LISTP_EMPTY(&list_in_the_sky));
  88. assert_list(&list_in_the_sky, 7, sol1, 0);
  89. /* Test LIST_ADD - i.e., adding things in the middle of the list*/
  90. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &list_in_the_sky, list) {
  91. if ((tmp->idx % 2) == 0) {
  92. tmp2 = malloc(sizeof(struct simple));
  93. tmp2->idx = (tmp->idx * 10) + 5;
  94. INIT_LIST_HEAD(tmp2, list);
  95. assert(LIST_EMPTY(tmp2, list));
  96. LIST_ADD(tmp2, tmp, list);
  97. assert(!LIST_EMPTY(tmp2, list));
  98. }
  99. }
  100. //print_list(&list_in_the_sky);
  101. //print_list_reverse(&list_in_the_sky);
  102. assert_list(&list_in_the_sky, 10, sol2, 0);
  103. /* Try adding some integers to the tail of the list */
  104. for (i = 0; i < 7; i++) {
  105. tmp = malloc(sizeof(struct simple));
  106. tmp->idx = 8 + i;
  107. INIT_LIST_HEAD(tmp, list);
  108. assert(LIST_EMPTY(tmp, list));
  109. LISTP_ADD_TAIL(tmp, &list_in_the_sky, list);
  110. assert(!LIST_EMPTY(tmp, list));
  111. }
  112. assert(!LISTP_EMPTY(&list_in_the_sky));
  113. assert_list(&list_in_the_sky, 17, sol3, 0);
  114. /* Test LIST_ADD_TAIL by adding ints from end */
  115. LISTP_FOR_EACH_ENTRY(tmp, &list_in_the_sky, list) {
  116. if (tmp->idx <= 7 || tmp->idx > 20)
  117. continue;
  118. if ((tmp->idx % 2) == 1) {
  119. tmp2 = malloc(sizeof(struct simple));
  120. tmp2->idx = ((tmp->idx - 1) * 10) + 5;
  121. INIT_LIST_HEAD(tmp2, list);
  122. assert(LIST_EMPTY(tmp2, list));
  123. LIST_ADD_TAIL(tmp2, tmp, list);
  124. assert(!LIST_EMPTY(tmp2, list));
  125. }
  126. }
  127. //print_list(&list_in_the_sky);
  128. //print_list_reverse(&list_in_the_sky);
  129. assert_list(&list_in_the_sky, 20, sol4, 0);
  130. printf("Deletion test starting\n\n");
  131. /* Test list deletion and safe iteration by destroying the list*/
  132. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &list_in_the_sky, list) {
  133. LISTP_DEL(tmp, &list_in_the_sky, list);
  134. free(tmp);
  135. //print_list(&list_in_the_sky);
  136. }
  137. assert(LISTP_EMPTY(&list_in_the_sky));
  138. printf("Deletion test Ending\n\n");
  139. /* Rebuild the list */
  140. for (i = 0; i < 7; i++) {
  141. tmp = malloc(sizeof(struct simple));
  142. tmp->idx = 7 - i;
  143. INIT_LIST_HEAD(tmp, list);
  144. assert(LIST_EMPTY(tmp, list));
  145. LISTP_ADD(tmp, &list_in_the_sky, list);
  146. assert(!LIST_EMPTY(tmp, list));
  147. }
  148. assert(!LISTP_EMPTY(&list_in_the_sky));
  149. printf("Deletion test 2 starting\n\n");
  150. /* Test LISTP_DEL_INIT by migrating to another list */
  151. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &list_in_the_sky, list) {
  152. LISTP_DEL(tmp, &list_in_the_sky, list);
  153. LISTP_ADD(tmp, &list_in_the_basement, list);
  154. //print_list(&list_in_the_sky);
  155. //print_list(&list_in_the_basement);
  156. }
  157. //print_list(&list_in_the_sky);
  158. //print_list(&list_in_the_basement);
  159. assert(LISTP_EMPTY(&list_in_the_sky));
  160. assert_list(&list_in_the_basement, 7, sol5, 0);
  161. /* Test LISTP_FIRST_ENTRY, for funzies */
  162. assert(LISTP_FIRST_ENTRY(&list_in_the_basement, simple, list)->idx == 7);
  163. /*
  164. printf("List in the sky:\n");
  165. print_list(&list_in_the_sky);
  166. printf("\nList in the basement:\n");
  167. print_list(&list_in_the_basement);
  168. printf("\nfin\n");
  169. printf("\nList in the basement, but backward:\n");
  170. print_list_reverse(&list_in_the_basement);
  171. printf("\nfin\n");
  172. printf("\nList in the sky, but backward:\n");
  173. print_list_reverse(&list_in_the_sky);
  174. printf("\nfin\n");
  175. */
  176. printf("Deletion test 2 Ending\n\n");
  177. /* Test LISTP_FOR_EACH_ENTRY_SAFE_CONTINUE; stop on 4
  178. * after deleting 6 and 4, break, and continue.
  179. * */
  180. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &list_in_the_basement, list) {
  181. if (0 == (tmp->idx % 2)) {
  182. int idx = tmp->idx;
  183. LISTP_DEL(tmp, &list_in_the_basement, list);
  184. // NB: The continue pointer needs to be valid (will probably work
  185. // by accident even if 4 isn't freed, so better to leak one node
  186. if (idx == 4)
  187. break;
  188. else
  189. free(tmp);
  190. }
  191. }
  192. //printf("Continuing\n");
  193. LISTP_FOR_EACH_ENTRY_SAFE_CONTINUE(tmp, n, &list_in_the_basement, list) {
  194. if (0 == (tmp->idx % 2)) {
  195. LISTP_DEL(tmp, &list_in_the_basement, list);
  196. free(tmp);
  197. }
  198. }
  199. //print_list(&list_in_the_sky);
  200. //print_list(&list_in_the_basement);
  201. assert(LISTP_EMPTY(&list_in_the_sky));
  202. assert_list(&list_in_the_basement, 4, sol6, 0);
  203. /* Test list_splice variants. Rebuild sky list again */
  204. /* Rebuild the list */
  205. for (i = 8; i < 14; i++) {
  206. tmp = malloc(sizeof(struct simple));
  207. tmp->idx = i;
  208. INIT_LIST_HEAD(tmp, list);
  209. assert(LIST_EMPTY(tmp, list));
  210. LISTP_ADD(tmp, &list_in_the_sky, list);
  211. assert(!LIST_EMPTY(tmp, list));
  212. }
  213. assert(!LISTP_EMPTY(&list_in_the_sky));
  214. printf("Begin splice tests \n\n");
  215. /* Test listp splice */
  216. LISTP_SPLICE_INIT(&list_in_the_basement, &list_in_the_sky, list, simple);
  217. assert(LISTP_EMPTY(&list_in_the_basement));
  218. assert_list(&list_in_the_sky, 10, sol7, 0);
  219. LISTP_SPLICE(&list_in_the_sky, &list_in_the_basement, list, simple);
  220. INIT_LISTP(&list_in_the_sky);
  221. assert(LISTP_EMPTY(&list_in_the_sky));
  222. assert_list(&list_in_the_basement, 10, sol7, 0);
  223. /* Test splicing onto the tail */
  224. /* Rebuild the list */
  225. for (i = 14; i < 21; i++) {
  226. tmp = malloc(sizeof(struct simple));
  227. tmp->idx = i;
  228. INIT_LIST_HEAD(tmp, list);
  229. assert(LIST_EMPTY(tmp, list));
  230. LISTP_ADD(tmp, &list_in_the_sky, list);
  231. assert(!LIST_EMPTY(tmp, list));
  232. }
  233. assert(!LISTP_EMPTY(&list_in_the_sky));
  234. LISTP_SPLICE_TAIL(&list_in_the_sky, &list_in_the_basement, list, simple);
  235. INIT_LISTP(&list_in_the_sky);
  236. /*
  237. printf("\nList in the basement:\n");
  238. print_list(&list_in_the_basement);
  239. printf("\nfin\n");
  240. printf("\nList in the sky:\n");
  241. print_list(&list_in_the_sky);
  242. printf("\nfin\n");
  243. */
  244. printf("Before list move test \n\n");
  245. /* Test LISTP_MOVE_TAIL */
  246. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &list_in_the_basement, list) {
  247. LISTP_MOVE_TAIL(tmp, &list_in_the_sky, &list_in_the_basement, list);
  248. }
  249. assert(LISTP_EMPTY(&list_in_the_basement));
  250. assert_list(&list_in_the_sky, 17, sol8, 0);
  251. printf("After list move test \n\n");
  252. /*
  253. printf("\nList in the basement:\n");
  254. print_list(&list_in_the_basement);
  255. printf("\nfin\n");
  256. printf("\nList in the sky:\n");
  257. print_list(&list_in_the_sky);
  258. printf("\nfin\n");
  259. */
  260. printf("All tests passed!\n");
  261. return 0;
  262. }