~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/test_memcat_p.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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_DESCRIPTION("Test cases for memcat_p() in lib/memcat_p.c");
116 MODULE_LICENSE("GPL");
117 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php