
https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Roland Levillain <roland@lrde.epita.fr> Improve tests on norms. * tests/norm/l1.cc, tests/norm/l2.cc, tests/norm/linfty.cc: Revamp and factor common parts... * tests/norm/common.hh: ...here (new file). * tests/norm/Makefile.am (l1_SOURCES, l2_SOURCES, linfty_SOURCES): Add common.hh. * tests/norm_l2.cc: Remove. * tests/Makefile.am (SUBDIRS): Add fun. (check_PROGRAMS): Remove norm_l2.cc. * mln/norm/l1.hh (l1_distance), * mln/norm/l2.hh (l2_distance): Fix indentation. Makefile.am | 3 -- norm/Makefile.am | 6 ++--- norm/common.hh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ norm/l1.cc | 49 ++++++++++++++++++++++++---------------------- norm/l2.cc | 49 +++++++++++++++++++++++++--------------------- norm/linfty.cc | 47 ++++++++++++++++++++++---------------------- 6 files changed, 139 insertions(+), 73 deletions(-) Index: mln/norm/l1.hh Index: mln/norm/l2.hh Index: tests/norm/l1.cc --- tests/norm/l1.cc (revision 1549) +++ tests/norm/l1.cc (working copy) @@ -30,44 +30,47 @@ * \brief Test the L1-norm. */ -#include <cassert> +#include <tests/norm/common.hh> #include <mln/metal/vec.hh> -#include <mln/math/abs.hh> #include <mln/norm/l1.hh> -// FIXME: We should have a almost_equal function somewhere in Milena. -static const float epsilon = 0.0001; -using mln::metal::vec; -using mln::norm::l1; - -template <typename V> -void check_l1 (const V& vec1, const V& vec2) +namespace test { - assert (mln::math::abs(mln::norm::l1(vec1) - mln::norm::l1(vec2)) - < epsilon); -} - template <typename V, typename S> -void check_l1_distance (const V& vec1, const V& vec2, const S& ref_val) + void + check_l1_norm_and_distance(const V& vec1, const V& vec2, const S& ref_val) { - assert (mln::math::abs(mln::norm::l1_distance(vec1, vec2) - ref_val) - < epsilon); + // Pointer on mln::norm::l1. + typedef mln_sum_(int) (*l1_t)(const V&); + l1_t l1 = mln::norm::l1; + + test::check_norm(l1, vec1, vec2); + + // Pointer on mln::norm::l1_distance. + typedef mln_sum_(int) (*l1_distance_t)(const V&, const V&); + l1_distance_t l1_distance = mln::norm::l1_distance; + + test::check_distance(l1_distance, vec1, vec2, ref_val); + } } int main () { - vec<3, int> t; t.set (1, -2, 3); - vec<3, int> u; u.set (5, 1, 0); + typedef mln::metal::vec<3, int> vec_t; + + // Reference value. int d = (5 - 1) + (1 + 2) + 3; - check_l1(t, u); - check_l1_distance (t, u, d); + // Tests using mln::metal::vec. + vec_t t, u; + t.set(1, -2, 3); + u.set(5, 1, 0); + test::check_l1_norm_and_distance(t, u, d); + // Tests using plain arrays. int v[] = {1, -2, 3}; int w[] = {5, 1, 0}; - - check_l1(v, w); - check_l1_distance (v, w, d); + test::check_l1_norm_and_distance(v, w, d); } Index: tests/norm/l2.cc --- tests/norm/l2.cc (revision 1549) +++ tests/norm/l2.cc (working copy) @@ -33,44 +33,49 @@ #include <cmath> #include <cassert> +#include <tests/norm/common.hh> + #include <mln/metal/vec.hh> -#include <mln/math/abs.hh> #include <mln/norm/l2.hh> -// FIXME: We should have a almost_equal function somewhere in Milena. -static const float epsilon = 0.0001; - -using mln::metal::vec; -using mln::norm::l2; -template <typename V> -void check_l2 (const V& vec1, const V& vec2) +namespace test { - assert (mln::math::abs(mln::norm::l2(vec1) - mln::norm::l2(vec2)) - < epsilon); -} - template <typename V, typename S> -void check_l2_distance (const V& vec1, const V& vec2, const S& ref_val) + void + check_l2_norm_and_distance(const V& vec1, const V& vec2, const S& ref_val) { - assert (mln::math::abs(mln::norm::l2_distance(vec1, vec2) - ref_val) - < epsilon); + // Pointer on mln::norm::l2. + typedef mln_sum_(int) (*l2_t)(const V&); + l2_t l2 = mln::norm::l2; + + test::check_norm(l2, vec1, vec2); + + // Pointer on mln::norm::l2_distance. + typedef mln_sum_(int) (*l2_distance_t)(const V&, const V&); + l2_distance_t l2_distance = mln::norm::l2_distance; + + test::check_distance(l2_distance, vec1, vec2, ref_val); + } } int main () { - vec<3, int> t; t.set (2, -2, 3); - vec<3, int> u; u.set (4, 1, 0); + typedef mln::metal::vec<3, int> vec_t; + + // Reference value. float d = std::sqrt((4 - 2) * (4 - 2) + (1 + 2) * (1 + 2) + (0 - 3) * (0 - 3)); - check_l2(t, u); - check_l2_distance (t, u, d); + // Tests using mln::metal::vec. + vec_t t, u; + t.set(2, -2, 3); + u.set(4, 1, 0); + test::check_l2_norm_and_distance(t, u, d); + // Tests using plain arrays. int v[] = {2, -2, 3}; int w[] = {4, 1, 0}; - - check_l2(v, w); - check_l2_distance (v, w, d); + test::check_l2_norm_and_distance(v, w, d); } Index: tests/norm/linfty.cc --- tests/norm/linfty.cc (revision 1549) +++ tests/norm/linfty.cc (working copy) @@ -30,47 +30,48 @@ * \brief Test the L-infinity-norm. */ -#include <cmath> -#include <cassert> +#include <tests/norm/common.hh> #include <mln/metal/vec.hh> #include <mln/math/abs.hh> #include <mln/norm/linfty.hh> -// FIXME: We should have a almost_equal function somewhere in Milena. -static const float epsilon = 0.0001; -using mln::metal::vec; -using mln::norm::linfty; - -template <typename V> -void check_linfty (const V& vec1, const V& vec2) +namespace test { - assert (mln::math::abs(mln::norm::linfty(vec1) - mln::norm::linfty(vec2)) - < epsilon); -} - template <typename V, typename S> -void check_linfty_distance (const V& vec1, const V& vec2, const S& ref_val) + void + check_linfty_norm_and_distance(const V& vec1, const V& vec2, const S& ref_val) { - assert (mln::math::abs(mln::norm::linfty_distance(vec1, vec2) - ref_val) - < epsilon); + // Pointer on mln::norm::linfty. + typedef int (*linfty_t)(const V&); + linfty_t linfty = mln::norm::linfty; + + test::check_norm(linfty, vec1, vec2); + + // Pointer on mln::norm::linfty_distance. + typedef int (*linfty_distance_t)(const V&, const V&); + linfty_distance_t linfty_distance = mln::norm::linfty_distance; + + test::check_distance(linfty_distance, vec1, vec2, ref_val); + } } int main () { - vec<3, int> t; t.set (2, -2, 4); - vec<3, int> u; u.set (4, 1, 0); + typedef mln::metal::vec<3, int> vec_t; + + // Reference value. float d = std::max(std::abs(4 - 2), std::max(std::abs(1 + 2), std::abs(0 - 4))); - check_linfty(t, u); - check_linfty_distance (t, u, d); + vec_t t, u; + t.set(2, -2, 4); + u.set(4, 1, 0); + test::check_linfty_norm_and_distance(t, u, d); int v[] = {2, -2, 4}; int w[] = {4, 1, 0}; - - check_linfty(v, w); - check_linfty_distance (v, w, d); + test::check_linfty_norm_and_distance(v, w, d); } Index: tests/norm/common.hh --- tests/norm/common.hh (revision 0) +++ tests/norm/common.hh (revision 0) @@ -0,0 +1,58 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef TESTS_NORM_COMMON_HH +# define TESTS_NORM_COMMON_HH + +#include <cassert> + +#include <mln/math/abs.hh> + +// FIXME: We should have a almost_equal function somewhere in Milena. +static const float epsilon = 0.0001; + +namespace test +{ + + template <typename Norm, typename V> + void + check_norm(const Norm norm, const V& vec1, const V& vec2) + { + assert(mln::math::abs(norm(vec1) - norm(vec2)) < epsilon); + } + + template <typename Distance, typename V, typename S> + void + check_distance (const Distance dist, const V& vec1, const V& vec2, + const S& ref_val) + { + assert (mln::math::abs(dist(vec1, vec2) - ref_val) < epsilon); + } + +} // End of namespace test. + +#endif // !TESTS_NORM_COMMON_HH Index: tests/norm/Makefile.am --- tests/norm/Makefile.am (revision 1549) +++ tests/norm/Makefile.am (working copy) @@ -7,8 +7,8 @@ l2 \ linfty -l1_SOURCES = l1.cc -l2_SOURCES = l2.cc -linfty_SOURCES = linfty.cc +l1_SOURCES = l1.cc common.hh +l2_SOURCES = l2.cc common.hh +linfty_SOURCES = linfty.cc common.hh TESTS = $(check_PROGRAMS) Index: tests/Makefile.am --- tests/Makefile.am (revision 1549) +++ tests/Makefile.am (working copy) @@ -9,6 +9,7 @@ canvas \ display \ draw \ + fun \ histo \ level \ logical \ @@ -103,8 +104,6 @@ morpho_opening_area \ morpho_thinning \ \ - norm_l2 \ - \ pixel \ pixter1d \ pixter2d \
participants (1)
-
Roland Levillain