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 | |
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>
-rw-r--r-- | lib/Kconfig.debug | 8 | ||||
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/sort.c | 41 | ||||
-rw-r--r-- | lib/test_sort.c | 43 |
4 files changed, 54 insertions, 39 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 213decb76922..55735c9bdb75 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
@@ -1739,6 +1739,14 @@ config TEST_LIST_SORT | |||
1739 | 1739 | ||
1740 | If unsure, say N. | 1740 | If unsure, say N. |
1741 | 1741 | ||
1742 | config TEST_SORT | ||
1743 | bool "Array-based sort test" | ||
1744 | depends on DEBUG_KERNEL | ||
1745 | help | ||
1746 | This option enables the self-test function of 'sort()' at boot. | ||
1747 | |||
1748 | If unsure, say N. | ||
1749 | |||
1742 | config KPROBES_SANITY_TEST | 1750 | config KPROBES_SANITY_TEST |
1743 | bool "Kprobes sanity tests" | 1751 | bool "Kprobes sanity tests" |
1744 | depends on DEBUG_KERNEL | 1752 | depends on DEBUG_KERNEL |
diff --git a/lib/Makefile b/lib/Makefile index d6d53b70f58d..445a39c21f46 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -50,6 +50,7 @@ obj-$(CONFIG_TEST_KASAN) += test_kasan.o | |||
50 | obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o | 50 | obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o |
51 | obj-$(CONFIG_TEST_LKM) += test_module.o | 51 | obj-$(CONFIG_TEST_LKM) += test_module.o |
52 | obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o | 52 | obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o |
53 | obj-$(CONFIG_TEST_SORT) += test_sort.o | ||
53 | obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o | 54 | obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o |
54 | obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o | 55 | obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o |
55 | obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o | 56 | obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o |
diff --git a/lib/sort.c b/lib/sort.c index fc20df42aa6f..975c6ef6fec7 100644 --- a/lib/sort.c +++ b/lib/sort.c | |||
@@ -4,6 +4,8 @@ | |||
4 | * Jan 23 2005 Matt Mackall <mpm@selenic.com> | 4 | * Jan 23 2005 Matt Mackall <mpm@selenic.com> |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
8 | |||
7 | #include <linux/types.h> | 9 | #include <linux/types.h> |
8 | #include <linux/export.h> | 10 | #include <linux/export.h> |
9 | #include <linux/sort.h> | 11 | #include <linux/sort.h> |
@@ -101,42 +103,3 @@ void sort(void *base, size_t num, size_t size, | |||
101 | } | 103 | } |
102 | 104 | ||
103 | EXPORT_SYMBOL(sort); | 105 | EXPORT_SYMBOL(sort); |
104 | |||
105 | #if 0 | ||
106 | #include <linux/slab.h> | ||
107 | /* a simple boot-time regression test */ | ||
108 | |||
109 | int cmpint(const void *a, const void *b) | ||
110 | { | ||
111 | return *(int *)a - *(int *)b; | ||
112 | } | ||
113 | |||
114 | static int sort_test(void) | ||
115 | { | ||
116 | int *a, i, r = 1; | ||
117 | |||
118 | a = kmalloc(1000 * sizeof(int), GFP_KERNEL); | ||
119 | BUG_ON(!a); | ||
120 | |||
121 | printk("testing sort()\n"); | ||
122 | |||
123 | for (i = 0; i < 1000; i++) { | ||
124 | r = (r * 725861) % 6599; | ||
125 | a[i] = r; | ||
126 | } | ||
127 | |||
128 | sort(a, 1000, sizeof(int), cmpint, NULL); | ||
129 | |||
130 | for (i = 0; i < 999; i++) | ||
131 | if (a[i] > a[i+1]) { | ||
132 | printk("sort() failed!\n"); | ||
133 | break; | ||
134 | } | ||
135 | |||
136 | kfree(a); | ||
137 | |||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | module_init(sort_test); | ||
142 | #endif | ||
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"); | ||