Skip to content

English home

Luiz Felipe edited this page Apr 11, 2020 · 4 revisions

This is the metric's documentation, a set of macros to make unit-tests and benchmarks in C language. These macros have been tested at C99 and work properly, I have not intended to make it compatible with C90 (but it's is really simple to do it, if you need).

At the inclusion of the header metric.h, the following macros have the definition verified:

  • If NDEBUG is defined, all macros are defined to expand to nothing.
  • If ANSI_COLORS is defined, the macros use ANSI escape to coloring the output.

Benchmarking code

If you need to measure the performance of a routine or part of the code, these macros will be useful for you. It measures the processor's clocks and the time in seconds that has elapsed since the measurement started.

  • METRIC_CALL (times, funcname, ...) -- Call a function repeating it n times.
  • METRIC_START () -- Start a new block to measure.
  • METRIC_STOP (name) -- Stop the measurement and show the results.

Useful macros

Some useful macros were created to any other usage.

Unit tests' macros

The libc have the <assert.h> header file and this has the assert function-like macro, but it is too simple to do unit tests.

The metric.h header defines the type test_t that should be used as the return type of a test function. The global static variables metric_count_tests_fail and metric_count_tests_ok can be used to read tests that failed and succeed, respectively.

Any assert macro can only be used inside a test function. The prototype of a test function is:

test_t test_name(void);