Unit-Test

Defines a functions to execute a single unit test.  Defines a macro for implementing a unit test and to report any errors.

Summary
Unit-TestDefines a functions to execute a single unit test.
CopyrightThis program is free software.
Files
C-kern/api/test/unittest.hHeader file of Unit-Test.
C-kern/test/unittest.cImplementation file Unit-Test impl.
Functions
test
unittest_test_unittestUnittest for <TEST> macro and log functions.
unittest_t
lifetime
initsingleton_unittestPrepares the single instance of unittest_t to execute tests.
freesingleton_unittestFrees any resources allocated with the single object of type unittest_t.
report
logf_unittestLogs a formatted string.
logfailed_unittestLogs “<filename>:<line_number>: <msg>\n”.
logfailedunexpected_unittestLogs “<filename>:<line_number>: UNEXPECTED VALUE <value>\n”.
logresult_unittestLogs “OK\n” or “FAILED\n”.
logrun_unittestLogs “RUN %s: “.
logsummary_unittestLogs how many tests passed and how many failed.
execute
execsingle_unittestRuns a single unit test.
execasprocess_unittestForks a child process which runs the test function.
macros
TESTTests CONDITION and jumps to label ONABORT in case of false.
TESTPTests (CMP FCTCALL) jumps to label ONABORT in case of false.

Copyright

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.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/test/unittest.h

Header file of Unit-Test.

C-kern/test/unittest.c

Implementation file Unit-Test impl.

Functions

Summary
test
unittest_test_unittestUnittest for <TEST> macro and log functions.

test

unittest_test_unittest

int unittest_test_unittest(void)

Unittest for <TEST> macro and log functions.

unittest_t

struct unittest_t
Summary
lifetime
initsingleton_unittestPrepares the single instance of unittest_t to execute tests.
freesingleton_unittestFrees any resources allocated with the single object of type unittest_t.
report
logf_unittestLogs a formatted string.
logfailed_unittestLogs “<filename>:<line_number>: <msg>\n”.
logfailedunexpected_unittestLogs “<filename>:<line_number>: UNEXPECTED VALUE <value>\n”.
logresult_unittestLogs “OK\n” or “FAILED\n”.
logrun_unittestLogs “RUN %s: “.
logsummary_unittestLogs how many tests passed and how many failed.
execute
execsingle_unittestRuns a single unit test.
execasprocess_unittestForks a child process which runs the test function.
macros
TESTTests CONDITION and jumps to label ONABORT in case of false.
TESTPTests (CMP FCTCALL) jumps to label ONABORT in case of false.

lifetime

initsingleton_unittest

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.

freesingleton_unittest

int freesingleton_unittest(void)

Frees any resources allocated with the single object of type unittest_t.

report

logf_unittest

void logf_unittest(
   const char *format,
    ...
) __attribute__ ((__format__ (__printf__, 1, 2)))

Logs a formatted string.  The output is truncated if the output exceeds 256 bytes.

logfailed_unittest

void logfailed_unittest(const char *filename,
unsigned line_number,
const char *msg)

Logs “<filename>:<line_number>: <msg>\n”.  If msg is set to 0 the msg is set to its default value “TEST FAILED”.  This is a thread-safe function!

logfailedunexpected_unittest

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!

logresult_unittest

void logresult_unittest(bool isFailed)

Logs “OK\n” or “FAILED\n”.  This is a thread-safe function!

logrun_unittest

void logrun_unittest(const char *testname)

Logs “RUN %s: “.

logsummary_unittest

void logsummary_unittest(void)

Logs how many tests passed and how many failed.

execute

execsingle_unittest

int execsingle_unittest(const char *testname,
int (*test_f)(void))

Runs a single unit test.  A return value of 0 indicates success, a return value != 0 indicates the test failed.

execasprocess_unittest

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.

macros

TEST

#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.

Parameters

CONDITIONCondition which is tested to be true.

Usage

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
}

TESTP

#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.

Parameters

PRIxprintf format type string of the value returned by the function.
CMPCondition which is tested as (CMP FCTCALL) to be true.
FCTCALLCall to function whose result is also stored in a temporary variable.

Usage

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
}
Defines a functions to execute a single unit test.
Implements functions defined in Unit-Test.
int unittest_test_unittest(void)
Unittest for TEST macro and log functions.
struct unittest_t
int initsingleton_unittest(const char *log_files_directory)
Prepares the single instance of unittest_t to execute tests.
int freesingleton_unittest(void)
Frees any resources allocated with the single object of type unittest_t.
void logf_unittest(
   const char *format,
    ...
) __attribute__ ((__format__ (__printf__, 1, 2)))
Logs a formatted string.
void logfailed_unittest(const char *filename,
unsigned line_number,
const char *msg)
Logs “filename:<line_number>: <msg>\n”.
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”.
void logresult_unittest(bool isFailed)
Logs “OK\n” or “FAILED\n”.
void logrun_unittest(const char *testname)
Logs “RUN %s: “.
void logsummary_unittest(void)
Logs how many tests passed and how many failed.
int execsingle_unittest(const char *testname,
int (*test_f)(void))
Runs a single unit test.
int execasprocess_unittest(int (*test_f)(void),
/*out*/int *retcode)
Forks a child process which runs the test function.
#define TEST(
   CONDITION
) if ( !(CONDITION) ) { logfailed_unittest(__FILE__, __LINE__, 0); goto ONABORT; }
Tests CONDITION and 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; } }
Tests (CMP FCTCALL) jumps to label ONABORT in case of false.
Close