18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * KUnit test for the Kernel Linked-list structures.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC.
68c2ecf20Sopenharmony_ci * Author: David Gow <davidgow@google.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <kunit/test.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/list.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistruct list_test_struct {
138c2ecf20Sopenharmony_ci	int data;
148c2ecf20Sopenharmony_ci	struct list_head list;
158c2ecf20Sopenharmony_ci};
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic void list_test_list_init(struct kunit *test)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	/* Test the different ways of initialising a list. */
208c2ecf20Sopenharmony_ci	struct list_head list1 = LIST_HEAD_INIT(list1);
218c2ecf20Sopenharmony_ci	struct list_head list2;
228c2ecf20Sopenharmony_ci	LIST_HEAD(list3);
238c2ecf20Sopenharmony_ci	struct list_head *list4;
248c2ecf20Sopenharmony_ci	struct list_head *list5;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&list2);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
298c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(list4);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
328c2ecf20Sopenharmony_ci	memset(list5, 0xFF, sizeof(*list5));
338c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(list5);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	/* list_empty_careful() checks both next and prev. */
368c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list1));
378c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
388c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list3));
398c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
408c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	kfree(list4);
438c2ecf20Sopenharmony_ci	kfree(list5);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic void list_test_list_add(struct kunit *test)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct list_head a, b;
498c2ecf20Sopenharmony_ci	LIST_HEAD(list);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	list_add(&a, &list);
528c2ecf20Sopenharmony_ci	list_add(&b, &list);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* should be [list] -> b -> a */
558c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
568c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
578c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void list_test_list_add_tail(struct kunit *test)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct list_head a, b;
638c2ecf20Sopenharmony_ci	LIST_HEAD(list);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
668c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/* should be [list] -> a -> b */
698c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &a);
708c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, a.prev, &list);
718c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, a.next, &b);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void list_test_list_del(struct kunit *test)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct list_head a, b;
778c2ecf20Sopenharmony_ci	LIST_HEAD(list);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
808c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	/* before: [list] -> a -> b */
838c2ecf20Sopenharmony_ci	list_del(&a);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* now: [list] -> b */
868c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
878c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void list_test_list_replace(struct kunit *test)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct list_head a_old, a_new, b;
938c2ecf20Sopenharmony_ci	LIST_HEAD(list);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	list_add_tail(&a_old, &list);
968c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/* before: [list] -> a_old -> b */
998c2ecf20Sopenharmony_ci	list_replace(&a_old, &a_new);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* now: [list] -> a_new -> b */
1028c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
1038c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic void list_test_list_replace_init(struct kunit *test)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct list_head a_old, a_new, b;
1098c2ecf20Sopenharmony_ci	LIST_HEAD(list);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	list_add_tail(&a_old, &list);
1128c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* before: [list] -> a_old -> b */
1158c2ecf20Sopenharmony_ci	list_replace_init(&a_old, &a_new);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* now: [list] -> a_new -> b */
1188c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
1198c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* check a_old is empty (initialized) */
1228c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a_old));
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic void list_test_list_swap(struct kunit *test)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct list_head a, b;
1288c2ecf20Sopenharmony_ci	LIST_HEAD(list);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
1318c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* before: [list] -> a -> b */
1348c2ecf20Sopenharmony_ci	list_swap(&a, &b);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* after: [list] -> b -> a */
1378c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &b, list.next);
1388c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &a, list.prev);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
1418c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &list, b.prev);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &list, a.next);
1448c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &b, a.prev);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic void list_test_list_del_init(struct kunit *test)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct list_head a, b;
1508c2ecf20Sopenharmony_ci	LIST_HEAD(list);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
1538c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* before: [list] -> a -> b */
1568c2ecf20Sopenharmony_ci	list_del_init(&a);
1578c2ecf20Sopenharmony_ci	/* after: [list] -> b, a initialised */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
1608c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
1618c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void list_test_list_move(struct kunit *test)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct list_head a, b;
1678c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
1688c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	list_add_tail(&a, &list1);
1718c2ecf20Sopenharmony_ci	list_add_tail(&b, &list2);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* before: [list1] -> a, [list2] -> b */
1748c2ecf20Sopenharmony_ci	list_move(&a, &list2);
1758c2ecf20Sopenharmony_ci	/* after: [list1] empty, [list2] -> a -> b */
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty(&list1));
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
1808c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic void list_test_list_move_tail(struct kunit *test)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct list_head a, b;
1868c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
1878c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	list_add_tail(&a, &list1);
1908c2ecf20Sopenharmony_ci	list_add_tail(&b, &list2);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* before: [list1] -> a, [list2] -> b */
1938c2ecf20Sopenharmony_ci	list_move_tail(&a, &list2);
1948c2ecf20Sopenharmony_ci	/* after: [list1] empty, [list2] -> b -> a */
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty(&list1));
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
1998c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic void list_test_list_bulk_move_tail(struct kunit *test)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct list_head a, b, c, d, x, y;
2058c2ecf20Sopenharmony_ci	struct list_head *list1_values[] = { &x, &b, &c, &y };
2068c2ecf20Sopenharmony_ci	struct list_head *list2_values[] = { &a, &d };
2078c2ecf20Sopenharmony_ci	struct list_head *ptr;
2088c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
2098c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
2108c2ecf20Sopenharmony_ci	int i = 0;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	list_add_tail(&x, &list1);
2138c2ecf20Sopenharmony_ci	list_add_tail(&y, &list1);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	list_add_tail(&a, &list2);
2168c2ecf20Sopenharmony_ci	list_add_tail(&b, &list2);
2178c2ecf20Sopenharmony_ci	list_add_tail(&c, &list2);
2188c2ecf20Sopenharmony_ci	list_add_tail(&d, &list2);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* before: [list1] -> x -> y, [list2] -> a -> b -> c -> d */
2218c2ecf20Sopenharmony_ci	list_bulk_move_tail(&y, &b, &c);
2228c2ecf20Sopenharmony_ci	/* after: [list1] -> x -> b -> c -> y, [list2] -> a -> d */
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	list_for_each(ptr, &list1) {
2258c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
2268c2ecf20Sopenharmony_ci		i++;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 4);
2298c2ecf20Sopenharmony_ci	i = 0;
2308c2ecf20Sopenharmony_ci	list_for_each(ptr, &list2) {
2318c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
2328c2ecf20Sopenharmony_ci		i++;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 2);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic void list_test_list_is_first(struct kunit *test)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct list_head a, b;
2408c2ecf20Sopenharmony_ci	LIST_HEAD(list);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
2438c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
2468c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void list_test_list_is_last(struct kunit *test)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	struct list_head a, b;
2528c2ecf20Sopenharmony_ci	LIST_HEAD(list);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
2558c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
2588c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic void list_test_list_empty(struct kunit *test)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct list_head a;
2648c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
2658c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	list_add_tail(&a, &list1);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_empty(&list1));
2708c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty(&list2));
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void list_test_list_empty_careful(struct kunit *test)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	/* This test doesn't check correctness under concurrent access */
2768c2ecf20Sopenharmony_ci	struct list_head a;
2778c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
2788c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	list_add_tail(&a, &list1);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
2838c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic void list_test_list_rotate_left(struct kunit *test)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct list_head a, b;
2898c2ecf20Sopenharmony_ci	LIST_HEAD(list);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
2928c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* before: [list] -> a -> b */
2958c2ecf20Sopenharmony_ci	list_rotate_left(&list);
2968c2ecf20Sopenharmony_ci	/* after: [list] -> b -> a */
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
2998c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
3008c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic void list_test_list_rotate_to_front(struct kunit *test)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	struct list_head a, b, c, d;
3068c2ecf20Sopenharmony_ci	struct list_head *list_values[] = { &c, &d, &a, &b };
3078c2ecf20Sopenharmony_ci	struct list_head *ptr;
3088c2ecf20Sopenharmony_ci	LIST_HEAD(list);
3098c2ecf20Sopenharmony_ci	int i = 0;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
3128c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
3138c2ecf20Sopenharmony_ci	list_add_tail(&c, &list);
3148c2ecf20Sopenharmony_ci	list_add_tail(&d, &list);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/* before: [list] -> a -> b -> c -> d */
3178c2ecf20Sopenharmony_ci	list_rotate_to_front(&c, &list);
3188c2ecf20Sopenharmony_ci	/* after: [list] -> c -> d -> a -> b */
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	list_for_each(ptr, &list) {
3218c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
3228c2ecf20Sopenharmony_ci		i++;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 4);
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic void list_test_list_is_singular(struct kunit *test)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct list_head a, b;
3308c2ecf20Sopenharmony_ci	LIST_HEAD(list);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* [list] empty */
3338c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	list_add_tail(&a, &list);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* [list] -> a */
3388c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_is_singular(&list));
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	list_add_tail(&b, &list);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	/* [list] -> a -> b */
3438c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic void list_test_list_cut_position(struct kunit *test)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	struct list_head entries[3], *cur;
3498c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
3508c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
3518c2ecf20Sopenharmony_ci	int i = 0;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list1);
3548c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list1);
3558c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list1);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
3588c2ecf20Sopenharmony_ci	list_cut_position(&list2, &list1, &entries[1]);
3598c2ecf20Sopenharmony_ci	/* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	list_for_each(cur, &list2) {
3628c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
3638c2ecf20Sopenharmony_ci		i++;
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 2);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	list_for_each(cur, &list1) {
3698c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
3708c2ecf20Sopenharmony_ci		i++;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic void list_test_list_cut_before(struct kunit *test)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct list_head entries[3], *cur;
3778c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
3788c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
3798c2ecf20Sopenharmony_ci	int i = 0;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list1);
3828c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list1);
3838c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list1);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
3868c2ecf20Sopenharmony_ci	list_cut_before(&list2, &list1, &entries[1]);
3878c2ecf20Sopenharmony_ci	/* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	list_for_each(cur, &list2) {
3908c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
3918c2ecf20Sopenharmony_ci		i++;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 1);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	list_for_each(cur, &list1) {
3978c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
3988c2ecf20Sopenharmony_ci		i++;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic void list_test_list_splice(struct kunit *test)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	struct list_head entries[5], *cur;
4058c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
4068c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
4078c2ecf20Sopenharmony_ci	int i = 0;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list1);
4108c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list1);
4118c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list2);
4128c2ecf20Sopenharmony_ci	list_add_tail(&entries[3], &list2);
4138c2ecf20Sopenharmony_ci	list_add_tail(&entries[4], &list1);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
4168c2ecf20Sopenharmony_ci	list_splice(&list2, &entries[1]);
4178c2ecf20Sopenharmony_ci	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	list_for_each(cur, &list1) {
4208c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
4218c2ecf20Sopenharmony_ci		i++;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 5);
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void list_test_list_splice_tail(struct kunit *test)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct list_head entries[5], *cur;
4308c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
4318c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
4328c2ecf20Sopenharmony_ci	int i = 0;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list1);
4358c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list1);
4368c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list2);
4378c2ecf20Sopenharmony_ci	list_add_tail(&entries[3], &list2);
4388c2ecf20Sopenharmony_ci	list_add_tail(&entries[4], &list1);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
4418c2ecf20Sopenharmony_ci	list_splice_tail(&list2, &entries[4]);
4428c2ecf20Sopenharmony_ci	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	list_for_each(cur, &list1) {
4458c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
4468c2ecf20Sopenharmony_ci		i++;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 5);
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic void list_test_list_splice_init(struct kunit *test)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	struct list_head entries[5], *cur;
4558c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
4568c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
4578c2ecf20Sopenharmony_ci	int i = 0;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list1);
4608c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list1);
4618c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list2);
4628c2ecf20Sopenharmony_ci	list_add_tail(&entries[3], &list2);
4638c2ecf20Sopenharmony_ci	list_add_tail(&entries[4], &list1);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
4668c2ecf20Sopenharmony_ci	list_splice_init(&list2, &entries[1]);
4678c2ecf20Sopenharmony_ci	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	list_for_each(cur, &list1) {
4708c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
4718c2ecf20Sopenharmony_ci		i++;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 5);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
4778c2ecf20Sopenharmony_ci}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_cistatic void list_test_list_splice_tail_init(struct kunit *test)
4808c2ecf20Sopenharmony_ci{
4818c2ecf20Sopenharmony_ci	struct list_head entries[5], *cur;
4828c2ecf20Sopenharmony_ci	LIST_HEAD(list1);
4838c2ecf20Sopenharmony_ci	LIST_HEAD(list2);
4848c2ecf20Sopenharmony_ci	int i = 0;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list1);
4878c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list1);
4888c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list2);
4898c2ecf20Sopenharmony_ci	list_add_tail(&entries[3], &list2);
4908c2ecf20Sopenharmony_ci	list_add_tail(&entries[4], &list1);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
4938c2ecf20Sopenharmony_ci	list_splice_tail_init(&list2, &entries[4]);
4948c2ecf20Sopenharmony_ci	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	list_for_each(cur, &list1) {
4978c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
4988c2ecf20Sopenharmony_ci		i++;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 5);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic void list_test_list_entry(struct kunit *test)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct list_test_struct test_struct;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
5118c2ecf20Sopenharmony_ci				struct list_test_struct, list));
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic void list_test_list_first_entry(struct kunit *test)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	struct list_test_struct test_struct1, test_struct2;
5178c2ecf20Sopenharmony_ci	LIST_HEAD(list);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	list_add_tail(&test_struct1.list, &list);
5208c2ecf20Sopenharmony_ci	list_add_tail(&test_struct2.list, &list);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
5248c2ecf20Sopenharmony_ci				struct list_test_struct, list));
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic void list_test_list_last_entry(struct kunit *test)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	struct list_test_struct test_struct1, test_struct2;
5308c2ecf20Sopenharmony_ci	LIST_HEAD(list);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	list_add_tail(&test_struct1.list, &list);
5338c2ecf20Sopenharmony_ci	list_add_tail(&test_struct2.list, &list);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
5378c2ecf20Sopenharmony_ci				struct list_test_struct, list));
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_cistatic void list_test_list_first_entry_or_null(struct kunit *test)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	struct list_test_struct test_struct1, test_struct2;
5438c2ecf20Sopenharmony_ci	LIST_HEAD(list);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
5468c2ecf20Sopenharmony_ci				struct list_test_struct, list));
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	list_add_tail(&test_struct1.list, &list);
5498c2ecf20Sopenharmony_ci	list_add_tail(&test_struct2.list, &list);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
5528c2ecf20Sopenharmony_ci			list_first_entry_or_null(&list,
5538c2ecf20Sopenharmony_ci				struct list_test_struct, list));
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic void list_test_list_next_entry(struct kunit *test)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	struct list_test_struct test_struct1, test_struct2;
5598c2ecf20Sopenharmony_ci	LIST_HEAD(list);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	list_add_tail(&test_struct1.list, &list);
5628c2ecf20Sopenharmony_ci	list_add_tail(&test_struct2.list, &list);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
5668c2ecf20Sopenharmony_ci				list));
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic void list_test_list_prev_entry(struct kunit *test)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	struct list_test_struct test_struct1, test_struct2;
5728c2ecf20Sopenharmony_ci	LIST_HEAD(list);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	list_add_tail(&test_struct1.list, &list);
5758c2ecf20Sopenharmony_ci	list_add_tail(&test_struct2.list, &list);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
5798c2ecf20Sopenharmony_ci				list));
5808c2ecf20Sopenharmony_ci}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_cistatic void list_test_list_for_each(struct kunit *test)
5838c2ecf20Sopenharmony_ci{
5848c2ecf20Sopenharmony_ci	struct list_head entries[3], *cur;
5858c2ecf20Sopenharmony_ci	LIST_HEAD(list);
5868c2ecf20Sopenharmony_ci	int i = 0;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list);
5898c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list);
5908c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	list_for_each(cur, &list) {
5938c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
5948c2ecf20Sopenharmony_ci		i++;
5958c2ecf20Sopenharmony_ci	}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 3);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic void list_test_list_for_each_prev(struct kunit *test)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci	struct list_head entries[3], *cur;
6038c2ecf20Sopenharmony_ci	LIST_HEAD(list);
6048c2ecf20Sopenharmony_ci	int i = 2;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list);
6078c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list);
6088c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	list_for_each_prev(cur, &list) {
6118c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
6128c2ecf20Sopenharmony_ci		i--;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, -1);
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_cistatic void list_test_list_for_each_safe(struct kunit *test)
6198c2ecf20Sopenharmony_ci{
6208c2ecf20Sopenharmony_ci	struct list_head entries[3], *cur, *n;
6218c2ecf20Sopenharmony_ci	LIST_HEAD(list);
6228c2ecf20Sopenharmony_ci	int i = 0;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list);
6268c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list);
6278c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	list_for_each_safe(cur, n, &list) {
6308c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
6318c2ecf20Sopenharmony_ci		list_del(&entries[i]);
6328c2ecf20Sopenharmony_ci		i++;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 3);
6368c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty(&list));
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic void list_test_list_for_each_prev_safe(struct kunit *test)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	struct list_head entries[3], *cur, *n;
6428c2ecf20Sopenharmony_ci	LIST_HEAD(list);
6438c2ecf20Sopenharmony_ci	int i = 2;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	list_add_tail(&entries[0], &list);
6468c2ecf20Sopenharmony_ci	list_add_tail(&entries[1], &list);
6478c2ecf20Sopenharmony_ci	list_add_tail(&entries[2], &list);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	list_for_each_prev_safe(cur, n, &list) {
6508c2ecf20Sopenharmony_ci		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
6518c2ecf20Sopenharmony_ci		list_del(&entries[i]);
6528c2ecf20Sopenharmony_ci		i--;
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, -1);
6568c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, list_empty(&list));
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_cistatic void list_test_list_for_each_entry(struct kunit *test)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	struct list_test_struct entries[5], *cur;
6628c2ecf20Sopenharmony_ci	LIST_HEAD(list);
6638c2ecf20Sopenharmony_ci	int i = 0;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	for (i = 0; i < 5; ++i) {
6668c2ecf20Sopenharmony_ci		entries[i].data = i;
6678c2ecf20Sopenharmony_ci		list_add_tail(&entries[i].list, &list);
6688c2ecf20Sopenharmony_ci	}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	i = 0;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	list_for_each_entry(cur, &list, list) {
6738c2ecf20Sopenharmony_ci		KUNIT_EXPECT_EQ(test, cur->data, i);
6748c2ecf20Sopenharmony_ci		i++;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, 5);
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_cistatic void list_test_list_for_each_entry_reverse(struct kunit *test)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct list_test_struct entries[5], *cur;
6838c2ecf20Sopenharmony_ci	LIST_HEAD(list);
6848c2ecf20Sopenharmony_ci	int i = 0;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	for (i = 0; i < 5; ++i) {
6878c2ecf20Sopenharmony_ci		entries[i].data = i;
6888c2ecf20Sopenharmony_ci		list_add_tail(&entries[i].list, &list);
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	i = 4;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(cur, &list, list) {
6948c2ecf20Sopenharmony_ci		KUNIT_EXPECT_EQ(test, cur->data, i);
6958c2ecf20Sopenharmony_ci		i--;
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	KUNIT_EXPECT_EQ(test, i, -1);
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistatic struct kunit_case list_test_cases[] = {
7028c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_init),
7038c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_add),
7048c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_add_tail),
7058c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_del),
7068c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_replace),
7078c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_replace_init),
7088c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_swap),
7098c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_del_init),
7108c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_move),
7118c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_move_tail),
7128c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_bulk_move_tail),
7138c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_is_first),
7148c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_is_last),
7158c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_empty),
7168c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_empty_careful),
7178c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_rotate_left),
7188c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_rotate_to_front),
7198c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_is_singular),
7208c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_cut_position),
7218c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_cut_before),
7228c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_splice),
7238c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_splice_tail),
7248c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_splice_init),
7258c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_splice_tail_init),
7268c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_entry),
7278c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_first_entry),
7288c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_last_entry),
7298c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_first_entry_or_null),
7308c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_next_entry),
7318c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_prev_entry),
7328c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_for_each),
7338c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_for_each_prev),
7348c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_for_each_safe),
7358c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_for_each_prev_safe),
7368c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_for_each_entry),
7378c2ecf20Sopenharmony_ci	KUNIT_CASE(list_test_list_for_each_entry_reverse),
7388c2ecf20Sopenharmony_ci	{},
7398c2ecf20Sopenharmony_ci};
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic struct kunit_suite list_test_module = {
7428c2ecf20Sopenharmony_ci	.name = "list-kunit-test",
7438c2ecf20Sopenharmony_ci	.test_cases = list_test_cases,
7448c2ecf20Sopenharmony_ci};
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cikunit_test_suites(&list_test_module);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
749