diff options
author | Kostenzer Felix <fkostenzer@live.at> | 2017-02-24 18:01:07 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-02-24 20:46:57 -0500 |
commit | c5adae9583ef6985875532904160c6bf9f07b453 (patch) | |
tree | 7f5db590f21b5784c3d89770735039144dca259d /lib/test_sort.c | |
parent | f231aebfc4cae2f6ed27a46a31e2630909513d77 (diff) |
lib: add CONFIG_TEST_SORT to enable self-test of sort()
Along with the addition made to Kconfig.debug, the prior existing but
permanently disabled test function has been slightly refactored.
Patch has been tested using QEMU 2.1.2 with a .config obtained through
'make defconfig' (x86_64) and manually enabling the option.
[arnd@arndb.de: move sort self-test into a separate file]
Link: http://lkml.kernel.org/r/20170112110657.3123790-1-arnd@arndb.de
Link: http://lkml.kernel.org/r/HE1PR09MB0394B0418D504DCD27167D4FD49B0@HE1PR09MB0394.eurprd09.prod.outlook.com
Signed-off-by: Kostenzer Felix <fkostenzer@live.at>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/test_sort.c')
-rw-r--r-- | lib/test_sort.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/test_sort.c b/lib/test_sort.c new file mode 100644 index 000000000000..d389c1cc2f6c --- /dev/null +++ b/lib/test_sort.c | |||
@@ -0,0 +1,43 @@ | |||
1 | #include <linux/sort.h> | ||
2 | #include <linux/slab.h> | ||
3 | #include <linux/module.h> | ||
4 | |||
5 | /* a simple boot-time regression test */ | ||
6 | |||
7 | #define TEST_LEN 1000 | ||
8 | |||
9 | static int __init cmpint(const void *a, const void *b) | ||
10 | { | ||
11 | return *(int *)a - *(int *)b; | ||
12 | } | ||
13 | |||
14 | static int __init test_sort_init(void) | ||
15 | { | ||
16 | int *a, i, r = 1, err = -ENOMEM; | ||
17 | |||
18 | a = kmalloc_array(TEST_LEN, sizeof(*a), GFP_KERNEL); | ||
19 | if (!a) | ||
20 | return err; | ||
21 | |||
22 | for (i = 0; i < TEST_LEN; i++) { | ||
23 | r = (r * 725861) % 6599; | ||
24 | a[i] = r; | ||
25 | } | ||
26 | |||
27 | sort(a, TEST_LEN, sizeof(*a), cmpint, NULL); | ||
28 | |||
29 | err = -EINVAL; | ||
30 | for (i = 0; i < TEST_LEN-1; i++) | ||
31 | if (a[i] > a[i+1]) { | ||
32 | pr_err("test has failed\n"); | ||
33 | goto exit; | ||
34 | } | ||
35 | err = 0; | ||
36 | pr_info("test passed\n"); | ||
37 | exit: | ||
38 | kfree(a); | ||
39 | return err; | ||
40 | } | ||
41 | |||
42 | module_init(test_sort_init); | ||
43 | MODULE_LICENSE("GPL"); | ||