Source
// SPDX-License-Identifier: GPL-2.0
/*
* Test cases for memcat_p() in lib/memcat_p.c
*/
struct test_struct {
int num;
unsigned int magic;
};
/* Size of each of the NULL-terminated input arrays */
/* Expected number of non-NULL elements in the output array */
static int __init test_memcat_p_init(void)
{
struct test_struct **in0, **in1, **out, **p;
int err = -ENOMEM, i, r, total = 0;
in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
if (!in0)
return err;
in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
if (!in1)
goto err_free_in0;
for (i = 0, r = 1; i < INPUT_MAX - 1; i++) {
in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL);
if (!in0[i])
goto err_free_elements;
in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
if (!in1[i]) {
kfree(in0[i]);
goto err_free_elements;
}
/* lifted from test_sort.c */
r = (r * 725861) % 6599;
in0[i]->num = r;
in1[i]->num = -r;
in0[i]->magic = MAGIC;
in1[i]->magic = MAGIC;
}
in0[i] = in1[i] = NULL;
out = memcat_p(in0, in1);
if (!out)
goto err_free_all_elements;
err = -EINVAL;
for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
total += (*p)->num;