Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 2 | |
| 3 | #include <kunit/test.h> |
| 4 | |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 5 | #include <linux/sort.h> |
| 6 | #include <linux/slab.h> |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 8 | |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 9 | /* a simple boot-time regression test */ |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 10 | |
| 11 | #define TEST_LEN 1000 |
| 12 | |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 13 | static int cmpint(const void *a, const void *b) |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 14 | { |
| 15 | return *(int *)a - *(int *)b; |
| 16 | } |
| 17 | |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 18 | static void test_sort(struct kunit *test) |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 19 | { |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 20 | int *a, i, r = 1; |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 21 | |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 22 | a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL); |
| 23 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a); |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 24 | |
| 25 | for (i = 0; i < TEST_LEN; i++) { |
| 26 | r = (r * 725861) % 6599; |
| 27 | a[i] = r; |
| 28 | } |
| 29 | |
| 30 | sort(a, TEST_LEN, sizeof(*a), cmpint, NULL); |
| 31 | |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 32 | for (i = 0; i < TEST_LEN-1; i++) |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 33 | KUNIT_ASSERT_LE(test, a[i], a[i + 1]); |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 34 | } |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 35 | |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 36 | static struct kunit_case sort_test_cases[] = { |
| 37 | KUNIT_CASE(test_sort), |
| 38 | {} |
| 39 | }; |
Pravin Shedge | 92fc7cb | 2018-02-06 15:38:42 -0800 | [diff] [blame] | 40 | |
Daniel Latypov | 36f33b5 | 2021-09-07 19:58:48 -0700 | [diff] [blame] | 41 | static struct kunit_suite sort_test_suite = { |
| 42 | .name = "lib_sort", |
| 43 | .test_cases = sort_test_cases, |
| 44 | }; |
| 45 | |
| 46 | kunit_test_suites(&sort_test_suite); |
Pravin Shedge | 92fc7cb | 2018-02-06 15:38:42 -0800 | [diff] [blame] | 47 | |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 48 | MODULE_LICENSE("GPL"); |