fs: fat: fix reading non-cluster-aligned root directory
[oweals/u-boot.git] / include / test / ut.h
index da7c1a9d265b4820582315ebbe04eddfb1172c6b..19bcb8c3748d7002abf10adecfb966c350bebdc4 100644 (file)
@@ -1,9 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
 /*
  * Simple unit test library
  *
  * Copyright (c) 2013 Google, Inc
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #ifndef __TEST_UT_H
@@ -17,9 +16,9 @@ struct unit_test_state;
  * ut_fail() - Record failure of a unit test
  *
  * @uts: Test state
- * @fname: Filename where the error occured
- * @line: Line number where the error occured
- * @func: Function name where the error occured
+ * @fname: Filename where the error occurred
+ * @line: Line number where the error occurred
+ * @func: Function name where the error occurred
  * @cond: The condition that failed
  */
 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
@@ -29,9 +28,9 @@ void ut_fail(struct unit_test_state *uts, const char *fname, int line,
  * ut_failf() - Record failure of a unit test
  *
  * @uts: Test state
- * @fname: Filename where the error occured
- * @line: Line number where the error occured
- * @func: Function name where the error occured
+ * @fname: Filename where the error occurred
+ * @line: Line number where the error occurred
+ * @func: Function name where the error occurred
  * @cond: The condition that failed
  * @fmt: printf() format string for the error, followed by args
  */
@@ -79,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);                    \
@@ -91,6 +108,18 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
        }                                                               \
 }
 
+/* Assert that a pointer is NULL */
+#define ut_assertnull(expr) {                                  \
+       const void *val = (expr);                                       \
+                                                                       \
+       if (val != NULL) {                                              \
+               ut_failf(uts, __FILE__, __LINE__, __func__,             \
+                        #expr " != NULL",                              \
+                        "Expected NULL, got %p", val);         \
+               return CMD_RET_FAILURE;                                 \
+       }                                                               \
+}
+
 /* Assert that a pointer is not NULL */
 #define ut_assertnonnull(expr) {                                       \
        const void *val = (expr);                                       \
@@ -104,7 +133,7 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
 }
 
 /* Assert that a pointer is not an error pointer */
-#define ut_assertok_ptr(expr) {                                        \
+#define ut_assertok_ptr(expr) {                                                \
        const void *val = (expr);                                       \
                                                                        \
        if (IS_ERR(val)) {                                              \