diff mbox series

[2/3] list: test: Add a test for hlist_cut_number()

Message ID 20240904134152.2141-3-thunder.leizhen@huawei.com (mailing list archive)
State New
Headers show
Series debugobjects: Add hlist_cut_number() and use it to optimize code | expand

Commit Message

Leizhen (ThunderTown) Sept. 4, 2024, 1:41 p.m. UTC
Test cases cover all possible situations:
1. The cut number is invalid: zero or negative
2. Partially cut.
3. Cut all.
4. The cut number is greater than the number of nodes in the old list.
5. The old list is empty.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 lib/list-test.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

Comments

David Gow Sept. 7, 2024, 6:49 a.m. UTC | #1
On Wed, 4 Sept 2024 at 21:43, 'Zhen Lei' via KUnit Development
<kunit-dev@googlegroups.com> wrote:
>
> Test cases cover all possible situations:
> 1. The cut number is invalid: zero or negative
> 2. Partially cut.
> 3. Cut all.
> 4. The cut number is greater than the number of nodes in the old list.
> 5. The old list is empty.
>
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---

Thanks very much for the detailed test. It's great to see these kept up-to-date!

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  lib/list-test.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 51 insertions(+)
>
> diff --git a/lib/list-test.c b/lib/list-test.c
> index 37cbc33e9fdb380..3c60a6458545452 100644
> --- a/lib/list-test.c
> +++ b/lib/list-test.c
> @@ -1172,6 +1172,56 @@ static void hlist_test_for_each_entry_safe(struct kunit *test)
>         KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
>  }
>
> +static void hlist_test_cut_number(struct kunit *test)
> +{
> +       struct hlist_node a[4], *last;
> +       HLIST_HEAD(old);
> +       HLIST_HEAD(new);
> +       int cnt;
> +
> +       hlist_add_head(&a[3], &old);
> +       hlist_add_head(&a[2], &old);
> +       hlist_add_head(&a[1], &old);
> +       hlist_add_head(&a[0], &old);
> +
> +       /* The cut number is less than 0 or zero */
> +       cnt = hlist_cut_number(&new, &old, -1, &last);
> +       KUNIT_EXPECT_EQ(test, cnt, 0);
> +       KUNIT_EXPECT_EQ(test, hlist_count_nodes(&old), 4);
> +       cnt = hlist_cut_number(&new, &old, 0, &last);
> +       KUNIT_EXPECT_EQ(test, cnt, 0);
> +       KUNIT_EXPECT_EQ(test, hlist_count_nodes(&old), 4);
> +
> +       /* The cut number is less than the number of nodes in the old list. */
> +       cnt = hlist_cut_number(&new, &old, 2, &last);
> +       KUNIT_EXPECT_EQ(test, cnt, 2);
> +       KUNIT_EXPECT_EQ(test, hlist_count_nodes(&old), 2);
> +       KUNIT_EXPECT_EQ(test, hlist_count_nodes(&new), 2);
> +       KUNIT_EXPECT_PTR_EQ(test, last, &a[1]);
> +       hlist_splice_init(&new, last, &old);
> +
> +       /* The cut number is equal to the number of nodes in the old list. */
> +       cnt = hlist_cut_number(&new, &old, 4, &last);
> +       KUNIT_EXPECT_EQ(test, cnt, 4);
> +       KUNIT_EXPECT_TRUE(test, hlist_empty(&old));
> +       KUNIT_EXPECT_EQ(test, hlist_count_nodes(&new), 4);
> +       KUNIT_EXPECT_PTR_EQ(test, last, &a[3]);
> +       hlist_splice_init(&new, last, &old);
> +
> +       /* The cut number is greater than the number of nodes in the old list. */
> +       cnt = hlist_cut_number(&new, &old, 5, &last);
> +       KUNIT_EXPECT_EQ(test, cnt, 4);
> +       KUNIT_EXPECT_TRUE(test, hlist_empty(&old));
> +       KUNIT_EXPECT_EQ(test, hlist_count_nodes(&new), 4);
> +       KUNIT_EXPECT_PTR_EQ(test, last, &a[3]);
> +
> +       /* The old list is empty. */
> +       cnt = hlist_cut_number(&new, &old, 1, &last);
> +       KUNIT_EXPECT_EQ(test, cnt, 0);
> +       KUNIT_EXPECT_TRUE(test, hlist_empty(&old));
> +       KUNIT_EXPECT_TRUE(test, hlist_empty(&new));
> +}
> +
>
>  static struct kunit_case hlist_test_cases[] = {
>         KUNIT_CASE(hlist_test_init),
> @@ -1192,6 +1242,7 @@ static struct kunit_case hlist_test_cases[] = {
>         KUNIT_CASE(hlist_test_for_each_entry_continue),
>         KUNIT_CASE(hlist_test_for_each_entry_from),
>         KUNIT_CASE(hlist_test_for_each_entry_safe),
> +       KUNIT_CASE(hlist_test_cut_number),
>         {},
>  };
>
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20240904134152.2141-3-thunder.leizhen%40huawei.com.
diff mbox series

Patch

diff --git a/lib/list-test.c b/lib/list-test.c
index 37cbc33e9fdb380..3c60a6458545452 100644
--- a/lib/list-test.c
+++ b/lib/list-test.c
@@ -1172,6 +1172,56 @@  static void hlist_test_for_each_entry_safe(struct kunit *test)
 	KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
 }
 
+static void hlist_test_cut_number(struct kunit *test)
+{
+	struct hlist_node a[4], *last;
+	HLIST_HEAD(old);
+	HLIST_HEAD(new);
+	int cnt;
+
+	hlist_add_head(&a[3], &old);
+	hlist_add_head(&a[2], &old);
+	hlist_add_head(&a[1], &old);
+	hlist_add_head(&a[0], &old);
+
+	/* The cut number is less than 0 or zero */
+	cnt = hlist_cut_number(&new, &old, -1, &last);
+	KUNIT_EXPECT_EQ(test, cnt, 0);
+	KUNIT_EXPECT_EQ(test, hlist_count_nodes(&old), 4);
+	cnt = hlist_cut_number(&new, &old, 0, &last);
+	KUNIT_EXPECT_EQ(test, cnt, 0);
+	KUNIT_EXPECT_EQ(test, hlist_count_nodes(&old), 4);
+
+	/* The cut number is less than the number of nodes in the old list. */
+	cnt = hlist_cut_number(&new, &old, 2, &last);
+	KUNIT_EXPECT_EQ(test, cnt, 2);
+	KUNIT_EXPECT_EQ(test, hlist_count_nodes(&old), 2);
+	KUNIT_EXPECT_EQ(test, hlist_count_nodes(&new), 2);
+	KUNIT_EXPECT_PTR_EQ(test, last, &a[1]);
+	hlist_splice_init(&new, last, &old);
+
+	/* The cut number is equal to the number of nodes in the old list. */
+	cnt = hlist_cut_number(&new, &old, 4, &last);
+	KUNIT_EXPECT_EQ(test, cnt, 4);
+	KUNIT_EXPECT_TRUE(test, hlist_empty(&old));
+	KUNIT_EXPECT_EQ(test, hlist_count_nodes(&new), 4);
+	KUNIT_EXPECT_PTR_EQ(test, last, &a[3]);
+	hlist_splice_init(&new, last, &old);
+
+	/* The cut number is greater than the number of nodes in the old list. */
+	cnt = hlist_cut_number(&new, &old, 5, &last);
+	KUNIT_EXPECT_EQ(test, cnt, 4);
+	KUNIT_EXPECT_TRUE(test, hlist_empty(&old));
+	KUNIT_EXPECT_EQ(test, hlist_count_nodes(&new), 4);
+	KUNIT_EXPECT_PTR_EQ(test, last, &a[3]);
+
+	/* The old list is empty. */
+	cnt = hlist_cut_number(&new, &old, 1, &last);
+	KUNIT_EXPECT_EQ(test, cnt, 0);
+	KUNIT_EXPECT_TRUE(test, hlist_empty(&old));
+	KUNIT_EXPECT_TRUE(test, hlist_empty(&new));
+}
+
 
 static struct kunit_case hlist_test_cases[] = {
 	KUNIT_CASE(hlist_test_init),
@@ -1192,6 +1242,7 @@  static struct kunit_case hlist_test_cases[] = {
 	KUNIT_CASE(hlist_test_for_each_entry_continue),
 	KUNIT_CASE(hlist_test_for_each_entry_from),
 	KUNIT_CASE(hlist_test_for_each_entry_safe),
+	KUNIT_CASE(hlist_test_cut_number),
 	{},
 };