Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 2 | #include <kunit/test.h> |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/list_sort.h> |
| 6 | #include <linux/list.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/printk.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/random.h> |
| 11 | |
| 12 | /* |
| 13 | * The pattern of set bits in the list length determines which cases |
| 14 | * are hit in list_sort(). |
| 15 | */ |
| 16 | #define TEST_LIST_LEN (512+128+2) /* not including head */ |
| 17 | |
| 18 | #define TEST_POISON1 0xDEADBEEF |
| 19 | #define TEST_POISON2 0xA324354C |
| 20 | |
| 21 | struct debug_el { |
| 22 | unsigned int poison1; |
| 23 | struct list_head list; |
| 24 | unsigned int poison2; |
| 25 | int value; |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 26 | unsigned int serial; |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 29 | static void check(struct kunit *test, struct debug_el *ela, struct debug_el *elb) |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 30 | { |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 31 | struct debug_el **elts = test->priv; |
| 32 | |
| 33 | KUNIT_EXPECT_LT_MSG(test, ela->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial"); |
| 34 | KUNIT_EXPECT_LT_MSG(test, elb->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial"); |
| 35 | |
| 36 | KUNIT_EXPECT_PTR_EQ_MSG(test, elts[ela->serial], ela, "phantom element"); |
| 37 | KUNIT_EXPECT_PTR_EQ_MSG(test, elts[elb->serial], elb, "phantom element"); |
| 38 | |
| 39 | KUNIT_EXPECT_EQ_MSG(test, ela->poison1, TEST_POISON1, "bad poison"); |
| 40 | KUNIT_EXPECT_EQ_MSG(test, ela->poison2, TEST_POISON2, "bad poison"); |
| 41 | |
| 42 | KUNIT_EXPECT_EQ_MSG(test, elb->poison1, TEST_POISON1, "bad poison"); |
| 43 | KUNIT_EXPECT_EQ_MSG(test, elb->poison2, TEST_POISON2, "bad poison"); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 46 | /* `priv` is the test pointer so check() can fail the test if the list is invalid. */ |
| 47 | static int cmp(void *priv, const struct list_head *a, const struct list_head *b) |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 48 | { |
| 49 | struct debug_el *ela, *elb; |
| 50 | |
| 51 | ela = container_of(a, struct debug_el, list); |
| 52 | elb = container_of(b, struct debug_el, list); |
| 53 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 54 | check(priv, ela, elb); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 55 | return ela->value - elb->value; |
| 56 | } |
| 57 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 58 | static void list_sort_test(struct kunit *test) |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 59 | { |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 60 | int i, count = 1; |
| 61 | struct debug_el *el, **elts; |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 62 | struct list_head *cur; |
| 63 | LIST_HEAD(head); |
| 64 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 65 | elts = kunit_kcalloc(test, TEST_LIST_LEN, sizeof(*elts), GFP_KERNEL); |
| 66 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, elts); |
| 67 | test->priv = elts; |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 68 | |
| 69 | for (i = 0; i < TEST_LIST_LEN; i++) { |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 70 | el = kunit_kmalloc(test, sizeof(*el), GFP_KERNEL); |
| 71 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, el); |
Markus Elfring | dc2bf000 | 2017-11-17 15:28:00 -0800 | [diff] [blame] | 72 | |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 73 | /* force some equivalencies */ |
Jason A. Donenfeld | 8032bf1 | 2022-10-09 20:44:02 -0600 | [diff] [blame] | 74 | el->value = get_random_u32_below(TEST_LIST_LEN / 3); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 75 | el->serial = i; |
| 76 | el->poison1 = TEST_POISON1; |
| 77 | el->poison2 = TEST_POISON2; |
| 78 | elts[i] = el; |
| 79 | list_add_tail(&el->list, &head); |
| 80 | } |
| 81 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 82 | list_sort(test, &head, cmp); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 83 | |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 84 | for (cur = head.next; cur->next != &head; cur = cur->next) { |
| 85 | struct debug_el *el1; |
| 86 | int cmp_result; |
| 87 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 88 | KUNIT_ASSERT_PTR_EQ_MSG(test, cur->next->prev, cur, |
| 89 | "list is corrupted"); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 90 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 91 | cmp_result = cmp(test, cur, cur->next); |
| 92 | KUNIT_ASSERT_LE_MSG(test, cmp_result, 0, "list is not sorted"); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 93 | |
| 94 | el = container_of(cur, struct debug_el, list); |
| 95 | el1 = container_of(cur->next, struct debug_el, list); |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 96 | if (cmp_result == 0) { |
| 97 | KUNIT_ASSERT_LE_MSG(test, el->serial, el1->serial, |
| 98 | "order of equivalent elements not preserved"); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 101 | check(test, el, el1); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 102 | count++; |
| 103 | } |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 104 | KUNIT_EXPECT_PTR_EQ_MSG(test, head.prev, cur, "list is corrupted"); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 105 | |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 106 | KUNIT_EXPECT_EQ_MSG(test, count, TEST_LIST_LEN, |
| 107 | "list length changed after sorting!"); |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 108 | } |
Daniel Latypov | ebd0957 | 2021-05-03 13:58:35 -0700 | [diff] [blame] | 109 | |
| 110 | static struct kunit_case list_sort_cases[] = { |
| 111 | KUNIT_CASE(list_sort_test), |
| 112 | {} |
| 113 | }; |
| 114 | |
| 115 | static struct kunit_suite list_sort_suite = { |
| 116 | .name = "list_sort", |
| 117 | .test_cases = list_sort_cases, |
| 118 | }; |
| 119 | |
| 120 | kunit_test_suites(&list_sort_suite); |
| 121 | |
Geert Uytterhoeven | e327fd7 | 2017-05-08 15:55:26 -0700 | [diff] [blame] | 122 | MODULE_LICENSE("GPL"); |