From 1deac989696fc216acb4dd1208dab8991e886c6c Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 13 Jan 2013 20:00:01 +0700 Subject: [PATCH] add ASSERT_INT, ASSERT_INT_EQUAL and test code for it --- tests/test/test_assertion.c | 41 ++++++++++++++++++++++++++++++------- tinyusb/common/assertion.h | 22 ++++++++------------ 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/tests/test/test_assertion.c b/tests/test/test_assertion.c index cbbc24375..781db9cb1 100644 --- a/tests/test/test_assertion.c +++ b/tests/test/test_assertion.c @@ -35,6 +35,7 @@ * This file is part of the tiny usb stack. */ +#include #include #include #include "unity.h" @@ -48,15 +49,41 @@ void tearDown(void) { } -void test_assert_true(void) +//--------------------------------------------------------------------+ +// Logical +//--------------------------------------------------------------------+ +void test_assert_logical_true(void) { - ASSERT(true, (void)0 ); - ASSERT_TRUE(true, (void)0 ); - ASSERT_TRUE(false, (void)0 ); + ASSERT (true , (void)0 ); + ASSERT_TRUE (true , (void)0 ); + + ASSERT_TRUE (false , (void)0 ); + TEST_FAIL(); } -void test_assert_false(void) +void test_assert_logical_false(void) { - ASSERT_FALSE(false, (void)0 ); - ASSERT_FALSE(true, (void)0 ); + ASSERT_FALSE(false , (void)0 ); + ASSERT_FALSE(true , (void)0 ); + + TEST_FAIL(); +} + +//--------------------------------------------------------------------+ +// Integer +//--------------------------------------------------------------------+ +void test_assert_int_eqal(void) +{ + ASSERT_INT (1, 1, (void) 0); + ASSERT_INT_EQUAL (1, 1, (void) 0); + + uint32_t x = 0; + uint32_t y = 0; + ASSERT_INT (x++, y++, (void) 0); // test side effect + TEST_ASSERT_EQUAL(1, x); + TEST_ASSERT_EQUAL(1, y); + + ASSERT_INT(0, 1, (void) 0); + + TEST_FAIL(); } diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h index 3da58fe5b..935b99522 100644 --- a/tinyusb/common/assertion.h +++ b/tinyusb/common/assertion.h @@ -72,9 +72,10 @@ extern "C" #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(ASSERT_TEST, error, format, __VA_ARGS__) \ +#define ASSERT_DEFINE(setup_statement, condition, error, format, ...) \ do{\ - if (!(ASSERT_TEST)) {\ + setup_statement;\ + if (!(condition)) {\ _PRINTF("Assert at %s: %s:%d: " format "\n", ASSERT_FILENAME, ASSERT_FUNCTION, __LINE__, __VA_ARGS__);\ return error;\ }\ @@ -97,23 +98,18 @@ extern "C" //--------------------------------------------------------------------+ // Logical Assert //--------------------------------------------------------------------+ -#define TEST_TRUE(condition) (condition) -#define TEST_FALSE(condition) (!condition) - -#define ASSERT(condition, error ) ASSERT_TRUE(condition, error) -#define ASSERT_TRUE(condition, error ) ASSERT_DEFINE(TEST_TRUE(condition), error, "%s", "evaluated to false") -#define ASSERT_FALSE(condition, error ) ASSERT_DEFINE(TEST_FALSE(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 TEST_INT_EQUAL -#define ASSERT_INT(expected, actual) ASSERT_INT_EQUAL(expected, actual) -#define ASSERT_INT_EQUAL(expected, actual) \ - uint32 exp = (expected);\ - uint32 act = (actual);\ - ASSERT_DEFINE(TEST_INT_EQUAL(condition), error, "expected %d, actual %d", exp, act) +#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) #define ASSERT_INT_WITHIN(lower, upper, actual)