xref: /openbmc/linux/lib/test_memcat_p.c (revision adb57164)
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
6 
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 
11 struct test_struct {
12 	int		num;
13 	unsigned int	magic;
14 };
15 
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)
21 
22 static int __init test_memcat_p_init(void)
23 {
24 	struct test_struct **in0, **in1, **out, **p;
25 	int err = -ENOMEM, i, r, total = 0;
26 
27 	in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
28 	if (!in0)
29 		return err;
30 
31 	in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
32 	if (!in1)
33 		goto err_free_in0;
34 
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;
39 
40 		in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
41 		if (!in1[i]) {
42 			kfree(in0[i]);
43 			goto err_free_elements;
44 		}
45 
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;
52 	}
53 
54 	in0[i] = in1[i] = NULL;
55 
56 	out = memcat_p(in0, in1);
57 	if (!out)
58 		goto err_free_all_elements;
59 
60 	err = -EINVAL;
61 	for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
62 		total += (*p)->num;
63 
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;
68 		}
69 	}
70 
71 	if (total) {
72 		pr_err("test failed: expected zero total, got %d\n", total);
73 		goto err_free_out;
74 	}
75 
76 	if (i != EXPECT) {
77 		pr_err("test failed: expected output size %d, got %d\n",
78 		       EXPECT, i);
79 		goto err_free_out;
80 	}
81 
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;
86 		}
87 
88 	err = 0;
89 	pr_info("test passed\n");
90 
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]);
99 	}
100 
101 	kfree(in1);
102 err_free_in0:
103 	kfree(in0);
104 
105 	return err;
106 }
107 
108 static void __exit test_memcat_p_exit(void)
109 {
110 }
111 
112 module_init(test_memcat_p_init);
113 module_exit(test_memcat_p_exit);
114 
115 MODULE_LICENSE("GPL");
116