-
Notifications
You must be signed in to change notification settings - Fork 0
English home
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.
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.
Some useful macros were created to any other usage.
- METRIC_LOG (format, ...) -- Displays a log message.
- METRIC_LOG_ARRAY (mask, array, length) -- Displays the array's content.
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.
- METRIC_TEST (test_name) -- Run a test function.
- METRIC_TEST_END () -- Displays status about failures and successes.
- METRIC_TEST_FAIL (message) -- Inside a test function, stop it with the fail status.
- METRIC_TEST_OK (message) -- Like above, but with success status.
- METRIC_ASSERT (expression) -- Fail the test function if the expression is false.
- METRIC_ASSERT_ARRAY (array1, array2, size) -- Fail if the arrays are not equal.
- METRIC_ASSERT_STRING (string1, string2) -- Fail if the strings are not equal.
The
metric.h
header defines the typetest_t
that should be used as the return type of a test function. The global static variablesmetric_count_tests_fail
andmetric_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);