Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 2 | #include <linux/sort.h> |
| 3 | #include <linux/slab.h> |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 4 | #include <linux/module.h> |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 5 | |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 6 | /* a simple boot-time regression test */ |
Kostenzer Felix | c5adae9 | 2017-02-24 15:01:07 -0800 | [diff] [blame] | 7 | |
| 8 | #define TEST_LEN 1000 |
| 9 | |
| 10 | static int __init cmpint(const void *a, const void *b) |
| 11 | { |
| 12 | return *(int *)a - *(int *)b; |
| 13 | } |
| 14 | |
| 15 | static int __init test_sort_init(void) |
| 16 | { |
| 17 | int *a, i, r = 1, err = -ENOMEM; |
| 18 | |
| 19 | a = kmalloc_array(TEST_LEN, sizeof(*a), GFP_KERNEL); |
| 20 | if (!a) |
| 21 | return err; |
| 22 | |
| 23 | for (i = 0; i < TEST_LEN; i++) { |
| 24 | r = (r * 725861) % 6599; |
| 25 | a[i] = r; |
| 26 | } |
| 27 | |
| 28 | sort(a, TEST_LEN, sizeof(*a), cmpint, NULL); |
| 29 | |
| 30 | err = -EINVAL; |
| 31 | for (i = 0; i < TEST_LEN-1; i++) |
| 32 | if (a[i] > a[i+1]) { |
| 33 | pr_err("test has failed\n"); |
| 34 | goto exit; |
| 35 | } |
| 36 | err = 0; |
| 37 | pr_info("test passed\n"); |
| 38 | exit: |
| 39 | kfree(a); |
| 40 | return err; |
| 41 | } |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 42 | |
Pravin Shedge | 92fc7cb | 2018-02-06 15:38:42 -0800 | [diff] [blame] | 43 | static void __exit test_sort_exit(void) |
| 44 | { |
| 45 | } |
| 46 | |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 47 | module_init(test_sort_init); |
Pravin Shedge | 92fc7cb | 2018-02-06 15:38:42 -0800 | [diff] [blame] | 48 | module_exit(test_sort_exit); |
| 49 | |
Geert Uytterhoeven | ebd03a9 | 2017-05-08 15:55:20 -0700 | [diff] [blame] | 50 | MODULE_LICENSE("GPL"); |