• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KUnit test for the Kernel Linked-list structures.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: David Gow <davidgow@google.com>
7  */
8 #include <kunit/test.h>
9 
10 #include <linux/list.h>
11 
12 struct list_test_struct {
13 	int data;
14 	struct list_head list;
15 };
16 
list_test_list_init(struct kunit * test)17 static void list_test_list_init(struct kunit *test)
18 {
19 	/* Test the different ways of initialising a list. */
20 	struct list_head list1 = LIST_HEAD_INIT(list1);
21 	struct list_head list2;
22 	LIST_HEAD(list3);
23 	struct list_head *list4;
24 	struct list_head *list5;
25 
26 	INIT_LIST_HEAD(&list2);
27 
28 	list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
29 	INIT_LIST_HEAD(list4);
30 
31 	list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
32 	memset(list5, 0xFF, sizeof(*list5));
33 	INIT_LIST_HEAD(list5);
34 
35 	/* list_empty_careful() checks both next and prev. */
36 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list1));
37 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
38 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list3));
39 	KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
40 	KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));
41 
42 	kfree(list4);
43 	kfree(list5);
44 }
45 
list_test_list_add(struct kunit * test)46 static void list_test_list_add(struct kunit *test)
47 {
48 	struct list_head a, b;
49 	LIST_HEAD(list);
50 
51 	list_add(&a, &list);
52 	list_add(&b, &list);
53 
54 	/* should be [list] -> b -> a */
55 	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
56 	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
57 	KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
58 }
59 
list_test_list_add_tail(struct kunit * test)60 static void list_test_list_add_tail(struct kunit *test)
61 {
62 	struct list_head a, b;
63 	LIST_HEAD(list);
64 
65 	list_add_tail(&a, &list);
66 	list_add_tail(&b, &list);
67 
68 	/* should be [list] -> a -> b */
69 	KUNIT_EXPECT_PTR_EQ(test, list.next, &a);
70 	KUNIT_EXPECT_PTR_EQ(test, a.prev, &list);
71 	KUNIT_EXPECT_PTR_EQ(test, a.next, &b);
72 }
73 
list_test_list_del(struct kunit * test)74 static void list_test_list_del(struct kunit *test)
75 {
76 	struct list_head a, b;
77 	LIST_HEAD(list);
78 
79 	list_add_tail(&a, &list);
80 	list_add_tail(&b, &list);
81 
82 	/* before: [list] -> a -> b */
83 	list_del(&a);
84 
85 	/* now: [list] -> b */
86 	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
87 	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
88 }
89 
list_test_list_replace(struct kunit * test)90 static void list_test_list_replace(struct kunit *test)
91 {
92 	struct list_head a_old, a_new, b;
93 	LIST_HEAD(list);
94 
95 	list_add_tail(&a_old, &list);
96 	list_add_tail(&b, &list);
97 
98 	/* before: [list] -> a_old -> b */
99 	list_replace(&a_old, &a_new);
100 
101 	/* now: [list] -> a_new -> b */
102 	KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
103 	KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
104 }
105 
list_test_list_replace_init(struct kunit * test)106 static void list_test_list_replace_init(struct kunit *test)
107 {
108 	struct list_head a_old, a_new, b;
109 	LIST_HEAD(list);
110 
111 	list_add_tail(&a_old, &list);
112 	list_add_tail(&b, &list);
113 
114 	/* before: [list] -> a_old -> b */
115 	list_replace_init(&a_old, &a_new);
116 
117 	/* now: [list] -> a_new -> b */
118 	KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
119 	KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
120 
121 	/* check a_old is empty (initialized) */
122 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a_old));
123 }
124 
list_test_list_swap(struct kunit * test)125 static void list_test_list_swap(struct kunit *test)
126 {
127 	struct list_head a, b;
128 	LIST_HEAD(list);
129 
130 	list_add_tail(&a, &list);
131 	list_add_tail(&b, &list);
132 
133 	/* before: [list] -> a -> b */
134 	list_swap(&a, &b);
135 
136 	/* after: [list] -> b -> a */
137 	KUNIT_EXPECT_PTR_EQ(test, &b, list.next);
138 	KUNIT_EXPECT_PTR_EQ(test, &a, list.prev);
139 
140 	KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
141 	KUNIT_EXPECT_PTR_EQ(test, &list, b.prev);
142 
143 	KUNIT_EXPECT_PTR_EQ(test, &list, a.next);
144 	KUNIT_EXPECT_PTR_EQ(test, &b, a.prev);
145 }
146 
list_test_list_del_init(struct kunit * test)147 static void list_test_list_del_init(struct kunit *test)
148 {
149 	struct list_head a, b;
150 	LIST_HEAD(list);
151 
152 	list_add_tail(&a, &list);
153 	list_add_tail(&b, &list);
154 
155 	/* before: [list] -> a -> b */
156 	list_del_init(&a);
157 	/* after: [list] -> b, a initialised */
158 
159 	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
160 	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
161 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
162 }
163 
list_test_list_move(struct kunit * test)164 static void list_test_list_move(struct kunit *test)
165 {
166 	struct list_head a, b;
167 	LIST_HEAD(list1);
168 	LIST_HEAD(list2);
169 
170 	list_add_tail(&a, &list1);
171 	list_add_tail(&b, &list2);
172 
173 	/* before: [list1] -> a, [list2] -> b */
174 	list_move(&a, &list2);
175 	/* after: [list1] empty, [list2] -> a -> b */
176 
177 	KUNIT_EXPECT_TRUE(test, list_empty(&list1));
178 
179 	KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
180 	KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
181 }
182 
list_test_list_move_tail(struct kunit * test)183 static void list_test_list_move_tail(struct kunit *test)
184 {
185 	struct list_head a, b;
186 	LIST_HEAD(list1);
187 	LIST_HEAD(list2);
188 
189 	list_add_tail(&a, &list1);
190 	list_add_tail(&b, &list2);
191 
192 	/* before: [list1] -> a, [list2] -> b */
193 	list_move_tail(&a, &list2);
194 	/* after: [list1] empty, [list2] -> b -> a */
195 
196 	KUNIT_EXPECT_TRUE(test, list_empty(&list1));
197 
198 	KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
199 	KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
200 }
201 
list_test_list_bulk_move_tail(struct kunit * test)202 static void list_test_list_bulk_move_tail(struct kunit *test)
203 {
204 	struct list_head a, b, c, d, x, y;
205 	struct list_head *list1_values[] = { &x, &b, &c, &y };
206 	struct list_head *list2_values[] = { &a, &d };
207 	struct list_head *ptr;
208 	LIST_HEAD(list1);
209 	LIST_HEAD(list2);
210 	int i = 0;
211 
212 	list_add_tail(&x, &list1);
213 	list_add_tail(&y, &list1);
214 
215 	list_add_tail(&a, &list2);
216 	list_add_tail(&b, &list2);
217 	list_add_tail(&c, &list2);
218 	list_add_tail(&d, &list2);
219 
220 	/* before: [list1] -> x -> y, [list2] -> a -> b -> c -> d */
221 	list_bulk_move_tail(&y, &b, &c);
222 	/* after: [list1] -> x -> b -> c -> y, [list2] -> a -> d */
223 
224 	list_for_each(ptr, &list1) {
225 		KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
226 		i++;
227 	}
228 	KUNIT_EXPECT_EQ(test, i, 4);
229 	i = 0;
230 	list_for_each(ptr, &list2) {
231 		KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
232 		i++;
233 	}
234 	KUNIT_EXPECT_EQ(test, i, 2);
235 }
236 
list_test_list_is_head(struct kunit * test)237 static void list_test_list_is_head(struct kunit *test)
238 {
239 	struct list_head a, b, c;
240 
241 	/* Two lists: [a] -> b, [c] */
242 	INIT_LIST_HEAD(&a);
243 	INIT_LIST_HEAD(&c);
244 	list_add_tail(&b, &a);
245 
246 	KUNIT_EXPECT_TRUE_MSG(test, list_is_head(&a, &a),
247 		"Head element of same list");
248 	KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &b),
249 		"Non-head element of same list");
250 	KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &c),
251 		"Head element of different list");
252 }
253 
254 
list_test_list_is_first(struct kunit * test)255 static void list_test_list_is_first(struct kunit *test)
256 {
257 	struct list_head a, b;
258 	LIST_HEAD(list);
259 
260 	list_add_tail(&a, &list);
261 	list_add_tail(&b, &list);
262 
263 	KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
264 	KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
265 }
266 
list_test_list_is_last(struct kunit * test)267 static void list_test_list_is_last(struct kunit *test)
268 {
269 	struct list_head a, b;
270 	LIST_HEAD(list);
271 
272 	list_add_tail(&a, &list);
273 	list_add_tail(&b, &list);
274 
275 	KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
276 	KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
277 }
278 
list_test_list_empty(struct kunit * test)279 static void list_test_list_empty(struct kunit *test)
280 {
281 	struct list_head a;
282 	LIST_HEAD(list1);
283 	LIST_HEAD(list2);
284 
285 	list_add_tail(&a, &list1);
286 
287 	KUNIT_EXPECT_FALSE(test, list_empty(&list1));
288 	KUNIT_EXPECT_TRUE(test, list_empty(&list2));
289 }
290 
list_test_list_empty_careful(struct kunit * test)291 static void list_test_list_empty_careful(struct kunit *test)
292 {
293 	/* This test doesn't check correctness under concurrent access */
294 	struct list_head a;
295 	LIST_HEAD(list1);
296 	LIST_HEAD(list2);
297 
298 	list_add_tail(&a, &list1);
299 
300 	KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
301 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
302 }
303 
list_test_list_rotate_left(struct kunit * test)304 static void list_test_list_rotate_left(struct kunit *test)
305 {
306 	struct list_head a, b;
307 	LIST_HEAD(list);
308 
309 	list_add_tail(&a, &list);
310 	list_add_tail(&b, &list);
311 
312 	/* before: [list] -> a -> b */
313 	list_rotate_left(&list);
314 	/* after: [list] -> b -> a */
315 
316 	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
317 	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
318 	KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
319 }
320 
list_test_list_rotate_to_front(struct kunit * test)321 static void list_test_list_rotate_to_front(struct kunit *test)
322 {
323 	struct list_head a, b, c, d;
324 	struct list_head *list_values[] = { &c, &d, &a, &b };
325 	struct list_head *ptr;
326 	LIST_HEAD(list);
327 	int i = 0;
328 
329 	list_add_tail(&a, &list);
330 	list_add_tail(&b, &list);
331 	list_add_tail(&c, &list);
332 	list_add_tail(&d, &list);
333 
334 	/* before: [list] -> a -> b -> c -> d */
335 	list_rotate_to_front(&c, &list);
336 	/* after: [list] -> c -> d -> a -> b */
337 
338 	list_for_each(ptr, &list) {
339 		KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
340 		i++;
341 	}
342 	KUNIT_EXPECT_EQ(test, i, 4);
343 }
344 
list_test_list_is_singular(struct kunit * test)345 static void list_test_list_is_singular(struct kunit *test)
346 {
347 	struct list_head a, b;
348 	LIST_HEAD(list);
349 
350 	/* [list] empty */
351 	KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
352 
353 	list_add_tail(&a, &list);
354 
355 	/* [list] -> a */
356 	KUNIT_EXPECT_TRUE(test, list_is_singular(&list));
357 
358 	list_add_tail(&b, &list);
359 
360 	/* [list] -> a -> b */
361 	KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
362 }
363 
list_test_list_cut_position(struct kunit * test)364 static void list_test_list_cut_position(struct kunit *test)
365 {
366 	struct list_head entries[3], *cur;
367 	LIST_HEAD(list1);
368 	LIST_HEAD(list2);
369 	int i = 0;
370 
371 	list_add_tail(&entries[0], &list1);
372 	list_add_tail(&entries[1], &list1);
373 	list_add_tail(&entries[2], &list1);
374 
375 	/* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
376 	list_cut_position(&list2, &list1, &entries[1]);
377 	/* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */
378 
379 	list_for_each(cur, &list2) {
380 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
381 		i++;
382 	}
383 
384 	KUNIT_EXPECT_EQ(test, i, 2);
385 
386 	list_for_each(cur, &list1) {
387 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
388 		i++;
389 	}
390 }
391 
list_test_list_cut_before(struct kunit * test)392 static void list_test_list_cut_before(struct kunit *test)
393 {
394 	struct list_head entries[3], *cur;
395 	LIST_HEAD(list1);
396 	LIST_HEAD(list2);
397 	int i = 0;
398 
399 	list_add_tail(&entries[0], &list1);
400 	list_add_tail(&entries[1], &list1);
401 	list_add_tail(&entries[2], &list1);
402 
403 	/* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
404 	list_cut_before(&list2, &list1, &entries[1]);
405 	/* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */
406 
407 	list_for_each(cur, &list2) {
408 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
409 		i++;
410 	}
411 
412 	KUNIT_EXPECT_EQ(test, i, 1);
413 
414 	list_for_each(cur, &list1) {
415 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
416 		i++;
417 	}
418 }
419 
list_test_list_splice(struct kunit * test)420 static void list_test_list_splice(struct kunit *test)
421 {
422 	struct list_head entries[5], *cur;
423 	LIST_HEAD(list1);
424 	LIST_HEAD(list2);
425 	int i = 0;
426 
427 	list_add_tail(&entries[0], &list1);
428 	list_add_tail(&entries[1], &list1);
429 	list_add_tail(&entries[2], &list2);
430 	list_add_tail(&entries[3], &list2);
431 	list_add_tail(&entries[4], &list1);
432 
433 	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
434 	list_splice(&list2, &entries[1]);
435 	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
436 
437 	list_for_each(cur, &list1) {
438 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
439 		i++;
440 	}
441 
442 	KUNIT_EXPECT_EQ(test, i, 5);
443 }
444 
list_test_list_splice_tail(struct kunit * test)445 static void list_test_list_splice_tail(struct kunit *test)
446 {
447 	struct list_head entries[5], *cur;
448 	LIST_HEAD(list1);
449 	LIST_HEAD(list2);
450 	int i = 0;
451 
452 	list_add_tail(&entries[0], &list1);
453 	list_add_tail(&entries[1], &list1);
454 	list_add_tail(&entries[2], &list2);
455 	list_add_tail(&entries[3], &list2);
456 	list_add_tail(&entries[4], &list1);
457 
458 	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
459 	list_splice_tail(&list2, &entries[4]);
460 	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
461 
462 	list_for_each(cur, &list1) {
463 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
464 		i++;
465 	}
466 
467 	KUNIT_EXPECT_EQ(test, i, 5);
468 }
469 
list_test_list_splice_init(struct kunit * test)470 static void list_test_list_splice_init(struct kunit *test)
471 {
472 	struct list_head entries[5], *cur;
473 	LIST_HEAD(list1);
474 	LIST_HEAD(list2);
475 	int i = 0;
476 
477 	list_add_tail(&entries[0], &list1);
478 	list_add_tail(&entries[1], &list1);
479 	list_add_tail(&entries[2], &list2);
480 	list_add_tail(&entries[3], &list2);
481 	list_add_tail(&entries[4], &list1);
482 
483 	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
484 	list_splice_init(&list2, &entries[1]);
485 	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
486 
487 	list_for_each(cur, &list1) {
488 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
489 		i++;
490 	}
491 
492 	KUNIT_EXPECT_EQ(test, i, 5);
493 
494 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
495 }
496 
list_test_list_splice_tail_init(struct kunit * test)497 static void list_test_list_splice_tail_init(struct kunit *test)
498 {
499 	struct list_head entries[5], *cur;
500 	LIST_HEAD(list1);
501 	LIST_HEAD(list2);
502 	int i = 0;
503 
504 	list_add_tail(&entries[0], &list1);
505 	list_add_tail(&entries[1], &list1);
506 	list_add_tail(&entries[2], &list2);
507 	list_add_tail(&entries[3], &list2);
508 	list_add_tail(&entries[4], &list1);
509 
510 	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
511 	list_splice_tail_init(&list2, &entries[4]);
512 	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
513 
514 	list_for_each(cur, &list1) {
515 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
516 		i++;
517 	}
518 
519 	KUNIT_EXPECT_EQ(test, i, 5);
520 
521 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
522 }
523 
list_test_list_entry(struct kunit * test)524 static void list_test_list_entry(struct kunit *test)
525 {
526 	struct list_test_struct test_struct;
527 
528 	KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
529 				struct list_test_struct, list));
530 }
531 
list_test_list_first_entry(struct kunit * test)532 static void list_test_list_first_entry(struct kunit *test)
533 {
534 	struct list_test_struct test_struct1, test_struct2;
535 	LIST_HEAD(list);
536 
537 	list_add_tail(&test_struct1.list, &list);
538 	list_add_tail(&test_struct2.list, &list);
539 
540 
541 	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
542 				struct list_test_struct, list));
543 }
544 
list_test_list_last_entry(struct kunit * test)545 static void list_test_list_last_entry(struct kunit *test)
546 {
547 	struct list_test_struct test_struct1, test_struct2;
548 	LIST_HEAD(list);
549 
550 	list_add_tail(&test_struct1.list, &list);
551 	list_add_tail(&test_struct2.list, &list);
552 
553 
554 	KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
555 				struct list_test_struct, list));
556 }
557 
list_test_list_first_entry_or_null(struct kunit * test)558 static void list_test_list_first_entry_or_null(struct kunit *test)
559 {
560 	struct list_test_struct test_struct1, test_struct2;
561 	LIST_HEAD(list);
562 
563 	KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
564 				struct list_test_struct, list));
565 
566 	list_add_tail(&test_struct1.list, &list);
567 	list_add_tail(&test_struct2.list, &list);
568 
569 	KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
570 			list_first_entry_or_null(&list,
571 				struct list_test_struct, list));
572 }
573 
list_test_list_next_entry(struct kunit * test)574 static void list_test_list_next_entry(struct kunit *test)
575 {
576 	struct list_test_struct test_struct1, test_struct2;
577 	LIST_HEAD(list);
578 
579 	list_add_tail(&test_struct1.list, &list);
580 	list_add_tail(&test_struct2.list, &list);
581 
582 
583 	KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
584 				list));
585 }
586 
list_test_list_prev_entry(struct kunit * test)587 static void list_test_list_prev_entry(struct kunit *test)
588 {
589 	struct list_test_struct test_struct1, test_struct2;
590 	LIST_HEAD(list);
591 
592 	list_add_tail(&test_struct1.list, &list);
593 	list_add_tail(&test_struct2.list, &list);
594 
595 
596 	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
597 				list));
598 }
599 
list_test_list_for_each(struct kunit * test)600 static void list_test_list_for_each(struct kunit *test)
601 {
602 	struct list_head entries[3], *cur;
603 	LIST_HEAD(list);
604 	int i = 0;
605 
606 	list_add_tail(&entries[0], &list);
607 	list_add_tail(&entries[1], &list);
608 	list_add_tail(&entries[2], &list);
609 
610 	list_for_each(cur, &list) {
611 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
612 		i++;
613 	}
614 
615 	KUNIT_EXPECT_EQ(test, i, 3);
616 }
617 
list_test_list_for_each_prev(struct kunit * test)618 static void list_test_list_for_each_prev(struct kunit *test)
619 {
620 	struct list_head entries[3], *cur;
621 	LIST_HEAD(list);
622 	int i = 2;
623 
624 	list_add_tail(&entries[0], &list);
625 	list_add_tail(&entries[1], &list);
626 	list_add_tail(&entries[2], &list);
627 
628 	list_for_each_prev(cur, &list) {
629 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
630 		i--;
631 	}
632 
633 	KUNIT_EXPECT_EQ(test, i, -1);
634 }
635 
list_test_list_for_each_safe(struct kunit * test)636 static void list_test_list_for_each_safe(struct kunit *test)
637 {
638 	struct list_head entries[3], *cur, *n;
639 	LIST_HEAD(list);
640 	int i = 0;
641 
642 
643 	list_add_tail(&entries[0], &list);
644 	list_add_tail(&entries[1], &list);
645 	list_add_tail(&entries[2], &list);
646 
647 	list_for_each_safe(cur, n, &list) {
648 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
649 		list_del(&entries[i]);
650 		i++;
651 	}
652 
653 	KUNIT_EXPECT_EQ(test, i, 3);
654 	KUNIT_EXPECT_TRUE(test, list_empty(&list));
655 }
656 
list_test_list_for_each_prev_safe(struct kunit * test)657 static void list_test_list_for_each_prev_safe(struct kunit *test)
658 {
659 	struct list_head entries[3], *cur, *n;
660 	LIST_HEAD(list);
661 	int i = 2;
662 
663 	list_add_tail(&entries[0], &list);
664 	list_add_tail(&entries[1], &list);
665 	list_add_tail(&entries[2], &list);
666 
667 	list_for_each_prev_safe(cur, n, &list) {
668 		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
669 		list_del(&entries[i]);
670 		i--;
671 	}
672 
673 	KUNIT_EXPECT_EQ(test, i, -1);
674 	KUNIT_EXPECT_TRUE(test, list_empty(&list));
675 }
676 
list_test_list_for_each_entry(struct kunit * test)677 static void list_test_list_for_each_entry(struct kunit *test)
678 {
679 	struct list_test_struct entries[5], *cur;
680 	LIST_HEAD(list);
681 	int i = 0;
682 
683 	for (i = 0; i < 5; ++i) {
684 		entries[i].data = i;
685 		list_add_tail(&entries[i].list, &list);
686 	}
687 
688 	i = 0;
689 
690 	list_for_each_entry(cur, &list, list) {
691 		KUNIT_EXPECT_EQ(test, cur->data, i);
692 		i++;
693 	}
694 
695 	KUNIT_EXPECT_EQ(test, i, 5);
696 }
697 
list_test_list_for_each_entry_reverse(struct kunit * test)698 static void list_test_list_for_each_entry_reverse(struct kunit *test)
699 {
700 	struct list_test_struct entries[5], *cur;
701 	LIST_HEAD(list);
702 	int i = 0;
703 
704 	for (i = 0; i < 5; ++i) {
705 		entries[i].data = i;
706 		list_add_tail(&entries[i].list, &list);
707 	}
708 
709 	i = 4;
710 
711 	list_for_each_entry_reverse(cur, &list, list) {
712 		KUNIT_EXPECT_EQ(test, cur->data, i);
713 		i--;
714 	}
715 
716 	KUNIT_EXPECT_EQ(test, i, -1);
717 }
718 
719 static struct kunit_case list_test_cases[] = {
720 	KUNIT_CASE(list_test_list_init),
721 	KUNIT_CASE(list_test_list_add),
722 	KUNIT_CASE(list_test_list_add_tail),
723 	KUNIT_CASE(list_test_list_del),
724 	KUNIT_CASE(list_test_list_replace),
725 	KUNIT_CASE(list_test_list_replace_init),
726 	KUNIT_CASE(list_test_list_swap),
727 	KUNIT_CASE(list_test_list_del_init),
728 	KUNIT_CASE(list_test_list_move),
729 	KUNIT_CASE(list_test_list_move_tail),
730 	KUNIT_CASE(list_test_list_bulk_move_tail),
731 	KUNIT_CASE(list_test_list_is_head),
732 	KUNIT_CASE(list_test_list_is_first),
733 	KUNIT_CASE(list_test_list_is_last),
734 	KUNIT_CASE(list_test_list_empty),
735 	KUNIT_CASE(list_test_list_empty_careful),
736 	KUNIT_CASE(list_test_list_rotate_left),
737 	KUNIT_CASE(list_test_list_rotate_to_front),
738 	KUNIT_CASE(list_test_list_is_singular),
739 	KUNIT_CASE(list_test_list_cut_position),
740 	KUNIT_CASE(list_test_list_cut_before),
741 	KUNIT_CASE(list_test_list_splice),
742 	KUNIT_CASE(list_test_list_splice_tail),
743 	KUNIT_CASE(list_test_list_splice_init),
744 	KUNIT_CASE(list_test_list_splice_tail_init),
745 	KUNIT_CASE(list_test_list_entry),
746 	KUNIT_CASE(list_test_list_first_entry),
747 	KUNIT_CASE(list_test_list_last_entry),
748 	KUNIT_CASE(list_test_list_first_entry_or_null),
749 	KUNIT_CASE(list_test_list_next_entry),
750 	KUNIT_CASE(list_test_list_prev_entry),
751 	KUNIT_CASE(list_test_list_for_each),
752 	KUNIT_CASE(list_test_list_for_each_prev),
753 	KUNIT_CASE(list_test_list_for_each_safe),
754 	KUNIT_CASE(list_test_list_for_each_prev_safe),
755 	KUNIT_CASE(list_test_list_for_each_entry),
756 	KUNIT_CASE(list_test_list_for_each_entry_reverse),
757 	{},
758 };
759 
760 static struct kunit_suite list_test_module = {
761 	.name = "list-kunit-test",
762 	.test_cases = list_test_cases,
763 };
764 
765 kunit_test_suites(&list_test_module);
766 
767 MODULE_LICENSE("GPL v2");
768