From: Denis Kenzior <denkenz@gmail.com>
To: ell@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH v2 4/4] unit: Add tests for the new bitmap utilities
Date: Tue, 16 Apr 2024 09:24:55 -0500 [thread overview]
Message-ID: <20240416142458.3353383-4-denkenz@gmail.com> (raw)
In-Reply-To: <20240416142458.3353383-1-denkenz@gmail.com>
---
unit/test-util.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 81 insertions(+)
diff --git a/unit/test-util.c b/unit/test-util.c
index f7a895f911d5..b178b86cf489 100644
--- a/unit/test-util.c
+++ b/unit/test-util.c
@@ -195,6 +195,81 @@ static void test_safe_atoux(const void *test_data)
assert(l_safe_atox8("0xffff", &c) == -ERANGE);
}
+static void test_set_bit(const void *test_data)
+{
+ uint32_t bitmap[2] = { };
+ int one = 0;
+
+ L_BIT_SET(&bitmap[0], 0);
+ L_BIT_SET(bitmap, 1);
+ L_BIT_SET(bitmap, 2);
+ L_BIT_SET(bitmap, 3);
+
+ assert(bitmap[0] == 0x0f);
+ assert(bitmap[1] == 0);
+
+ L_BIT_SET(bitmap, 63);
+ L_BIT_SET(bitmap, 62);
+ L_BIT_SET(bitmap, 61);
+ L_BIT_SET(bitmap, 60);
+
+ assert(bitmap[0] == 0x0fU);
+ assert(bitmap[1] == 0xf0000000U);
+
+ L_BIT_SET(&one, 0);
+ assert(one == 1);
+}
+
+static void test_clear_bit(const void *test_data)
+{
+ uint32_t bitmap[2] = { 0xfU, 0xf0000000U };
+
+ L_BIT_CLEAR(&bitmap[0], 3);
+ L_BIT_CLEAR(bitmap, 63);
+
+ assert(bitmap[0] == 0x07U);
+ assert(bitmap[1] == 0x70000000U);
+}
+
+static void test_is_bit_set(const void *test_data)
+{
+ uint32_t bitmap[2] = { 0xfU, 0xf0000000U };
+ uint8_t one = 1;
+
+ assert(L_BIT_TEST(&bitmap[0], 0) == true);
+ assert(L_BIT_TEST(bitmap, 1) == true);
+ assert(L_BIT_TEST(bitmap, 2) == true);
+ assert(L_BIT_TEST(bitmap, 3) == true);
+ assert(L_BIT_TEST(bitmap, 4) == false);
+
+ assert(L_BIT_TEST(bitmap, 63) == true);
+ assert(L_BIT_TEST(bitmap, 55) == false);
+
+ assert(L_BIT_TEST(&one, 0) == true);
+ assert(L_BIT_TEST(&one, 1) == false);
+}
+
+static void test_set_bits(const void *test_data)
+{
+ uint16_t bitmap[4] = {};
+
+ L_BITS_SET(bitmap, 0, 1, 16, 32, 48);
+
+ assert(bitmap[0] == 0x3);
+ assert(bitmap[1] == 0x1);
+ assert(bitmap[2] == 0x1);
+ assert(bitmap[3] == 0x1);
+}
+
+static void test_clear_bits(const void *test_data)
+{
+ uint16_t bitmap[4] = { 0x3, 0x1, 0x1, 0x1 };
+
+ L_BITS_CLEAR(bitmap, 0, 1, 16, 32, 48);
+
+ assert(l_memeqzero(bitmap, sizeof(bitmap)));
+}
+
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
@@ -212,5 +287,11 @@ int main(int argc, char *argv[])
l_test_add("l_safe_atoux", test_safe_atoux, NULL);
+ l_test_add("L_BIT_SET", test_set_bit, NULL);
+ l_test_add("L_BIT_CLEAR", test_clear_bit, NULL);
+ l_test_add("L_BIT_TEST", test_is_bit_set, NULL);
+ l_test_add("L_BITS_SET", test_set_bits, NULL);
+ l_test_add("L_BITS_CLEAR", test_clear_bits, NULL);
+
return l_test_run();
}
--
2.44.0
next prev parent reply other threads:[~2024-04-16 14:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-16 14:24 [PATCH v2 1/4] util: Remove confusing static keyword use Denis Kenzior
2024-04-16 14:24 ` [PATCH v2 2/4] util: Add L_BIT_{SET|CLEAR|TEST} macros Denis Kenzior
2024-04-16 14:24 ` [PATCH v2 3/4] util: Add L_BITS_{SET|CLEAR} Denis Kenzior
2024-04-16 14:24 ` Denis Kenzior [this message]
2024-04-16 19:07 ` [PATCH v2 1/4] util: Remove confusing static keyword use Denis Kenzior
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=20240416142458.3353383-4-denkenz@gmail.com \
--to=denkenz@gmail.com \
--cc=ell@lists.linux.dev \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).