2 * Copyright (C) 2015 Intel Corporation. All rights reserved.
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.
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.
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/>.
21 #include <libkmod/libkmod-internal.h>
23 /* FIXME: hack, change name so we don't clash */
25 #include "testsuite.h"
27 static int len(struct kmod_list
*list
)
31 kmod_list_foreach(l
, list
)
37 static int test_list_last(const struct test
*t
)
39 struct kmod_list
*list
= NULL
, *last
;
41 const char *v
[] = { "v1", "v2", "v3", "v4", "v5" };
42 const int N
= ARRAY_SIZE(v
);
44 for (i
= 0; i
< N
; i
++)
45 list
= kmod_list_append(list
, v
[i
]);
46 assert_return(len(list
) == N
, EXIT_FAILURE
);
48 last
= kmod_list_last(list
);
49 assert_return(last
->data
== v
[N
- 1], EXIT_FAILURE
);
53 DEFINE_TEST(test_list_last
,
54 .description
= "test for the last element of a list");
57 static int test_list_prev(const struct test
*t
)
59 struct kmod_list
*list
= NULL
, *l
, *p
;
61 const char *v
[] = { "v1", "v2", "v3", "v4", "v5" };
62 const int N
= ARRAY_SIZE(v
);
64 l
= kmod_list_prev(list
, list
);
65 assert_return(l
== NULL
, EXIT_FAILURE
);
67 for (i
= 0; i
< N
; i
++)
68 list
= kmod_list_append(list
, v
[i
]);
70 l
= kmod_list_prev(list
, list
);
71 assert_return(l
== NULL
, EXIT_FAILURE
);
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
);
82 DEFINE_TEST(test_list_prev
,
83 .description
= "test list prev");
86 static int test_list_remove_n_latest(const struct test
*t
)
88 struct kmod_list
*list
= NULL
, *l
;
90 const char *v
[] = { "v1", "v2", "v3", "v4", "v5" };
91 const int N
= ARRAY_SIZE(v
), M
= N
/ 2;
93 for (i
= 0; i
< N
; i
++)
94 list
= kmod_list_append(list
, v
[i
]);
95 assert_return(len(list
) == N
, EXIT_FAILURE
);
97 list
= kmod_list_remove_n_latest(list
, M
);
98 assert_return(len(list
) == N
- M
, EXIT_FAILURE
);
101 kmod_list_foreach (l
, list
) {
102 assert_return(l
->data
== v
[i
], EXIT_FAILURE
);
108 DEFINE_TEST(test_list_remove_n_latest
,
109 .description
= "test list function to remove n latest elements");
112 static int test_list_remove_data(const struct test
*t
)
114 struct kmod_list
*list
= NULL
, *l
;
116 const char *v
[] = { "v1", "v2", "v3", "v4", "v5" }, *removed
;
117 const int N
= ARRAY_SIZE(v
);
119 for (i
= 0; i
< N
; i
++)
120 list
= kmod_list_append(list
, v
[i
]);
123 list
= kmod_list_remove_data(list
, removed
);
124 assert_return(len(list
) == N
- 1, EXIT_FAILURE
);
126 kmod_list_foreach (l
, list
)
127 assert_return(l
->data
!= removed
, EXIT_FAILURE
);
131 DEFINE_TEST(test_list_remove_data
,
132 .description
= "test list function to remove element by data");
135 static int test_list_append_list(const struct test
*t
)
137 struct kmod_list
*a
= NULL
, *b
= NULL
, *c
, *l
;
139 const char *v
[] = { "v1", "v2", "v3", "v4", "v5" };
140 const int N
= ARRAY_SIZE(v
), M
= N
/ 2;
142 for (i
= 0; i
< M
; i
++)
143 a
= kmod_list_append(a
, v
[i
]);
144 assert_return(len(a
) == M
, EXIT_FAILURE
);
146 for (i
= M
; i
< N
; i
++)
147 b
= kmod_list_append(b
, v
[i
]);
148 assert_return(len(b
) == N
- M
, EXIT_FAILURE
);
150 a
= kmod_list_append_list(a
, NULL
);
151 assert_return(len(a
) == M
, EXIT_FAILURE
);
153 b
= kmod_list_append_list(NULL
, b
);
154 assert_return(len(b
) == N
- M
, EXIT_FAILURE
);
156 c
= kmod_list_append_list(a
, b
);
157 assert_return(len(c
) == N
, EXIT_FAILURE
);
160 kmod_list_foreach (l
, c
) {
161 assert_return(l
->data
== v
[i
], EXIT_FAILURE
);
167 DEFINE_TEST(test_list_append_list
,
168 .description
= "test list function to append another list");
171 static int test_list_insert_before(const struct test
*t
)
173 struct kmod_list
*list
= NULL
, *l
;
174 const char *v1
= "v1", *v2
= "v2", *v3
= "v3", *vx
= "vx";
176 list
= kmod_list_insert_before(list
, v3
);
177 assert_return(len(list
) == 1, EXIT_FAILURE
);
179 list
= kmod_list_insert_before(list
, v2
);
180 list
= kmod_list_insert_before(list
, v1
);
181 assert_return(len(list
) == 3, EXIT_FAILURE
);
184 assert_return(l
->data
== v1
, EXIT_FAILURE
);
186 l
= kmod_list_next(list
, l
);
187 assert_return(l
->data
== v2
, EXIT_FAILURE
);
189 l
= kmod_list_insert_before(l
, vx
);
190 assert_return(len(list
) == 4, EXIT_FAILURE
);
191 assert_return(l
->data
== vx
, EXIT_FAILURE
);
193 l
= kmod_list_next(list
, l
);
194 assert_return(l
->data
== v2
, EXIT_FAILURE
);
196 l
= kmod_list_next(list
, l
);
197 assert_return(l
->data
== v3
, EXIT_FAILURE
);
201 DEFINE_TEST(test_list_insert_before
,
202 .description
= "test list function to insert before element");
205 static int test_list_insert_after(const struct test
*t
)
207 struct kmod_list
*list
= NULL
, *l
;
208 const char *v1
= "v1", *v2
= "v2", *v3
= "v3", *vx
= "vx";
210 list
= kmod_list_insert_after(list
, v1
);
211 assert_return(len(list
) == 1, EXIT_FAILURE
);
213 list
= kmod_list_insert_after(list
, v3
);
214 list
= kmod_list_insert_after(list
, v2
);
215 assert_return(len(list
) == 3, EXIT_FAILURE
);
218 assert_return(l
->data
== v1
, EXIT_FAILURE
);
220 l
= kmod_list_insert_after(l
, vx
);
221 assert_return(len(list
) == 4, EXIT_FAILURE
);
222 assert_return(l
->data
== v1
, EXIT_FAILURE
);
224 l
= kmod_list_next(list
, l
);
225 assert_return(l
->data
== vx
, EXIT_FAILURE
);
227 l
= kmod_list_next(list
, l
);
228 assert_return(l
->data
== v2
, EXIT_FAILURE
);
230 l
= kmod_list_next(list
, l
);
231 assert_return(l
->data
== v3
, EXIT_FAILURE
);
235 DEFINE_TEST(test_list_insert_after
,
236 .description
= "test list function to insert after element");