|
@@ -0,0 +1,235 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/** @file
|
|
22
|
+ *
|
|
23
|
+ * List function tests
|
|
24
|
+ *
|
|
25
|
+ */
|
|
26
|
+
|
|
27
|
+/* Forcibly enable assertions for list_check() */
|
|
28
|
+#undef NDEBUG
|
|
29
|
+
|
|
30
|
+#include <assert.h>
|
|
31
|
+#include <string.h>
|
|
32
|
+#include <stdio.h>
|
|
33
|
+#include <ipxe/list.h>
|
|
34
|
+#include <ipxe/test.h>
|
|
35
|
+
|
|
36
|
+/** A list test structure */
|
|
37
|
+struct list_test {
|
|
38
|
+ /** List element */
|
|
39
|
+ struct list_head list;
|
|
40
|
+ /** Label */
|
|
41
|
+ char label;
|
|
42
|
+};
|
|
43
|
+
|
|
44
|
+/** List test elements */
|
|
45
|
+static struct list_test list_tests[] = {
|
|
46
|
+ { .label = '0' },
|
|
47
|
+ { .label = '1' },
|
|
48
|
+ { .label = '2' },
|
|
49
|
+ { .label = '3' },
|
|
50
|
+ { .label = '4' },
|
|
51
|
+ { .label = '5' },
|
|
52
|
+ { .label = '6' },
|
|
53
|
+ { .label = '7' },
|
|
54
|
+ { .label = '8' },
|
|
55
|
+ { .label = '9' },
|
|
56
|
+};
|
|
57
|
+
|
|
58
|
+/** Test list */
|
|
59
|
+static LIST_HEAD ( test_list );
|
|
60
|
+
|
|
61
|
+/**
|
|
62
|
+ * Check list contents are as expected
|
|
63
|
+ *
|
|
64
|
+ * @v list Test list
|
|
65
|
+ * @v expected Expected contents
|
|
66
|
+ * @v ok List contents are as expected
|
|
67
|
+ */
|
|
68
|
+static int list_check_contents ( struct list_head *list,
|
|
69
|
+ const char *expected ) {
|
|
70
|
+ struct list_test *entry;
|
|
71
|
+ size_t num_entries = 0;
|
|
72
|
+
|
|
73
|
+ /* Determine size of list */
|
|
74
|
+ list_for_each_entry ( entry, list, list )
|
|
75
|
+ num_entries++;
|
|
76
|
+
|
|
77
|
+ {
|
|
78
|
+ char found[ num_entries + 1 ];
|
|
79
|
+ char found_rev[ num_entries + 1 ];
|
|
80
|
+ char *tmp;
|
|
81
|
+
|
|
82
|
+ /* Build up list content string */
|
|
83
|
+ tmp = found;
|
|
84
|
+ list_for_each_entry ( entry, list, list )
|
|
85
|
+ *(tmp++) = entry->label;
|
|
86
|
+ *tmp = '\0';
|
|
87
|
+
|
|
88
|
+ /* Sanity check reversed list */
|
|
89
|
+ tmp = &found_rev[ sizeof ( found_rev ) - 1 ];
|
|
90
|
+ *tmp = '\0';
|
|
91
|
+ list_for_each_entry_reverse ( entry, list, list )
|
|
92
|
+ *(--tmp) = entry->label;
|
|
93
|
+ if ( strcmp ( found, found_rev ) != 0 ) {
|
|
94
|
+ printf ( "FAILURE: list reversal mismatch (forward "
|
|
95
|
+ "\"%s\", reverse \"%s\")\n",
|
|
96
|
+ found, found_rev );
|
|
97
|
+ return 0;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ /* Compare against expected content */
|
|
101
|
+ if ( strcmp ( found, expected ) == 0 ) {
|
|
102
|
+ return 1;
|
|
103
|
+ } else {
|
|
104
|
+ printf ( "FAILURE: expected \"%s\", got \"%s\"\n",
|
|
105
|
+ expected, found );
|
|
106
|
+ return 0;
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+/**
|
|
112
|
+ * Report list test result
|
|
113
|
+ *
|
|
114
|
+ * @v list Test list
|
|
115
|
+ * @v expected Expected contents
|
|
116
|
+ */
|
|
117
|
+#define list_contents_ok( list, expected ) do { \
|
|
118
|
+ ok ( list_check_contents ( (list), (expected) ) ); \
|
|
119
|
+ } while ( 0 )
|
|
120
|
+
|
|
121
|
+/**
|
|
122
|
+ * Perform list self-test
|
|
123
|
+ *
|
|
124
|
+ */
|
|
125
|
+static void list_test_exec ( void ) {
|
|
126
|
+ struct list_head *list = &test_list;
|
|
127
|
+
|
|
128
|
+ /* Test initialiser and list_empty() */
|
|
129
|
+ ok ( list_empty ( list ) );
|
|
130
|
+ list_contents_ok ( list, "" );
|
|
131
|
+
|
|
132
|
+ /* Test list_add(), list_add_tail() and list_del() */
|
|
133
|
+ INIT_LIST_HEAD ( list );
|
|
134
|
+ list_contents_ok ( list, "" );
|
|
135
|
+ list_add ( &list_tests[4].list, list ); /* prepend */
|
|
136
|
+ list_contents_ok ( list, "4" );
|
|
137
|
+ list_add ( &list_tests[2].list, list ); /* prepend */
|
|
138
|
+ list_contents_ok ( list, "24" );
|
|
139
|
+ list_add_tail ( &list_tests[7].list, list ); /* append */
|
|
140
|
+ list_contents_ok ( list, "247" );
|
|
141
|
+ list_add ( &list_tests[1].list, &list_tests[4].list ); /* after */
|
|
142
|
+ list_contents_ok ( list, "2417" );
|
|
143
|
+ list_add_tail ( &list_tests[8].list, &list_tests[7].list ); /* before */
|
|
144
|
+ list_contents_ok ( list, "24187" );
|
|
145
|
+ list_del ( &list_tests[4].list ); /* delete middle */
|
|
146
|
+ list_contents_ok ( list, "2187" );
|
|
147
|
+ list_del ( &list_tests[2].list ); /* delete first */
|
|
148
|
+ list_contents_ok ( list, "187" );
|
|
149
|
+ list_del ( &list_tests[7].list ); /* delete last */
|
|
150
|
+ list_contents_ok ( list, "18" );
|
|
151
|
+ list_del ( &list_tests[1].list ); /* delete all */
|
|
152
|
+ list_del ( &list_tests[8].list ); /* delete all */
|
|
153
|
+ list_contents_ok ( list, "" );
|
|
154
|
+ ok ( list_empty ( list ) );
|
|
155
|
+
|
|
156
|
+ /* Test list_entry() */
|
|
157
|
+ INIT_LIST_HEAD ( &list_tests[3].list ); // for list_check()
|
|
158
|
+ ok ( list_entry ( &list_tests[3].list, struct list_test, list )
|
|
159
|
+ == &list_tests[3] );
|
|
160
|
+
|
|
161
|
+ /* Test list_first_entry() */
|
|
162
|
+ INIT_LIST_HEAD ( list );
|
|
163
|
+ list_add_tail ( &list_tests[9].list, list );
|
|
164
|
+ list_add_tail ( &list_tests[5].list, list );
|
|
165
|
+ list_add_tail ( &list_tests[6].list, list );
|
|
166
|
+ ok ( list_first_entry ( list, struct list_test, list )
|
|
167
|
+ == &list_tests[9] );
|
|
168
|
+ list_del ( &list_tests[9].list );
|
|
169
|
+ ok ( list_first_entry ( list, struct list_test, list )
|
|
170
|
+ == &list_tests[5] );
|
|
171
|
+
|
|
172
|
+ /* Test list_for_each() */
|
|
173
|
+ INIT_LIST_HEAD ( list );
|
|
174
|
+ list_add_tail ( &list_tests[6].list, list );
|
|
175
|
+ list_add_tail ( &list_tests[7].list, list );
|
|
176
|
+ list_add_tail ( &list_tests[3].list, list );
|
|
177
|
+ {
|
|
178
|
+ char *expected = "673";
|
|
179
|
+ struct list_head *pos;
|
|
180
|
+ struct list_test *entry;
|
|
181
|
+ list_for_each ( pos, list ) {
|
|
182
|
+ entry = list_entry ( pos, struct list_test, list );
|
|
183
|
+ ok ( entry->label == *(expected++) );
|
|
184
|
+ }
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ /* list_for_each_entry() and list_for_each_entry_reverse() are
|
|
188
|
+ * already tested as part of list_contents_ok()
|
|
189
|
+ */
|
|
190
|
+
|
|
191
|
+ /* Test list_for_each_entry_safe() */
|
|
192
|
+ INIT_LIST_HEAD ( list );
|
|
193
|
+ list_add_tail ( &list_tests[2].list, list );
|
|
194
|
+ list_add_tail ( &list_tests[4].list, list );
|
|
195
|
+ list_add_tail ( &list_tests[1].list, list );
|
|
196
|
+ {
|
|
197
|
+ char *expected = "241";
|
|
198
|
+ struct list_test *pos;
|
|
199
|
+ struct list_test *tmp;
|
|
200
|
+ list_for_each_entry_safe ( pos, tmp, list, list ) {
|
|
201
|
+ list_contents_ok ( list, expected );
|
|
202
|
+ list_del ( &pos->list );
|
|
203
|
+ expected++;
|
|
204
|
+ list_contents_ok ( list, expected );
|
|
205
|
+ }
|
|
206
|
+ }
|
|
207
|
+ ok ( list_empty ( list ) );
|
|
208
|
+
|
|
209
|
+ /* Test list_contains() and list_contains_entry() */
|
|
210
|
+ INIT_LIST_HEAD ( list );
|
|
211
|
+ INIT_LIST_HEAD ( &list_tests[3].list );
|
|
212
|
+ list_add ( &list_tests[8].list, list );
|
|
213
|
+ list_add ( &list_tests[5].list, list );
|
|
214
|
+ ok ( list_contains ( &list_tests[8].list, list ) );
|
|
215
|
+ ok ( list_contains_entry ( &list_tests[8], list, list ) );
|
|
216
|
+ ok ( list_contains ( &list_tests[5].list, list ) );
|
|
217
|
+ ok ( list_contains_entry ( &list_tests[5], list, list ) );
|
|
218
|
+ ok ( ! list_contains ( &list_tests[3].list, list ) );
|
|
219
|
+ ok ( ! list_contains_entry ( &list_tests[3], list, list ) );
|
|
220
|
+
|
|
221
|
+ /* Test list_check_contains_entry() */
|
|
222
|
+ INIT_LIST_HEAD ( list );
|
|
223
|
+ list_add ( &list_tests[4].list, list );
|
|
224
|
+ list_add ( &list_tests[0].list, list );
|
|
225
|
+ list_add ( &list_tests[3].list, list );
|
|
226
|
+ list_check_contains_entry ( &list_tests[4], list, list );
|
|
227
|
+ list_check_contains_entry ( &list_tests[0], list, list );
|
|
228
|
+ list_check_contains_entry ( &list_tests[3], list, list );
|
|
229
|
+}
|
|
230
|
+
|
|
231
|
+/** List self-test */
|
|
232
|
+struct self_test list_test __self_test = {
|
|
233
|
+ .name = "list",
|
|
234
|
+ .exec = list_test_exec,
|
|
235
|
+};
|