CLibs
Loading...
Searching...
No Matches
unit_tests.h File Reference
#include "filenames.h"
#include "misc.h"
#include "terminal_colors.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define COLOR_FAIL   FOREGROUND_RED
#define COLOR_SUCC   FOREGROUND_GREEN
#define COLOR_NOTE   FOREGROUND_CYAN
#define COLOR_TEST_TAG   FOREGROUND_YELLOW
#define TEST_NAME_CREATOR(TOK)
 To avoid namespace collisions.
#define TEST(HANDLE)
#define END_TEST
#define RUN_TEST(HANDLE)
#define UNIT_TEST(CONDITION)
#define CRITICAL_TEST(CONDITION)

Functions

LibraryDefined NoReturn void FINISH_TESTING (void)
LibraryDefined void SET_UNIT_TEST_VERBOSITY (const bool verbose)
LibraryDefined bool GET_UNIT_TEST_VERBOSITY (void)

Macro Definition Documentation

◆ COLOR_FAIL

#define COLOR_FAIL   FOREGROUND_RED

◆ COLOR_NOTE

#define COLOR_NOTE   FOREGROUND_CYAN

◆ COLOR_SUCC

#define COLOR_SUCC   FOREGROUND_GREEN

◆ COLOR_TEST_TAG

#define COLOR_TEST_TAG   FOREGROUND_YELLOW

◆ CRITICAL_TEST

#define CRITICAL_TEST ( CONDITION)
Value:
do \
{ \
++TEST_NAME_CREATOR( ran_total ); \
if ( !UNIT_TEST_( #CONDITION, CONDITION, __FILE_NAME__, __LINE__, true ) ) \
{ \
++TEST_NAME_CREATOR( failed_total ); \
{ END_TEST /* the END_TEST macro has a terminating bracket `}' */ \
} \
} \
while ( 0 )
#define TEST_NAME_CREATOR(TOK)
To avoid namespace collisions.
Definition unit_tests.h:52
#define END_TEST
Definition unit_tests.h:97

If condition evaluates to true, "SUCCESS" is printed in the color defined in COLOR_SUCC (green by default).

If condition evaluates to false, "FAILURE" is printed in the color defined in COLOR_FAIL (red by default), and the TEST() case returns.

◆ END_TEST

#define END_TEST
Value:
printf( COLOR_TEST_TAG "[TEST]" COLOR_DEFAULT " %s: ran %i tests, " PRINT_COLOR \
"%i successful" COLOR_DEFAULT ", " PRINT_COLOR \
"%i failed" COLOR_DEFAULT "\n", \
TEST_NAME_CREATOR( test_handle ), \
TEST_NAME_CREATOR( ran_total ), \
TEST_NAME_CREATOR( ran_total ) - TEST_NAME_CREATOR( failed_total ) == 0 \
TEST_NAME_CREATOR( ran_total ) - TEST_NAME_CREATOR( failed_total ), \
TEST_NAME_CREATOR( failed_total ) == 0 ? COLOR_SUCC : COLOR_FAIL, \
TEST_NAME_CREATOR( failed_total ) ); \
TEST_NAME_CREATOR( TOTAL_FAILED_UNIT ) += TEST_NAME_CREATOR( failed_total ); \
TEST_NAME_CREATOR( TOTAL_RAN_UNIT ) += TEST_NAME_CREATOR( ran_total ); \
return TEST_NAME_CREATOR( failed_total ) == 0 ? 0 : 1; \
}
#define COLOR_DEFAULT
Definition terminal_colors.h:33
#define PRINT_COLOR
Definition terminal_colors.h:18
#define COLOR_TEST_TAG
Definition unit_tests.h:47
#define COLOR_SUCC
Definition unit_tests.h:39
#define COLOR_FAIL
Definition unit_tests.h:35

Ends a TEST() block.

Prints a summary of the test case and returns if it failed.

◆ RUN_TEST

#define RUN_TEST ( HANDLE)
Value:
do \
{ \
TEST_NAME_CREATOR( TOTAL_FAILED ) += TEST_NAME_CREATOR( HANDLE )(); \
++TEST_NAME_CREATOR( TOTAL_RAN ); \
} \
while ( 0 )

Runs a test block and increments the proper static variables.

Parameters
HANDLEhandle as used in TEST( HANDLE )

◆ TEST

#define TEST ( HANDLE)
Value:
int TEST_NAME_CREATOR( HANDLE )( void ) \
{ \
int TEST_NAME_CREATOR( failed_total ) = 0; \
int TEST_NAME_CREATOR( ran_total ) = 0; \
const char *TEST_NAME_CREATOR( test_handle ) = #HANDLE; \
printf( COLOR_TEST_TAG "[TEST]" COLOR_DEFAULT " " #HANDLE "\n" );

Begin a test block.

Counts the number of tests ran/failed. Must be terminated by END_TEST.

If UNIT_TESTS_SILENT is not defined, this macro prints the test name as the start of the test case.

Parameters
HANDLEhandle of the test case – must be unique

◆ TEST_NAME_CREATOR

#define TEST_NAME_CREATOR ( TOK)
Value:
CLIBS_UNIT_TESTS_## TOK

To avoid namespace collisions.

◆ UNIT_TEST

#define UNIT_TEST ( CONDITION)
Value:
do \
{ \
if ( !UNIT_TEST_( #CONDITION, CONDITION, __FILE_NAME__, __LINE__, false ) ) \
++TEST_NAME_CREATOR( failed_total ); \
++TEST_NAME_CREATOR( ran_total ); \
} \
while ( 0 )

If condition evaluates to true, "SUCCESS" is printed in the color defined in COLOR_SUCC (green by default).

If condition evaluates to false, "FAILURE" is printed in the color defined in COLOR_FAIL (red by default)

Function Documentation

◆ FINISH_TESTING()

LibraryDefined NoReturn void FINISH_TESTING ( void )

Prints a summary of all TEST() and UNIT_TEST() cases and exits.

Exit value is EXIT_SUCCESS if no unit test failed, else EXIT_FAILURE.

◆ GET_UNIT_TEST_VERBOSITY()

LibraryDefined bool GET_UNIT_TEST_VERBOSITY ( void )
Returns
value of the static verbosity variable

◆ SET_UNIT_TEST_VERBOSITY()

LibraryDefined void SET_UNIT_TEST_VERBOSITY ( const bool verbose)

If set to false, UNIT_TESTs do not show successes.

This option is independent of the macro UNIT_TESTS_SILENT, which silences all messages.

Parameters
verbosetrue by default