| @@ -1,60 +1,66 @@ |
| 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> |
| 13 | 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); |
| 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 = { |
| @@ -63,15 +69,14 @@ static __init int test_heapify_all(bool min_heap) · 2 unmodified lines |
| 63 | 69 | .size = ARRAY_SIZE(values), |
| 64 | 70 | }; |
| 65 | 71 | struct min_heap_callbacks funcs = { |
| 66 | | − .less = min_heap ? less_than : greater_than, |
| 72 | + .less = params->min_heap ? less_than : greater_than, |
| 67 | 73 | .swp = NULL, |
| 68 | 74 | }; |
| 69 | | − int i, err; |
| 75 | + int i; |
| 70 | 76 | |
| 71 | 77 | /* Test with known set of values. */ |
| 72 | 78 | min_heapify_all_inline(&heap, &funcs, NULL); |
| 73 | | − err = pop_verify_heap(min_heap, &heap, &funcs); |
| 74 | | − |
| 79 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 75 | 80 | |
| 76 | 81 | /* Test with randomly generated values. */ |
| 77 | 82 | heap.nr = ARRAY_SIZE(values); |
| @@ -79,13 +84,12 @@ static __init int test_heapify_all(bool min_heap) · 1 unmodified line |
| 79 | 84 | values[i] = get_random_u32(); |
| 80 | 85 | |
| 81 | 86 | min_heapify_all_inline(&heap, &funcs, NULL); |
| 82 | | − err += pop_verify_heap(min_heap, &heap, &funcs); |
| 83 | | − |
| 84 | | − return err; |
| 87 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 85 | 88 | } |
| 86 | 89 | |
| 87 | | −static __init int test_heap_push(bool min_heap) |
| 90 | +static void test_heap_push(struct kunit *test) |
| 88 | 91 | { |
| 92 | + const struct min_heap_test_case *params = test->param_value; |
| 89 | 93 | const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, |
| 90 | 94 | -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; |
| 91 | 95 | int values[ARRAY_SIZE(data)]; |
| @@ -95,29 +99,28 @@ static __init int test_heap_push(bool min_heap) · 3 unmodified lines |
| 95 | 99 | .size = ARRAY_SIZE(values), |
| 96 | 100 | }; |
| 97 | 101 | struct min_heap_callbacks funcs = { |
| 98 | | − .less = min_heap ? less_than : greater_than, |
| 102 | + .less = params->min_heap ? less_than : greater_than, |
| 99 | 103 | .swp = NULL, |
| 100 | 104 | }; |
| 101 | | − int i, temp, err; |
| 105 | + int i, temp; |
| 102 | 106 | |
| 103 | 107 | /* Test with known set of values copied from data. */ |
| 104 | 108 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| 105 | 109 | min_heap_push_inline(&heap, &data[i], &funcs, NULL); |
| 106 | 110 | |
| 107 | | − err = pop_verify_heap(min_heap, &heap, &funcs); |
| 111 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 108 | 112 | |
| 109 | 113 | /* Test with randomly generated values. */ |
| 110 | 114 | while (heap.nr < heap.size) { |
| 111 | 115 | temp = get_random_u32(); |
| 112 | 116 | min_heap_push_inline(&heap, &temp, &funcs, NULL); |
| 113 | 117 | } |
| 114 | | − err += pop_verify_heap(min_heap, &heap, &funcs); |
| 115 | | − |
| 116 | | − return err; |
| 118 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 117 | 119 | } |
| 118 | 120 | |
| 119 | | −static __init int test_heap_pop_push(bool min_heap) |
| 121 | +static void test_heap_pop_push(struct kunit *test) |
| 120 | 122 | { |
| 123 | + const struct min_heap_test_case *params = test->param_value; |
| 121 | 124 | const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, |
| 122 | 125 | -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; |
| 123 | 126 | int values[ARRAY_SIZE(data)]; |
| @@ -127,13 +130,13 @@ static __init int test_heap_pop_push(bool min_heap) · 3 unmodified lines |
| 127 | 130 | .size = ARRAY_SIZE(values), |
| 128 | 131 | }; |
| 129 | 132 | struct min_heap_callbacks funcs = { |
| 130 | | − .less = min_heap ? less_than : greater_than, |
| 133 | + .less = params->min_heap ? less_than : greater_than, |
| 131 | 134 | .swp = NULL, |
| 132 | 135 | }; |
| 133 | | − int i, temp, err; |
| 136 | + int i, temp; |
| 134 | 137 | |
| 135 | 138 | /* Fill values with data to pop and replace. */ |
| 136 | | − temp = min_heap ? 0x80000000 : 0x7FFFFFFF; |
| 139 | + temp = params->min_heap ? 0x80000000 : 0x7FFFFFFF; |
| 137 | 140 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| 138 | 141 | min_heap_push_inline(&heap, &temp, &funcs, NULL); |
| 139 | 142 | |
| @@ -141,7 +144,7 @@ static __init int test_heap_pop_push(bool min_heap) · 1 unmodified line |
| 141 | 144 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| 142 | 145 | min_heap_pop_push_inline(&heap, &data[i], &funcs, NULL); |
| 143 | 146 | |
| 144 | | − err = pop_verify_heap(min_heap, &heap, &funcs); |
| 147 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 145 | 148 | |
| 146 | 149 | heap.nr = 0; |
| 147 | 150 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| @@ -152,13 +155,12 @@ static __init int test_heap_pop_push(bool min_heap) · 4 unmodified lines |
| 152 | 155 | temp = get_random_u32(); |
| 153 | 156 | min_heap_pop_push_inline(&heap, &temp, &funcs, NULL); |
| 154 | 157 | } |
| 155 | | − err += pop_verify_heap(min_heap, &heap, &funcs); |
| 156 | | − |
| 157 | | − return err; |
| 158 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 158 | 159 | } |
| 159 | 160 | |
| 160 | | −static __init int test_heap_del(bool min_heap) |
| 161 | +static void test_heap_del(struct kunit *test) |
| 161 | 162 | { |
| 163 | + const struct min_heap_test_case *params = test->param_value; |
| 162 | 164 | int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, |
| 163 | 165 | -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; |
| 164 | 166 | struct min_heap_test heap; |
| @@ -166,17 +168,16 @@ static __init int test_heap_del(bool min_heap) · 1 unmodified line |
| 166 | 168 | min_heap_init_inline(&heap, values, ARRAY_SIZE(values)); |
| 167 | 169 | heap.nr = ARRAY_SIZE(values); |
| 168 | 170 | struct min_heap_callbacks funcs = { |
| 169 | | − .less = min_heap ? less_than : greater_than, |
| 171 | + .less = params->min_heap ? less_than : greater_than, |
| 170 | 172 | .swp = NULL, |
| 171 | 173 | }; |
| 172 | | − int i, err; |
| 174 | + int i; |
| 173 | 175 | |
| 174 | 176 | /* Test with known set of values. */ |
| 175 | 177 | min_heapify_all_inline(&heap, &funcs, NULL); |
| 176 | 178 | for (i = 0; i < ARRAY_SIZE(values) / 2; i++) |
| 177 | 179 | min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); |
| 178 | | − err = pop_verify_heap(min_heap, &heap, &funcs); |
| 179 | | − |
| 180 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 180 | 181 | |
| 181 | 182 | /* Test with randomly generated values. */ |
| 182 | 183 | heap.nr = ARRAY_SIZE(values); |
| @@ -186,37 +187,23 @@ static __init int test_heap_del(bool min_heap) · 3 unmodified lines |
| 186 | 187 | |
| 187 | 188 | for (i = 0; i < ARRAY_SIZE(values) / 2; i++) |
| 188 | 189 | min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); |
| 189 | | − err += pop_verify_heap(min_heap, &heap, &funcs); |
| 190 | | − |
| 191 | | − return err; |
| 190 | + pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 192 | 191 | } |
| 193 | 192 | |
| 194 | | −static int __init test_min_heap_init(void) |
| 195 | | −{ |
| 196 | | − int err = 0; |
| 197 | | − |
| 198 | | − err += test_heapify_all(true); |
| 199 | | − err += test_heapify_all(false); |
| 200 | | − err += test_heap_push(true); |
| 201 | | − err += test_heap_push(false); |
| 202 | | − err += test_heap_pop_push(true); |
| 203 | | − err += test_heap_pop_push(false); |
| 204 | | − err += test_heap_del(true); |
| 205 | | − err += test_heap_del(false); |
| 206 | | − if (err) { |
| 207 | | − pr_err("test failed with %d errors\n", err); |
| 208 | | − return -EINVAL; |
| 209 | | − } |
| 210 | | − pr_info("test passed\n"); |
| 211 | | − return 0; |
| 212 | | −} |
| 213 | | −module_init(test_min_heap_init); |
| 193 | +static struct kunit_case min_heap_test_cases[] = { |
| 194 | + KUNIT_CASE_PARAM(test_heapify_all, min_heap_gen_params), |
| 195 | + KUNIT_CASE_PARAM(test_heap_push, min_heap_gen_params), |
| 196 | + KUNIT_CASE_PARAM(test_heap_pop_push, min_heap_gen_params), |
| 197 | + KUNIT_CASE_PARAM(test_heap_del, min_heap_gen_params), |
| 198 | + {}, |
| 199 | +}; |
| 214 | 200 | |
| 215 | | −static void __exit test_min_heap_exit(void) |
| 216 | | −{ |
| 217 | | − /* do nothing */ |
| 218 | | −} |
| 219 | | −module_exit(test_min_heap_exit); |
| 201 | +static struct kunit_suite min_heap_test_suite = { |
| 202 | + .name = "min_heap", |
| 203 | + .test_cases = min_heap_test_cases, |
| 204 | +}; |
| 205 | + |
| 206 | +kunit_test_suite(min_heap_test_suite); |
| 220 | 207 | |
| 221 | 208 | MODULE_DESCRIPTION("Test cases for the min max heap"); |
| 222 | 209 | MODULE_LICENSE("GPL"); |