C Unit Test Framework  0.1
 All Files Functions Enumerations Enumerator Macros Pages
Enumerations | Functions
testlib.c File Reference
#include "testlib.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include "timer.h"

Enumerations

enum  VerbosityLevel {
  LEVEL_RESULT = 0, LEVEL_GOODRESULT, LEVEL_INFO, LEVEL_LOC,
  LEVEL_FUNC, LEVEL_DEBUG
}
 Verbosity level for output function. More...
 

Functions

int testlib_verbose ()
 Increase verbosity level to get more output. More...
 
int testlib_silent ()
 Decrease verbosity level to get less output. More...
 
void testlib_add_column (const char *name, const char *fmt)
 Adds a new data column for data-driven tests. More...
 
void testlib_add_row (const char *name,...)
 Adds a new data row for data-driven tests. More...
 
void * testlib_fetch (const char *name)
 Returns the data for the given column name of the current test dataset. More...
 
int testlib_fetch_int (const char *name)
 This function behaves exactly like testlib_fetch() but returns an int value previously stored in a %i or %d column. More...
 
unsigned int testlib_fetch_uint (const char *name)
 This function behaves exactly like testlib_fetch() but returns an unsigned int value previously stored in a %u column. More...
 
double testlib_fetch_double (const char *name)
 This function behaves exactly like testlib_fetch() but returns a double value previously stored in a %f column. More...
 
void testlib_run_tests (const char *testname, const char *testset)
 Executes registered test functions. More...
 
void testlib_list_tests ()
 Lists all registered test functions. More...
 
struct testlib_stat * testlib_result ()
 Returns the test result statistic. More...
 
int testlib_main (int argc, char *argv[])
 Test main implementation used by UTEST_MAIN. More...
 

Detailed Description

Todo:

add test suites

add support for test skipping

Enumeration Type Documentation

Verbosity level for output function.

The verbosity level can be increased by calling testlib_verbose.

By default if a test fails you get detailed information about the problem:

* FAIL! : test_toupper(umlauts) Compared values are not the same.
* Actual (tmp): öäü
* Expected (result): ÖÄÜ
* Loc: [/home/gergap/work/unittest/src/test/main.c(41)]
*

If the test passes you only see the test result.

* PASS : test_toupper
*

By increasing the verbosity leve you can get more information also for passed tests. You can increase the verbosity level by calling testlib_verbose().

Enumerator
LEVEL_RESULT 

report results

LEVEL_GOODRESULT 

report also good results if tests that have PASSED

LEVEL_INFO 

report additional info (e.g.

actual/expected values for XFAIL)

LEVEL_LOC 

report locality information also for PASSED tests

LEVEL_FUNC 

report test function entry and leave

LEVEL_DEBUG 

report additional debug info

Function Documentation

void testlib_add_column ( const char *  name,
const char *  fmt 
)

Adds a new data column for data-driven tests.

Call this function only in a test-data preperation function.

Parameters
nameName of the column. Used also in testlib_fetch().
fmtPrintf like format specifier.

The following format specifiers are currently supported:

Fmt Description
%s C String
%p Pointer type
%i / %d int
%u unsigned int
%f double

Note: In C float values passed via ... to a variadic function are promoted to double. char and short are promoted to int. That's why there are only testlib_fetch_int and testlib_fetch_double functions and no functions for float, short and char.

void testlib_add_row ( const char *  name,
  ... 
)

Adds a new data row for data-driven tests.

Call this function only in a test-data preperation function.

Parameters
nameName of the test set. This is used in the output to see with what dataset the test fails.
...The subsequent arguments must match the number of specified columns and type.
See Also
testlib_add_column
void* testlib_fetch ( const char *  name)

Returns the data for the given column name of the current test dataset.

Only call this in data-driven test functions.

The test function is called once for each dataset.

Parameters
namethe column name registered with testlib_add_column()
Returns
Returns the stored pointer or NULL if not found. This function can be used for %s and %p format specifiers.
double testlib_fetch_double ( const char *  name)

This function behaves exactly like testlib_fetch() but returns a double value previously stored in a %f column.

Parameters
namethe column name registered with testlib_add_column()
Returns
the stored value or 0.0 if not found.
int testlib_fetch_int ( const char *  name)

This function behaves exactly like testlib_fetch() but returns an int value previously stored in a %i or %d column.

Parameters
namethe column name registered with testlib_add_column()
Returns
the stored value or 0 if not found.
unsigned int testlib_fetch_uint ( const char *  name)

This function behaves exactly like testlib_fetch() but returns an unsigned int value previously stored in a %u column.

Parameters
namethe column name registered with testlib_add_column()
Returns
the stored value or 0 if not found.
void testlib_list_tests ( )

Lists all registered test functions.

int testlib_main ( int  argc,
char *  argv[] 
)

Test main implementation used by UTEST_MAIN.

This is a simplified version of testlib_main for systems which have no getopt() implementation or if you simply want to avoid the additional code of getopt(). Of course here you have no commandline option parsing, but it still allows to specify one testname.

struct testlib_stat* testlib_result ( )

Returns the test result statistic.

void testlib_run_tests ( const char *  testname,
const char *  testset 
)

Executes registered test functions.

If testname is NULL all functions are executed, otherwise only the one matching testname. If testset is NULL all datasets are used for data-driven tests, otherwise only the one matching testset.

int testlib_silent ( )

Decrease verbosity level to get less output.

int testlib_verbose ( )

Increase verbosity level to get more output.