* tests/data/transform-lambda.cc (mln::fun::C< R (*)(A1, A2) >): New explicit class template specialization. (mln::category< R (*)(A1, A2) >): Likewise. (main): Use `mln::test::predicate' and a binary lambda to check the output instead of a hand-made browsing. --- milena/ChangeLog | 10 ++++++ milena/tests/data/transform-lambda.cc | 55 ++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-)
diff --git a/milena/ChangeLog b/milena/ChangeLog index a4f8dfb..e8c17cd 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,5 +1,15 @@ 2012-05-22 Roland Levillain roland@lrde.epita.fr
+ Exercise mln::transform with lambdas with more than one argument. + + * tests/data/transform-lambda.cc (mln::fun::C< R (*)(A1, A2) >): + New explicit class template specialization. + (mln::category< R (*)(A1, A2) >): Likewise. + (main): Use `mln::test::predicate' and a binary lambda to check + the output instead of a hand-made browsing. + +2012-05-22 Roland Levillain roland@lrde.epita.fr + Simplify and generalize the wrapping of lambdas in Milena functions.
* tests/data/transform-lambda.cc (first_arg_of, return_of): diff --git a/milena/tests/data/transform-lambda.cc b/milena/tests/data/transform-lambda.cc index 1873b16..242a3e8 100644 --- a/milena/tests/data/transform-lambda.cc +++ b/milena/tests/data/transform-lambda.cc @@ -36,6 +36,55 @@ #include <mln/data/transform.hh> #include <mln/debug/iota.hh>
+#include <mln/test/predicate.hh> + + +/*---------------------------------------------------. +| Extension of mln::fun::C to 2-argument functions. | +`---------------------------------------------------*/ + +// FIXME: To be completed, revamped and moved into Milena. +namespace mln +{ + + /// Category declaration for a unary C function. + template <typename R, typename A1, typename A2> + struct category< R (*)(A1, A2) > + { + typedef C_Function<void> ret; + }; + + namespace fun + { + + /// Wrapper of a pointer to a 2-argument function into a Milena + /// function. + template <typename R, typename A1, typename A2> + struct C< R (*)(A1, A2) > + : + // FIXME: Hard-coded base class, so as to make this part of the + // code as small as possible for this test. + // + // This functor should inherit from either `Function_vv2b' or + // `Function_vv2b', depending on the result type. See how it is + // implemented in `C< R (*)(A) >' (located in mln/fun/c.hh). + Function_vv2b< C< R (*)(A1, A2) > > + { + C() {} + C(R (*f)(A1, A2)) : f_(f) {} + typedef R result; + R operator()(const mlc_unqualif(A1)& a, const mlc_unqualif(A2)& b) const + { + return f_(a, b); + } + protected: + R (*f_)(A1, A2); + }; + + } // end of namespace mln::fun + +} // end of namespace mln::fun +
/*----------------. | Lambda traits. | @@ -92,8 +141,6 @@ main()
I output = data::transform(input, to_fun([](V x) { return x * x; }));
- // FIXME: Or use mln::test instead? - mln_piter_(I) p(output.domain()); - for_all(p) - mln_assertion(output(p) = math::sqr(input(p))); + mln_assertion(test::predicate(output, input, + to_fun([](V x, V y) { return x == y * y; }))); }