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_del_init_careful(struct kunit *test)
166 /* NOTE: This test only checks the behaviour of this function in
167 * isolation. It does not verify memory model guarantees.
169 struct list_head a, b;
172 list_add_tail(&a, &list);
173 list_add_tail(&b, &list);
175 /* before: [list] -> a -> b */
176 list_del_init_careful(&a);
177 /* after: [list] -> b, a initialised */
179 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
180 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
181 KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
184 static void list_test_list_move(struct kunit *test)
186 struct list_head a, b;
190 list_add_tail(&a, &list1);
191 list_add_tail(&b, &list2);
193 /* before: [list1] -> a, [list2] -> b */
194 list_move(&a, &list2);
195 /* after: [list1] empty, [list2] -> a -> b */
197 KUNIT_EXPECT_TRUE(test, list_empty(&list1));
199 KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
200 KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
203 static void list_test_list_move_tail(struct kunit *test)
205 struct list_head a, b;
209 list_add_tail(&a, &list1);
210 list_add_tail(&b, &list2);
212 /* before: [list1] -> a, [list2] -> b */
213 list_move_tail(&a, &list2);
214 /* after: [list1] empty, [list2] -> b -> a */
216 KUNIT_EXPECT_TRUE(test, list_empty(&list1));
218 KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
219 KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
222 static void list_test_list_bulk_move_tail(struct kunit *test)
224 struct list_head a, b, c, d, x, y;
225 struct list_head *list1_values[] = { &x, &b, &c, &y };
226 struct list_head *list2_values[] = { &a, &d };
227 struct list_head *ptr;
232 list_add_tail(&x, &list1);
233 list_add_tail(&y, &list1);
235 list_add_tail(&a, &list2);
236 list_add_tail(&b, &list2);
237 list_add_tail(&c, &list2);
238 list_add_tail(&d, &list2);
240 /* before: [list1] -> x -> y, [list2] -> a -> b -> c -> d */
241 list_bulk_move_tail(&y, &b, &c);
242 /* after: [list1] -> x -> b -> c -> y, [list2] -> a -> d */
244 list_for_each(ptr, &list1) {
245 KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
248 KUNIT_EXPECT_EQ(test, i, 4);
250 list_for_each(ptr, &list2) {
251 KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
254 KUNIT_EXPECT_EQ(test, i, 2);
257 static void list_test_list_is_head(struct kunit *test)
259 struct list_head a, b, c;
261 /* Two lists: [a] -> b, [c] */
264 list_add_tail(&b, &a);
266 KUNIT_EXPECT_TRUE_MSG(test, list_is_head(&a, &a),
267 "Head element of same list");
268 KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &b),
269 "Non-head element of same list");
270 KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &c),
271 "Head element of different list");
275 static void list_test_list_is_first(struct kunit *test)
277 struct list_head a, b;
280 list_add_tail(&a, &list);
281 list_add_tail(&b, &list);
283 KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
284 KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
287 static void list_test_list_is_last(struct kunit *test)
289 struct list_head a, b;
292 list_add_tail(&a, &list);
293 list_add_tail(&b, &list);
295 KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
296 KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
299 static void list_test_list_empty(struct kunit *test)
305 list_add_tail(&a, &list1);
307 KUNIT_EXPECT_FALSE(test, list_empty(&list1));
308 KUNIT_EXPECT_TRUE(test, list_empty(&list2));
311 static void list_test_list_empty_careful(struct kunit *test)
313 /* This test doesn't check correctness under concurrent access */
318 list_add_tail(&a, &list1);
320 KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
321 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
324 static void list_test_list_rotate_left(struct kunit *test)
326 struct list_head a, b;
329 list_add_tail(&a, &list);
330 list_add_tail(&b, &list);
332 /* before: [list] -> a -> b */
333 list_rotate_left(&list);
334 /* after: [list] -> b -> a */
336 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
337 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
338 KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
341 static void list_test_list_rotate_to_front(struct kunit *test)
343 struct list_head a, b, c, d;
344 struct list_head *list_values[] = { &c, &d, &a, &b };
345 struct list_head *ptr;
349 list_add_tail(&a, &list);
350 list_add_tail(&b, &list);
351 list_add_tail(&c, &list);
352 list_add_tail(&d, &list);
354 /* before: [list] -> a -> b -> c -> d */
355 list_rotate_to_front(&c, &list);
356 /* after: [list] -> c -> d -> a -> b */
358 list_for_each(ptr, &list) {
359 KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
362 KUNIT_EXPECT_EQ(test, i, 4);
365 static void list_test_list_is_singular(struct kunit *test)
367 struct list_head a, b;
371 KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
373 list_add_tail(&a, &list);
376 KUNIT_EXPECT_TRUE(test, list_is_singular(&list));
378 list_add_tail(&b, &list);
380 /* [list] -> a -> b */
381 KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
384 static void list_test_list_cut_position(struct kunit *test)
386 struct list_head entries[3], *cur;
391 list_add_tail(&entries[0], &list1);
392 list_add_tail(&entries[1], &list1);
393 list_add_tail(&entries[2], &list1);
395 /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
396 list_cut_position(&list2, &list1, &entries[1]);
397 /* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */
399 list_for_each(cur, &list2) {
400 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
404 KUNIT_EXPECT_EQ(test, i, 2);
406 list_for_each(cur, &list1) {
407 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
412 static void list_test_list_cut_before(struct kunit *test)
414 struct list_head entries[3], *cur;
419 list_add_tail(&entries[0], &list1);
420 list_add_tail(&entries[1], &list1);
421 list_add_tail(&entries[2], &list1);
423 /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
424 list_cut_before(&list2, &list1, &entries[1]);
425 /* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */
427 list_for_each(cur, &list2) {
428 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
432 KUNIT_EXPECT_EQ(test, i, 1);
434 list_for_each(cur, &list1) {
435 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
440 static void list_test_list_splice(struct kunit *test)
442 struct list_head entries[5], *cur;
447 list_add_tail(&entries[0], &list1);
448 list_add_tail(&entries[1], &list1);
449 list_add_tail(&entries[2], &list2);
450 list_add_tail(&entries[3], &list2);
451 list_add_tail(&entries[4], &list1);
453 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
454 list_splice(&list2, &entries[1]);
455 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
457 list_for_each(cur, &list1) {
458 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
462 KUNIT_EXPECT_EQ(test, i, 5);
465 static void list_test_list_splice_tail(struct kunit *test)
467 struct list_head entries[5], *cur;
472 list_add_tail(&entries[0], &list1);
473 list_add_tail(&entries[1], &list1);
474 list_add_tail(&entries[2], &list2);
475 list_add_tail(&entries[3], &list2);
476 list_add_tail(&entries[4], &list1);
478 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
479 list_splice_tail(&list2, &entries[4]);
480 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
482 list_for_each(cur, &list1) {
483 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
487 KUNIT_EXPECT_EQ(test, i, 5);
490 static void list_test_list_splice_init(struct kunit *test)
492 struct list_head entries[5], *cur;
497 list_add_tail(&entries[0], &list1);
498 list_add_tail(&entries[1], &list1);
499 list_add_tail(&entries[2], &list2);
500 list_add_tail(&entries[3], &list2);
501 list_add_tail(&entries[4], &list1);
503 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
504 list_splice_init(&list2, &entries[1]);
505 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
507 list_for_each(cur, &list1) {
508 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
512 KUNIT_EXPECT_EQ(test, i, 5);
514 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
517 static void list_test_list_splice_tail_init(struct kunit *test)
519 struct list_head entries[5], *cur;
524 list_add_tail(&entries[0], &list1);
525 list_add_tail(&entries[1], &list1);
526 list_add_tail(&entries[2], &list2);
527 list_add_tail(&entries[3], &list2);
528 list_add_tail(&entries[4], &list1);
530 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
531 list_splice_tail_init(&list2, &entries[4]);
532 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
534 list_for_each(cur, &list1) {
535 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
539 KUNIT_EXPECT_EQ(test, i, 5);
541 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
544 static void list_test_list_entry(struct kunit *test)
546 struct list_test_struct test_struct;
548 KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
549 struct list_test_struct, list));
552 static void list_test_list_entry_is_head(struct kunit *test)
554 struct list_test_struct test_struct1, test_struct2, test_struct3;
556 INIT_LIST_HEAD(&test_struct1.list);
557 INIT_LIST_HEAD(&test_struct3.list);
559 list_add_tail(&test_struct2.list, &test_struct1.list);
561 KUNIT_EXPECT_TRUE_MSG(test,
562 list_entry_is_head((&test_struct1), &test_struct1.list, list),
563 "Head element of same list");
564 KUNIT_EXPECT_FALSE_MSG(test,
565 list_entry_is_head((&test_struct2), &test_struct1.list, list),
566 "Non-head element of same list");
567 KUNIT_EXPECT_FALSE_MSG(test,
568 list_entry_is_head((&test_struct3), &test_struct1.list, list),
569 "Head element of different list");
572 static void list_test_list_first_entry(struct kunit *test)
574 struct list_test_struct test_struct1, test_struct2;
577 list_add_tail(&test_struct1.list, &list);
578 list_add_tail(&test_struct2.list, &list);
581 KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
582 struct list_test_struct, list));
585 static void list_test_list_last_entry(struct kunit *test)
587 struct list_test_struct test_struct1, test_struct2;
590 list_add_tail(&test_struct1.list, &list);
591 list_add_tail(&test_struct2.list, &list);
594 KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
595 struct list_test_struct, list));
598 static void list_test_list_first_entry_or_null(struct kunit *test)
600 struct list_test_struct test_struct1, test_struct2;
603 KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
604 struct list_test_struct, list));
606 list_add_tail(&test_struct1.list, &list);
607 list_add_tail(&test_struct2.list, &list);
609 KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
610 list_first_entry_or_null(&list,
611 struct list_test_struct, list));
614 static void list_test_list_next_entry(struct kunit *test)
616 struct list_test_struct test_struct1, test_struct2;
619 list_add_tail(&test_struct1.list, &list);
620 list_add_tail(&test_struct2.list, &list);
623 KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
627 static void list_test_list_prev_entry(struct kunit *test)
629 struct list_test_struct test_struct1, test_struct2;
632 list_add_tail(&test_struct1.list, &list);
633 list_add_tail(&test_struct2.list, &list);
636 KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
640 static void list_test_list_for_each(struct kunit *test)
642 struct list_head entries[3], *cur;
646 list_add_tail(&entries[0], &list);
647 list_add_tail(&entries[1], &list);
648 list_add_tail(&entries[2], &list);
650 list_for_each(cur, &list) {
651 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
655 KUNIT_EXPECT_EQ(test, i, 3);
658 static void list_test_list_for_each_prev(struct kunit *test)
660 struct list_head entries[3], *cur;
664 list_add_tail(&entries[0], &list);
665 list_add_tail(&entries[1], &list);
666 list_add_tail(&entries[2], &list);
668 list_for_each_prev(cur, &list) {
669 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
673 KUNIT_EXPECT_EQ(test, i, -1);
676 static void list_test_list_for_each_safe(struct kunit *test)
678 struct list_head entries[3], *cur, *n;
683 list_add_tail(&entries[0], &list);
684 list_add_tail(&entries[1], &list);
685 list_add_tail(&entries[2], &list);
687 list_for_each_safe(cur, n, &list) {
688 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
689 list_del(&entries[i]);
693 KUNIT_EXPECT_EQ(test, i, 3);
694 KUNIT_EXPECT_TRUE(test, list_empty(&list));
697 static void list_test_list_for_each_prev_safe(struct kunit *test)
699 struct list_head entries[3], *cur, *n;
703 list_add_tail(&entries[0], &list);
704 list_add_tail(&entries[1], &list);
705 list_add_tail(&entries[2], &list);
707 list_for_each_prev_safe(cur, n, &list) {
708 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
709 list_del(&entries[i]);
713 KUNIT_EXPECT_EQ(test, i, -1);
714 KUNIT_EXPECT_TRUE(test, list_empty(&list));
717 static void list_test_list_for_each_entry(struct kunit *test)
719 struct list_test_struct entries[5], *cur;
723 for (i = 0; i < 5; ++i) {
725 list_add_tail(&entries[i].list, &list);
730 list_for_each_entry(cur, &list, list) {
731 KUNIT_EXPECT_EQ(test, cur->data, i);
735 KUNIT_EXPECT_EQ(test, i, 5);
738 static void list_test_list_for_each_entry_reverse(struct kunit *test)
740 struct list_test_struct entries[5], *cur;
744 for (i = 0; i < 5; ++i) {
746 list_add_tail(&entries[i].list, &list);
751 list_for_each_entry_reverse(cur, &list, list) {
752 KUNIT_EXPECT_EQ(test, cur->data, i);
756 KUNIT_EXPECT_EQ(test, i, -1);
759 static struct kunit_case list_test_cases[] = {
760 KUNIT_CASE(list_test_list_init),
761 KUNIT_CASE(list_test_list_add),
762 KUNIT_CASE(list_test_list_add_tail),
763 KUNIT_CASE(list_test_list_del),
764 KUNIT_CASE(list_test_list_replace),
765 KUNIT_CASE(list_test_list_replace_init),
766 KUNIT_CASE(list_test_list_swap),
767 KUNIT_CASE(list_test_list_del_init),
768 KUNIT_CASE(list_test_list_del_init_careful),
769 KUNIT_CASE(list_test_list_move),
770 KUNIT_CASE(list_test_list_move_tail),
771 KUNIT_CASE(list_test_list_bulk_move_tail),
772 KUNIT_CASE(list_test_list_is_head),
773 KUNIT_CASE(list_test_list_is_first),
774 KUNIT_CASE(list_test_list_is_last),
775 KUNIT_CASE(list_test_list_empty),
776 KUNIT_CASE(list_test_list_empty_careful),
777 KUNIT_CASE(list_test_list_rotate_left),
778 KUNIT_CASE(list_test_list_rotate_to_front),
779 KUNIT_CASE(list_test_list_is_singular),
780 KUNIT_CASE(list_test_list_cut_position),
781 KUNIT_CASE(list_test_list_cut_before),
782 KUNIT_CASE(list_test_list_splice),
783 KUNIT_CASE(list_test_list_splice_tail),
784 KUNIT_CASE(list_test_list_splice_init),
785 KUNIT_CASE(list_test_list_splice_tail_init),
786 KUNIT_CASE(list_test_list_entry),
787 KUNIT_CASE(list_test_list_entry_is_head),
788 KUNIT_CASE(list_test_list_first_entry),
789 KUNIT_CASE(list_test_list_last_entry),
790 KUNIT_CASE(list_test_list_first_entry_or_null),
791 KUNIT_CASE(list_test_list_next_entry),
792 KUNIT_CASE(list_test_list_prev_entry),
793 KUNIT_CASE(list_test_list_for_each),
794 KUNIT_CASE(list_test_list_for_each_prev),
795 KUNIT_CASE(list_test_list_for_each_safe),
796 KUNIT_CASE(list_test_list_for_each_prev_safe),
797 KUNIT_CASE(list_test_list_for_each_entry),
798 KUNIT_CASE(list_test_list_for_each_entry_reverse),
802 static struct kunit_suite list_test_module = {
803 .name = "list-kunit-test",
804 .test_cases = list_test_cases,
807 struct hlist_test_struct {
809 struct hlist_node list;
812 static void hlist_test_init(struct kunit *test)
814 /* Test the different ways of initialising a list. */
815 struct hlist_head list1 = HLIST_HEAD_INIT;
816 struct hlist_head list2;
818 struct hlist_head *list4;
819 struct hlist_head *list5;
821 INIT_HLIST_HEAD(&list2);
823 list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
824 INIT_HLIST_HEAD(list4);
826 list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
827 memset(list5, 0xFF, sizeof(*list5));
828 INIT_HLIST_HEAD(list5);
830 KUNIT_EXPECT_TRUE(test, hlist_empty(&list1));
831 KUNIT_EXPECT_TRUE(test, hlist_empty(&list2));
832 KUNIT_EXPECT_TRUE(test, hlist_empty(&list3));
833 KUNIT_EXPECT_TRUE(test, hlist_empty(list4));
834 KUNIT_EXPECT_TRUE(test, hlist_empty(list5));
840 static void hlist_test_unhashed(struct kunit *test)
847 /* is unhashed by default */
848 KUNIT_EXPECT_TRUE(test, hlist_unhashed(&a));
850 hlist_add_head(&a, &list);
852 /* is hashed once added to list */
853 KUNIT_EXPECT_FALSE(test, hlist_unhashed(&a));
857 /* is again unhashed after del_init */
858 KUNIT_EXPECT_TRUE(test, hlist_unhashed(&a));
861 /* Doesn't test concurrency guarantees */
862 static void hlist_test_unhashed_lockless(struct kunit *test)
869 /* is unhashed by default */
870 KUNIT_EXPECT_TRUE(test, hlist_unhashed_lockless(&a));
872 hlist_add_head(&a, &list);
874 /* is hashed once added to list */
875 KUNIT_EXPECT_FALSE(test, hlist_unhashed_lockless(&a));
879 /* is again unhashed after del_init */
880 KUNIT_EXPECT_TRUE(test, hlist_unhashed_lockless(&a));
883 static void hlist_test_del(struct kunit *test)
885 struct hlist_node a, b;
888 hlist_add_head(&a, &list);
889 hlist_add_behind(&b, &a);
891 /* before: [list] -> a -> b */
894 /* now: [list] -> b */
895 KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
896 KUNIT_EXPECT_PTR_EQ(test, b.pprev, &list.first);
899 static void hlist_test_del_init(struct kunit *test)
901 struct hlist_node a, b;
904 hlist_add_head(&a, &list);
905 hlist_add_behind(&b, &a);
907 /* before: [list] -> a -> b */
910 /* now: [list] -> b */
911 KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
912 KUNIT_EXPECT_PTR_EQ(test, b.pprev, &list.first);
914 /* a is now initialised */
915 KUNIT_EXPECT_PTR_EQ(test, a.next, NULL);
916 KUNIT_EXPECT_PTR_EQ(test, a.pprev, NULL);
919 /* Tests all three hlist_add_* functions */
920 static void hlist_test_add(struct kunit *test)
922 struct hlist_node a, b, c, d;
925 hlist_add_head(&a, &list);
926 hlist_add_head(&b, &list);
927 hlist_add_before(&c, &a);
928 hlist_add_behind(&d, &a);
930 /* should be [list] -> b -> c -> a -> d */
931 KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
933 KUNIT_EXPECT_PTR_EQ(test, c.pprev, &(b.next));
934 KUNIT_EXPECT_PTR_EQ(test, b.next, &c);
936 KUNIT_EXPECT_PTR_EQ(test, a.pprev, &(c.next));
937 KUNIT_EXPECT_PTR_EQ(test, c.next, &a);
939 KUNIT_EXPECT_PTR_EQ(test, d.pprev, &(a.next));
940 KUNIT_EXPECT_PTR_EQ(test, a.next, &d);
943 /* Tests both hlist_fake() and hlist_add_fake() */
944 static void hlist_test_fake(struct kunit *test)
950 /* not fake after init */
951 KUNIT_EXPECT_FALSE(test, hlist_fake(&a));
956 KUNIT_EXPECT_TRUE(test, hlist_fake(&a));
959 static void hlist_test_is_singular_node(struct kunit *test)
961 struct hlist_node a, b;
965 KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&a, &list));
967 hlist_add_head(&a, &list);
968 KUNIT_EXPECT_TRUE(test, hlist_is_singular_node(&a, &list));
970 hlist_add_head(&b, &list);
971 KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&a, &list));
972 KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&b, &list));
975 static void hlist_test_empty(struct kunit *test)
980 /* list starts off empty */
981 KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
983 hlist_add_head(&a, &list);
985 /* list is no longer empty */
986 KUNIT_EXPECT_FALSE(test, hlist_empty(&list));
989 static void hlist_test_move_list(struct kunit *test)
995 hlist_add_head(&a, &list1);
997 KUNIT_EXPECT_FALSE(test, hlist_empty(&list1));
998 KUNIT_EXPECT_TRUE(test, hlist_empty(&list2));
999 hlist_move_list(&list1, &list2);
1000 KUNIT_EXPECT_TRUE(test, hlist_empty(&list1));
1001 KUNIT_EXPECT_FALSE(test, hlist_empty(&list2));
1005 static void hlist_test_entry(struct kunit *test)
1007 struct hlist_test_struct test_struct;
1009 KUNIT_EXPECT_PTR_EQ(test, &test_struct,
1010 hlist_entry(&(test_struct.list),
1011 struct hlist_test_struct, list));
1014 static void hlist_test_entry_safe(struct kunit *test)
1016 struct hlist_test_struct test_struct;
1018 KUNIT_EXPECT_PTR_EQ(test, &test_struct,
1019 hlist_entry_safe(&(test_struct.list),
1020 struct hlist_test_struct, list));
1022 KUNIT_EXPECT_PTR_EQ(test, NULL,
1023 hlist_entry_safe((struct hlist_node *)NULL,
1024 struct hlist_test_struct, list));
1027 static void hlist_test_for_each(struct kunit *test)
1029 struct hlist_node entries[3], *cur;
1033 hlist_add_head(&entries[0], &list);
1034 hlist_add_behind(&entries[1], &entries[0]);
1035 hlist_add_behind(&entries[2], &entries[1]);
1037 hlist_for_each(cur, &list) {
1038 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
1042 KUNIT_EXPECT_EQ(test, i, 3);
1046 static void hlist_test_for_each_safe(struct kunit *test)
1048 struct hlist_node entries[3], *cur, *n;
1052 hlist_add_head(&entries[0], &list);
1053 hlist_add_behind(&entries[1], &entries[0]);
1054 hlist_add_behind(&entries[2], &entries[1]);
1056 hlist_for_each_safe(cur, n, &list) {
1057 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
1058 hlist_del(&entries[i]);
1062 KUNIT_EXPECT_EQ(test, i, 3);
1063 KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
1066 static void hlist_test_for_each_entry(struct kunit *test)
1068 struct hlist_test_struct entries[5], *cur;
1072 entries[0].data = 0;
1073 hlist_add_head(&entries[0].list, &list);
1074 for (i = 1; i < 5; ++i) {
1075 entries[i].data = i;
1076 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1081 hlist_for_each_entry(cur, &list, list) {
1082 KUNIT_EXPECT_EQ(test, cur->data, i);
1086 KUNIT_EXPECT_EQ(test, i, 5);
1089 static void hlist_test_for_each_entry_continue(struct kunit *test)
1091 struct hlist_test_struct entries[5], *cur;
1095 entries[0].data = 0;
1096 hlist_add_head(&entries[0].list, &list);
1097 for (i = 1; i < 5; ++i) {
1098 entries[i].data = i;
1099 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1102 /* We skip the first (zero-th) entry. */
1106 hlist_for_each_entry_continue(cur, list) {
1107 KUNIT_EXPECT_EQ(test, cur->data, i);
1108 /* Stamp over the entry. */
1113 KUNIT_EXPECT_EQ(test, i, 5);
1114 /* The first entry was not visited. */
1115 KUNIT_EXPECT_EQ(test, entries[0].data, 0);
1116 /* The second (and presumably others), were. */
1117 KUNIT_EXPECT_EQ(test, entries[1].data, 42);
1120 static void hlist_test_for_each_entry_from(struct kunit *test)
1122 struct hlist_test_struct entries[5], *cur;
1126 entries[0].data = 0;
1127 hlist_add_head(&entries[0].list, &list);
1128 for (i = 1; i < 5; ++i) {
1129 entries[i].data = i;
1130 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1136 hlist_for_each_entry_from(cur, list) {
1137 KUNIT_EXPECT_EQ(test, cur->data, i);
1138 /* Stamp over the entry. */
1143 KUNIT_EXPECT_EQ(test, i, 5);
1144 /* The first entry was visited. */
1145 KUNIT_EXPECT_EQ(test, entries[0].data, 42);
1148 static void hlist_test_for_each_entry_safe(struct kunit *test)
1150 struct hlist_test_struct entries[5], *cur;
1151 struct hlist_node *tmp_node;
1155 entries[0].data = 0;
1156 hlist_add_head(&entries[0].list, &list);
1157 for (i = 1; i < 5; ++i) {
1158 entries[i].data = i;
1159 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1164 hlist_for_each_entry_safe(cur, tmp_node, &list, list) {
1165 KUNIT_EXPECT_EQ(test, cur->data, i);
1166 hlist_del(&cur->list);
1170 KUNIT_EXPECT_EQ(test, i, 5);
1171 KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
1175 static struct kunit_case hlist_test_cases[] = {
1176 KUNIT_CASE(hlist_test_init),
1177 KUNIT_CASE(hlist_test_unhashed),
1178 KUNIT_CASE(hlist_test_unhashed_lockless),
1179 KUNIT_CASE(hlist_test_del),
1180 KUNIT_CASE(hlist_test_del_init),
1181 KUNIT_CASE(hlist_test_add),
1182 KUNIT_CASE(hlist_test_fake),
1183 KUNIT_CASE(hlist_test_is_singular_node),
1184 KUNIT_CASE(hlist_test_empty),
1185 KUNIT_CASE(hlist_test_move_list),
1186 KUNIT_CASE(hlist_test_entry),
1187 KUNIT_CASE(hlist_test_entry_safe),
1188 KUNIT_CASE(hlist_test_for_each),
1189 KUNIT_CASE(hlist_test_for_each_safe),
1190 KUNIT_CASE(hlist_test_for_each_entry),
1191 KUNIT_CASE(hlist_test_for_each_entry_continue),
1192 KUNIT_CASE(hlist_test_for_each_entry_from),
1193 KUNIT_CASE(hlist_test_for_each_entry_safe),
1197 static struct kunit_suite hlist_test_module = {
1199 .test_cases = hlist_test_cases,
1202 kunit_test_suites(&list_test_module, &hlist_test_module);
1204 MODULE_LICENSE("GPL v2");