Add/update tests.
[eruntime.git] / tests / test-list.c
bloba0ef80df305b52db66988187247350cab5f1cb1b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "eruntime/list.h"
7 static int t_list_init (void);
8 static int t_list_create (void);
9 static int t_list_append (void);
10 static int t_list_prepend (void);
11 static int t_list_apply (void);
13 typedef int (*test_case_func) (void);
15 typedef struct _test_case_struct
17 const char *name;
18 const char *desc;
19 test_case_func func;
21 } test_case_t;
23 /* {{{ static test_case_t list_tests[] = { ... } */
24 static test_case_t list_tests[] =
27 "list_init",
28 "function list_init()",
29 t_list_init,
32 "list_create",
33 "function list_create()",
34 t_list_create,
37 "list_append",
38 "function list_append()",
39 t_list_append,
42 "list_prepend",
43 "function list_prepend()",
44 t_list_prepend,
47 {NULL, NULL, NULL},
49 /* }}} */
51 /* {{{ int main() */
52 int
53 main (void)
55 int i, ret;
57 for (i = 0; list_tests[i].name; ++i)
59 printf("Testing %s...", list_tests[i].desc);
61 ret = list_tests[i].func();
62 printf(" %s\n", ret == EXIT_SUCCESS ? "success" : "failure");
63 if (ret == EXIT_SUCCESS)
64 continue;
66 fprintf(stderr, "\n``` Test case '%s' failed.\n", list_tests[i].name);
69 return 0;
71 /* }}} */
73 /* {{{ #define LIST_INIT_IS_SANE() */
74 #define LIST_INIT_IS_SANE(list) \
75 do \
76 { \
77 list_t *__list = (list); \
79 if (__list->length) \
80 return EXIT_FAILURE; \
81 if (__list->head || __list->tail) \
82 return EXIT_FAILURE; \
83 } \
84 while (0)
85 /* }}} */
87 /* {{{ static int t_list_init() */
88 static int
89 t_list_init (void)
91 list_t list;
93 if (list_init(&list) < 0)
94 return EXIT_FAILURE;
96 LIST_INIT_IS_SANE(&list);
98 return EXIT_SUCCESS;
100 /* }}} */
102 /* {{{ static int t_list_create() */
103 static int
104 t_list_create (void)
106 list_t *list = NULL;
108 list = list_create();
109 if (!list)
110 return EXIT_FAILURE;
112 LIST_INIT_IS_SANE(list);
113 list_destroy(list);
115 return EXIT_SUCCESS;
117 /* }}} */
119 /* {{{ static int t_list_append() */
120 static int
121 t_list_append (void)
123 list_t *list = NULL;
124 list_node_t *cur = NULL;
125 char *strings[] = {"afoo", "bfoo", "cfoo", NULL};
126 int i, c;
128 list = list_create();
129 if (!list)
130 return EXIT_FAILURE;
132 LIST_INIT_IS_SANE(list);
134 for (i = 0; strings[i]; ++i)
136 if (list_append(list, strings[i]) < 0)
138 list_destroy(list);
139 return EXIT_FAILURE;
143 i = c = 0;
144 LIST_FOREACH (list, cur)
146 if (cur->data == strings[i])
147 ++c;
148 ++i;
151 list_destroy(list);
153 return c == 3 ? EXIT_SUCCESS : EXIT_FAILURE;
155 /* }}} */
157 /* {{{ static int t_list_prepend() */
158 static int
159 t_list_prepend (void)
161 list_t *list = NULL;
162 list_node_t *cur = NULL;
163 char *strings[] = {"cfoo", "bfoo", "afoo", NULL};
164 int i, c;
166 list = list_create();
167 if (!list)
168 return EXIT_FAILURE;
170 LIST_INIT_IS_SANE(list);
172 for (i = 0; strings[i]; ++i)
174 if (list_prepend(list, strings[i]) < 0)
176 list_destroy(list);
177 return EXIT_FAILURE;
181 i = 2, c = 0;
182 LIST_FOREACH (list, cur)
184 if (cur->data == strings[i])
185 ++c;
186 --i;
189 list_destroy(list);
191 return c == 3 ? EXIT_SUCCESS : EXIT_FAILURE;
193 /* }}} */
196 * vim: ts=8 sw=8 noet fdm=marker tw=80