[MERGED] libosmocore[master]: Add unit tests for bcd2char and char2bcd conversion

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Oct 27 15:26:52 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: Add unit tests for bcd2char and char2bcd conversion
......................................................................


Add unit tests for bcd2char and char2bcd conversion

Sounds stupid, but we actually didn't support hex nibbles in one of
the two directions of the conversion, so let's make sure we test for
this.

Change-Id: I8445da54cc4f9b1cd64f286c2b238f4f7c87accb
---
M tests/utils/utils_test.c
M tests/utils/utils_test.ok
2 files changed, 61 insertions(+), 0 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c
index 4a4b121..e6d7ae8 100644
--- a/tests/utils/utils_test.c
+++ b/tests/utils/utils_test.c
@@ -26,6 +26,7 @@
 #include <osmocom/core/utils.h>
 
 #include <stdio.h>
+#include <ctype.h>
 
 static void hexdump_test(void)
 {
@@ -281,6 +282,47 @@
 	return pass;
 }
 
+struct bcdcheck {
+	uint8_t bcd;
+	char ch;
+};
+
+static const struct bcdcheck bcdchecks[]  = {
+	{ 0, '0' },
+	{ 1, '1' },
+	{ 2, '2' },
+	{ 3, '3' },
+	{ 4, '4' },
+	{ 5, '5' },
+	{ 6, '6' },
+	{ 7, '7' },
+	{ 8, '8' },
+	{ 9, '9' },
+	{ 0xA, 'A' },
+	{ 0xB, 'B' },
+	{ 0xC, 'C' },
+	{ 0xD, 'D' },
+	{ 0xE, 'E' },
+	{ 0xF, 'F' },
+};
+
+static void bcd_test(void)
+{
+	int i;
+
+	printf("\nTesting BCD conversion\n");
+	for (i = 0; i < ARRAY_SIZE(bcdchecks); i++) {
+		const struct bcdcheck *check = &bcdchecks[i];
+		char ch = osmo_bcd2char(check->bcd);
+		printf("\tval=0x%x, expected=%c, found=%c\n", check->bcd, check->ch, ch);
+		OSMO_ASSERT(osmo_bcd2char(check->bcd) == check->ch);
+		/* test char -> bcd back-coversion */
+		OSMO_ASSERT(osmo_char2bcd(ch) == check->bcd);
+		/* test for lowercase hex char */
+		OSMO_ASSERT(osmo_char2bcd(tolower(ch)) == check->bcd);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	static const struct log_info log_info = {};
@@ -290,5 +332,6 @@
 	hexparse_test();
 	test_idtag_parsing();
 	test_is_hexstr();
+	bcd_test();
 	return 0;
 }
diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok
index 45156f7..33a185b 100644
--- a/tests/utils/utils_test.ok
+++ b/tests/utils/utils_test.ok
@@ -57,3 +57,21 @@
 26: pass str='BeadedBeeAced1EbbedDefacedFacade' min=32 max=32 even=1 expect=valid
 27: pass str='C01ffedC1cadaeAc1d1f1edAcac1aB0a' min=32 max=32 even=0 expect=valid
 28: pass str='DeafBeddedBabeAcceededFadedDecaff' min=32 max=32 even=0 expect=invalid
+
+Testing BCD conversion
+	val=0x0, expected=0, found=0
+	val=0x1, expected=1, found=1
+	val=0x2, expected=2, found=2
+	val=0x3, expected=3, found=3
+	val=0x4, expected=4, found=4
+	val=0x5, expected=5, found=5
+	val=0x6, expected=6, found=6
+	val=0x7, expected=7, found=7
+	val=0x8, expected=8, found=8
+	val=0x9, expected=9, found=9
+	val=0xa, expected=A, found=A
+	val=0xb, expected=B, found=B
+	val=0xc, expected=C, found=C
+	val=0xd, expected=D, found=D
+	val=0xe, expected=E, found=E
+	val=0xf, expected=F, found=F

-- 
To view, visit https://gerrit.osmocom.org/4448
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8445da54cc4f9b1cd64f286c2b238f4f7c87accb
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list