]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-list.c
testsuite: depmod: check netsted loops reporting
[thirdparty/kmod.git] / testsuite / test-list.c
1 /*
2 * Copyright (C) 2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This program 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include <libkmod/libkmod-internal.h>
22
23 /* FIXME: hack, change name so we don't clash */
24 #undef ERR
25 #include "testsuite.h"
26
27 static int len(struct kmod_list *list)
28 {
29 int count = 0;
30 struct kmod_list *l;
31 kmod_list_foreach(l, list)
32 count++;
33 return count;
34 }
35
36
37 static int test_list_last(const struct test *t)
38 {
39 struct kmod_list *list = NULL, *last;
40 int i;
41 const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
42 const int N = ARRAY_SIZE(v);
43
44 for (i = 0; i < N; i++)
45 list = kmod_list_append(list, v[i]);
46 assert_return(len(list) == N, EXIT_FAILURE);
47
48 last = kmod_list_last(list);
49 assert_return(last->data == v[N - 1], EXIT_FAILURE);
50
51 return 0;
52 }
53 DEFINE_TEST(test_list_last,
54 .description = "test for the last element of a list");
55
56
57 static int test_list_prev(const struct test *t)
58 {
59 struct kmod_list *list = NULL, *l, *p;
60 int i;
61 const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
62 const int N = ARRAY_SIZE(v);
63
64 l = kmod_list_prev(list, list);
65 assert_return(l == NULL, EXIT_FAILURE);
66
67 for (i = 0; i < N; i++)
68 list = kmod_list_append(list, v[i]);
69
70 l = kmod_list_prev(list, list);
71 assert_return(l == NULL, EXIT_FAILURE);
72
73 l = list;
74 for (i = 0; i < N - 1; i++) {
75 l = kmod_list_next(list, l);
76 p = kmod_list_prev(list, l);
77 assert_return(p->data == v[i], EXIT_FAILURE);
78 }
79
80 return 0;
81 }
82 DEFINE_TEST(test_list_prev,
83 .description = "test list prev");
84
85
86 static int test_list_remove_n_latest(const struct test *t)
87 {
88 struct kmod_list *list = NULL, *l;
89 int i;
90 const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
91 const int N = ARRAY_SIZE(v), M = N / 2;
92
93 for (i = 0; i < N; i++)
94 list = kmod_list_append(list, v[i]);
95 assert_return(len(list) == N, EXIT_FAILURE);
96
97 list = kmod_list_remove_n_latest(list, M);
98 assert_return(len(list) == N - M, EXIT_FAILURE);
99
100 i = 0;
101 kmod_list_foreach (l, list) {
102 assert_return(l->data == v[i], EXIT_FAILURE);
103 i++;
104 }
105
106 return 0;
107 }
108 DEFINE_TEST(test_list_remove_n_latest,
109 .description = "test list function to remove n latest elements");
110
111
112 static int test_list_remove_data(const struct test *t)
113 {
114 struct kmod_list *list = NULL, *l;
115 int i;
116 const char *v[] = { "v1", "v2", "v3", "v4", "v5" }, *removed;
117 const int N = ARRAY_SIZE(v);
118
119 for (i = 0; i < N; i++)
120 list = kmod_list_append(list, v[i]);
121
122 removed = v[N / 2];
123 list = kmod_list_remove_data(list, removed);
124 assert_return(len(list) == N - 1, EXIT_FAILURE);
125
126 kmod_list_foreach (l, list)
127 assert_return(l->data != removed, EXIT_FAILURE);
128
129 return 0;
130 }
131 DEFINE_TEST(test_list_remove_data,
132 .description = "test list function to remove element by data");
133
134
135 static int test_list_append_list(const struct test *t)
136 {
137 struct kmod_list *a = NULL, *b = NULL, *c, *l;
138 int i;
139 const char *v[] = { "v1", "v2", "v3", "v4", "v5" };
140 const int N = ARRAY_SIZE(v), M = N / 2;
141
142 for (i = 0; i < M; i++)
143 a = kmod_list_append(a, v[i]);
144 assert_return(len(a) == M, EXIT_FAILURE);
145
146 for (i = M; i < N; i++)
147 b = kmod_list_append(b, v[i]);
148 assert_return(len(b) == N - M, EXIT_FAILURE);
149
150 a = kmod_list_append_list(a, NULL);
151 assert_return(len(a) == M, EXIT_FAILURE);
152
153 b = kmod_list_append_list(NULL, b);
154 assert_return(len(b) == N - M, EXIT_FAILURE);
155
156 c = kmod_list_append_list(a, b);
157 assert_return(len(c) == N, EXIT_FAILURE);
158
159 i = 0;
160 kmod_list_foreach (l, c) {
161 assert_return(l->data == v[i], EXIT_FAILURE);
162 i++;
163 }
164
165 return 0;
166 }
167 DEFINE_TEST(test_list_append_list,
168 .description = "test list function to append another list");
169
170
171 static int test_list_insert_before(const struct test *t)
172 {
173 struct kmod_list *list = NULL, *l;
174 const char *v1 = "v1", *v2 = "v2", *v3 = "v3", *vx = "vx";
175
176 list = kmod_list_insert_before(list, v3);
177 assert_return(len(list) == 1, EXIT_FAILURE);
178
179 list = kmod_list_insert_before(list, v2);
180 list = kmod_list_insert_before(list, v1);
181 assert_return(len(list) == 3, EXIT_FAILURE);
182
183 l = list;
184 assert_return(l->data == v1, EXIT_FAILURE);
185
186 l = kmod_list_next(list, l);
187 assert_return(l->data == v2, EXIT_FAILURE);
188
189 l = kmod_list_insert_before(l, vx);
190 assert_return(len(list) == 4, EXIT_FAILURE);
191 assert_return(l->data == vx, EXIT_FAILURE);
192
193 l = kmod_list_next(list, l);
194 assert_return(l->data == v2, EXIT_FAILURE);
195
196 l = kmod_list_next(list, l);
197 assert_return(l->data == v3, EXIT_FAILURE);
198
199 return 0;
200 }
201 DEFINE_TEST(test_list_insert_before,
202 .description = "test list function to insert before element");
203
204
205 static int test_list_insert_after(const struct test *t)
206 {
207 struct kmod_list *list = NULL, *l;
208 const char *v1 = "v1", *v2 = "v2", *v3 = "v3", *vx = "vx";
209
210 list = kmod_list_insert_after(list, v1);
211 assert_return(len(list) == 1, EXIT_FAILURE);
212
213 list = kmod_list_insert_after(list, v3);
214 list = kmod_list_insert_after(list, v2);
215 assert_return(len(list) == 3, EXIT_FAILURE);
216
217 l = list;
218 assert_return(l->data == v1, EXIT_FAILURE);
219
220 l = kmod_list_insert_after(l, vx);
221 assert_return(len(list) == 4, EXIT_FAILURE);
222 assert_return(l->data == v1, EXIT_FAILURE);
223
224 l = kmod_list_next(list, l);
225 assert_return(l->data == vx, EXIT_FAILURE);
226
227 l = kmod_list_next(list, l);
228 assert_return(l->data == v2, EXIT_FAILURE);
229
230 l = kmod_list_next(list, l);
231 assert_return(l->data == v3, EXIT_FAILURE);
232
233 return 0;
234 }
235 DEFINE_TEST(test_list_insert_after,
236 .description = "test list function to insert after element");
237
238 TESTSUITE_MAIN();