Linux Audio

Check our new training course

Loading...
v5.14.15
 1// SPDX-License-Identifier: GPL-2.0-only
 2#include <linux/sort.h>
 3#include <linux/slab.h>
 4#include <linux/module.h>
 5
 6/* a simple boot-time regression test */
 7
 8#define TEST_LEN 1000
 9
10static int __init cmpint(const void *a, const void *b)
11{
12	return *(int *)a - *(int *)b;
13}
14
15static 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");
38exit:
39	kfree(a);
40	return err;
41}
42
43static void __exit test_sort_exit(void)
44{
45}
46
47module_init(test_sort_init);
48module_exit(test_sort_exit);
49
50MODULE_LICENSE("GPL");
v4.17
 
 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
 9static int __init cmpint(const void *a, const void *b)
10{
11	return *(int *)a - *(int *)b;
12}
13
14static 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");
37exit:
38	kfree(a);
39	return err;
40}
41
42static void __exit test_sort_exit(void)
43{
44}
45
46module_init(test_sort_init);
47module_exit(test_sort_exit);
48
49MODULE_LICENSE("GPL");