From beb20b975bb1a1871598bb1014a872fdd4aded6f Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 13 Jan 2013 20:09:51 +0700 Subject: [PATCH] add ASSERT_INT_WITHIN and its test code --- tests/test/test_assertion.c | 12 ++++++++++++ tinyusb/common/assertion.h | 13 +++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test/test_assertion.c b/tests/test/test_assertion.c index 781db9cb..3c7b1615 100644 --- a/tests/test/test_assertion.c +++ b/tests/test/test_assertion.c @@ -87,3 +87,15 @@ void test_assert_int_eqal(void) TEST_FAIL(); } + +void test_assert_int_within(void) +{ + ASSERT_INT_WITHIN (1, 5, 3, (void) 0); + ASSERT_INT_WITHIN (1, 5, 1, (void) 0); + ASSERT_INT_WITHIN (1, 5, 5, (void) 0); + + ASSERT_INT_WITHIN (1, 5, 10, (void) 0); + ASSERT_INT_WITHIN (1, 5, 0, (void) 0); + + TEST_FAIL(); +} diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h index 935b9952..26dad566 100644 --- a/tinyusb/common/assertion.h +++ b/tinyusb/common/assertion.h @@ -109,9 +109,18 @@ extern "C" #define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__) #define ASSERT_INT_EQUAL(expected, actual, error) \ - ASSERT_DEFINE( uint32_t exp = (expected); uint32_t act = (actual), exp==act, error, "expected %d, actual %d", exp, act) + ASSERT_DEFINE(\ + uint32_t exp = (expected); uint32_t act = (actual),\ + exp==act,\ + error,\ + "expected %d, actual %d", exp, act) -#define ASSERT_INT_WITHIN(lower, upper, actual) +#define ASSERT_INT_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 %d-%d, actual %d", low, up, act) #ifdef __cplusplus