MLT Release 1.0.3
Mulato Library of Tests
After some time I am back to my roots, the new release of MLT (Mulato Library of Tests) proves that. With an objective of provide a simple C Header file to test your C software with Unit tests and more.
How to use it
There are some steps to add the C Header file and plug into your code
- Obviously copy the
mlt.h
into yoursrc/
or/include
folder. - Tweak your
Makefile
or any of your build system and add-DMLT_TESTING=1
when you be compiling your sample of tests. - Add the
#ifndef MLT_TESTING
into your before you call themain()
function.
Step 1: Copy the C Header file.
git clone https://github.com/mulatinho/mlt.git
cp -pv mlt/mlt.h /workdir/your-code/src/mlt.h
Step 2: Add some test
Here is an example of testing using the latency-test software
mkdir tests && vim Makefile tests/latency-test_test.c
Compile your code and generate its object, for linking with MLT test.
Makefile
test:
gcc -DMLT_TESTING=1 -o src/latency-test.o -c src/latency-test.c
gcc -DMLT_TESTING=1 -o tests/latency-test_test.o -c tests/latency-test_test.c
#link everything to run the tests.
gcc -DMLT_TESTING=1 -o tests/latency-test_test src/latency-test.o tests/latency-test_test.o
latency-test_test.c:
#include "../src/mlt.h"
#include "../src/latency-test.h"
void unit_test_get_ip(void)
{
int BATTERY_INPUT = 0, BATTERY_OUTPUT = 1;
char *battery_test[][NAME_MAX] = {
{ "localhost", "127.0.0.1" },
{ "127.0.0.1", "127.0.0.1" },
{ "dns.google", "8.8.8.8" },
};
int battery_size = sizeof(battery_test) / sizeof(battery_test[0]);
for (int battery = 0; battery < battery_size; battery++) {
char *result = latency_host_to_ip(battery_test[battery][BATTERY_INPUT]);
mlt_assert(result != NULL);
mlt_streq(result, battery_test[battery][BATTERY_OUTPUT]);
}
mlt_assert(latency_host_to_ip("sdadi.dwsf") == NULL);
}
int main(void)
{
mlt_start();
unit_test_get_ip();
mlt_finish();
}
Step 3: add the #ifndef
into your main()
function
Since we have two main()
functions, one for execute the tests and another to execute your code, to avoid change anything in your software all you need to do is add the #ifned
macro like that
src/main.c
#include <stdio.h>
// the main() will be ignored only in the tests.
#ifndef MLT_TESTING
int main(int argc, char **argv)
{
// DO YOUR CODE HERE
return 0;
}
#endif
Thats it! As you can see you will have a minimal effort and the MLT will be plugged into your code and ready to execute your tests, here is a sample output.
$ make test && ./tests/latency-test_test
:. Started test(s) on 'tests/latency-test_test.c' at Mon May 12 12:11:32 2025
return success in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 27,
test 'result != ((void *)0)'
return success in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 28,
test 'strncmp(result, battery_test[battery][BATTERY_OUTPUT], strlen(result)) == 0'
return success in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 27,
test 'result != ((void *)0)'
return success in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 28,
test 'strncmp(result, battery_test[battery][BATTERY_OUTPUT], strlen(result)) == 0'
return success in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 27,
test 'result != ((void *)0)'
return error in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 28,
test 'strncmp(result, battery_test[battery][BATTERY_OUTPUT], strlen(result)) == 0'
return success in 'tests/latency-test_test.c' on function 'unit_test_get_ip()' line 31,
test 'latency_host_to_ip("sdadi.dwsf") == ((void *)0)'
:. Result: FAILED, Time Elapsed: 0.213ms, Filename: 'tests/latency-test_test.c'
:. Tests run: 7, Tests PASSED: 6, Tests FAILED: 1
Let me know what you think, see you later! :-)