split dev_queue
[cor.git] / lib / test_memcat_p.c
blob849c477d49d0cdeab3ee23f470b468c3df7a9dc4
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Test cases for memcat_p() in lib/memcat_p.c
4 */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
11 struct test_struct {
12 int num;
13 unsigned int magic;
16 #define MAGIC 0xf00ff00f
17 /* Size of each of the NULL-terminated input arrays */
18 #define INPUT_MAX 128
19 /* Expected number of non-NULL elements in the output array */
20 #define EXPECT (INPUT_MAX * 2 - 2)
22 static int __init test_memcat_p_init(void)
24 struct test_struct **in0, **in1, **out, **p;
25 int err = -ENOMEM, i, r, total = 0;
27 in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
28 if (!in0)
29 return err;
31 in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
32 if (!in1)
33 goto err_free_in0;
35 for (i = 0, r = 1; i < INPUT_MAX - 1; i++) {
36 in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL);
37 if (!in0[i])
38 goto err_free_elements;
40 in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
41 if (!in1[i]) {
42 kfree(in0[i]);
43 goto err_free_elements;
46 /* lifted from test_sort.c */
47 r = (r * 725861) % 6599;
48 in0[i]->num = r;
49 in1[i]->num = -r;
50 in0[i]->magic = MAGIC;
51 in1[i]->magic = MAGIC;
54 in0[i] = in1[i] = NULL;
56 out = memcat_p(in0, in1);
57 if (!out)
58 goto err_free_all_elements;
60 err = -EINVAL;
61 for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
62 total += (*p)->num;
64 if ((*p)->magic != MAGIC) {
65 pr_err("test failed: wrong magic at %d: %u\n", i,
66 (*p)->magic);
67 goto err_free_out;
71 if (total) {
72 pr_err("test failed: expected zero total, got %d\n", total);
73 goto err_free_out;
76 if (i != EXPECT) {
77 pr_err("test failed: expected output size %d, got %d\n",
78 EXPECT, i);
79 goto err_free_out;
82 for (i = 0; i < INPUT_MAX - 1; i++)
83 if (out[i] != in0[i] || out[i + INPUT_MAX - 1] != in1[i]) {
84 pr_err("test failed: wrong element order at %d\n", i);
85 goto err_free_out;
88 err = 0;
89 pr_info("test passed\n");
91 err_free_out:
92 kfree(out);
93 err_free_all_elements:
94 i = INPUT_MAX;
95 err_free_elements:
96 for (i--; i >= 0; i--) {
97 kfree(in1[i]);
98 kfree(in0[i]);
101 kfree(in1);
102 err_free_in0:
103 kfree(in0);
105 return err;
108 static void __exit test_memcat_p_exit(void)
112 module_init(test_memcat_p_init);
113 module_exit(test_memcat_p_exit);
115 MODULE_LICENSE("GPL");