Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Zhen Lei <thunder.leizhen@huawei.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	<linux-kernel@vger.kernel.org>, David Gow <davidgow@google.com>,
	<linux-kselftest@vger.kernel.org>, <kunit-dev@googlegroups.com>
Cc: Zhen Lei <thunder.leizhen@huawei.com>
Subject: [PATCH 2/3] list: test: Add a test for hlist_cut_number()
Date: Wed, 4 Sep 2024 21:41:51 +0800	[thread overview]
Message-ID: <20240904134152.2141-3-thunder.leizhen@huawei.com> (raw)
In-Reply-To: <20240904134152.2141-1-thunder.leizhen@huawei.com>

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(+)

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


  parent reply	other threads:[~2024-09-04 13:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-04 13:41 [PATCH 0/3] debugobjects: Add hlist_cut_number() and use it to optimize code Zhen Lei
2024-09-04 13:41 ` [PATCH 1/3] list: add hlist_cut_number() Zhen Lei
2024-09-04 13:41 ` Zhen Lei [this message]
2024-09-07  6:49   ` [PATCH 2/3] list: test: Add a test for hlist_cut_number() David Gow
2024-09-04 13:41 ` [PATCH 3/3] debugobjects: Use hlist_cut_number() to optimize performance and improve readability Zhen Lei
2024-09-09 18:41   ` Thomas Gleixner
2024-09-10  4:00     ` Leizhen (ThunderTown)
2024-09-10 11:44       ` Thomas Gleixner
2024-09-11  7:44         ` Leizhen (ThunderTown)
2024-09-11  8:54           ` Thomas Gleixner
2024-09-11  9:38             ` Leizhen (ThunderTown)
2024-10-07 12:22             ` Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240904134152.2141-3-thunder.leizhen@huawei.com \
    --to=thunder.leizhen@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox