Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/tests: convert test_min_heap module to KUnit

Move lib/test_min_heap.c to lib/tests/min_heap_kunit.c and convert it to
use KUnit.

This change switches the ad-hoc test code to standard KUnit test cases.
The test data remains the same, but the verification logic is updated to
use KUNIT_EXPECT_* macros.

Also remove CONFIG_TEST_MIN_HEAP from arch/*/configs/* because it is no
longer used. The new CONFIG_MIN_HEAP_KUNIT_TEST will be automatically
enabled by CONFIG_KUNIT_ALL_TESTS.

The reasons for converting to KUnit are:

1. Standardization:
Switching from ad-hoc printk-based reporting to the standard
KTAP format makes it easier for CI systems to parse and report test
results

2. Better Diagnostics:
Using KUNIT_EXPECT_* macros automatically provides detailed
diagnostics on failure.

3. Tooling Integration:
It allows the test to be managed and executed using standard
KUnit tools.

Link: https://lkml.kernel.org/r/20251221133516.321846-1-sakamo.ryota@gmail.com
Signed-off-by: Ryota Sakamoto <sakamo.ryota@gmail.com>
Acked-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: David Gow <davidgow@google.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Ryota Sakamoto and committed by
Andrew Morton
d30aca3e 0e7fd23f

+80 -105
+1 -1
MAINTAINERS
··· 17456 17456 F: Documentation/core-api/min_heap.rst 17457 17457 F: include/linux/min_heap.h 17458 17458 F: lib/min_heap.c 17459 - F: lib/test_min_heap.c 17459 + F: lib/tests/min_heap_kunit.c 17460 17460 17461 17461 MIPI CCS, SMIA AND SMIA++ IMAGE SENSOR DRIVER 17462 17462 M: Sakari Ailus <sakari.ailus@linux.intel.com>
-1
arch/m68k/configs/amiga_defconfig
··· 609 609 CONFIG_KUNIT=m 610 610 CONFIG_KUNIT_ALL_TESTS=m 611 611 CONFIG_TEST_DHRY=m 612 - CONFIG_TEST_MIN_HEAP=m 613 612 CONFIG_TEST_DIV64=m 614 613 CONFIG_TEST_MULDIV64=m 615 614 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/apollo_defconfig
··· 566 566 CONFIG_KUNIT=m 567 567 CONFIG_KUNIT_ALL_TESTS=m 568 568 CONFIG_TEST_DHRY=m 569 - CONFIG_TEST_MIN_HEAP=m 570 569 CONFIG_TEST_DIV64=m 571 570 CONFIG_TEST_MULDIV64=m 572 571 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/atari_defconfig
··· 586 586 CONFIG_KUNIT=m 587 587 CONFIG_KUNIT_ALL_TESTS=m 588 588 CONFIG_TEST_DHRY=m 589 - CONFIG_TEST_MIN_HEAP=m 590 589 CONFIG_TEST_DIV64=m 591 590 CONFIG_TEST_MULDIV64=m 592 591 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/bvme6000_defconfig
··· 558 558 CONFIG_KUNIT=m 559 559 CONFIG_KUNIT_ALL_TESTS=m 560 560 CONFIG_TEST_DHRY=m 561 - CONFIG_TEST_MIN_HEAP=m 562 561 CONFIG_TEST_DIV64=m 563 562 CONFIG_TEST_MULDIV64=m 564 563 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/hp300_defconfig
··· 568 568 CONFIG_KUNIT=m 569 569 CONFIG_KUNIT_ALL_TESTS=m 570 570 CONFIG_TEST_DHRY=m 571 - CONFIG_TEST_MIN_HEAP=m 572 571 CONFIG_TEST_DIV64=m 573 572 CONFIG_TEST_MULDIV64=m 574 573 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/mac_defconfig
··· 585 585 CONFIG_KUNIT=m 586 586 CONFIG_KUNIT_ALL_TESTS=m 587 587 CONFIG_TEST_DHRY=m 588 - CONFIG_TEST_MIN_HEAP=m 589 588 CONFIG_TEST_DIV64=m 590 589 CONFIG_TEST_MULDIV64=m 591 590 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/multi_defconfig
··· 672 672 CONFIG_KUNIT=m 673 673 CONFIG_KUNIT_ALL_TESTS=m 674 674 CONFIG_TEST_DHRY=m 675 - CONFIG_TEST_MIN_HEAP=m 676 675 CONFIG_TEST_DIV64=m 677 676 CONFIG_TEST_MULDIV64=m 678 677 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/mvme147_defconfig
··· 558 558 CONFIG_KUNIT=m 559 559 CONFIG_KUNIT_ALL_TESTS=m 560 560 CONFIG_TEST_DHRY=m 561 - CONFIG_TEST_MIN_HEAP=m 562 561 CONFIG_TEST_DIV64=m 563 562 CONFIG_TEST_MULDIV64=m 564 563 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/mvme16x_defconfig
··· 559 559 CONFIG_KUNIT=m 560 560 CONFIG_KUNIT_ALL_TESTS=m 561 561 CONFIG_TEST_DHRY=m 562 - CONFIG_TEST_MIN_HEAP=m 563 562 CONFIG_TEST_DIV64=m 564 563 CONFIG_TEST_MULDIV64=m 565 564 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/q40_defconfig
··· 575 575 CONFIG_KUNIT=m 576 576 CONFIG_KUNIT_ALL_TESTS=m 577 577 CONFIG_TEST_DHRY=m 578 - CONFIG_TEST_MIN_HEAP=m 579 578 CONFIG_TEST_DIV64=m 580 579 CONFIG_TEST_MULDIV64=m 581 580 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/sun3_defconfig
··· 555 555 CONFIG_KUNIT=m 556 556 CONFIG_KUNIT_ALL_TESTS=m 557 557 CONFIG_TEST_DHRY=m 558 - CONFIG_TEST_MIN_HEAP=m 559 558 CONFIG_TEST_DIV64=m 560 559 CONFIG_TEST_MULDIV64=m 561 560 CONFIG_REED_SOLOMON_TEST=m
-1
arch/m68k/configs/sun3x_defconfig
··· 556 556 CONFIG_KUNIT=m 557 557 CONFIG_KUNIT_ALL_TESTS=m 558 558 CONFIG_TEST_DHRY=m 559 - CONFIG_TEST_MIN_HEAP=m 560 559 CONFIG_TEST_DIV64=m 561 560 CONFIG_TEST_MULDIV64=m 562 561 CONFIG_REED_SOLOMON_TEST=m
-1
arch/powerpc/configs/ppc64_defconfig
··· 426 426 CONFIG_KUNIT=m 427 427 CONFIG_KUNIT_ALL_TESTS=m 428 428 CONFIG_LKDTM=m 429 - CONFIG_TEST_MIN_HEAP=m 430 429 CONFIG_TEST_DIV64=m 431 430 CONFIG_BACKTRACE_SELF_TEST=m 432 431 CONFIG_TEST_REF_TRACKER=m
+1 -1
arch/s390/configs/debug_defconfig
··· 921 921 CONFIG_FAULT_INJECTION_CONFIGFS=y 922 922 CONFIG_FAULT_INJECTION_STACKTRACE_FILTER=y 923 923 CONFIG_LKDTM=m 924 - CONFIG_TEST_MIN_HEAP=y 924 + CONFIG_MIN_HEAP_KUNIT_TEST=m 925 925 CONFIG_KPROBES_SANITY_TEST=m 926 926 CONFIG_RBTREE_TEST=y 927 927 CONFIG_INTERVAL_TREE_TEST=m
+11 -10
lib/Kconfig.debug
··· 2278 2278 2279 2279 If unsure, say N. 2280 2280 2281 - config TEST_MIN_HEAP 2282 - tristate "Min heap test" 2283 - depends on DEBUG_KERNEL || m 2284 - help 2285 - Enable this to turn on min heap function tests. This test is 2286 - executed only once during system boot (so affects only boot time), 2287 - or at module load time. 2288 - 2289 - If unsure, say N. 2290 - 2291 2281 config TEST_SORT 2292 2282 tristate "Array-based sort test" if !KUNIT_ALL_TESTS 2293 2283 depends on KUNIT ··· 2867 2877 to the KUnit documentation in Documentation/dev-tools/kunit/. 2868 2878 2869 2879 If unsure, say N. 2880 + 2881 + config MIN_HEAP_KUNIT_TEST 2882 + tristate "Min heap test" if !KUNIT_ALL_TESTS 2883 + depends on KUNIT 2884 + default KUNIT_ALL_TESTS 2885 + help 2886 + This option enables the KUnit test suite for the min heap library 2887 + which provides functions for creating and managing min heaps. 2888 + The test suite checks the functionality of the min heap library. 2889 + 2890 + If unsure, say N 2870 2891 2871 2892 config IS_SIGNED_TYPE_KUNIT_TEST 2872 2893 tristate "Test is_signed_type() macro" if !KUNIT_ALL_TESTS
-1
lib/Makefile
··· 75 75 CFLAGS_test_ubsan.o += $(call cc-disable-warning, unused-but-set-variable) 76 76 UBSAN_SANITIZE_test_ubsan.o := y 77 77 obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o 78 - obj-$(CONFIG_TEST_MIN_HEAP) += test_min_heap.o 79 78 obj-$(CONFIG_TEST_LKM) += test_module.o 80 79 obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o 81 80 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
+66 -79
lib/test_min_heap.c lib/tests/min_heap_kunit.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #define pr_fmt(fmt) "min_heap_test: " fmt 3 - 4 2 /* 5 3 * Test cases for the min max heap. 6 4 */ 7 5 8 - #include <linux/log2.h> 6 + #include <kunit/test.h> 9 7 #include <linux/min_heap.h> 10 8 #include <linux/module.h> 11 - #include <linux/printk.h> 12 9 #include <linux/random.h> 10 + 11 + struct min_heap_test_case { 12 + const char *str; 13 + bool min_heap; 14 + }; 15 + 16 + static struct min_heap_test_case min_heap_cases[] = { 17 + { 18 + .str = "min", 19 + .min_heap = true, 20 + }, 21 + { 22 + .str = "max", 23 + .min_heap = false, 24 + }, 25 + }; 26 + 27 + KUNIT_ARRAY_PARAM_DESC(min_heap, min_heap_cases, str); 13 28 14 29 DEFINE_MIN_HEAP(int, min_heap_test); 15 30 16 - static __init bool less_than(const void *lhs, const void *rhs, void __always_unused *args) 31 + static bool less_than(const void *lhs, const void *rhs, void __always_unused *args) 17 32 { 18 33 return *(int *)lhs < *(int *)rhs; 19 34 } 20 35 21 - static __init bool greater_than(const void *lhs, const void *rhs, void __always_unused *args) 36 + static bool greater_than(const void *lhs, const void *rhs, void __always_unused *args) 22 37 { 23 38 return *(int *)lhs > *(int *)rhs; 24 39 } 25 40 26 - static __init int pop_verify_heap(bool min_heap, 27 - struct min_heap_test *heap, 28 - const struct min_heap_callbacks *funcs) 41 + static void pop_verify_heap(struct kunit *test, 42 + bool min_heap, 43 + struct min_heap_test *heap, 44 + const struct min_heap_callbacks *funcs) 29 45 { 30 46 int *values = heap->data; 31 - int err = 0; 32 47 int last; 33 48 34 49 last = values[0]; 35 50 min_heap_pop_inline(heap, funcs, NULL); 36 51 while (heap->nr > 0) { 37 - if (min_heap) { 38 - if (last > values[0]) { 39 - pr_err("error: expected %d <= %d\n", last, 40 - values[0]); 41 - err++; 42 - } 43 - } else { 44 - if (last < values[0]) { 45 - pr_err("error: expected %d >= %d\n", last, 46 - values[0]); 47 - err++; 48 - } 49 - } 52 + if (min_heap) 53 + KUNIT_EXPECT_LE(test, last, values[0]); 54 + else 55 + KUNIT_EXPECT_GE(test, last, values[0]); 50 56 last = values[0]; 51 57 min_heap_pop_inline(heap, funcs, NULL); 52 58 } 53 - return err; 54 59 } 55 60 56 - static __init int test_heapify_all(bool min_heap) 61 + static void test_heapify_all(struct kunit *test) 57 62 { 63 + const struct min_heap_test_case *params = test->param_value; 58 64 int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, 59 65 -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; 60 66 struct min_heap_test heap = { ··· 69 63 .size = ARRAY_SIZE(values), 70 64 }; 71 65 struct min_heap_callbacks funcs = { 72 - .less = min_heap ? less_than : greater_than, 66 + .less = params->min_heap ? less_than : greater_than, 73 67 .swp = NULL, 74 68 }; 75 - int i, err; 69 + int i; 76 70 77 71 /* Test with known set of values. */ 78 72 min_heapify_all_inline(&heap, &funcs, NULL); 79 - err = pop_verify_heap(min_heap, &heap, &funcs); 80 - 73 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 81 74 82 75 /* Test with randomly generated values. */ 83 76 heap.nr = ARRAY_SIZE(values); ··· 84 79 values[i] = get_random_u32(); 85 80 86 81 min_heapify_all_inline(&heap, &funcs, NULL); 87 - err += pop_verify_heap(min_heap, &heap, &funcs); 88 - 89 - return err; 82 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 90 83 } 91 84 92 - static __init int test_heap_push(bool min_heap) 85 + static void test_heap_push(struct kunit *test) 93 86 { 87 + const struct min_heap_test_case *params = test->param_value; 94 88 const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, 95 89 -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; 96 90 int values[ARRAY_SIZE(data)]; ··· 99 95 .size = ARRAY_SIZE(values), 100 96 }; 101 97 struct min_heap_callbacks funcs = { 102 - .less = min_heap ? less_than : greater_than, 98 + .less = params->min_heap ? less_than : greater_than, 103 99 .swp = NULL, 104 100 }; 105 - int i, temp, err; 101 + int i, temp; 106 102 107 103 /* Test with known set of values copied from data. */ 108 104 for (i = 0; i < ARRAY_SIZE(data); i++) 109 105 min_heap_push_inline(&heap, &data[i], &funcs, NULL); 110 106 111 - err = pop_verify_heap(min_heap, &heap, &funcs); 107 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 112 108 113 109 /* Test with randomly generated values. */ 114 110 while (heap.nr < heap.size) { 115 111 temp = get_random_u32(); 116 112 min_heap_push_inline(&heap, &temp, &funcs, NULL); 117 113 } 118 - err += pop_verify_heap(min_heap, &heap, &funcs); 119 - 120 - return err; 114 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 121 115 } 122 116 123 - static __init int test_heap_pop_push(bool min_heap) 117 + static void test_heap_pop_push(struct kunit *test) 124 118 { 119 + const struct min_heap_test_case *params = test->param_value; 125 120 const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, 126 121 -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; 127 122 int values[ARRAY_SIZE(data)]; ··· 130 127 .size = ARRAY_SIZE(values), 131 128 }; 132 129 struct min_heap_callbacks funcs = { 133 - .less = min_heap ? less_than : greater_than, 130 + .less = params->min_heap ? less_than : greater_than, 134 131 .swp = NULL, 135 132 }; 136 - int i, temp, err; 133 + int i, temp; 137 134 138 135 /* Fill values with data to pop and replace. */ 139 - temp = min_heap ? 0x80000000 : 0x7FFFFFFF; 136 + temp = params->min_heap ? 0x80000000 : 0x7FFFFFFF; 140 137 for (i = 0; i < ARRAY_SIZE(data); i++) 141 138 min_heap_push_inline(&heap, &temp, &funcs, NULL); 142 139 ··· 144 141 for (i = 0; i < ARRAY_SIZE(data); i++) 145 142 min_heap_pop_push_inline(&heap, &data[i], &funcs, NULL); 146 143 147 - err = pop_verify_heap(min_heap, &heap, &funcs); 144 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 148 145 149 146 heap.nr = 0; 150 147 for (i = 0; i < ARRAY_SIZE(data); i++) ··· 155 152 temp = get_random_u32(); 156 153 min_heap_pop_push_inline(&heap, &temp, &funcs, NULL); 157 154 } 158 - err += pop_verify_heap(min_heap, &heap, &funcs); 159 - 160 - return err; 155 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 161 156 } 162 157 163 - static __init int test_heap_del(bool min_heap) 158 + static void test_heap_del(struct kunit *test) 164 159 { 160 + const struct min_heap_test_case *params = test->param_value; 165 161 int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, 166 162 -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; 167 163 struct min_heap_test heap; ··· 168 166 min_heap_init_inline(&heap, values, ARRAY_SIZE(values)); 169 167 heap.nr = ARRAY_SIZE(values); 170 168 struct min_heap_callbacks funcs = { 171 - .less = min_heap ? less_than : greater_than, 169 + .less = params->min_heap ? less_than : greater_than, 172 170 .swp = NULL, 173 171 }; 174 - int i, err; 172 + int i; 175 173 176 174 /* Test with known set of values. */ 177 175 min_heapify_all_inline(&heap, &funcs, NULL); 178 176 for (i = 0; i < ARRAY_SIZE(values) / 2; i++) 179 177 min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); 180 - err = pop_verify_heap(min_heap, &heap, &funcs); 181 - 178 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 182 179 183 180 /* Test with randomly generated values. */ 184 181 heap.nr = ARRAY_SIZE(values); ··· 187 186 188 187 for (i = 0; i < ARRAY_SIZE(values) / 2; i++) 189 188 min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); 190 - err += pop_verify_heap(min_heap, &heap, &funcs); 191 - 192 - return err; 189 + pop_verify_heap(test, params->min_heap, &heap, &funcs); 193 190 } 194 191 195 - static int __init test_min_heap_init(void) 196 - { 197 - int err = 0; 192 + static struct kunit_case min_heap_test_cases[] = { 193 + KUNIT_CASE_PARAM(test_heapify_all, min_heap_gen_params), 194 + KUNIT_CASE_PARAM(test_heap_push, min_heap_gen_params), 195 + KUNIT_CASE_PARAM(test_heap_pop_push, min_heap_gen_params), 196 + KUNIT_CASE_PARAM(test_heap_del, min_heap_gen_params), 197 + {}, 198 + }; 198 199 199 - err += test_heapify_all(true); 200 - err += test_heapify_all(false); 201 - err += test_heap_push(true); 202 - err += test_heap_push(false); 203 - err += test_heap_pop_push(true); 204 - err += test_heap_pop_push(false); 205 - err += test_heap_del(true); 206 - err += test_heap_del(false); 207 - if (err) { 208 - pr_err("test failed with %d errors\n", err); 209 - return -EINVAL; 210 - } 211 - pr_info("test passed\n"); 212 - return 0; 213 - } 214 - module_init(test_min_heap_init); 200 + static struct kunit_suite min_heap_test_suite = { 201 + .name = "min_heap", 202 + .test_cases = min_heap_test_cases, 203 + }; 215 204 216 - static void __exit test_min_heap_exit(void) 217 - { 218 - /* do nothing */ 219 - } 220 - module_exit(test_min_heap_exit); 205 + kunit_test_suite(min_heap_test_suite); 221 206 222 207 MODULE_DESCRIPTION("Test cases for the min max heap"); 223 208 MODULE_LICENSE("GPL");
+1
lib/tests/Makefile
··· 33 33 obj-$(CONFIG_LONGEST_SYM_KUNIT_TEST) += longest_symbol_kunit.o 34 34 35 35 obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o 36 + obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o 36 37 CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare) 37 38 obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o 38 39 obj-$(CONFIG_PRINTF_KUNIT_TEST) += printf_kunit.o