From 08810d605b3ab286de89c27932c3a0c857ef6739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Sat, 8 Nov 2014 01:54:59 +0100 Subject: [PATCH] embunit: fix macros --- tests/unittests/embunit/embUnit/AssertImpl.c | 20 ++++--- tests/unittests/embunit/embUnit/AssertImpl.h | 60 +++++++++++++++----- tests/unittests/embunit/embUnit/stdImpl.c | 26 ++++++--- tests/unittests/embunit/embUnit/stdImpl.h | 7 ++- 4 files changed, 82 insertions(+), 31 deletions(-) diff --git a/tests/unittests/embunit/embUnit/AssertImpl.c b/tests/unittests/embunit/embUnit/AssertImpl.c index 4cf37bd20c..24dcf010b9 100644 --- a/tests/unittests/embunit/embUnit/AssertImpl.c +++ b/tests/unittests/embunit/embUnit/AssertImpl.c @@ -36,20 +36,26 @@ #include "stdImpl.h" #include "AssertImpl.h" -void assertImplementationInt(int expected,int actual, long line, const char *file) +#include + +/* This is the maximal length of 2^n in decimal notation. */ +/* http://www.wolframalpha.com/input/?i=%28%28n-2%29%2F3%2B1%29%2F%28log_10%282^n%29%29%3E%3D1 */ +#define MAX_LL_LEN ((CHAR_BIT*sizeof(long long) - 2)/3 + 1) + +void assertImplementationLongLong(long long expected,long long actual, long line, const char *file) { - char buffer[32]; /*"exp -2147483647 was -2147483647"*/ - char numbuf[12]; /*32bit int decimal maximum column is 11 (-2147483647~2147483647)*/ + char numbuf[MAX_LL_LEN + 2]; /* one (signed) decimal number + sign + null */ + char buffer[2*(sizeof(numbuf) - 1) + 10]; /* "exp " + decimal + " was " + decimal + null */ stdimpl_strcpy(buffer, "exp "); - { stdimpl_itoa(expected, numbuf, 10); - stdimpl_strncat(buffer, numbuf, 11); } + stdimpl_lltoa(expected, numbuf, 10); + stdimpl_strncat(buffer, numbuf, sizeof(numbuf)); stdimpl_strcat(buffer, " was "); - { stdimpl_itoa(actual, numbuf, 10); - stdimpl_strncat(buffer, numbuf, 11); } + stdimpl_lltoa(actual, numbuf, 10); + stdimpl_strncat(buffer, numbuf, sizeof(numbuf)); addFailure(buffer, line, file); } diff --git a/tests/unittests/embunit/embUnit/AssertImpl.h b/tests/unittests/embunit/embUnit/AssertImpl.h index 800a116ac4..2694db82cd 100644 --- a/tests/unittests/embunit/embUnit/AssertImpl.h +++ b/tests/unittests/embunit/embUnit/AssertImpl.h @@ -41,29 +41,59 @@ extern "C" { void addFailure(const char *msg, long line, const char *file); /*TestCase.c*/ -void assertImplementationInt(int expected,int actual, long line, const char *file); +void assertImplementationLongLong(long long expected,long long actual, long line, const char *file); void assertImplementationCStr(const char *expected,const char *actual, long line, const char *file); -#define TEST_ASSERT_EQUAL_STRING(expected,actual)\ - if (expected && actual && (stdimpl_strcmp(expected,actual)==0)) {} else {assertImplementationCStr(expected,actual,__LINE__,__FILE__);return;} +#define TEST_ASSERT_EQUAL_STRING(expected_, actual_) \ + do { \ + __typeof__(expected_) ____expected__ = expected_; \ + __typeof__(actual_) ____actual__ = actual_; \ + if (stdimpl_strcmp(____expected__, ____actual__) != 0) { \ + assertImplementationCStr(____expected__, ____actual__, __LINE__, __FILE__); \ + return; \ + } \ + } while (0) -#define TEST_ASSERT_EQUAL_INT(expected,actual)\ - if (expected == actual) {} else {assertImplementationInt(expected,actual,__LINE__,__FILE__);return;} +#define TEST_ASSERT_EQUAL_INT(expected_, actual_) \ + do { \ + long long ____expected__ = (long long) (expected_); \ + long long ____actual__ = (long long) (actual_); \ + if (____expected__ != ____actual__) { \ + assertImplementationLongLong(____expected__, ____actual__, __LINE__, __FILE__); \ + return; \ + } \ + } while (0) -#define TEST_ASSERT_NULL(pointer)\ - TEST_ASSERT_MESSAGE(pointer == NULL,#pointer " was not null.") +#define TEST_ASSERT_NULL(pointer_) \ + do { \ + __typeof__(pointer_) ____pointer__ = (pointer_); \ + TEST_ASSERT_MESSAGE(____pointer__ == NULL, #pointer_ " was not null."); \ + } while (0) -#define TEST_ASSERT_NOT_NULL(pointer)\ - TEST_ASSERT_MESSAGE(pointer != NULL,#pointer " was null.") +#define TEST_ASSERT_NOT_NULL(pointer_) \ + do { \ + __typeof__(pointer_) ____pointer__ = (pointer_); \ + TEST_ASSERT_MESSAGE(____pointer__ != NULL, #pointer_ " was null."); \ + } while (0) -#define TEST_ASSERT_MESSAGE(condition, message)\ - if (condition) {} else {TEST_FAIL(message);} +#define TEST_ASSERT_MESSAGE(condition_, message) \ + do { \ + __typeof__(condition_) ____condition__ = (condition_); \ + if (!____condition__) { \ + TEST_FAIL((message)); \ + } \ + } while (0) -#define TEST_ASSERT(condition)\ - if (condition) {} else {TEST_FAIL(#condition);} +#define TEST_ASSERT(condition) \ + do { \ + TEST_ASSERT_MESSAGE((condition), #condition); \ + } while (0) -#define TEST_FAIL(message)\ - if (0) {} else {addFailure(message,__LINE__,__FILE__);return;} +#define TEST_FAIL(message) \ + do { \ + addFailure((message), __LINE__, __FILE__); \ + return; \ + } while (0) #ifdef __cplusplus } diff --git a/tests/unittests/embunit/embUnit/stdImpl.c b/tests/unittests/embunit/embUnit/stdImpl.c index f6ee5ddce0..5068be8a36 100644 --- a/tests/unittests/embunit/embUnit/stdImpl.c +++ b/tests/unittests/embunit/embUnit/stdImpl.c @@ -100,15 +100,25 @@ int stdimpl_strlen(const char *str) int stdimpl_strcmp(const char *s1, const char *s2) { - char c1,c2; - do { - c1 = *s1++; - c2 = *s2++; - } while ((c1) && (c2) && (c1==c2)); - return c1 - c2; + if (s1 == s2) { + return 0; + } + else if (s1 && !s2) { + return +1; + } + else if (!s1 && s2) { + return -1; + } else { + char c1,c2; + do { + c1 = *s1++; + c2 = *s2++; + } while (c1 && c2 && (c1==c2)); + return c1 - c2; + } } -static char* _xtoa(unsigned long v,char *string, int r, int is_neg) +static char* _xtoa(unsigned long long v,char *string, int r, int is_neg) { char *start = string; char buf[33],*p; @@ -132,7 +142,7 @@ static char* _xtoa(unsigned long v,char *string, int r, int is_neg) return start; } -char* stdimpl_itoa(int v,char *string,int r) +char* stdimpl_lltoa(long long v,char *string,int r) { if ((r == 10) && (v < 0)) { return _xtoa((unsigned long)(-v), string, r, 1); diff --git a/tests/unittests/embunit/embUnit/stdImpl.h b/tests/unittests/embunit/embUnit/stdImpl.h index fde9301a62..5cc03d9775 100644 --- a/tests/unittests/embunit/embUnit/stdImpl.h +++ b/tests/unittests/embunit/embUnit/stdImpl.h @@ -48,7 +48,12 @@ char* stdimpl_strcat(char *dst, const char *src); char* stdimpl_strncat(char *dst, const char *src,unsigned int count); int stdimpl_strlen(const char *str); int stdimpl_strcmp(const char *s1, const char *s2); -char* stdimpl_itoa(int v,char *string,int r); +char* stdimpl_lltoa(long long v,char *string,int r); + +static inline char* stdimpl_itoa(int v,char *string,int r) +{ + return stdimpl_lltoa(v,string,r); +} #ifdef __cplusplus }