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