提交 edfba379 编写于 作者: M Mark VanderVoord

Merge pull request #151 from jsalling/bugfix/add-tests-for-printing-numbers

Add tests for printing numbers, interface for spying on output strings (Thanks! I like this idea!)
...@@ -28,7 +28,7 @@ compiler: ...@@ -28,7 +28,7 @@ compiler:
- '-Wstrict-overflow=5' - '-Wstrict-overflow=5'
- '-Wuninitialized' - '-Wuninitialized'
- '-Wunused' - '-Wunused'
- '-Wunreachable-code' # - '-Wunreachable-code'
- '-Wreturn-type' - '-Wreturn-type'
- '-Wshadow' - '-Wshadow'
- '-Wundef' - '-Wundef'
...@@ -57,6 +57,7 @@ compiler: ...@@ -57,6 +57,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_INCLUDE_DOUBLE - UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_64 - UNITY_SUPPORT_64
- UNITY_OUTPUT_RESULTS_FILE - UNITY_OUTPUT_RESULTS_FILE
...@@ -65,7 +66,7 @@ compiler: ...@@ -65,7 +66,7 @@ compiler:
extension: '.o' extension: '.o'
destination: *build_path destination: *build_path
linker: linker:
path: gcc path: clang
options: options:
- -lm - -lm
- '-m64' - '-m64'
......
...@@ -28,7 +28,7 @@ compiler: ...@@ -28,7 +28,7 @@ compiler:
- '-Wstrict-overflow=5' - '-Wstrict-overflow=5'
- '-Wuninitialized' - '-Wuninitialized'
- '-Wunused' - '-Wunused'
- '-Wunreachable-code' # - '-Wunreachable-code'
- '-Wreturn-type' - '-Wreturn-type'
- '-Wshadow' - '-Wshadow'
- '-Wundef' - '-Wundef'
...@@ -57,6 +57,7 @@ compiler: ...@@ -57,6 +57,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_INCLUDE_DOUBLE - UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES - UNITY_SUPPORT_TEST_CASES
- UNITY_SUPPORT_64 - UNITY_SUPPORT_64
...@@ -65,7 +66,7 @@ compiler: ...@@ -65,7 +66,7 @@ compiler:
extension: '.o' extension: '.o'
destination: *build_path destination: *build_path
linker: linker:
path: gcc path: clang
options: options:
- -lm - -lm
- '-m64' - '-m64'
......
...@@ -19,6 +19,7 @@ compiler: ...@@ -19,6 +19,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_EXCLUDE_STDINT_H - UNITY_EXCLUDE_STDINT_H
- UNITY_EXCLUDE_LIMITS_H - UNITY_EXCLUDE_LIMITS_H
- UNITY_EXCLUDE_SIZEOF - UNITY_EXCLUDE_SIZEOF
......
...@@ -19,6 +19,7 @@ compiler: ...@@ -19,6 +19,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_EXCLUDE_STDINT_H - UNITY_EXCLUDE_STDINT_H
- UNITY_EXCLUDE_LIMITS_H - UNITY_EXCLUDE_LIMITS_H
- UNITY_EXCLUDE_SIZEOF - UNITY_EXCLUDE_SIZEOF
......
...@@ -19,6 +19,7 @@ compiler: ...@@ -19,6 +19,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_EXCLUDE_STDINT_H - UNITY_EXCLUDE_STDINT_H
- UNITY_INCLUDE_DOUBLE - UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES - UNITY_SUPPORT_TEST_CASES
......
...@@ -19,8 +19,9 @@ compiler: ...@@ -19,8 +19,9 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_EXCLUDE_STDINT_H - UNITY_EXCLUDE_STDINT_H
- UNITY_EXCLUDE_LIMTIS_H - UNITY_EXCLUDE_LIMITS_H
- UNITY_INCLUDE_DOUBLE - UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES - UNITY_SUPPORT_TEST_CASES
- UNITY_SUPPORT_64 - UNITY_SUPPORT_64
......
...@@ -32,6 +32,7 @@ compiler: ...@@ -32,6 +32,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_INCLUDE_DOUBLE - UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES - UNITY_SUPPORT_TEST_CASES
- UNITY_SUPPORT_64 - UNITY_SUPPORT_64
......
...@@ -19,6 +19,7 @@ compiler: ...@@ -19,6 +19,7 @@ compiler:
defines: defines:
prefix: '-D' prefix: '-D'
items: items:
- UNITY_OUTPUT_CHAR=putcharSpy
- UNITY_EXCLUDE_MATH_H - UNITY_EXCLUDE_MATH_H
- UNITY_INCLUDE_DOUBLE - UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES - UNITY_SUPPORT_TEST_CASES
......
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
========================================== */ ========================================== */
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h>
#include "unity.h" #include "unity.h"
int putcharSpy(int c) {return putchar(c);} // include passthrough for linking tests
#define TEST_CASE(...) #define TEST_CASE(...)
#define EXPECT_ABORT_BEGIN \ #define EXPECT_ABORT_BEGIN \
......
...@@ -2158,8 +2158,122 @@ void testIgnoredAndThenFailInTearDown(void) ...@@ -2158,8 +2158,122 @@ void testIgnoredAndThenFailInTearDown(void)
TEST_IGNORE(); TEST_IGNORE();
} }
// Tricky series of macros to set USING_OUTPUT_SPY
#define USING_SPY_AS(a) EXPAND_AND_USE_2ND(ASSIGN_VALUE(a), 0)
#define ASSIGN_VALUE(a) VAL_FUNC_##a
#define VAL_FUNC_putcharSpy() 0, 1
#define EXPAND_AND_USE_2ND(a, b) SECOND_PARAM(a, b, throwaway)
#define SECOND_PARAM(a, b, ...) b
#if USING_SPY_AS(UNITY_OUTPUT_CHAR())
#define USING_OUTPUT_SPY // true only if UNITY_OUTPUT_CHAR = putchar_Spy
#endif
#ifdef USING_OUTPUT_SPY
#include <stdio.h>
#define SPY_BUFFER_MAX 40
static char putcharSpyBuffer[SPY_BUFFER_MAX];
#endif
static int indexSpyBuffer;
static int putcharSpyEnabled;
void startPutcharSpy(void) {indexSpyBuffer = 0; putcharSpyEnabled = 1;}
void endPutcharSpy(void) {putcharSpyEnabled = 0;}
char* getBufferPutcharSpy(void)
{
#ifdef USING_OUTPUT_SPY
putcharSpyBuffer[indexSpyBuffer] = '\0';
return putcharSpyBuffer;
#else
return NULL;
#endif
}
int putcharSpy(int c)
{
#ifdef USING_OUTPUT_SPY
if (putcharSpyEnabled)
{
if (indexSpyBuffer < SPY_BUFFER_MAX - 1)
putcharSpyBuffer[indexSpyBuffer++] = (char)c;
} else
c = putchar(c);
#endif
return c;
}
#define TEST_ASSERT_EQUAL_PRINT_NUMBERS(expected, actual) { \
startPutcharSpy(); UnityPrintNumber((actual)); endPutcharSpy(); \
TEST_ASSERT_EQUAL_STRING((expected), getBufferPutcharSpy()); \
}
#define TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS(expected, actual) { \
startPutcharSpy(); UnityPrintNumberUnsigned((actual)); endPutcharSpy(); \
TEST_ASSERT_EQUAL_STRING((expected), getBufferPutcharSpy()); \
}
void testPrintNumbers32(void)
{
#ifndef USING_OUTPUT_SPY
TEST_IGNORE_MESSAGE("Compile with '-D UNITY_OUTPUT_CHAR=putcharSpy' to enable print testing");
#else
TEST_ASSERT_EQUAL_PRINT_NUMBERS("0", 0);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("1", 1);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("-1", -1);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("2000000000", 2000000000);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("-2147483648", (_US32)0x80000000);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("-1", (_US32)0xFFFFFFFF);
#endif
}
void testPrintNumbersUnsigned32(void)
{
#ifndef USING_OUTPUT_SPY
TEST_IGNORE();
#else
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("0", 0);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("1", 1);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("1500000000", 1500000000);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("2147483648", (_UU32)0x80000000);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("4294967295", (_UU32)0xFFFFFFFF);
#endif
}
// ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES 64 BIT SUPPORT ================== // ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES 64 BIT SUPPORT ==================
void testPrintNumbersInt64(void)
{
#ifndef UNITY_SUPPORT_64
TEST_IGNORE();
#else
#ifndef USING_OUTPUT_SPY
TEST_IGNORE();
#else
TEST_ASSERT_EQUAL_PRINT_NUMBERS("0", 0);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("10000000000", 10000000000);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("-9223372036854775808", (_U_SINT)0x8000000000000000);
TEST_ASSERT_EQUAL_PRINT_NUMBERS("-1", (_U_SINT)0xFFFFFFFFFFFFFFFF);
#endif
#endif
}
void testPrintNumbersUInt64(void)
{
#ifndef UNITY_SUPPORT_64
TEST_IGNORE();
#else
#ifndef USING_OUTPUT_SPY
TEST_IGNORE();
#else
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("0", 0);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("70000000000", 70000000000);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("9223372036854775808", (_U_UINT)0x8000000000000000);
TEST_ASSERT_EQUAL_PRINT_UNSIGNED_NUMBERS("18446744073709551615", (_U_UINT)0xFFFFFFFFFFFFFFFF);
#endif
#endif
}
void testEqualHex64s(void) void testEqualHex64s(void)
{ {
#ifndef UNITY_SUPPORT_64 #ifndef UNITY_SUPPORT_64
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册