1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "min_heap_test: " fmt
5 * Test cases for the min max heap.
8 #include <linux/log2.h>
9 #include <linux/min_heap.h>
10 #include <linux/module.h>
11 #include <linux/printk.h>
12 #include <linux/random.h>
14 DEFINE_MIN_HEAP(int, min_heap_test);
16 static __init bool less_than(const void *lhs, const void *rhs, void __always_unused *args)
18 return *(int *)lhs < *(int *)rhs;
21 static __init bool greater_than(const void *lhs, const void *rhs, void __always_unused *args)
23 return *(int *)lhs > *(int *)rhs;
26 static __init void swap_ints(void *lhs, void *rhs, void __always_unused *args)
28 int temp = *(int *)lhs;
30 *(int *)lhs = *(int *)rhs;
34 static __init int pop_verify_heap(bool min_heap,
35 struct min_heap_test *heap,
36 const struct min_heap_callbacks *funcs)
38 int *values = heap->data;
43 min_heap_pop(heap, funcs, NULL);
44 while (heap->nr > 0) {
46 if (last > values[0]) {
47 pr_err("error: expected %d <= %d\n", last,
52 if (last < values[0]) {
53 pr_err("error: expected %d >= %d\n", last,
59 min_heap_pop(heap, funcs, NULL);
64 static __init int test_heapify_all(bool min_heap)
66 int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0,
67 -3, -1, -2, -4, 0x8000000, 0x7FFFFFF };
68 struct min_heap_test heap = {
70 .nr = ARRAY_SIZE(values),
71 .size = ARRAY_SIZE(values),
73 struct min_heap_callbacks funcs = {
74 .less = min_heap ? less_than : greater_than,
79 /* Test with known set of values. */
80 min_heapify_all(&heap, &funcs, NULL);
81 err = pop_verify_heap(min_heap, &heap, &funcs);
84 /* Test with randomly generated values. */
85 heap.nr = ARRAY_SIZE(values);
86 for (i = 0; i < heap.nr; i++)
87 values[i] = get_random_u32();
89 min_heapify_all(&heap, &funcs, NULL);
90 err += pop_verify_heap(min_heap, &heap, &funcs);
95 static __init int test_heap_push(bool min_heap)
97 const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0,
98 -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF };
99 int values[ARRAY_SIZE(data)];
100 struct min_heap_test heap = {
103 .size = ARRAY_SIZE(values),
105 struct min_heap_callbacks funcs = {
106 .less = min_heap ? less_than : greater_than,
111 /* Test with known set of values copied from data. */
112 for (i = 0; i < ARRAY_SIZE(data); i++)
113 min_heap_push(&heap, &data[i], &funcs, NULL);
115 err = pop_verify_heap(min_heap, &heap, &funcs);
117 /* Test with randomly generated values. */
118 while (heap.nr < heap.size) {
119 temp = get_random_u32();
120 min_heap_push(&heap, &temp, &funcs, NULL);
122 err += pop_verify_heap(min_heap, &heap, &funcs);
127 static __init int test_heap_pop_push(bool min_heap)
129 const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0,
130 -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF };
131 int values[ARRAY_SIZE(data)];
132 struct min_heap_test heap = {
135 .size = ARRAY_SIZE(values),
137 struct min_heap_callbacks funcs = {
138 .less = min_heap ? less_than : greater_than,
143 /* Fill values with data to pop and replace. */
144 temp = min_heap ? 0x80000000 : 0x7FFFFFFF;
145 for (i = 0; i < ARRAY_SIZE(data); i++)
146 min_heap_push(&heap, &temp, &funcs, NULL);
148 /* Test with known set of values copied from data. */
149 for (i = 0; i < ARRAY_SIZE(data); i++)
150 min_heap_pop_push(&heap, &data[i], &funcs, NULL);
152 err = pop_verify_heap(min_heap, &heap, &funcs);
155 for (i = 0; i < ARRAY_SIZE(data); i++)
156 min_heap_push(&heap, &temp, &funcs, NULL);
158 /* Test with randomly generated values. */
159 for (i = 0; i < ARRAY_SIZE(data); i++) {
160 temp = get_random_u32();
161 min_heap_pop_push(&heap, &temp, &funcs, NULL);
163 err += pop_verify_heap(min_heap, &heap, &funcs);
168 static __init int test_heap_del(bool min_heap)
170 int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0,
171 -3, -1, -2, -4, 0x8000000, 0x7FFFFFF };
172 struct min_heap_test heap;
174 min_heap_init(&heap, values, ARRAY_SIZE(values));
175 heap.nr = ARRAY_SIZE(values);
176 struct min_heap_callbacks funcs = {
177 .less = min_heap ? less_than : greater_than,
182 /* Test with known set of values. */
183 min_heapify_all(&heap, &funcs, NULL);
184 for (i = 0; i < ARRAY_SIZE(values) / 2; i++)
185 min_heap_del(&heap, get_random_u32() % heap.nr, &funcs, NULL);
186 err = pop_verify_heap(min_heap, &heap, &funcs);
189 /* Test with randomly generated values. */
190 heap.nr = ARRAY_SIZE(values);
191 for (i = 0; i < heap.nr; i++)
192 values[i] = get_random_u32();
193 min_heapify_all(&heap, &funcs, NULL);
195 for (i = 0; i < ARRAY_SIZE(values) / 2; i++)
196 min_heap_del(&heap, get_random_u32() % heap.nr, &funcs, NULL);
197 err += pop_verify_heap(min_heap, &heap, &funcs);
202 static int __init test_min_heap_init(void)
206 err += test_heapify_all(true);
207 err += test_heapify_all(false);
208 err += test_heap_push(true);
209 err += test_heap_push(false);
210 err += test_heap_pop_push(true);
211 err += test_heap_pop_push(false);
212 err += test_heap_del(true);
213 err += test_heap_del(false);
215 pr_err("test failed with %d errors\n", err);
218 pr_info("test passed\n");
221 module_init(test_min_heap_init);
223 static void __exit test_min_heap_exit(void)
227 module_exit(test_min_heap_exit);
229 MODULE_DESCRIPTION("Test cases for the min max heap");
230 MODULE_LICENSE("GPL");