test_memcat_p.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <linux/string.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. struct test_struct {
  10. int num;
  11. unsigned int magic;
  12. };
  13. #define MAGIC 0xf00ff00f
  14. /* Size of each of the NULL-terminated input arrays */
  15. #define INPUT_MAX 128
  16. /* Expected number of non-NULL elements in the output array */
  17. #define EXPECT (INPUT_MAX * 2 - 2)
  18. static int __init test_memcat_p_init(void)
  19. {
  20. struct test_struct **in0, **in1, **out, **p;
  21. int err = -ENOMEM, i, r, total = 0;
  22. in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL);
  23. if (!in0)
  24. return err;
  25. in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL);
  26. if (!in1)
  27. goto err_free_in0;
  28. for (i = 0, r = 1; i < INPUT_MAX - 1; i++) {
  29. in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL);
  30. if (!in0[i])
  31. goto err_free_elements;
  32. in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL);
  33. if (!in1[i]) {
  34. kfree(in0[i]);
  35. goto err_free_elements;
  36. }
  37. /* lifted from test_sort.c */
  38. r = (r * 725861) % 6599;
  39. in0[i]->num = r;
  40. in1[i]->num = -r;
  41. in0[i]->magic = MAGIC;
  42. in1[i]->magic = MAGIC;
  43. }
  44. in0[i] = in1[i] = NULL;
  45. out = memcat_p(in0, in1);
  46. if (!out)
  47. goto err_free_all_elements;
  48. err = -EINVAL;
  49. for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) {
  50. total += (*p)->num;
  51. if ((*p)->magic != MAGIC) {
  52. pr_err("test failed: wrong magic at %d: %u\n", i,
  53. (*p)->magic);
  54. goto err_free_out;
  55. }
  56. }
  57. if (total) {
  58. pr_err("test failed: expected zero total, got %d\n", total);
  59. goto err_free_out;
  60. }
  61. if (i != EXPECT) {
  62. pr_err("test failed: expected output size %d, got %d\n",
  63. EXPECT, i);
  64. goto err_free_out;
  65. }
  66. for (i = 0; i < INPUT_MAX - 1; i++)
  67. if (out[i] != in0[i] || out[i + INPUT_MAX - 1] != in1[i]) {
  68. pr_err("test failed: wrong element order at %d\n", i);
  69. goto err_free_out;
  70. }
  71. err = 0;
  72. pr_info("test passed\n");
  73. err_free_out:
  74. kfree(out);
  75. err_free_all_elements:
  76. i = INPUT_MAX;
  77. err_free_elements:
  78. for (i--; i >= 0; i--) {
  79. kfree(in1[i]);
  80. kfree(in0[i]);
  81. }
  82. kfree(in1);
  83. err_free_in0:
  84. kfree(in0);
  85. return err;
  86. }
  87. static void __exit test_memcat_p_exit(void)
  88. {
  89. }
  90. module_init(test_memcat_p_init);
  91. module_exit(test_memcat_p_exit);
  92. MODULE_LICENSE("GPL");