S
Siemel Naran
Hi. I have a file of test functions.
int test1() {
return 0;
}
int test2()
return 0;
}
There is another function that calls all the tests.
int doall() {
test1();
test2();
}
Problem is that if I add more tests, I may forget to update doall. Anyone
have any tricks to help me remember to update doall? (Of course, I can test
my code thoroughly, but I'd rather catch silly omissions earlier with the
compiler's help.)
Here is my present trick, but it uses macros.
#include <vector>
#include <numeric>
std::vector<int (*)()> testfuncs;
struct AddTest
{
AddTest(int (*func)())
{
testfuncs.push_back(func);
}
};
#define NEW_TEST(name) int name(); \
AddTest AddTest_##name(&name); \
int name()
struct callfunc
{
int operator()(int previousresult, int (*func)()) const
{
int newresult = (*func)();
return newresult + previousresult;
}
};
////////////////////////////////////////////////////////////////////////////
////
NEW_TEST(test1) {
return 0;
}
NEW_TEST(test2) {
return 0;
}
int doall()
{
int out = 0;
std::accumulate(testfuncs.begin(), testfuncs.end(), out, callfunc());
return out;
}
int test1() {
return 0;
}
int test2()
return 0;
}
There is another function that calls all the tests.
int doall() {
test1();
test2();
}
Problem is that if I add more tests, I may forget to update doall. Anyone
have any tricks to help me remember to update doall? (Of course, I can test
my code thoroughly, but I'd rather catch silly omissions earlier with the
compiler's help.)
Here is my present trick, but it uses macros.
#include <vector>
#include <numeric>
std::vector<int (*)()> testfuncs;
struct AddTest
{
AddTest(int (*func)())
{
testfuncs.push_back(func);
}
};
#define NEW_TEST(name) int name(); \
AddTest AddTest_##name(&name); \
int name()
struct callfunc
{
int operator()(int previousresult, int (*func)()) const
{
int newresult = (*func)();
return newresult + previousresult;
}
};
////////////////////////////////////////////////////////////////////////////
////
NEW_TEST(test1) {
return 0;
}
NEW_TEST(test2) {
return 0;
}
int doall()
{
int out = 0;
std::accumulate(testfuncs.begin(), testfuncs.end(), out, callfunc());
return out;
}