0001
0002 #include <kunit/test.h>
0003
0004 #include <linux/kernel.h>
0005 #include <linux/list_sort.h>
0006 #include <linux/list.h>
0007 #include <linux/module.h>
0008 #include <linux/printk.h>
0009 #include <linux/slab.h>
0010 #include <linux/random.h>
0011
0012
0013
0014
0015
0016 #define TEST_LIST_LEN (512+128+2)
0017
0018 #define TEST_POISON1 0xDEADBEEF
0019 #define TEST_POISON2 0xA324354C
0020
0021 struct debug_el {
0022 unsigned int poison1;
0023 struct list_head list;
0024 unsigned int poison2;
0025 int value;
0026 unsigned int serial;
0027 };
0028
0029 static void check(struct kunit *test, struct debug_el *ela, struct debug_el *elb)
0030 {
0031 struct debug_el **elts = test->priv;
0032
0033 KUNIT_EXPECT_LT_MSG(test, ela->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial");
0034 KUNIT_EXPECT_LT_MSG(test, elb->serial, (unsigned int)TEST_LIST_LEN, "incorrect serial");
0035
0036 KUNIT_EXPECT_PTR_EQ_MSG(test, elts[ela->serial], ela, "phantom element");
0037 KUNIT_EXPECT_PTR_EQ_MSG(test, elts[elb->serial], elb, "phantom element");
0038
0039 KUNIT_EXPECT_EQ_MSG(test, ela->poison1, TEST_POISON1, "bad poison");
0040 KUNIT_EXPECT_EQ_MSG(test, ela->poison2, TEST_POISON2, "bad poison");
0041
0042 KUNIT_EXPECT_EQ_MSG(test, elb->poison1, TEST_POISON1, "bad poison");
0043 KUNIT_EXPECT_EQ_MSG(test, elb->poison2, TEST_POISON2, "bad poison");
0044 }
0045
0046
0047 static int cmp(void *priv, const struct list_head *a, const struct list_head *b)
0048 {
0049 struct debug_el *ela, *elb;
0050
0051 ela = container_of(a, struct debug_el, list);
0052 elb = container_of(b, struct debug_el, list);
0053
0054 check(priv, ela, elb);
0055 return ela->value - elb->value;
0056 }
0057
0058 static void list_sort_test(struct kunit *test)
0059 {
0060 int i, count = 1;
0061 struct debug_el *el, **elts;
0062 struct list_head *cur;
0063 LIST_HEAD(head);
0064
0065 elts = kunit_kcalloc(test, TEST_LIST_LEN, sizeof(*elts), GFP_KERNEL);
0066 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, elts);
0067 test->priv = elts;
0068
0069 for (i = 0; i < TEST_LIST_LEN; i++) {
0070 el = kunit_kmalloc(test, sizeof(*el), GFP_KERNEL);
0071 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, el);
0072
0073
0074 el->value = prandom_u32() % (TEST_LIST_LEN / 3);
0075 el->serial = i;
0076 el->poison1 = TEST_POISON1;
0077 el->poison2 = TEST_POISON2;
0078 elts[i] = el;
0079 list_add_tail(&el->list, &head);
0080 }
0081
0082 list_sort(test, &head, cmp);
0083
0084 for (cur = head.next; cur->next != &head; cur = cur->next) {
0085 struct debug_el *el1;
0086 int cmp_result;
0087
0088 KUNIT_ASSERT_PTR_EQ_MSG(test, cur->next->prev, cur,
0089 "list is corrupted");
0090
0091 cmp_result = cmp(test, cur, cur->next);
0092 KUNIT_ASSERT_LE_MSG(test, cmp_result, 0, "list is not sorted");
0093
0094 el = container_of(cur, struct debug_el, list);
0095 el1 = container_of(cur->next, struct debug_el, list);
0096 if (cmp_result == 0) {
0097 KUNIT_ASSERT_LE_MSG(test, el->serial, el1->serial,
0098 "order of equivalent elements not preserved");
0099 }
0100
0101 check(test, el, el1);
0102 count++;
0103 }
0104 KUNIT_EXPECT_PTR_EQ_MSG(test, head.prev, cur, "list is corrupted");
0105
0106 KUNIT_EXPECT_EQ_MSG(test, count, TEST_LIST_LEN,
0107 "list length changed after sorting!");
0108 }
0109
0110 static struct kunit_case list_sort_cases[] = {
0111 KUNIT_CASE(list_sort_test),
0112 {}
0113 };
0114
0115 static struct kunit_suite list_sort_suite = {
0116 .name = "list_sort",
0117 .test_cases = list_sort_cases,
0118 };
0119
0120 kunit_test_suites(&list_sort_suite);
0121
0122 MODULE_LICENSE("GPL");