metric is a framework with a set of macros to do unit tests and benchmark code.
You can read the documentation in the GitHub's Wiki in any of the following languages:
This repository has CMake files to build, install and run tests on the project. The directory structure of a project using the metric framework is:
include
| Directory with header filessrc
| Directory with source codearch
| Architecture dependent source filesarch/{arch_name}/include
| Header files to specific architecture
tests/feature
| Feature teststests/unit
| Unit tests
By default CMake is configured to compile a project with C (.c
) and Assembly (.s
) source files. The src/arch
directory
is expected to have architecture dependent source files. Inside the directory is expected to have a subdirectory with the name
of the target architecture and inside it the source code. Example: src/arch/x86_64/source.c
The possible names of architecture is x86_64
, i386
, ia64
, arm64
and any other name used in uname -m
command.
This repository is a template repository, so you can create a new repository using this template as base following the instructions:
- English: Creating a repository from a template
- Português: Criar um repositório a partir de um modelo
In CMakeLists.txt
update the project command to set the project's name, version and languages.
In configuration.cmake
you can update the configurations about the project. Read the commentaries on the file.
First of all, run cmake .
in the project directory and CMake will generate the files to build the project.
$ cmake --build . -t output_bin
Or to build also the tests' binaries just remove the -t
option.
$ sudo cmake --install .
$ cmake --build .
$ ctest
You can use the macros including metric.h
header and following the instructions in the documentation.
#include <stdio.h>
#include <inttypes.h>
#include "metric.h"
uint64_t fib(uint64_t n)
{
if (n < 2) return n;
return fib(n-1) + fib(n-2);
}
test_t test_fib(void)
{
METRIC_ASSERT( fib(12) == 144 );
METRIC_ASSERT( fib(17) == 1597 );
METRIC_ASSERT( fib(1) == 1 );
METRIC_TEST_OK("");
}
int main(void)
{
METRIC_TEST(test_fib);
METRIC_TEST_END();
}
Output:
#include <stdio.h>
#include <inttypes.h>
#include "metric.h"
uint64_t fib(uint64_t n)
{
if (n < 2) return n;
return fib(n-1) + fib(n-2);
}
int main(void)
{
METRIC_START();
METRIC_CALL(1, fib, 20);
METRIC_CALL(5000, fib, 20);
METRIC_CALL(1, fib, 40);
METRIC_STOP("total_bench");
return 0;
}
Output: