| @@ -1,60 +1,66 @@ |
| 1 | // SPDX-License-Identifier: GPL-2.0-only | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | #define pr_fmt(fmt) "min_heap_test: " fmt | | |
| 3 | | | |
| 4 | /* | 2 | /* |
| 5 | * Test cases for the min max heap. | 3 | * Test cases for the min max heap. |
| 6 | */ | 4 | */ |
| 7 | | 5 | |
| 8 | #include <linux/log2.h> | 6 | #include <kunit/test.h> |
| 9 | #include <linux/min_heap.h> | 7 | #include <linux/min_heap.h> |
| 10 | #include <linux/module.h> | 8 | #include <linux/module.h> |
| 11 | #include <linux/printk.h> | | |
| 12 | #include <linux/random.h> | 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 | DEFINE_MIN_HEAP(int, min_heap_test); | 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 | return *(int *)lhs < *(int *)rhs; | 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 | return *(int *)lhs > *(int *)rhs; | 38 | return *(int *)lhs > *(int *)rhs; |
| 24 | } | 39 | } |
| 25 | | 40 | |
| 26 | static __init int pop_verify_heap(bool min_heap, | 41 | static void pop_verify_heap(struct kunit *test, |
| 27 | struct min_heap_test *heap, | 42 | bool min_heap, |
| 28 | const struct min_heap_callbacks *funcs) | 43 | struct min_heap_test *heap, |
| | 44 | const struct min_heap_callbacks *funcs) |
| 29 | { | 45 | { |
| 30 | int *values = heap->data; | 46 | int *values = heap->data; |
| 31 | int err = 0; | | |
| 32 | int last; | 47 | int last; |
| 33 | | 48 | |
| 34 | last = values[0]; | 49 | last = values[0]; |
| 35 | min_heap_pop_inline(heap, funcs, NULL); | 50 | min_heap_pop_inline(heap, funcs, NULL); |
| 36 | while (heap->nr > 0) { | 51 | while (heap->nr > 0) { |
| 37 | if (min_heap) { | 52 | if (min_heap) |
| 38 | if (last > values[0]) { | 53 | KUNIT_EXPECT_LE(test, last, values[0]); |
| 39 | pr_err("error: expected %d <= %d\n", last, | 54 | else |
| 40 | values[0]); | 55 | KUNIT_EXPECT_GE(test, last, 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 | } | | |
| 50 | last = values[0]; | 56 | last = values[0]; |
| 51 | min_heap_pop_inline(heap, funcs, NULL); | 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 | int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, | 64 | int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, |
| 59 | -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; | 65 | -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; |
| 60 | struct min_heap_test heap = { | 66 | struct min_heap_test heap = { |
| @@ -63,15 +69,14 @@ static __init int test_heapify_all(bool min_heap) · 2 unmodified lines |
| 63 | .size = ARRAY_SIZE(values), | 69 | .size = ARRAY_SIZE(values), |
| 64 | }; | 70 | }; |
| 65 | struct min_heap_callbacks funcs = { | 71 | struct min_heap_callbacks funcs = { |
| 66 | .less = min_heap ? less_than : greater_than, | 72 | .less = params->min_heap ? less_than : greater_than, |
| 67 | .swp = NULL, | 73 | .swp = NULL, |
| 68 | }; | 74 | }; |
| 69 | int i, err; | 75 | int i; |
| 70 | | 76 | |
| 71 | /* Test with known set of values. */ | 77 | /* Test with known set of values. */ |
| 72 | min_heapify_all_inline(&heap, &funcs, NULL); | 78 | min_heapify_all_inline(&heap, &funcs, NULL); |
| 73 | err = pop_verify_heap(min_heap, &heap, &funcs); | 79 | pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 74 | | | |
| 75 | | 80 | |
| 76 | /* Test with randomly generated values. */ | 81 | /* Test with randomly generated values. */ |
| 77 | heap.nr = ARRAY_SIZE(values); | 82 | heap.nr = ARRAY_SIZE(values); |
| @@ -79,13 +84,12 @@ static __init int test_heapify_all(bool min_heap) · 1 unmodified line |
| 79 | values[i] = get_random_u32(); | 84 | values[i] = get_random_u32(); |
| 80 | | 85 | |
| 81 | min_heapify_all_inline(&heap, &funcs, NULL); | 86 | min_heapify_all_inline(&heap, &funcs, NULL); |
| 82 | err += pop_verify_heap(min_heap, &heap, &funcs); | 87 | pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 83 | | | |
| 84 | return err; | | |
| 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 | const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, | 93 | const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, |
| 90 | -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; | 94 | -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; |
| 91 | int values[ARRAY_SIZE(data)]; | 95 | int values[ARRAY_SIZE(data)]; |
| @@ -95,29 +99,28 @@ static __init int test_heap_push(bool min_heap) · 3 unmodified lines |
| 95 | .size = ARRAY_SIZE(values), | 99 | .size = ARRAY_SIZE(values), |
| 96 | }; | 100 | }; |
| 97 | struct min_heap_callbacks funcs = { | 101 | struct min_heap_callbacks funcs = { |
| 98 | .less = min_heap ? less_than : greater_than, | 102 | .less = params->min_heap ? less_than : greater_than, |
| 99 | .swp = NULL, | 103 | .swp = NULL, |
| 100 | }; | 104 | }; |
| 101 | int i, temp, err; | 105 | int i, temp; |
| 102 | | 106 | |
| 103 | /* Test with known set of values copied from data. */ | 107 | /* Test with known set of values copied from data. */ |
| 104 | for (i = 0; i < ARRAY_SIZE(data); i++) | 108 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| 105 | min_heap_push_inline(&heap, &data[i], &funcs, NULL); | 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 | /* Test with randomly generated values. */ | 113 | /* Test with randomly generated values. */ |
| 110 | while (heap.nr < heap.size) { | 114 | while (heap.nr < heap.size) { |
| 111 | temp = get_random_u32(); | 115 | temp = get_random_u32(); |
| 112 | min_heap_push_inline(&heap, &temp, &funcs, NULL); | 116 | min_heap_push_inline(&heap, &temp, &funcs, NULL); |
| 113 | } | 117 | } |
| 114 | err += pop_verify_heap(min_heap, &heap, &funcs); | 118 | pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 115 | | | |
| 116 | return err; | | |
| 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 | const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, | 124 | const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, |
| 122 | -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; | 125 | -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; |
| 123 | int values[ARRAY_SIZE(data)]; | 126 | int values[ARRAY_SIZE(data)]; |
| @@ -127,13 +130,13 @@ static __init int test_heap_pop_push(bool min_heap) · 3 unmodified lines |
| 127 | .size = ARRAY_SIZE(values), | 130 | .size = ARRAY_SIZE(values), |
| 128 | }; | 131 | }; |
| 129 | struct min_heap_callbacks funcs = { | 132 | struct min_heap_callbacks funcs = { |
| 130 | .less = min_heap ? less_than : greater_than, | 133 | .less = params->min_heap ? less_than : greater_than, |
| 131 | .swp = NULL, | 134 | .swp = NULL, |
| 132 | }; | 135 | }; |
| 133 | int i, temp, err; | 136 | int i, temp; |
| 134 | | 137 | |
| 135 | /* Fill values with data to pop and replace. */ | 138 | /* Fill values with data to pop and replace. */ |
| 136 | temp = min_heap ? 0x80000000 : 0x7FFFFFFF; | 139 | temp = params->min_heap ? 0x80000000 : 0x7FFFFFFF; |
| 137 | for (i = 0; i < ARRAY_SIZE(data); i++) | 140 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| 138 | min_heap_push_inline(&heap, &temp, &funcs, NULL); | 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 | for (i = 0; i < ARRAY_SIZE(data); i++) | 144 | for (i = 0; i < ARRAY_SIZE(data); i++) |
| 142 | min_heap_pop_push_inline(&heap, &data[i], &funcs, NULL); | 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 | heap.nr = 0; | 149 | heap.nr = 0; |
| 147 | for (i = 0; i < ARRAY_SIZE(data); i++) | 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 | temp = get_random_u32(); | 155 | temp = get_random_u32(); |
| 153 | min_heap_pop_push_inline(&heap, &temp, &funcs, NULL); | 156 | min_heap_pop_push_inline(&heap, &temp, &funcs, NULL); |
| 154 | } | 157 | } |
| 155 | err += pop_verify_heap(min_heap, &heap, &funcs); | 158 | pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 156 | | | |
| 157 | return err; | | |
| 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 | int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, | 164 | int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, |
| 163 | -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; | 165 | -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; |
| 164 | struct min_heap_test heap; | 166 | struct min_heap_test heap; |
| @@ -166,17 +168,16 @@ static __init int test_heap_del(bool min_heap) · 1 unmodified line |
| 166 | min_heap_init_inline(&heap, values, ARRAY_SIZE(values)); | 168 | min_heap_init_inline(&heap, values, ARRAY_SIZE(values)); |
| 167 | heap.nr = ARRAY_SIZE(values); | 169 | heap.nr = ARRAY_SIZE(values); |
| 168 | struct min_heap_callbacks funcs = { | 170 | struct min_heap_callbacks funcs = { |
| 169 | .less = min_heap ? less_than : greater_than, | 171 | .less = params->min_heap ? less_than : greater_than, |
| 170 | .swp = NULL, | 172 | .swp = NULL, |
| 171 | }; | 173 | }; |
| 172 | int i, err; | 174 | int i; |
| 173 | | 175 | |
| 174 | /* Test with known set of values. */ | 176 | /* Test with known set of values. */ |
| 175 | min_heapify_all_inline(&heap, &funcs, NULL); | 177 | min_heapify_all_inline(&heap, &funcs, NULL); |
| 176 | for (i = 0; i < ARRAY_SIZE(values) / 2; i++) | 178 | for (i = 0; i < ARRAY_SIZE(values) / 2; i++) |
| 177 | min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); | 179 | min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); |
| 178 | err = pop_verify_heap(min_heap, &heap, &funcs); | 180 | pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 179 | | | |
| 180 | | 181 | |
| 181 | /* Test with randomly generated values. */ | 182 | /* Test with randomly generated values. */ |
| 182 | heap.nr = ARRAY_SIZE(values); | 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 | for (i = 0; i < ARRAY_SIZE(values) / 2; i++) | 188 | for (i = 0; i < ARRAY_SIZE(values) / 2; i++) |
| 188 | min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); | 189 | min_heap_del_inline(&heap, get_random_u32() % heap.nr, &funcs, NULL); |
| 189 | err += pop_verify_heap(min_heap, &heap, &funcs); | 190 | pop_verify_heap(test, params->min_heap, &heap, &funcs); |
| 190 | | | |
| 191 | return err; | | |
| 192 | } | 191 | } |
| 193 | | 192 | |
| 194 | static int __init test_min_heap_init(void) | 193 | static struct kunit_case min_heap_test_cases[] = { |
| 195 | { | 194 | KUNIT_CASE_PARAM(test_heapify_all, min_heap_gen_params), |
| 196 | int err = 0; | 195 | KUNIT_CASE_PARAM(test_heap_push, min_heap_gen_params), |
| 197 | | 196 | KUNIT_CASE_PARAM(test_heap_pop_push, min_heap_gen_params), |
| 198 | err += test_heapify_all(true); | 197 | KUNIT_CASE_PARAM(test_heap_del, min_heap_gen_params), |
| 199 | err += test_heapify_all(false); | 198 | {}, |
| 200 | err += test_heap_push(true); | 199 | }; |
| 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); | | |
| 214 | | 200 | |
| 215 | static void __exit test_min_heap_exit(void) | 201 | static struct kunit_suite min_heap_test_suite = { |
| 216 | { | 202 | .name = "min_heap", |
| 217 | /* do nothing */ | 203 | .test_cases = min_heap_test_cases, |
| 218 | } | 204 | }; |
| 219 | module_exit(test_min_heap_exit); | 205 | |
| | 206 | kunit_test_suite(min_heap_test_suite); |
| 220 | | 207 | |
| 221 | MODULE_DESCRIPTION("Test cases for the min max heap"); | 208 | MODULE_DESCRIPTION("Test cases for the min max heap"); |
| 222 | MODULE_LICENSE("GPL"); | 209 | MODULE_LICENSE("GPL"); |