summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <[email protected]>2013-01-14 00:41:23 +0700
committerhathach <[email protected]>2013-01-14 00:41:23 +0700
commit3d8babcb147022a6bced9b61455b301e3ea95150 (patch)
treed1b55e5ed49d81d6220d1b77219f047b9bcd23fb
parentf9914751833deb1b135568b7c09271261f356a06 (diff)
refractor to remove duplicate code between ASSERT_HEX and ASSERT_INT
use static inline min_of and max_of instead of macro
-rw-r--r--tests/test/test_assertion.c19
-rw-r--r--tinyusb/common/assertion.h37
-rw-r--r--tinyusb/common/common.h12
3 files changed, 43 insertions, 25 deletions
diff --git a/tests/test/test_assertion.c b/tests/test/test_assertion.c
index 7f2394093..a289f2bdf 100644
--- a/tests/test/test_assertion.c
+++ b/tests/test/test_assertion.c
@@ -145,6 +145,25 @@ void test_assert_hex_within_greater(void)
TEST_FAIL();
}
+//--------------------------------------------------------------------+
+// HEX
+//--------------------------------------------------------------------+
+void test_assert_bin_equal(void)
+{
+// ASSERT_HEX (0xffee, 0xffee, (void) 0);
+// ASSERT_HEX_EQUAL (0xffee, 0xffee, (void) 0);
+//
+// uint32_t x = 0xf0f0;
+// uint32_t y = 0xf0f0;
+// ASSERT_HEX (x++, y++, (void) 0); // test side effect
+// TEST_ASSERT_EQUAL(0xf0f1, x);
+// TEST_ASSERT_EQUAL(0xf0f1, y);
+//
+// ASSERT_HEX(0x1234, 0x4321, (void) 0);
+
+ TEST_IGNORE();
+}
+
diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h
index 8fc19289a..a1aa968e9 100644
--- a/tinyusb/common/assertion.h
+++ b/tinyusb/common/assertion.h
@@ -70,7 +70,6 @@ extern "C"
//--------------------------------------------------------------------+
#define ASSERT_FILENAME __BASE_FILE__
#define ASSERT_FUNCTION __PRETTY_FUNCTION__
-//#define ASSERT_STATEMENT _PRINTF("Assert at %s line %d: %s %s\n", ASSERT_FILENAME, ASSERT_FUNCTION, __LINE__);
#define ASSERT_STATEMENT _PRINTF("assert at %s: %s :%d :\n", ASSERT_FILENAME, ASSERT_FUNCTION, __LINE__)
#define ASSERT_DEFINE(setup_statement, condition, error, format, ...) \
do{\
@@ -98,45 +97,37 @@ extern "C"
//--------------------------------------------------------------------+
// Logical Assert
//--------------------------------------------------------------------+
-#define ASSERT(...) ASSERT_TRUE(__VA_ARGS__)
-#define ASSERT_TRUE(condition , error ) ASSERT_DEFINE( ,(condition), error, "%s", "evaluated to false")
-#define ASSERT_FALSE(condition , error ) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true")
+#define ASSERT(...) ASSERT_TRUE(__VA_ARGS__)
+#define ASSERT_TRUE(condition , error) ASSERT_DEFINE( ,(condition), error, "%s", "evaluated to false")
+#define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true")
//--------------------------------------------------------------------+
// Integer Assert
//--------------------------------------------------------------------+
-#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__)
-#define ASSERT_INT_EQUAL(expected, actual, error) \
+#define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \
ASSERT_DEFINE(\
uint32_t exp = (expected); uint32_t act = (actual),\
exp==act,\
error,\
- "expected %d, actual %d", exp, act)
+ "expected " type_format ", actual " type_format, exp, act)
-#define ASSERT_INT_WITHIN(lower, upper, actual, error) \
+#define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \
ASSERT_DEFINE(\
uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
(low <= act) && (act <= up),\
error,\
- "expected within %d-%d, actual %d", low, up, act)
+ "expected within " type_format " - " type_format ", actual " type_format, low, up, act)
+
+#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__)
+#define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__)
+#define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__)
//--------------------------------------------------------------------+
// Hex Assert
//--------------------------------------------------------------------+
-#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__)
-#define ASSERT_HEX_EQUAL(expected, actual, error) \
- ASSERT_DEFINE(\
- uint32_t exp = (expected); uint32_t act = (actual),\
- exp==act,\
- error,\
- "expected 0x%x, actual 0x%x", exp, act)
-
-#define ASSERT_HEX_WITHIN(lower, upper, actual, error) \
- ASSERT_DEFINE(\
- uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
- (low <= act) && (act <= up),\
- error,\
- "expected within 0x%x-0x%x, actual 0x%x", low, up, act)
+#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__)
+#define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__)
+#define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__)
#ifdef __cplusplus
}
diff --git a/tinyusb/common/common.h b/tinyusb/common/common.h
index 921de01bc..7d003d57b 100644
--- a/tinyusb/common/common.h
+++ b/tinyusb/common/common.h
@@ -73,10 +73,18 @@
#include "core/std_descriptors.h"
/// min value
-#define MIN_(x, y) (((x) < (y)) ? (x) : (y))
+static inline uint32_t min_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE;
+static inline uint32_t min_of(uint32_t x, uint32_t y)
+{
+ return (x < y) ? x : y;
+}
/// max value
-#define MAX_(x, y) (((x) > (y)) ? (x) : (y))
+static inline uint32_t max_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE;
+static inline uint32_t max_of(uint32_t x, uint32_t y)
+{
+ return (x > y) ? x : y;
+}
#ifdef __cplusplus
}