test: Add underscore prefix to macro parameters
authorSimon Glass <sjg@chromium.org>
Mon, 27 Jan 2020 15:49:41 +0000 (08:49 -0700)
committerSimon Glass <sjg@chromium.org>
Thu, 6 Feb 2020 02:33:45 +0000 (19:33 -0700)
If a test happens to use the same variable as the macro parameter the
macro does not work as intended. Add an underscore to guard against this.

Signed-off-by: Simon Glass <sjg@chromium.org>
include/test/ut.h

index f616c202f357a4f72d8ca75e4f5045174fb33bd0..c9fc9cc8395bb91b323106357cff884494b130cd 100644 (file)
@@ -56,39 +56,39 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
 
 /* Assert that two int expressions are equal */
 #define ut_asserteq(expr1, expr2) {                                    \
-       unsigned int val1 = (expr1), val2 = (expr2);                    \
+       unsigned int _val1 = (expr1), _val2 = (expr2);                  \
                                                                        \
-       if (val1 != val2) {                                             \
+       if (_val1 != _val2) {                                           \
                ut_failf(uts, __FILE__, __LINE__, __func__,             \
                         #expr1 " == " #expr2,                          \
-                        "Expected %#x (%d), got %#x (%d)", val1, val1, \
-                        val2, val2);                                   \
+                        "Expected %#x (%d), got %#x (%d)",             \
+                        _val1, _val1, _val2, _val2);                   \
                return CMD_RET_FAILURE;                                 \
        }                                                               \
 }
 
 /* Assert that two string expressions are equal */
 #define ut_asserteq_str(expr1, expr2) {                                        \
-       const char *val1 = (expr1), *val2 = (expr2);                    \
+       const char *_val1 = (expr1), *_val2 = (expr2);                  \
                                                                        \
-       if (strcmp(val1, val2)) {                                       \
+       if (strcmp(_val1, _val2)) {                                     \
                ut_failf(uts, __FILE__, __LINE__, __func__,             \
                         #expr1 " = " #expr2,                           \
-                        "Expected \"%s\", got \"%s\"", val1, val2);    \
+                        "Expected \"%s\", got \"%s\"", _val1, _val2);  \
                return CMD_RET_FAILURE;                                 \
        }                                                               \
 }
 
 /* Assert that two memory areas are equal */
 #define ut_asserteq_mem(expr1, expr2, len) {                           \
-       const u8 *val1 = (u8 *)(expr1), *val2 = (u8 *)(expr2);          \
+       const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2);        \
        const uint __len = len;                                         \
                                                                        \
-       if (memcmp(val1, val2, __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));            \
+               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\"",                 \
@@ -99,33 +99,33 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,
 
 /* Assert that two pointers are equal */
 #define ut_asserteq_ptr(expr1, expr2) {                                        \
-       const void *val1 = (expr1), *val2 = (expr2);                    \
+       const void *_val1 = (expr1), *_val2 = (expr2);                  \
                                                                        \
-       if (val1 != val2) {                                             \
+       if (_val1 != _val2) {                                           \
                ut_failf(uts, __FILE__, __LINE__, __func__,             \
                         #expr1 " = " #expr2,                           \
-                        "Expected %p, got %p", val1, val2);            \
+                        "Expected %p, got %p", _val1, _val2);          \
                return CMD_RET_FAILURE;                                 \
        }                                                               \
 }
 
 /* Assert that a pointer is NULL */
 #define ut_assertnull(expr) {                                  \
-       const void *val = (expr);                                       \
+       const void *_val = (expr);                                      \
                                                                        \
-       if (val != NULL) {                                              \
+       if (_val) {                                             \
                ut_failf(uts, __FILE__, __LINE__, __func__,             \
                         #expr " != NULL",                              \
-                        "Expected NULL, got %p", val);         \
+                        "Expected NULL, got %p", _val);                \
                return CMD_RET_FAILURE;                                 \
        }                                                               \
 }
 
 /* Assert that a pointer is not NULL */
 #define ut_assertnonnull(expr) {                                       \
-       const void *val = (expr);                                       \
+       const void *_val = (expr);                                      \
                                                                        \
-       if (val == NULL) {                                              \
+       if (!_val) {                                            \
                ut_failf(uts, __FILE__, __LINE__, __func__,             \
                         #expr " = NULL",                               \
                         "Expected non-null, got NULL");                \
@@ -135,13 +135,13 @@ 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) {                                                \
-       const void *val = (expr);                                       \
+       const void *_val = (expr);                                      \
                                                                        \
-       if (IS_ERR(val)) {                                              \
+       if (IS_ERR(_val)) {                                             \
                ut_failf(uts, __FILE__, __LINE__, __func__,             \
                         #expr " = NULL",                               \
                         "Expected pointer, got error %ld",             \
-                        PTR_ERR(val));                                 \
+                        PTR_ERR(_val));                                \
                return CMD_RET_FAILURE;                                 \
        }                                                               \
 }