0001
0002
0003
0004
0005 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0006
0007 #include <linux/string.h>
0008 #include <linux/slab.h>
0009 #include <linux/module.h>
0010
0011 struct test_struct {
0012 int num;
0013 unsigned int magic;
0014 };
0015
0016 #define MAGIC 0xf00ff00f
0017
0018 #define INPUT_MAX 128
0019
0020 #define EXPECT (INPUT_MAX * 2 - 2)
0021
0022 static int __init test_memcat_p_init(void)
0023 {
0024 struct test_struct **in0, **in1, **out, **p;
0025 int err = -ENOMEM, i, r, total = 0;
0026
0027 in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
0028 if (!in0)
0029 return err;
0030
0031 in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
0032 if (!in1)
0033 goto err_free_in0;
0034
0035 for (i = 0, r = 1; i < INPUT_MAX - 1; i++) {
0036 in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL);
0037 if (!in0[i])
0038 goto err_free_elements;
0039
0040 in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
0041 if (!in1[i]) {
0042 kfree(in0[i]);
0043 goto err_free_elements;
0044 }
0045
0046
0047 r = (r * 725861) % 6599;
0048 in0[i]->num = r;
0049 in1[i]->num = -r;
0050 in0[i]->magic = MAGIC;
0051 in1[i]->magic = MAGIC;
0052 }
0053
0054 in0[i] = in1[i] = NULL;
0055
0056 out = memcat_p(in0, in1);
0057 if (!out)
0058 goto err_free_all_elements;
0059
0060 err = -EINVAL;
0061 for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
0062 total += (*p)->num;
0063
0064 if ((*p)->magic != MAGIC) {
0065 pr_err("test failed: wrong magic at %d: %u\n", i,
0066 (*p)->magic);
0067 goto err_free_out;
0068 }
0069 }
0070
0071 if (total) {
0072 pr_err("test failed: expected zero total, got %d\n", total);
0073 goto err_free_out;
0074 }
0075
0076 if (i != EXPECT) {
0077 pr_err("test failed: expected output size %d, got %d\n",
0078 EXPECT, i);
0079 goto err_free_out;
0080 }
0081
0082 for (i = 0; i < INPUT_MAX - 1; i++)
0083 if (out[i] != in0[i] || out[i + INPUT_MAX - 1] != in1[i]) {
0084 pr_err("test failed: wrong element order at %d\n", i);
0085 goto err_free_out;
0086 }
0087
0088 err = 0;
0089 pr_info("test passed\n");
0090
0091 err_free_out:
0092 kfree(out);
0093 err_free_all_elements:
0094 i = INPUT_MAX;
0095 err_free_elements:
0096 for (i--; i >= 0; i--) {
0097 kfree(in1[i]);
0098 kfree(in0[i]);
0099 }
0100
0101 kfree(in1);
0102 err_free_in0:
0103 kfree(in0);
0104
0105 return err;
0106 }
0107
0108 static void __exit test_memcat_p_exit(void)
0109 {
0110 }
0111
0112 module_init(test_memcat_p_init);
0113 module_exit(test_memcat_p_exit);
0114
0115 MODULE_LICENSE("GPL");