From cc909efed33165be83732e2f6a502459663914c1 Mon Sep 17 00:00:00 2001 From: balaksh Date: Mon, 10 Apr 2017 15:08:01 +1200 Subject: [PATCH] Implement optional printing of execution time for each test --- examples/unity_config.h | 8 +++++ extras/fixture/src/unity_fixture.c | 4 +++ src/unity.c | 4 ++- src/unity_internals.h | 52 ++++++++++++++++++++++++++++++ test/tests/testunity.c | 8 +++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/examples/unity_config.h b/examples/unity_config.h index da3c2af..d586448 100644 --- a/examples/unity_config.h +++ b/examples/unity_config.h @@ -236,4 +236,12 @@ /* #define UNITY_PTR_ATTRIBUTE __attribute__((far)) */ /* #define UNITY_PTR_ATTRIBUTE near */ +/* Print execution time of each test when executed in verbose mode + * + * Example: + * + * TEST - PASS (10 ms) + */ +/* #define UNITY_INCLUDE_EXEC_TIME */ + #endif /* UNITY_CONFIG_H */ diff --git a/extras/fixture/src/unity_fixture.c b/extras/fixture/src/unity_fixture.c index 3872bd8..36c1fb1 100644 --- a/extras/fixture/src/unity_fixture.c +++ b/extras/fixture/src/unity_fixture.c @@ -93,6 +93,8 @@ void UnityTestRunner(unityfunction* setup, UnityMalloc_StartTest(); UnityPointer_Init(); + UNITY_EXEC_TIME_START(); + if (TEST_PROTECT()) { setup(); @@ -418,6 +420,8 @@ void UnityConcludeFixtureTest(void) if (UnityFixture.Verbose) { UnityPrint(" PASS"); + UNITY_EXEC_TIME_STOP(); + UNITY_PRINT_EXEC_TIME(); UNITY_PRINT_EOL(); } } diff --git a/src/unity.c b/src/unity.c index a493b76..cae7bbd 100644 --- a/src/unity.c +++ b/src/unity.c @@ -370,6 +370,7 @@ void UnityConcludeTest(void) Unity.CurrentTestFailed = 0; Unity.CurrentTestIgnored = 0; + UNITY_EXEC_TIME_RESET(); UNITY_PRINT_EOL(); UNITY_FLUSH_CALL(); } @@ -958,7 +959,7 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta, if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) { if (actual > expected) - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); + Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); else Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); } @@ -1351,6 +1352,7 @@ void UnityBegin(const char* filename) Unity.TestIgnores = 0; Unity.CurrentTestFailed = 0; Unity.CurrentTestIgnored = 0; + UNITY_EXEC_TIME_RESET(); UNITY_CLR_DETAILS(); UNITY_OUTPUT_START(); diff --git a/src/unity_internals.h b/src/unity_internals.h index 1b57cd0..e161627 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -32,6 +32,10 @@ #include #endif +#ifndef UNITY_EXCLUDE_TIME_H +#include +#endif + /*------------------------------------------------------- * Guess Widths If Not Specified *-------------------------------------------------------*/ @@ -285,6 +289,44 @@ extern void UNITY_OMIT_OUTPUT_FLUSH_HEADER_DECLARATION; #define UNITY_OUTPUT_COMPLETE() #endif +#ifndef UNITY_EXEC_TIME_RESET +#ifdef UNITY_INCLUDE_EXEC_TIME +#define UNITY_EXEC_TIME_RESET()\ + Unity.CurrentTestStartTime = 0;\ + Unity.CurrentTestStopTime = 0; +#else +#define UNITY_EXEC_TIME_RESET() +#endif +#endif + +#ifndef UNITY_EXEC_TIME_START +#ifdef UNITY_INCLUDE_EXEC_TIME +#define UNITY_EXEC_TIME_START() Unity.CurrentTestStartTime = UNITY_CLOCK_MS(); +#else +#define UNITY_EXEC_TIME_START() +#endif +#endif + +#ifndef UNITY_EXEC_TIME_START +#ifdef UNITY_INCLUDE_EXEC_TIME +#define UNITY_EXEC_TIME_STOP() Unity.CurrentTestStopTime = UNITY_CLOCK_MS(); +#else +#define UNITY_EXEC_TIME_STOP() +#endif +#endif + +#ifndef UNITY_PRINT_EXEC_TIME +#ifdef UNITY_INCLUDE_EXEC_TIME +#define UNITY_PRINT_EXEC_TIME() \ + UnityPrint(" (");\ + UNITY_COUNTER_TYPE execTimeMs = (Unity.CurrentTestStopTime - Unity.CurrentTestStartTime); + UnityPrintNumberUnsigned(execTimeMs);\ + UnityPrint(" ms)"); +#else +#define UNITY_PRINT_EXEC_TIME() +#endif +#endif + /*------------------------------------------------------- * Footprint *-------------------------------------------------------*/ @@ -387,6 +429,10 @@ struct UNITY_STORAGE_T UNITY_COUNTER_TYPE TestIgnores; UNITY_COUNTER_TYPE CurrentTestFailed; UNITY_COUNTER_TYPE CurrentTestIgnored; +#ifdef UNITY_INCLUDE_EXEC_TIME + UNITY_COUNTER_TYPE CurrentTestStartTime; + UNITY_COUNTER_TYPE CurrentTestStopTime; +#endif #ifndef UNITY_EXCLUDE_SETJMP_H jmp_buf AbortFrame; #endif @@ -590,6 +636,12 @@ extern const char UnityStrErr64[]; #define TEST_ABORT() return #endif +#ifndef UNITY_EXCLUDE_TIME_H +#define UNITY_CLOCK_MS() (UNITY_COUNTER_TYPE)((clock() * 1000) / CLOCKS_PER_SEC) +#else +#define UNITY_CLOCK_MS() +#endif + /* This tricky series of macros gives us an optional line argument to treat it as RUN_TEST(func, num=__LINE__) */ #ifndef RUN_TEST #ifdef __STDC_VERSION__ diff --git a/test/tests/testunity.c b/test/tests/testunity.c index af06647..88fb284 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -99,6 +99,10 @@ void testUnitySizeInitializationReminder(void) UNITY_COUNTER_TYPE TestIgnores; UNITY_COUNTER_TYPE CurrentTestFailed; UNITY_COUNTER_TYPE CurrentTestIgnored; +#ifdef UNITY_INCLUDE_EXEC_TIME + UNITY_COUNTER_TYPE CurrentTestStartTime; + UNITY_COUNTER_TYPE CurrentTestStopTime; +#endif #ifndef UNITY_EXCLUDE_SETJMP_H jmp_buf AbortFrame; #endif @@ -115,6 +119,10 @@ void testUnitySizeInitializationReminder(void) UNITY_COUNTER_TYPE TestIgnores; UNITY_COUNTER_TYPE CurrentTestFailed; UNITY_COUNTER_TYPE CurrentTestIgnored; +#ifdef UNITY_INCLUDE_EXEC_TIME + UNITY_COUNTER_TYPE CurrentTestStartTime; + UNITY_COUNTER_TYPE CurrentTestStopTime; +#endif #ifndef UNITY_EXCLUDE_SETJMP_H jmp_buf AbortFrame; #endif