1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for the Kernel Linked-list structures.
5 * Copyright (C) 2019, Google LLC.
6 * Author: David Gow <davidgow@google.com>
8 #include <kunit/test.h>
10 #include <linux/list.h>
12 struct list_test_struct {
14 struct list_head list;
17 static void list_test_list_init(struct kunit *test)
19 /* Test the different ways of initialising a list. */
20 struct list_head list1 = LIST_HEAD_INIT(list1);
21 struct list_head list2;
23 struct list_head *list4;
24 struct list_head *list5;
26 INIT_LIST_HEAD(&list2);
28 list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
29 INIT_LIST_HEAD(list4);
31 list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
32 memset(list5, 0xFF, sizeof(*list5));
33 INIT_LIST_HEAD(list5);
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));
46 static void list_test_list_add(struct kunit *test)
48 struct list_head a, b;
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);
60 static void list_test_list_add_tail(struct kunit *test)
62 struct list_head a, b;
65 list_add_tail(&a, &list);
66 list_add_tail(&b, &list);
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);
74 static void list_test_list_del(struct kunit *test)
76 struct list_head a, b;
79 list_add_tail(&a, &list);
80 list_add_tail(&b, &list);
82 /* before: [list] -> a -> b */
85 /* now: [list] -> b */
86 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
87 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
90 static void list_test_list_replace(struct kunit *test)
92 struct list_head a_old, a_new, b;
95 list_add_tail(&a_old, &list);
96 list_add_tail(&b, &list);
98 /* before: [list] -> a_old -> b */
99 list_replace(&a_old, &a_new);
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);
106 static void list_test_list_replace_init(struct kunit *test)
108 struct list_head a_old, a_new, b;
111 list_add_tail(&a_old, &list);
112 list_add_tail(&b, &list);
114 /* before: [list] -> a_old -> b */
115 list_replace_init(&a_old, &a_new);
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);
121 /* check a_old is empty (initialized) */
122 KUNIT_EXPECT_TRUE(test, list_empty_careful(&a_old));
125 static void list_test_list_swap(struct kunit *test)
127 struct list_head a, b;
130 list_add_tail(&a, &list);
131 list_add_tail(&b, &list);
133 /* before: [list] -> a -> b */
136 /* after: [list] -> b -> a */
137 KUNIT_EXPECT_PTR_EQ(test, &b, list.next);
138 KUNIT_EXPECT_PTR_EQ(test, &a, list.prev);
140 KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
141 KUNIT_EXPECT_PTR_EQ(test, &list, b.prev);
143 KUNIT_EXPECT_PTR_EQ(test, &list, a.next);
144 KUNIT_EXPECT_PTR_EQ(test, &b, a.prev);
147 static void list_test_list_del_init(struct kunit *test)
149 struct list_head a, b;
152 list_add_tail(&a, &list);
153 list_add_tail(&b, &list);
155 /* before: [list] -> a -> b */
157 /* after: [list] -> b, a initialised */
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));
164 static void list_test_list_move(struct kunit *test)
166 struct list_head a, b;
170 list_add_tail(&a, &list1);
171 list_add_tail(&b, &list2);
173 /* before: [list1] -> a, [list2] -> b */
174 list_move(&a, &list2);
175 /* after: [list1] empty, [list2] -> a -> b */
177 KUNIT_EXPECT_TRUE(test, list_empty(&list1));
179 KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
180 KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
183 static void list_test_list_move_tail(struct kunit *test)
185 struct list_head a, b;
189 list_add_tail(&a, &list1);
190 list_add_tail(&b, &list2);
192 /* before: [list1] -> a, [list2] -> b */
193 list_move_tail(&a, &list2);
194 /* after: [list1] empty, [list2] -> b -> a */
196 KUNIT_EXPECT_TRUE(test, list_empty(&list1));
198 KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
199 KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
202 static void list_test_list_bulk_move_tail(struct kunit *test)
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;
212 list_add_tail(&x, &list1);
213 list_add_tail(&y, &list1);
215 list_add_tail(&a, &list2);
216 list_add_tail(&b, &list2);
217 list_add_tail(&c, &list2);
218 list_add_tail(&d, &list2);
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 */
224 list_for_each(ptr, &list1) {
225 KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
228 KUNIT_EXPECT_EQ(test, i, 4);
230 list_for_each(ptr, &list2) {
231 KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
234 KUNIT_EXPECT_EQ(test, i, 2);
237 static void list_test_list_is_first(struct kunit *test)
239 struct list_head a, b;
242 list_add_tail(&a, &list);
243 list_add_tail(&b, &list);
245 KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
246 KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
249 static void list_test_list_is_last(struct kunit *test)
251 struct list_head a, b;
254 list_add_tail(&a, &list);
255 list_add_tail(&b, &list);
257 KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
258 KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
261 static void list_test_list_empty(struct kunit *test)
267 list_add_tail(&a, &list1);
269 KUNIT_EXPECT_FALSE(test, list_empty(&list1));
270 KUNIT_EXPECT_TRUE(test, list_empty(&list2));
273 static void list_test_list_empty_careful(struct kunit *test)
275 /* This test doesn't check correctness under concurrent access */
280 list_add_tail(&a, &list1);
282 KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
283 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
286 static void list_test_list_rotate_left(struct kunit *test)
288 struct list_head a, b;
291 list_add_tail(&a, &list);
292 list_add_tail(&b, &list);
294 /* before: [list] -> a -> b */
295 list_rotate_left(&list);
296 /* after: [list] -> b -> a */
298 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
299 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
300 KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
303 static void list_test_list_rotate_to_front(struct kunit *test)
305 struct list_head a, b, c, d;
306 struct list_head *list_values[] = { &c, &d, &a, &b };
307 struct list_head *ptr;
311 list_add_tail(&a, &list);
312 list_add_tail(&b, &list);
313 list_add_tail(&c, &list);
314 list_add_tail(&d, &list);
316 /* before: [list] -> a -> b -> c -> d */
317 list_rotate_to_front(&c, &list);
318 /* after: [list] -> c -> d -> a -> b */
320 list_for_each(ptr, &list) {
321 KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
324 KUNIT_EXPECT_EQ(test, i, 4);
327 static void list_test_list_is_singular(struct kunit *test)
329 struct list_head a, b;
333 KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
335 list_add_tail(&a, &list);
338 KUNIT_EXPECT_TRUE(test, list_is_singular(&list));
340 list_add_tail(&b, &list);
342 /* [list] -> a -> b */
343 KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
346 static void list_test_list_cut_position(struct kunit *test)
348 struct list_head entries[3], *cur;
353 list_add_tail(&entries[0], &list1);
354 list_add_tail(&entries[1], &list1);
355 list_add_tail(&entries[2], &list1);
357 /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
358 list_cut_position(&list2, &list1, &entries[1]);
359 /* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */
361 list_for_each(cur, &list2) {
362 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
366 KUNIT_EXPECT_EQ(test, i, 2);
368 list_for_each(cur, &list1) {
369 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
374 static void list_test_list_cut_before(struct kunit *test)
376 struct list_head entries[3], *cur;
381 list_add_tail(&entries[0], &list1);
382 list_add_tail(&entries[1], &list1);
383 list_add_tail(&entries[2], &list1);
385 /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
386 list_cut_before(&list2, &list1, &entries[1]);
387 /* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */
389 list_for_each(cur, &list2) {
390 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
394 KUNIT_EXPECT_EQ(test, i, 1);
396 list_for_each(cur, &list1) {
397 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
402 static void list_test_list_splice(struct kunit *test)
404 struct list_head entries[5], *cur;
409 list_add_tail(&entries[0], &list1);
410 list_add_tail(&entries[1], &list1);
411 list_add_tail(&entries[2], &list2);
412 list_add_tail(&entries[3], &list2);
413 list_add_tail(&entries[4], &list1);
415 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
416 list_splice(&list2, &entries[1]);
417 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
419 list_for_each(cur, &list1) {
420 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
424 KUNIT_EXPECT_EQ(test, i, 5);
427 static void list_test_list_splice_tail(struct kunit *test)
429 struct list_head entries[5], *cur;
434 list_add_tail(&entries[0], &list1);
435 list_add_tail(&entries[1], &list1);
436 list_add_tail(&entries[2], &list2);
437 list_add_tail(&entries[3], &list2);
438 list_add_tail(&entries[4], &list1);
440 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
441 list_splice_tail(&list2, &entries[4]);
442 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
444 list_for_each(cur, &list1) {
445 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
449 KUNIT_EXPECT_EQ(test, i, 5);
452 static void list_test_list_splice_init(struct kunit *test)
454 struct list_head entries[5], *cur;
459 list_add_tail(&entries[0], &list1);
460 list_add_tail(&entries[1], &list1);
461 list_add_tail(&entries[2], &list2);
462 list_add_tail(&entries[3], &list2);
463 list_add_tail(&entries[4], &list1);
465 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
466 list_splice_init(&list2, &entries[1]);
467 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
469 list_for_each(cur, &list1) {
470 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
474 KUNIT_EXPECT_EQ(test, i, 5);
476 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
479 static void list_test_list_splice_tail_init(struct kunit *test)
481 struct list_head entries[5], *cur;
486 list_add_tail(&entries[0], &list1);
487 list_add_tail(&entries[1], &list1);
488 list_add_tail(&entries[2], &list2);
489 list_add_tail(&entries[3], &list2);
490 list_add_tail(&entries[4], &list1);
492 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
493 list_splice_tail_init(&list2, &entries[4]);
494 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
496 list_for_each(cur, &list1) {
497 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
501 KUNIT_EXPECT_EQ(test, i, 5);
503 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
506 static void list_test_list_entry(struct kunit *test)
508 struct list_test_struct test_struct;
510 KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
511 struct list_test_struct, list));
514 static void list_test_list_first_entry(struct kunit *test)
516 struct list_test_struct test_struct1, test_struct2;
519 list_add_tail(&test_struct1.list, &list);
520 list_add_tail(&test_struct2.list, &list);
523 KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
524 struct list_test_struct, list));
527 static void list_test_list_last_entry(struct kunit *test)
529 struct list_test_struct test_struct1, test_struct2;
532 list_add_tail(&test_struct1.list, &list);
533 list_add_tail(&test_struct2.list, &list);
536 KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
537 struct list_test_struct, list));
540 static void list_test_list_first_entry_or_null(struct kunit *test)
542 struct list_test_struct test_struct1, test_struct2;
545 KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
546 struct list_test_struct, list));
548 list_add_tail(&test_struct1.list, &list);
549 list_add_tail(&test_struct2.list, &list);
551 KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
552 list_first_entry_or_null(&list,
553 struct list_test_struct, list));
556 static void list_test_list_next_entry(struct kunit *test)
558 struct list_test_struct test_struct1, test_struct2;
561 list_add_tail(&test_struct1.list, &list);
562 list_add_tail(&test_struct2.list, &list);
565 KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
569 static void list_test_list_prev_entry(struct kunit *test)
571 struct list_test_struct test_struct1, test_struct2;
574 list_add_tail(&test_struct1.list, &list);
575 list_add_tail(&test_struct2.list, &list);
578 KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
582 static void list_test_list_for_each(struct kunit *test)
584 struct list_head entries[3], *cur;
588 list_add_tail(&entries[0], &list);
589 list_add_tail(&entries[1], &list);
590 list_add_tail(&entries[2], &list);
592 list_for_each(cur, &list) {
593 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
597 KUNIT_EXPECT_EQ(test, i, 3);
600 static void list_test_list_for_each_prev(struct kunit *test)
602 struct list_head entries[3], *cur;
606 list_add_tail(&entries[0], &list);
607 list_add_tail(&entries[1], &list);
608 list_add_tail(&entries[2], &list);
610 list_for_each_prev(cur, &list) {
611 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
615 KUNIT_EXPECT_EQ(test, i, -1);
618 static void list_test_list_for_each_safe(struct kunit *test)
620 struct list_head entries[3], *cur, *n;
625 list_add_tail(&entries[0], &list);
626 list_add_tail(&entries[1], &list);
627 list_add_tail(&entries[2], &list);
629 list_for_each_safe(cur, n, &list) {
630 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
631 list_del(&entries[i]);
635 KUNIT_EXPECT_EQ(test, i, 3);
636 KUNIT_EXPECT_TRUE(test, list_empty(&list));
639 static void list_test_list_for_each_prev_safe(struct kunit *test)
641 struct list_head entries[3], *cur, *n;
645 list_add_tail(&entries[0], &list);
646 list_add_tail(&entries[1], &list);
647 list_add_tail(&entries[2], &list);
649 list_for_each_prev_safe(cur, n, &list) {
650 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
651 list_del(&entries[i]);
655 KUNIT_EXPECT_EQ(test, i, -1);
656 KUNIT_EXPECT_TRUE(test, list_empty(&list));
659 static void list_test_list_for_each_entry(struct kunit *test)
661 struct list_test_struct entries[5], *cur;
665 for (i = 0; i < 5; ++i) {
667 list_add_tail(&entries[i].list, &list);
672 list_for_each_entry(cur, &list, list) {
673 KUNIT_EXPECT_EQ(test, cur->data, i);
677 KUNIT_EXPECT_EQ(test, i, 5);
680 static void list_test_list_for_each_entry_reverse(struct kunit *test)
682 struct list_test_struct entries[5], *cur;
686 for (i = 0; i < 5; ++i) {
688 list_add_tail(&entries[i].list, &list);
693 list_for_each_entry_reverse(cur, &list, list) {
694 KUNIT_EXPECT_EQ(test, cur->data, i);
698 KUNIT_EXPECT_EQ(test, i, -1);
701 static struct kunit_case list_test_cases[] = {
702 KUNIT_CASE(list_test_list_init),
703 KUNIT_CASE(list_test_list_add),
704 KUNIT_CASE(list_test_list_add_tail),
705 KUNIT_CASE(list_test_list_del),
706 KUNIT_CASE(list_test_list_replace),
707 KUNIT_CASE(list_test_list_replace_init),
708 KUNIT_CASE(list_test_list_swap),
709 KUNIT_CASE(list_test_list_del_init),
710 KUNIT_CASE(list_test_list_move),
711 KUNIT_CASE(list_test_list_move_tail),
712 KUNIT_CASE(list_test_list_bulk_move_tail),
713 KUNIT_CASE(list_test_list_is_first),
714 KUNIT_CASE(list_test_list_is_last),
715 KUNIT_CASE(list_test_list_empty),
716 KUNIT_CASE(list_test_list_empty_careful),
717 KUNIT_CASE(list_test_list_rotate_left),
718 KUNIT_CASE(list_test_list_rotate_to_front),
719 KUNIT_CASE(list_test_list_is_singular),
720 KUNIT_CASE(list_test_list_cut_position),
721 KUNIT_CASE(list_test_list_cut_before),
722 KUNIT_CASE(list_test_list_splice),
723 KUNIT_CASE(list_test_list_splice_tail),
724 KUNIT_CASE(list_test_list_splice_init),
725 KUNIT_CASE(list_test_list_splice_tail_init),
726 KUNIT_CASE(list_test_list_entry),
727 KUNIT_CASE(list_test_list_first_entry),
728 KUNIT_CASE(list_test_list_last_entry),
729 KUNIT_CASE(list_test_list_first_entry_or_null),
730 KUNIT_CASE(list_test_list_next_entry),
731 KUNIT_CASE(list_test_list_prev_entry),
732 KUNIT_CASE(list_test_list_for_each),
733 KUNIT_CASE(list_test_list_for_each_prev),
734 KUNIT_CASE(list_test_list_for_each_safe),
735 KUNIT_CASE(list_test_list_for_each_prev_safe),
736 KUNIT_CASE(list_test_list_for_each_entry),
737 KUNIT_CASE(list_test_list_for_each_entry_reverse),
741 static struct kunit_suite list_test_module = {
742 .name = "list-kunit-test",
743 .test_cases = list_test_cases,
746 kunit_test_suites(&list_test_module);
748 MODULE_LICENSE("GPL v2");