2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Library General Public License for more details.
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 #define OBJECT0 (void *)0x01234567
28 #define OBJECT1 (void *)0x12345678
29 #define OBJECT2 (void *)0x23456789
30 #define OBJECT3 (void *)0x3456789a
31 #define OBJECT4 (void *)0x456789ab
33 #define OBJECT5 (void *)0x56789abc
34 #define OBJECT6 (void *)0x6789abcd
35 #define OBJECT7 (void *)0x789abcde
36 #define OBJECT8 (void *)0x89abcdef
37 #define OBJECT9 (void *)0x9abcdef0
39 #define OBJECTA (void *)0xabcdef01
41 #define DEADINT 0xdeadbeef
42 #define DEADPTR (void *)0xdeadbeef
45 link_head_t *elem_list;
46 link_elem_t *elem_array;
50 /* Check return value of add operation and report PASS/FAIL */
52 check_result(unsigned long result, unsigned long expected, char *test,
55 if (result != expected) {
56 printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info,
61 printf("PASS/%s:%s correctly returned %lu\n", test, info, result);
64 /* Check that a list head matches expectations */
66 check_list(link_head_t *list, unsigned int count, link_elem_t *head,
67 link_elem_t *tail, char *test, char *info)
69 if (list->lh_count != count) { /* Check count first */
70 printf("FAIL/%s_count:%s: Count mismatch\n", test, info);
73 printf("PASS/%s_count:%s: Counts match\n", test, info);
75 if (list->lh_first != head) { /* then check the head pointer */
76 printf("FAIL/%s_first:%s: Head pointer mismatch\n", test, info);
79 printf("PASS/%s_first:%s: Head pointers match\n", test, info);
81 if (list->lh_last != tail) { /* finally check the tail pointer */
82 printf("FAIL/%s_last:%s: Tail pointer mismatch\n", test, info);
85 printf("PASS/%s_last:%s: Tail pointers match\n", test, info);
89 check_flush(link_head_t *head, link_elem_t *elem, void *extra)
91 struct flushcheck *itcheck;
95 /* OK, verify that the list head is the same as the one we expect */
96 if (head != itcheck->elem_list)
97 printf("FAIL/ll_flush_funchead_e%d:List heads do not match\n",
100 printf("PASS/ll_flush_funchead_e%d:List heads match\n", itcheck->elem_idx);
102 /* Now verify that the element is what we expect. */
103 if (elem != &itcheck->elem_array[itcheck->elem_idx])
104 printf("FAIL/ll_flush_funcelem_e%d:Elements do not match\n",
107 printf("PASS/ll_flush_funcelem_e%d:Elements match\n", itcheck->elem_idx);
109 /* Increment index and return error if it was 0 */
110 return (!itcheck->elem_idx++ ? EINVAL : 0);
114 main(int argc, char **argv)
117 link_head_t list[] = { /* some lists to operate on */
120 { DEADINT, DEADINT, DEADPTR, DEADPTR, 0 } /* list[2] is a bad list */
122 link_elem_t elem[] = { /* some elements to operate on */
123 LINK_ELEM_INIT(OBJECT0),
124 LINK_ELEM_INIT(OBJECT1),
125 LINK_ELEM_INIT(OBJECT2),
126 LINK_ELEM_INIT(OBJECT3),
127 LINK_ELEM_INIT(OBJECT4),
128 LINK_ELEM_INIT(OBJECT5),
129 LINK_ELEM_INIT(OBJECT6),
130 LINK_ELEM_INIT(OBJECT7),
131 LINK_ELEM_INIT(OBJECT8),
132 LINK_ELEM_INIT(OBJECT9),
133 LINK_ELEM_INIT(OBJECTA),
135 struct flushcheck itcheck = { 0, 0, 0 };
137 /* First, build the lists */
138 for (i = 0; i < 5; i++)
139 if (ll_add(&list[0], &elem[i], LINK_LOC_TAIL, 0) ||
140 ll_add(&list[1], &elem[i + 5], LINK_LOC_TAIL, 0))
141 return -1; /* failed to initialize test */
143 /* Baseline checks */
144 check_list(&list[0], 5, &elem[0], &elem[4], "ll_flush_baseline_l0",
145 "Verify baseline list 0");
146 check_list(&list[1], 5, &elem[5], &elem[9], "ll_flush_baseline_l1",
147 "Verify baseline list 1");
149 /* Check to see if ll_flush() verifies its arguments correctly */
150 check_result(ll_flush(0, 0, 0), DB_ERR_BADARGS, "ll_flush_noargs",
151 "ll_flush() with no arguments", 0);
152 check_result(ll_flush(&list[2], check_flush, &itcheck), DB_ERR_BADARGS,
153 "ll_flush_badlist", "ll_flush() with bad list", 0);
155 /* Check to see if ll_flush() operates properly with no flush function */
156 check_result(ll_flush(&list[1], 0, 0), 0, "ll_flush_nofunc",
157 "ll_flush() with no flush function", 0);
158 check_list(&list[1], 0, 0, 0, "ll_flush_nofunc",
159 "Test ll_flush() element removal (no flush function)");
161 /* Now check to see if ll_flush() returns what the flush function returns */
162 itcheck.elem_list = &list[0];
163 itcheck.elem_array = elem;
164 itcheck.elem_idx = 0;
165 check_result(ll_flush(&list[0], check_flush, &itcheck), EINVAL,
166 "ll_flush_funcreturn",
167 "ll_flush() returning flush function return value", 0);
168 check_list(&list[0], 4, &elem[1], &elem[4], "ll_flush_funcreturn",
169 "Test ll_flush() element removal (function return non-zero)");
171 /* Now flush the list completely */
172 check_result(ll_flush(&list[0], check_flush, &itcheck), 0,
173 "ll_flush_function", "ll_flush() flush", 0);
174 check_list(&list[0], 0, 0, 0, "ll_flush_function",
175 "Test ll_flush() element removal (function return zero)");
177 /* Did it check them all? */
178 if (itcheck.elem_idx == 5)
179 printf("PASS/ll_flush_func_count:ll_flush() visited all items\n");
181 printf("PASS/ll_flush_func_count:ll_flush() visited only %d items\n",