Skip to content

Commit 276fb97

Browse files
committed
Elapsed time for each is test is displayed
1 parent 752b93e commit 276fb97

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

tests/run_all_tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TESTS_DIR=`dirname $0`
66
# Compiling and running tests with GCC
77
#######################################################################
88
export CC=gcc
9-
export CFLAGS="-Wall -Wextra -std=c99"
9+
export CFLAGS="-Wall -Wextra -std=c99 -DYT__TESTING"
1010

1111
echo "Test 1/2: Running tests with C Compiler ($CC)"
1212
bash $TESTS_DIR/test.sh || exit
@@ -15,7 +15,7 @@ bash $TESTS_DIR/test.sh || exit
1515
# Compiling and running tests with G++
1616
#######################################################################
1717
export CC=g++
18-
export CFLAGS="-Wall -Wextra"
18+
export CFLAGS="-Wall -Wextra -DYT__TESTING"
1919

2020
echo "Test 2/2: Running tests with C++ Compiler ($CC)"
2121
bash $TESTS_DIR/test.sh || exit

yukti.h

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <stdlib.h>
3636
#include <stdbool.h>
3737
#include <stdarg.h>
38+
#include <sys/time.h>
3839

3940
/*
4041
* ========================================================================================
@@ -377,6 +378,7 @@ void yt_reset(); // MUST BE DEFINED BY THE USER OF THIS HEADER FILE.
377378

378379
typedef struct YT__TestRecord {
379380
char test_function_name[YT__MAX_TEST_FUNCTION_NAME_LENGTH];
381+
struct timeval start_time; /* Time when the test started.*/
380382
uint32_t total_exp_count;
381383
uint32_t failed_exp_count;
382384
size_t parameterised_test_number; /* Parameterised Test number (starts from 1) */
@@ -971,6 +973,10 @@ static int YT__equal_mem (const void* a, const void* b, unsigned long size, int*
971973
* in the previous test's YT_END. */ \
972974
assert (YT__current_testrecord == NULL); \
973975
YT__current_testrecord = YT__create_testRecord (#fn, count, tn); \
976+
if (gettimeofday (&YT__current_testrecord->start_time, NULL)) { \
977+
perror ("gettimeofday"); \
978+
YT__PANIC (NULL); \
979+
} \
974980
do
975981

976982
#define YT__TESTP_DECLARE_TEST_FUNC(fn, ...) \
@@ -1005,22 +1011,60 @@ static int YT__equal_mem (const void* a, const void* b, unsigned long size, int*
10051011
#tf, #fn, YT__COL_RESET); \
10061012
YT__TEST_IMPL_BODY (tf, fn, 1, 1)
10071013

1014+
#ifndef YT__TESTING
1015+
static double yt__test_elapsed_time_ms()
1016+
{
1017+
struct timeval end_time, start_time = YT__current_testrecord->start_time;
1018+
if (gettimeofday (&end_time, NULL)) {
1019+
perror ("gettimeofday");
1020+
YT__PANIC (NULL);
1021+
}
1022+
return (((end_time.tv_sec - start_time.tv_sec) +
1023+
(end_time.tv_usec - start_time.tv_usec) / 1e6) *
1024+
1000.0);
1025+
}
1026+
1027+
#define YT__PRINT_SUCCESS_MESSAGE() \
1028+
do { \
1029+
printf ("\n %sAll test expectations passed [0 of %d failed] [%1.4f ms]%s", \
1030+
YT__COL_GREEN, YT__current_testrecord->total_exp_count, \
1031+
yt__test_elapsed_time_ms(), YT__COL_RESET); \
1032+
} while (0)
1033+
1034+
#define YT__PRINT_FAILURE_MESSAGE() \
1035+
do { \
1036+
printf ("\n %sSome test expectations failed [%d of %d failed] [%1.4f ms]%s", \
1037+
YT__COL_RED, YT__current_testrecord->failed_exp_count, \
1038+
YT__current_testrecord->total_exp_count, yt__test_elapsed_time_ms(), \
1039+
YT__COL_RESET); \
1040+
} while (0)
1041+
#else
1042+
#define YT__PRINT_SUCCESS_MESSAGE() \
1043+
do { \
1044+
printf ("\n %sAll test expectations passed [0 of %d failed]%s", YT__COL_GREEN, \
1045+
YT__current_testrecord->total_exp_count, YT__COL_RESET); \
1046+
} while (0)
1047+
1048+
#define YT__PRINT_FAILURE_MESSAGE() \
1049+
do { \
1050+
printf ("\n %sSome test expectations failed [%d of %d failed]%s", YT__COL_RED, \
1051+
YT__current_testrecord->failed_exp_count, \
1052+
YT__current_testrecord->total_exp_count, YT__COL_RESET); \
1053+
} while (0)
1054+
#endif /* YT__TESTING */
1055+
10081056
// clang-format off
10091057
#define YT_END() \
10101058
YT__validate_expectations(); \
10111059
YT__teardown(); \
10121060
if (YT__current_testrecord->failed_exp_count != 0) { \
1013-
/* Add test to failed test list */ \
1014-
printf ("\n %sSome test expectations failed [%d of %d failed]%s", \
1015-
YT__COL_RED, YT__current_testrecord->failed_exp_count, \
1016-
YT__current_testrecord->total_exp_count, YT__COL_RESET); \
1061+
YT__PRINT_FAILURE_MESSAGE(); \
10171062
YT__failed_test_count++; \
1063+
/* Add test to failed test list */ \
10181064
acl_list_add_before (&YT__failedTestsListHead, \
10191065
&YT__current_testrecord->failedTestListNode); \
10201066
} else { \
1021-
printf ("\n %sAll test expectations passed [0 of %d failed]%s", \
1022-
YT__COL_GREEN, YT__current_testrecord->total_exp_count, \
1023-
YT__COL_RESET); \
1067+
YT__PRINT_SUCCESS_MESSAGE(); \
10241068
YT__free_testRecord (YT__current_testrecord); \
10251069
} \
10261070
YT__current_testrecord = NULL; \
@@ -1031,7 +1075,7 @@ static int YT__equal_mem (const void* a, const void* b, unsigned long size, int*
10311075

10321076
#endif /* YUKTI_TEST_IMPLEMENTATION */
10331077

1034-
/* When YUKTI_TEST_STRIP_PREFIX is defined, the all public interfaces (expect yt_reset & YT_INIT) will
1078+
/* When YUKTI_TEST_STRIP_PREFIX is defined all public interfaces (expect yt_reset & YT_INIT) will
10351079
* have another variant without the 'YT_' prefix.
10361080
*/
10371081
#ifdef YUKTI_TEST_STRIP_PREFIX

0 commit comments

Comments
 (0)