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 44a8003..103bde2 100644 --- a/src/unity.c +++ b/src/unity.c @@ -391,6 +391,7 @@ void UnityConcludeTest(void) Unity.CurrentTestFailed = 0; Unity.CurrentTestIgnored = 0; + UNITY_EXEC_TIME_RESET(); UNITY_PRINT_EOL(); UNITY_FLUSH_CALL(); } @@ -974,7 +975,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); } @@ -1356,6 +1357,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 415a29b..351d9fe 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 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; #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_STOP +#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 *-------------------------------------------------------*/ @@ -396,6 +438,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 @@ -594,6 +640,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 95c8280..7ecb459 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -103,6 +103,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 @@ -119,6 +123,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