Fixes to work on github and all compilers

This commit is contained in:
Mattias
2022-05-18 14:49:29 +02:00
parent 6fb3ff7bf8
commit b36b026668
+166 -36
View File
@@ -3,7 +3,7 @@
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
testfw.h - v1.0 - Basic test framwework for C/C++.
testfw.h - v1.1 - Basic test framework for C/C++.
Do this:
#define TESTFW_IMPLEMENTATION
@@ -15,20 +15,34 @@ before you include this file in *one* C/C++ file to create the implementation.
#define TESTFW_INIT() testfw_init()
#define TESTFW_SUMMARY() testfw_summary( __FILE__, __func__, __LINE__ )
#define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ );
#define TESTFW_TEST_END() testfw_test_end( __FILE__, __func__, __LINE__ )
#if defined( _WIN32 ) && !defined( TESTFW_NO_SEH ) && !defined( __TINYC__ )
#if defined __cplusplus
extern "C" unsigned long __cdecl _exception_code(void);
#else
unsigned long __cdecl _exception_code(void);
#endif
#define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ ); __try {
#define TESTFW_TEST_END() } __except( 1 /*EXCEPTION_EXECUTE_HANDLER*/ ) \
{ testfw_exception( _exception_code/*GetExceptionCode*/() ); } \
testfw_test_end( __FILE__, __func__, __LINE__ )
#else
#define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ ); {
#define TESTFW_TEST_END() testfw_test_end( __FILE__, __func__, __LINE__ ); }
#endif
#define TESTFW_EXPECTED( expression ) testfw_expected( (expression) ? 1 : 0, #expression, __FILE__, __func__, __LINE__ )
void testfw_init();
void testfw_summary( char const* filename, char const* funcname, int line );
void testfw_init( void );
int testfw_summary( char const* filename, char const* funcname, int line );
void testfw_test_begin( char const* desc, char const* filename, char const* funcname, int line );
void testfw_test_end( char const* filename, char const* funcname, int line );
void testfw_expected( int expression, char const* expression_str, char const* filename, char const* funcname, int line );
void testfw_print_test_desc();
void testfw_print_test_desc( void );
void testfw_print_failure( char const* filename, int line );
void testfw_assertion_count_inc();
void testfw_current_test_assertion_failed();
void testfw_assertion_count_inc( void );
void testfw_current_test_assertion_failed( void );
#if defined( _WIN32 ) && !defined( TESTFW_NO_SEH ) && !defined( __TINYC__ )
void testfw_exception( unsigned int exception_code );
#endif
#endif /* testfw_h */
@@ -42,7 +56,7 @@ void testfw_current_test_assertion_failed();
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#ifdef _WIN32
#if defined( _WIN32 ) && !defined( __TINYC__ )
#pragma warning( push )
#pragma warning( disable: 4619 ) // pragma warning : there is no warning number 'number'
#pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0'
@@ -55,7 +69,7 @@ void testfw_current_test_assertion_failed();
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define TESTFW_PRINTF printf
#define TESTFW_PRINTF( format, ... ) printf( format, ##__VA_ARGS__ )
#endif
#ifdef TESTFW_NO_ANSI
@@ -149,14 +163,17 @@ static struct
int tests_failed;
int assertions_total;
int assertions_failed;
testfw_internal_current_test_state_t current_test;
} testfw_internal_state = {};
struct testfw_internal_current_test_state_t current_test;
#if defined( _WIN32 ) && defined( _DEBUG ) && !defined( __TINYC__ )
int total_leaks;
#endif
} testfw_internal_state;
static void testfw_internal_print_progress_divider( char ch, int fail, int total )
{
int width = 79;
int first = (int)( ( width * fail ) / total );
int first = (int)( ( width * fail ) / ( total ? total : 1 ) );
int second = width - first;
if( fail > 0 && first == 0 )
{
@@ -172,12 +189,12 @@ static void testfw_internal_print_progress_divider( char ch, int fail, int total
}
#if defined( _WIN32 ) && defined( _DEBUG )
#if defined( _WIN32 ) && defined( _DEBUG ) && !defined( __TINYC__ )
static int testfw_internal_debug_report_hook( int report_type, char* message, int* return_value )
{
static int total_leaks = 0;
_flushall();
(void) report_type;
*return_value = 0; // Don't break to debugger
if( stricmp( message, "Detected memory leaks!\n" ) == 0 )
@@ -203,21 +220,21 @@ static void testfw_internal_print_progress_divider( char ch, int fail, int total
for( unsigned int i = 0; i < 5; ++i )
{
if( i != 2 && state.lCounts[ i ] )
TESTFW_PRINTF( "%Id bytes in %Id %hs Blocks.\n", (long long) state.lSizes[ i ],
TESTFW_PRINTF( "%lld bytes in %lld %hs Blocks.\n", (long long) state.lSizes[ i ],
(long long) state.lCounts[ i ], block_use_names[ i ] );
}
TESTFW_PRINTF( "\n" );
TESTFW_PRINTF( "High water mark: %Id bytes.\n", (long long)state.lHighWaterCount);
TESTFW_PRINTF( "All allocations: %Id bytes.\n", (long long)state.lTotalCount);
TESTFW_PRINTF( "High water mark: %lld bytes.\n", (long long)state.lHighWaterCount);
TESTFW_PRINTF( "All allocations: %lld bytes.\n", (long long)state.lTotalCount);
TESTFW_PRINTF( "\n" );
TESTFW_PRINTF( "===============================================================================\n" );
TESTFW_PRINTF( "%sMEMORY LEAKS: %d%s\n\n", TESTFW_ANSI_LIGHT_RED, total_leaks, TESTFW_ANSI_RESET );
TESTFW_PRINTF( "%sMEMORY LEAKS: %d%s\n\n", TESTFW_ANSI_LIGHT_RED, testfw_internal_state.total_leaks, TESTFW_ANSI_RESET );
return 1; // Tell CRT not to print the message
}
else if( strnicmp( message, " Data: <", 8 ) == 0 )
{
++total_leaks;
++testfw_internal_state.total_leaks;
return 1; // Tell CRT not to print the message
}
@@ -227,22 +244,27 @@ static void testfw_internal_print_progress_divider( char ch, int fail, int total
#endif /* _WIN32 && _DEBUG */
void testfw_init()
void testfw_init( void )
{
memset( &testfw_internal_state, 0, sizeof( testfw_internal_state ) );
#ifdef _WIN32
#if defined( _WIN32 ) && !defined( __TINYC__ )
int flag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // Get current flag
flag ^= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag( flag ); // Set flag to the new value
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
_CrtSetReportHook2( _CRT_RPTHOOK_INSTALL, testfw_internal_debug_report_hook );
_CrtSetReportHook( testfw_internal_debug_report_hook );
#endif /* _WIN32 */
}
static int testfw_internal_must_be_in_test()
static int testfw_internal_must_be_in_test( void )
{
if( !testfw_internal_state.current_test.is_active )
TESTFW_PRINTF( "\n\n%sEXPECTED TO BE IN AN ACTIVE TEST, BUT NO TEST IS CURRENTLY ACTIVE.%s\n\n",
@@ -251,7 +273,7 @@ static int testfw_internal_must_be_in_test()
}
static int testfw_internal_can_not_be_in_test()
static int testfw_internal_can_not_be_in_test( void )
{
if( testfw_internal_state.current_test.is_active )
TESTFW_PRINTF( "\n\n%sEXPECTED TO NOT BE IN AN ACTIVE TEST, BUT A TEST IS CURRENTLY ACTIVE.%s\n\n",
@@ -260,7 +282,7 @@ static int testfw_internal_can_not_be_in_test()
}
void testfw_print_test_desc()
void testfw_print_test_desc( void )
{
if( testfw_internal_must_be_in_test() ) return;
@@ -273,19 +295,21 @@ void testfw_print_test_desc()
TESTFW_PRINTF( "%s\n", testfw_internal_state.current_test.desc ? testfw_internal_state.current_test.desc :
"<NO DESCRIPTION>" );
TESTFW_PRINTF( "-------------------------------------------------------------------------------\n" );
TESTFW_PRINTF( "%s%s(%d): %s%s\n", TESTFW_ANSI_GREY, testfw_internal_state.current_test.file,
TESTFW_PRINTF( "%s%s(%d): %s%s\n", TESTFW_ANSI_LIGHT_GREY, testfw_internal_state.current_test.file,
testfw_internal_state.current_test.line, TESTFW_ANSI_RESET, testfw_internal_state.current_test.func );
TESTFW_PRINTF( "%s", TESTFW_ANSI_GREY );
TESTFW_PRINTF( "%s", TESTFW_ANSI_LIGHT_GREY );
TESTFW_PRINTF( "...............................................................................\n" );
TESTFW_PRINTF( "%s", TESTFW_ANSI_RESET );
}
}
void testfw_summary( char const* filename, char const* funcname, int line )
int testfw_summary( char const* filename, char const* funcname, int line )
{
if( testfw_internal_can_not_be_in_test() ) return;
(void) filename; (void) funcname; (void) line;
if( testfw_internal_can_not_be_in_test() ) return -1;
TESTFW_PRINTF( "\n" );
testfw_internal_print_progress_divider( '=', testfw_internal_state.tests_failed, testfw_internal_state.tests_total );
@@ -305,6 +329,13 @@ void testfw_summary( char const* filename, char const* funcname, int line )
}
TESTFW_PRINTF( "\n\n" );
#if defined( _WIN32 ) && defined( _DEBUG ) && !defined( __TINYC__ )
int result = _CrtDumpMemoryLeaks();
testfw_internal_state.tests_failed += result ? 1 : 0;
#endif
return testfw_internal_state.tests_failed;
}
@@ -327,13 +358,111 @@ void testfw_test_begin( char const* desc, char const* filename, char const* func
void testfw_test_end( char const* filename, char const* funcname, int line )
{
(void) filename; (void) funcname; (void) line;
if( testfw_internal_must_be_in_test() ) return;
memset( &testfw_internal_state.current_test, 0, sizeof( testfw_internal_state.current_test ) );
}
void testfw_current_test_assertion_failed()
#if defined( _WIN32 ) && !defined( TESTFW_NO_SEH ) && !defined( __TINYC__ )
#pragma warning( push )
#pragma warning( disable: 4668 )
#include <windows.h>
#pragma warning( pop )
void testfw_exception( unsigned int exception_code )
{
if( testfw_internal_must_be_in_test() ) return;
if( !testfw_internal_state.current_test.counted_as_failed )
{
testfw_internal_state.current_test.counted_as_failed = 1;
++testfw_internal_state.tests_failed;
}
char exception_str[ 64 ];
switch( exception_code )
{
case EXCEPTION_ACCESS_VIOLATION:
strcpy( exception_str, "EXCEPTION_ACCESS_VIOLATION" );
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
strcpy( exception_str, "EXCEPTION_DATATYPE_MISALIGNMENT" );
break;
case EXCEPTION_BREAKPOINT:
strcpy( exception_str, "EXCEPTION_BREAKPOINT" );
break;
case EXCEPTION_SINGLE_STEP:
strcpy( exception_str, "EXCEPTION_SINGLE_STEP" );
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
strcpy( exception_str, "EXCEPTION_ARRAY_BOUNDS_EXCEEDED" );
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
strcpy( exception_str, "EXCEPTION_FLT_DENORMAL_OPERAND" );
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
strcpy( exception_str, "EXCEPTION_FLT_DIVIDE_BY_ZERO" );
break;
case EXCEPTION_FLT_INEXACT_RESULT:
strcpy( exception_str, "EXCEPTION_FLT_INEXACT_RESULT" );
break;
case EXCEPTION_FLT_INVALID_OPERATION:
strcpy( exception_str, "EXCEPTION_FLT_INVALID_OPERATION" );
break;
case EXCEPTION_FLT_OVERFLOW:
strcpy( exception_str, "EXCEPTION_FLT_OVERFLOW" );
break;
case EXCEPTION_FLT_STACK_CHECK:
strcpy( exception_str, "EXCEPTION_FLT_STACK_CHECK" );
break;
case EXCEPTION_FLT_UNDERFLOW:
strcpy( exception_str, "EXCEPTION_FLT_UNDERFLOW" );
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
strcpy( exception_str, "EXCEPTION_INT_DIVIDE_BY_ZERO" );
break;
case EXCEPTION_INT_OVERFLOW:
strcpy( exception_str, "EXCEPTION_INT_OVERFLOW" );
break;
case EXCEPTION_PRIV_INSTRUCTION:
strcpy( exception_str, "EXCEPTION_PRIV_INSTRUCTION" );
break;
case EXCEPTION_IN_PAGE_ERROR:
strcpy( exception_str, "EXCEPTION_IN_PAGE_ERROR" );
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
strcpy( exception_str, "EXCEPTION_ILLEGAL_INSTRUCTION" );
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
strcpy( exception_str, "EXCEPTION_NONCONTINUABLE_EXCEPTION" );
break;
case EXCEPTION_STACK_OVERFLOW:
strcpy( exception_str, "EXCEPTION_STACK_OVERFLOW" );
break;
case EXCEPTION_INVALID_DISPOSITION:
strcpy( exception_str, "EXCEPTION_INVALID_DISPOSITION" );
break;
case EXCEPTION_GUARD_PAGE:
strcpy( exception_str, "EXCEPTION_GUARD_PAGE" );
break;
case EXCEPTION_INVALID_HANDLE:
strcpy( exception_str, "EXCEPTION_INVALID_HANDLE" );
break;
default:
sprintf( exception_str, "%X", exception_code );
}
testfw_print_test_desc();
TESTFW_PRINTF( "\n%s%s(%d): %sFAILED:%s\n", TESTFW_ANSI_LIGHT_GREY, testfw_internal_state.current_test.file,
testfw_internal_state.current_test.line, TESTFW_ANSI_LIGHT_RED, TESTFW_ANSI_RESET );
TESTFW_PRINTF( "\n %sEXCEPTION( %s%s%s )%s\n", TESTFW_ANSI_CYAN, TESTFW_ANSI_WHITE, exception_str,
TESTFW_ANSI_LIGHT_GREY, TESTFW_ANSI_RESET );
}
#endif
void testfw_current_test_assertion_failed( void )
{
if( testfw_internal_must_be_in_test() ) return;
@@ -347,7 +476,7 @@ void testfw_current_test_assertion_failed()
}
void testfw_assertion_count_inc()
void testfw_assertion_count_inc( void )
{
if( testfw_internal_must_be_in_test() ) return;
@@ -357,7 +486,7 @@ void testfw_assertion_count_inc()
void testfw_print_failure( char const* filename, int line )
{
TESTFW_PRINTF( "\n%s%s(%d): %sFAILED:%s\n", TESTFW_ANSI_GREY, filename, line, TESTFW_ANSI_LIGHT_RED,
TESTFW_PRINTF( "\n%s%s(%d): %sFAILED:%s\n", TESTFW_ANSI_LIGHT_GREY, filename, line, TESTFW_ANSI_LIGHT_RED,
TESTFW_ANSI_RESET );
}
@@ -365,6 +494,7 @@ void testfw_print_failure( char const* filename, int line )
void testfw_expected( int expression, char const* expression_str, char const* filename,
char const* funcname, int line )
{
(void) funcname;
if( testfw_internal_must_be_in_test() ) return;
testfw_assertion_count_inc();
@@ -374,8 +504,8 @@ void testfw_expected( int expression, char const* expression_str, char const* fi
testfw_current_test_assertion_failed();
testfw_print_test_desc();
testfw_print_failure( filename, line );
TESTFW_PRINTF( "\n %sTESTFW_EXPECTED( %s%s%s )%s\n", TESTFW_ANSI_CYAN, TESTFW_ANSI_WHITE, expression_str,
TESTFW_ANSI_CYAN, TESTFW_ANSI_RESET );
TESTFW_PRINTF( "\n %sTESTFW_EXPECTED( %s%s%s )%s\n", TESTFW_ANSI_LIGHT_MAGENTA, TESTFW_ANSI_WHITE, expression_str,
TESTFW_ANSI_LIGHT_MAGENTA, TESTFW_ANSI_RESET );
}
}