proto-1.0 203: Fix level::arith

Il faudrait qu'on voie ce qu'on fait pour oln/level/arith.hh : plus() est écrit à l'aide d'un objet fonction, alors que les autres utilisent les opérateurs de oln/ops/arith.hh. Par ailleurs, je me demande s'il n'y a pas de duplication de code dans oln/core/apply.hh : apply() (et apply2()) sont définis à la fois pour les objets fonctions de Metalic (mlc/fun.hh) et ceux d'Olena (oln/funobj). https://svn.lrde.epita.fr/svn/oln/prototypes/proto-1.0 ChangeLog | 10 ++++ oln/level/arith.hh | 11 ++--- tests/level/tests/arith | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 5 deletions(-) Index: olena/ChangeLog from Roland Levillain <roland@lrde.epita.fr> Fix level::arith. * oln/level/arith.hh (oln/core/apply.hh): Include it. (f_plus): Rename as... (plus): ...this. (minus, times, div, mod): Fix the operators used in these functions. * tests/level/tests/arith: New test. Index: olena/oln/level/arith.hh --- olena/oln/level/arith.hh (revision 202) +++ olena/oln/level/arith.hh (working copy) @@ -32,6 +32,7 @@ # include <oln/core/ch_value_type.hh> # include <oln/funobj/arith.hh> +# include <oln/core/apply.hh> # include <oln/ops/arith.hh> # include <oln/utils/clone.hh> @@ -111,7 +112,7 @@ template <typename I1, typename I2> oln_arith_output(plus, I1, I2) - f_plus (const abstract::image<I1>& input1, + plus (const abstract::image<I1>& input1, const abstract::image<I2>& input2) { // FIXME: recording(?) @@ -162,7 +163,7 @@ precondition(input1.size() == input2.size()); oln_arith_output(minus,I1,I2) output("output"); - output = clone( input1 + input2 ); + output = clone( input1 - input2 ); exiting("level::minus"); return output; @@ -185,7 +186,7 @@ precondition(input1.size() == input2.size()); oln_arith_output(times,I1,I2) output("output"); - output = clone( input1 + input2 ); + output = clone( input1 * input2 ); exiting("level::times"); return output; @@ -208,7 +209,7 @@ precondition(input1.size() == input2.size()); oln_arith_output(div,I1,I2) output("output"); - output = clone( input1 + input2 ); + output = clone( input1 / input2 ); exiting("level::div"); return output; @@ -231,7 +232,7 @@ precondition(input1.size() == input2.size()); oln_arith_output(mod,I1,I2) output("output"); - output = clone( input1 + input2 ); + output = clone( input1 % input2 ); exiting("level::mod"); return output; Index: olena/tests/level/tests/arith --- olena/tests/level/tests/arith (revision 0) +++ olena/tests/level/tests/arith (revision 0) @@ -0,0 +1,97 @@ + // -*- C++ -*- +#include <ntg/int.hh> +#include <oln/basics2d.hh> +#include <oln/level/arith.hh> +#include <oln/level/fill.hh> +#include <oln/level/compare.hh> + +using namespace oln; + + +// FIXME: Not safe, nor generic. Re-introduce static initialization +// in Olena and use it instead. +template<typename T> +image2d<T> +image_from_array(coord_t nrows, coord_t ncols, T values[]) +{ + image2d<T> output(nrows, ncols); + unsigned i = 0; + oln_type_of(image2d<T>, fwd_piter) p(output.size()); + for_all_p (p) + { + output[p] = values[i]; + ++i; + } + return output; +} + + +bool check() +{ + typedef ntg::int_s<32, ntg::unsafe> value_type; + + image2d<value_type> ima1(3, 3); + ima1(0, 0) = 0; ima1(0, 1) = 1; ima1(0, 2) = 2; + ima1(1, 0) = 3; ima1(1, 1) = 4; ima1(1, 2) = 5; + ima1(2, 0) = 6; ima1(2, 1) = 7; ima1(2, 2) = 8; + + image2d<value_type> ima2(3, 3); + level::fill(ima2, 3); + + // Checking level::plus. + { + image2d<value_type> sum = level::plus(ima1, ima2); + value_type sum_ref[] = { 3, 4, 5, + 6, 7, 8, + 9, 10, 11 }; + if (not level::is_equal(sum, + image_from_array<value_type>(3, 3, sum_ref))) + return true; + } + + // Checking level::minus. + { + image2d<value_type> diff = level::minus(ima1, ima2); + value_type diff_ref[] = { -3, -2, -1, + 0, 1, 2, + 3, 4, 5 }; + if (not level::is_equal(diff, + image_from_array<value_type>(3, 3, diff_ref))) + return true; + } + + // Checking level::times. + { + image2d<value_type> product = level::times(ima1, ima2); + value_type product_ref[] = { 0, 3, 6, + 9, 12, 15, + 18, 21, 24 }; + if (not level::is_equal(product, + image_from_array<value_type>(3, 3, product_ref))) + return true; + } + + // Checking level::div. + { + image2d<value_type> quotient = level::div(ima1, ima2); + value_type quotient_ref[] = { 0, 0, 0, + 1, 1, 1, + 2, 2, 2 }; + if (not level::is_equal(quotient, + image_from_array<value_type>(3, 3, quotient_ref))) + return true; + } + + // Checking level::mod. + { + image2d<value_type> remain = level::mod(ima1, ima2); + value_type remain_ref[] = { 0, 1, 2, + 0, 1, 2, + 0, 1, 2 }; + if (not level::is_equal(remain, + image_from_array<value_type>(3, 3, remain_ref))) + return true; + } + + return false; +}
participants (1)
-
Roland Levillain