EDIT TITLE (I changed the title because it was wrongly referring to macros and compilation time, leading to confusion about my question)
In order to help with the output of my tests in c programs, I print the test number before each test. Something like this output :
[1/2] test :
// some test
[2/2] test :
// some test
the numeration of the test is in two parts :
[ <number of this test> / <total number of tests> ]
If I add a test or remove one, I don't have to hard-written the <number of this test>
, because it's an int that increment itself each time. But, I have to count the <total number of test>
and write it down manually (for example, in a macro, but that could be a variable as well).
here is how the code looks like :
#include <iostream>
#define N_TEST "2"
int main() {
int i = 0;
std::cout << "\n[" << i << "/" N_TEST "] test something :\n";
{
// some tests here
}
std::cout << "\n[" << i << "/" N_TEST "] test another thing :\n";
{
// some different tests here
}
return 0;
}
Is there a way to fill the <total number of tests>
automatically ?
CodePudding user response:
It is not clear why you are asking for a macro. If possible better avoid macros. As suggested in a comment, you can register tests in a container and once you know how many tests there are in total, you can print the total together with the running test number:
#include <vector>
#include <functional>
#include <iostream>
int main(int argc, char *argv[])
{
std::vector<std::function<void()>> tests;
tests.push_back(
[](){
std::cout << "hello test\n";
}
);
tests.push_back(
[](){
std::cout << "hello another test.\n";
}
);
int counter = 0;
for (const auto& test : tests){
std::cout << counter << "/" << tests.size() << " ";
test();
}
}
1/2 hello test
2/2 hello another test.