diff mbox series

[U-Boot,v3,4/6] test: Add ut_asserteq_mem

Message ID 20180626112854.13285-4-mario.six@gdsys.cc
State Superseded
Delegated to: Anatolij Gustschin
Headers show
Series [U-Boot,v3,1/6] drivers: Add OSD uclass | expand

Commit Message

Mario Six June 26, 2018, 11:28 a.m. UTC
Add a unit test assert-method, which compares two given memory areas for
byte-wise equality.

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

---

v2 -> v3:
New in v3

---
 include/test/ut.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

--
2.11.0

Comments

Simon Glass June 26, 2018, 11:18 p.m. UTC | #1
On 26 June 2018 at 04:28, Mario Six <mario.six@gdsys.cc> wrote:
> Add a unit test assert-method, which compares two given memory areas for
> byte-wise equality.
>
> Signed-off-by: Mario Six <mario.six@gdsys.cc>
>
> ---
>
> v2 -> v3:
> New in v3
>
> ---
>  include/test/ut.h | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>
diff mbox series

Patch

diff --git a/include/test/ut.h b/include/test/ut.h
index 59b23a25a4..11d1b2d089 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -78,6 +78,24 @@  void ut_failf(struct unit_test_state *uts, const char *fname, int line,
 	}								\
 }

+/* Assert that two memory areas are equal */
+#define ut_asserteq_mem(expr1, expr2, len) {				\
+	const u8 *val1 = (u8 *)(expr1), *val2 = (u8 *)(expr2);		\
+	const uint __len = len;						\
+									\
+	if (memcmp(val1, val2, __len)) {				\
+		char __buf1[64 + 1] = "\0";				\
+		char __buf2[64 + 1] = "\0";				\
+		bin2hex(__buf1, val1, min(__len, (uint)32));		\
+		bin2hex(__buf2, val2, min(__len, (uint)32));		\
+		ut_failf(uts, __FILE__, __LINE__, __func__,		\
+			 #expr1 " = " #expr2,				\
+			 "Expected \"%s\", got \"%s\"",			\
+			 __buf1, __buf2);				\
+		return CMD_RET_FAILURE;					\
+	}								\
+}
+
 /* Assert that two pointers are equal */
 #define ut_asserteq_ptr(expr1, expr2) {					\
 	const void *val1 = (expr1), *val2 = (expr2);			\