1 // SPDX-License-Identifier: GPL-2.0
3 * Test cases for memcat_p() in lib/memcat_p.c
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
16 #define MAGIC 0xf00ff00f
17 /* Size of each of the NULL-terminated input arrays */
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
);
31 in1
= kcalloc(INPUT_MAX
, sizeof(*in1
), GFP_KERNEL
);
35 for (i
= 0, r
= 1; i
< INPUT_MAX
- 1; i
++) {
36 in0
[i
] = kmalloc(sizeof(**in0
), GFP_KERNEL
);
38 goto err_free_elements
;
40 in1
[i
] = kmalloc(sizeof(**in1
), GFP_KERNEL
);
43 goto err_free_elements
;
46 /* lifted from test_sort.c */
47 r
= (r
* 725861) % 6599;
50 in0
[i
]->magic
= MAGIC
;
51 in1
[i
]->magic
= MAGIC
;
54 in0
[i
] = in1
[i
] = NULL
;
56 out
= memcat_p(in0
, in1
);
58 goto err_free_all_elements
;
61 for (i
= 0, p
= out
; *p
&& (i
< INPUT_MAX
* 2 - 1); p
++, i
++) {
64 if ((*p
)->magic
!= MAGIC
) {
65 pr_err("test failed: wrong magic at %d: %u\n", i
,
72 pr_err("test failed: expected zero total, got %d\n", total
);
77 pr_err("test failed: expected output size %d, got %d\n",
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
);
89 pr_info("test passed\n");
93 err_free_all_elements
:
96 for (i
--; i
>= 0; i
--) {
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");