diff mbox series

[v3,15/17] lib/test_min_heap: Add test for heap_del()

Message ID 20240406164727.577914-16-visitorckw@gmail.com (mailing list archive)
State Not Applicable, archived
Delegated to: Mike Snitzer
Headers show
Series treewide: Refactor heap related implementation | expand

Commit Message

Kuan-Wei Chiu April 6, 2024, 4:47 p.m. UTC
Add test cases for the min_heap_del() to ensure its functionality is
thoroughly tested.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 lib/test_min_heap.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

Comments

Ian Rogers April 7, 2024, 7:23 p.m. UTC | #1
On Sat, Apr 6, 2024 at 9:48 AM Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
>
> Add test cases for the min_heap_del() to ensure its functionality is
> thoroughly tested.
>
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>

Reviewed-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
>  lib/test_min_heap.c | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
>
> diff --git a/lib/test_min_heap.c b/lib/test_min_heap.c
> index 67dd4f644f6c..9d4bbb590a49 100644
> --- a/lib/test_min_heap.c
> +++ b/lib/test_min_heap.c
> @@ -160,6 +160,40 @@ static __init int test_heap_pop_push(bool min_heap)
>         return err;
>  }
>
> +static __init int test_heap_del(bool min_heap)
> +{
> +       int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0,
> +                        -3, -1, -2, -4, 0x8000000, 0x7FFFFFF };
> +       struct min_heap_test heap;
> +
> +       min_heap_init(&heap, values, ARRAY_SIZE(values));
> +       heap.nr = ARRAY_SIZE(values);
> +       struct min_heap_callbacks funcs = {
> +               .less = min_heap ? less_than : greater_than,
> +               .swp = swap_ints,
> +       };
> +       int i, err;
> +
> +       /* Test with known set of values. */
> +       min_heapify_all(&heap, &funcs, NULL);
> +       for (i = 0; i < ARRAY_SIZE(values) / 2; i++)
> +               min_heap_del(&heap, get_random_u32() % heap.nr, &funcs, NULL);
> +       err = pop_verify_heap(min_heap, &heap, &funcs);
> +
> +
> +       /* Test with randomly generated values. */
> +       heap.nr = ARRAY_SIZE(values);
> +       for (i = 0; i < heap.nr; i++)
> +               values[i] = get_random_u32();
> +       min_heapify_all(&heap, &funcs, NULL);
> +
> +       for (i = 0; i < ARRAY_SIZE(values) / 2; i++)
> +               min_heap_del(&heap, get_random_u32() % heap.nr, &funcs, NULL);
> +       err += pop_verify_heap(min_heap, &heap, &funcs);
> +
> +       return err;
> +}
> +
>  static int __init test_min_heap_init(void)
>  {
>         int err = 0;
> @@ -170,6 +204,8 @@ static int __init test_min_heap_init(void)
>         err += test_heap_push(false);
>         err += test_heap_pop_push(true);
>         err += test_heap_pop_push(false);
> +       err += test_heap_del(true);
> +       err += test_heap_del(false);
>         if (err) {
>                 pr_err("test failed with %d errors\n", err);
>                 return -EINVAL;
> --
> 2.34.1
>
diff mbox series

Patch

diff --git a/lib/test_min_heap.c b/lib/test_min_heap.c
index 67dd4f644f6c..9d4bbb590a49 100644
--- a/lib/test_min_heap.c
+++ b/lib/test_min_heap.c
@@ -160,6 +160,40 @@  static __init int test_heap_pop_push(bool min_heap)
 	return err;
 }
 
+static __init int test_heap_del(bool min_heap)
+{
+	int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0,
+			 -3, -1, -2, -4, 0x8000000, 0x7FFFFFF };
+	struct min_heap_test heap;
+
+	min_heap_init(&heap, values, ARRAY_SIZE(values));
+	heap.nr = ARRAY_SIZE(values);
+	struct min_heap_callbacks funcs = {
+		.less = min_heap ? less_than : greater_than,
+		.swp = swap_ints,
+	};
+	int i, err;
+
+	/* Test with known set of values. */
+	min_heapify_all(&heap, &funcs, NULL);
+	for (i = 0; i < ARRAY_SIZE(values) / 2; i++)
+		min_heap_del(&heap, get_random_u32() % heap.nr, &funcs, NULL);
+	err = pop_verify_heap(min_heap, &heap, &funcs);
+
+
+	/* Test with randomly generated values. */
+	heap.nr = ARRAY_SIZE(values);
+	for (i = 0; i < heap.nr; i++)
+		values[i] = get_random_u32();
+	min_heapify_all(&heap, &funcs, NULL);
+
+	for (i = 0; i < ARRAY_SIZE(values) / 2; i++)
+		min_heap_del(&heap, get_random_u32() % heap.nr, &funcs, NULL);
+	err += pop_verify_heap(min_heap, &heap, &funcs);
+
+	return err;
+}
+
 static int __init test_min_heap_init(void)
 {
 	int err = 0;
@@ -170,6 +204,8 @@  static int __init test_min_heap_init(void)
 	err += test_heap_push(false);
 	err += test_heap_pop_push(true);
 	err += test_heap_pop_push(false);
+	err += test_heap_del(true);
+	err += test_heap_del(false);
 	if (err) {
 		pr_err("test failed with %d errors\n", err);
 		return -EINVAL;