Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 #include <kunit/test.h>
0004 
0005 #include <linux/sort.h>
0006 #include <linux/slab.h>
0007 #include <linux/module.h>
0008 
0009 /* a simple boot-time regression test */
0010 
0011 #define TEST_LEN 1000
0012 
0013 static int cmpint(const void *a, const void *b)
0014 {
0015     return *(int *)a - *(int *)b;
0016 }
0017 
0018 static void test_sort(struct kunit *test)
0019 {
0020     int *a, i, r = 1;
0021 
0022     a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
0023     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
0024 
0025     for (i = 0; i < TEST_LEN; i++) {
0026         r = (r * 725861) % 6599;
0027         a[i] = r;
0028     }
0029 
0030     sort(a, TEST_LEN, sizeof(*a), cmpint, NULL);
0031 
0032     for (i = 0; i < TEST_LEN-1; i++)
0033         KUNIT_ASSERT_LE(test, a[i], a[i + 1]);
0034 }
0035 
0036 static struct kunit_case sort_test_cases[] = {
0037     KUNIT_CASE(test_sort),
0038     {}
0039 };
0040 
0041 static struct kunit_suite sort_test_suite = {
0042     .name = "lib_sort",
0043     .test_cases = sort_test_cases,
0044 };
0045 
0046 kunit_test_suites(&sort_test_suite);
0047 
0048 MODULE_LICENSE("GPL");