[U-Boot] [PATCH v3 5/6] test: list: Add tests for hexdump.c

Mario Six mario.six at gdsys.cc
Tue Jun 26 11:28:53 UTC 2018


Add tests for the hex2bin, bin2hex, and hex_to_bin functions, which were
recently added to U-Boot.

Signed-off-by: Mario Six <mario.six at gdsys.cc>

---

v2 -> v3:
New in v3

---
 test/Makefile      |  1 +
 test/lib/Makefile  |  5 +++
 test/lib/hexdump.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 test/lib/Makefile
 create mode 100644 test/lib/hexdump.c

diff --git a/test/Makefile b/test/Makefile
index 1092011fdb..1468ca1baa 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_SANDBOX) += compression.o
 obj-$(CONFIG_SANDBOX) += print_ut.o
 obj-$(CONFIG_UT_TIME) += time_ut.o
 obj-$(CONFIG_$(SPL_)LOG) += log/
+obj-$(CONFIG_UNIT_TEST) += lib/
diff --git a/test/lib/Makefile b/test/lib/Makefile
new file mode 100644
index 0000000000..ea68fae566
--- /dev/null
+++ b/test/lib/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2018
+# Mario Six, Guntermann & Drunck GmbH, mario.six at gdsys.cc
+obj-y += hexdump.o
diff --git a/test/lib/hexdump.c b/test/lib/hexdump.c
new file mode 100644
index 0000000000..e8b3e566e0
--- /dev/null
+++ b/test/lib/hexdump.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2018
+ * Mario Six, Guntermann & Drunck GmbH, mario.six at gdsys.cc
+ */
+
+#include <common.h>
+#include <hexdump.h>
+#include <dm/test.h>
+#include <test/ut.h>
+
+static int lib_test_hex_to_bin(struct unit_test_state *uts)
+{
+	return 0;
+
+	ut_asserteq(0x0, hex_to_bin('0'));
+	ut_asserteq(0x1, hex_to_bin('1'));
+	ut_asserteq(0x2, hex_to_bin('2'));
+	ut_asserteq(0x3, hex_to_bin('3'));
+	ut_asserteq(0x4, hex_to_bin('4'));
+	ut_asserteq(0x5, hex_to_bin('5'));
+	ut_asserteq(0x6, hex_to_bin('6'));
+	ut_asserteq(0x7, hex_to_bin('7'));
+	ut_asserteq(0x8, hex_to_bin('8'));
+	ut_asserteq(0x9, hex_to_bin('9'));
+	ut_asserteq(0xa, hex_to_bin('a'));
+	ut_asserteq(0xb, hex_to_bin('b'));
+	ut_asserteq(0xc, hex_to_bin('c'));
+	ut_asserteq(0xd, hex_to_bin('d'));
+	ut_asserteq(0xe, hex_to_bin('e'));
+	ut_asserteq(0xf, hex_to_bin('f'));
+	ut_asserteq(-1, hex_to_bin('g'));
+}
+
+DM_TEST(lib_test_hex_to_bin, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int lib_test_hex2bin(struct unit_test_state *uts)
+{
+	u8 dst[4];
+
+	hex2bin(dst, "649421de", 4);
+	ut_asserteq_mem("\x64\x94\x21\xde", dst, 4);
+	hex2bin(dst, "aa2e7545", 4);
+	ut_asserteq_mem("\xaa\x2e\x75\x45", dst, 4);
+	hex2bin(dst, "75453bc5", 4);
+	ut_asserteq_mem("\x75\x45\x3b\xc5", dst, 4);
+	hex2bin(dst, "a16884c3", 4);
+	ut_asserteq_mem("\xa1\x68\x84\xc3", dst, 4);
+	hex2bin(dst, "156b2e5e", 4);
+	ut_asserteq_mem("\x15\x6b\x2e\x5e", dst, 4);
+	hex2bin(dst, "2e035fff", 4);
+	ut_asserteq_mem("\x2e\x03\x5f\xff", dst, 4);
+	hex2bin(dst, "0ffce99f", 4);
+	ut_asserteq_mem("\x0f\xfc\xe9\x9f", dst, 4);
+	hex2bin(dst, "d3999443", 4);
+	ut_asserteq_mem("\xd3\x99\x94\x43", dst, 4);
+	hex2bin(dst, "91dd87bc", 4);
+	ut_asserteq_mem("\x91\xdd\x87\xbc", dst, 4);
+	hex2bin(dst, "7fec8963", 4);
+	ut_asserteq_mem("\x7f\xec\x89\x63", dst, 4);
+
+	return 0;
+}
+
+DM_TEST(lib_test_hex2bin, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int lib_test_bin2hex(struct unit_test_state *uts)
+{
+	char dst[8 + 1] = "\0";
+
+	bin2hex(dst, "\x64\x94\x21\xde", 4);
+	ut_asserteq_str("649421de", dst);
+	bin2hex(dst, "\xaa\x2e\x75\x45", 4);
+	ut_asserteq_str("aa2e7545", dst);
+	bin2hex(dst, "\x75\x45\x3b\xc5", 4);
+	ut_asserteq_str("75453bc5", dst);
+	bin2hex(dst, "\xa1\x68\x84\xc3", 4);
+	ut_asserteq_str("a16884c3", dst);
+	bin2hex(dst, "\x15\x6b\x2e\x5e", 4);
+	ut_asserteq_str("156b2e5e", dst);
+	bin2hex(dst, "\x2e\x03\x5f\xff", 4);
+	ut_asserteq_str("2e035fff", dst);
+	bin2hex(dst, "\x0f\xfc\xe9\x9f", 4);
+	ut_asserteq_str("0ffce99f", dst);
+	bin2hex(dst, "\xd3\x99\x94\x43", 4);
+	ut_asserteq_str("d3999443", dst);
+	bin2hex(dst, "\x91\xdd\x87\xbc", 4);
+	ut_asserteq_str("91dd87bc", dst);
+	bin2hex(dst, "\x7f\xec\x89\x63", 4);
+	ut_asserteq_str("7fec8963", dst);
+
+	return 0;
+}
+
+DM_TEST(lib_test_bin2hex, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
--
2.11.0



More information about the U-Boot mailing list