I'm working on something like a small quick testing class that will take a function/functor to execute, and function/functor that generates input to the function to execute.
For example if the first function is sqrt(x)
and the second one has a state and is doing counting, I want the class to
execute
sqrt(0.0);
sqrt(1.0);
...
for some number of iterations.
I can do that relatively easily (2 std::function
s or templates) but
how would one go about implementing that In case first function takes more than 1 argument? I can only think of forcing one function to take tuple
, and forcing another to return tuple
, but that looks ugly and forces interface on tested function.
EDIT:what I have now:
template <typename Func, typename ArgGen>
class Magic
{
std::function<double (time_t)> time_to_frequency;
std::set<decltype(Func()(ArgGen()()))> results_ok;
std::set<decltype(Func()(ArgGen()()))> results_fail;
ArgGen ag;
Func f;
public:
Magic(std::set<decltype(Func()(ArgGen()()))> results_ok, std::set<decltype(Func()(ArgGen()()))> results_fail, std::function<double (const time_t)> time_to_frequency) :
results_ok(results_ok), results_fail(results_fail), time_to_frequency(time_to_frequency)
{}
Magic()
{}
void run(const int n)
{
bool success=true;
for (int i=0; i< n; ++i)
{
auto rv = f(ag());
const bool result_ok = results_ok.count(rv);
if (result_ok)
{
printf(".");
}
else
{
success = false;
std::cout << i <<": value unexpected "<< std::endl;
}
}
std::cout << "run succeeded: " << std::boolalpha << success;
}
};