Defines a functions to execute a single unit test. Defines a macro for implementing a unit test and to report any errors.
Unit-Test | Defines a functions to execute a single unit test. |
Copyright | This program is free software. |
Files | |
C-kern/ | Header file of Unit-Test. |
C-kern/ | Implementation file Unit-Test impl. |
Functions | |
test | |
unittest_test_unittest | Unittest for <TEST> macro and log functions. |
unittest_t | |
lifetime | |
initsingleton_unittest | Prepares the single instance of unittest_t to execute tests. |
freesingleton_unittest | Frees any resources allocated with the single object of type unittest_t. |
report | |
logf_unittest | Logs a formatted string. |
logfailed_unittest | Logs “<filename>:<line_number>: <msg>\n”. |
logfailedunexpected_unittest | Logs “<filename>:<line_number>: UNEXPECTED VALUE <value>\n”. |
logresult_unittest | Logs “OK\n” or “FAILED\n”. |
logrun_unittest | Logs “RUN %s: “. |
logsummary_unittest | Logs how many tests passed and how many failed. |
execute | |
execsingle_unittest | Runs a single unit test. |
execasprocess_unittest | Forks a child process which runs the test function. |
macros | |
TEST | Tests CONDITION and jumps to label ONABORT in case of false. |
TESTP | Tests (CMP FCTCALL) jumps to label ONABORT in case of false. |
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2011 Jörg Seebohn
Header file of Unit-Test.
Implementation file Unit-Test impl.
test | |
unittest_test_unittest | Unittest for <TEST> macro and log functions. |
struct unittest_t
lifetime | |
initsingleton_unittest | Prepares the single instance of unittest_t to execute tests. |
freesingleton_unittest | Frees any resources allocated with the single object of type unittest_t. |
report | |
logf_unittest | Logs a formatted string. |
logfailed_unittest | Logs “<filename>:<line_number>: <msg>\n”. |
logfailedunexpected_unittest | Logs “<filename>:<line_number>: UNEXPECTED VALUE <value>\n”. |
logresult_unittest | Logs “OK\n” or “FAILED\n”. |
logrun_unittest | Logs “RUN %s: “. |
logsummary_unittest | Logs how many tests passed and how many failed. |
execute | |
execsingle_unittest | Runs a single unit test. |
execasprocess_unittest | Forks a child process which runs the test function. |
macros | |
TEST | Tests CONDITION and jumps to label ONABORT in case of false. |
TESTP | Tests (CMP FCTCALL) jumps to label ONABORT in case of false. |
int initsingleton_unittest( const char * log_files_directory )
Prepares the single instance of unittest_t to execute tests. Parameter log_files_directory contains the directory path where all of the log files are stored. The stored log files are compared to the generated log during test execution. The adapter interface which is pointed to by parameter adapter has to be valid until freesingleton_unittest is called.
All test results (logs) are written to STDOUT.
int freesingleton_unittest( void )
Frees any resources allocated with the single object of type unittest_t.
void logfailedunexpected_unittest( const char * filename, unsigned line_number, const char * format, ... ) __attribute__ ((__format__ (__printf__, 3, 4)))
Logs “<filename>:<line_number>: UNEXPECTED VALUE <value>\n”. The format of the value is expected in format. The value is expected as last argument. This is a thread-safe function!
int execasprocess_unittest( int (*test_f)(void), /*out*/int * retcode )
Forks a child process which runs the test function. The parameter retcode is set to the value returned by test_f. If test_t exits abnormally with a signal retcode is set to EINTR. Also the content of the buffered errorlog is transfered via pipe at the end of test_f to the calling process and printed to its errorlog.
Use this function only within the execution of a unittest.
#define TEST( CONDITION ) if ( !(CONDITION) ) { logfailed_unittest(__FILE__, __LINE__, 0); goto ONABORT; }
Tests CONDITION and jumps to label ONABORT in case of false. If CONDITION is false an error is printed and computation continues at the label ONABORT.
CONDITION | Condition which is tested to be true. |
The following demonstrates how this macro is used:
int unittest_module() { type_t type = type_FREE; TEST(0 == init_type(&type)); TEST(0 == free_type(&type)); return 0; // success ONABORT: free_type(&type); return EINVAL; // any error code }
#define TESTP( PRIx, CMP, FCTCALL ) { typeof(FCTCALL) _r = FCTCALL; if ( !(CMP _r) ) { logfailedunexpected_unittest( __FILE__, __LINE__, "%" PRIx, _r); goto ONABORT; } }
Tests (CMP FCTCALL) jumps to label ONABORT in case of false. If (CMP FCTCALL) is false an error is printed and the value retured by the function call and computation continues at label ONABORT.
PRIx | printf format type string of the value returned by the function. |
CMP | Condition which is tested as (CMP FCTCALL) to be true. |
FCTCALL | Call to function whose result is also stored in a temporary variable. |
The following demonstrates how this macro is used:
int unittest_module() { type_t type = type_FREE; TESTP(d,0 ==,init_type(&type)); TESTP(d,0 ==,free_type(&type)); return 0; // success ONABORT: free_type(&type); return EINVAL; // any error code }
Unittest for TEST macro and log functions.
int unittest_test_unittest( void )
struct unittest_t
Prepares the single instance of unittest_t to execute tests.
int initsingleton_unittest( const char * log_files_directory )
Frees any resources allocated with the single object of type unittest_t.
int freesingleton_unittest( void )
Logs a formatted string.
void logf_unittest( const char * format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)))
Logs “filename:<line_number>: <msg>\n”.
void logfailed_unittest( const char * filename, unsigned line_number, const char * msg )
Logs “filename:<line_number>: UNEXPECTED VALUE <value>\n”.
void logfailedunexpected_unittest( const char * filename, unsigned line_number, const char * format, ... ) __attribute__ ((__format__ (__printf__, 3, 4)))
Logs “OK\n” or “FAILED\n”.
void logresult_unittest( bool isFailed )
Logs “RUN %s: “.
void logrun_unittest( const char * testname )
Logs how many tests passed and how many failed.
void logsummary_unittest( void )
Runs a single unit test.
int execsingle_unittest( const char * testname, int (*test_f)(void) )
Forks a child process which runs the test function.
int execasprocess_unittest( int (*test_f)(void), /*out*/int * retcode )
Tests CONDITION and jumps to label ONABORT in case of false.
#define TEST( CONDITION ) if ( !(CONDITION) ) { logfailed_unittest(__FILE__, __LINE__, 0); goto ONABORT; }
Tests (CMP FCTCALL) jumps to label ONABORT in case of false.
#define TESTP( PRIx, CMP, FCTCALL ) { typeof(FCTCALL) _r = FCTCALL; if ( !(CMP _r) ) { logfailedunexpected_unittest( __FILE__, __LINE__, "%" PRIx, _r); goto ONABORT; } }