diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a6d99391ee370b40ca0680b572b426040c37df36..724d35d257ffb59863fdc6d1e4795fafea136612 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,3 +40,4 @@ ENDMACRO(ADD_TESTCASE) ADD_TESTCASE (test-successstatistics FALSE) ADD_TESTCASE (test-distribution FALSE) +ADD_TESTCASE (test-statistics FALSE) diff --git a/tests/test-statistics.cc b/tests/test-statistics.cc new file mode 100644 index 0000000000000000000000000000000000000000..26b52e1c627bd070a304afd0c9bc62cb098921b1 --- /dev/null +++ b/tests/test-statistics.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2014, LAAS-CNRS +// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) +// +// This file is part of hpp-statistics. +// hpp-statistics is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation, either version +// 3 of the License, or (at your option) any later version. +// +// hpp-statistics 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 Lesser Public License for more details. You should have +// received a copy of the GNU Lesser General Public License along with +// hpp-statistics. If not, see <http://www.gnu.org/licenses/>. + +#include <iostream> +#include <time.h> +#include <stdlib.h> +#include <list> + +#include "hpp/statistics/bin.hh" + +using hpp::statistics::Bin; +using hpp::statistics::Statistics; + +class TestBin : public Bin { + public: + int val; + std::list < double > l; + + TestBin (int i) : val (i) {} + + std::ostream& printValue (std::ostream& os) const + { + os << val << " has :"; + for (std::list <double>::const_iterator it = l.begin (); + it != l.end (); it++) + os << " " << *it << std::endl; + return os << std::endl; + } + + bool operator< (const TestBin& rhs) const { + return val < rhs.val; + } + + bool operator== (const TestBin& rhs) const { + return val == rhs.val; + } +}; + +class TestStatistics : public Statistics <TestBin> { + public: + void add (int n, double d) { + TestBin b(n); + TestStatistics::iterator it = insert (b); + it->l.push_back (d); + } +}; + +int main () +{ + srand (time(NULL)); + + TestStatistics t; + double *check = new double [100]; + for (int i = 0; i < 100; i++) { + check[i] = ((double)(rand())/(double)rand()); + t.add (i, check[i]); + } + for (int i = 0; i < 100; i++) { + TestStatistics::const_iterator it = t.find (i); + if (it == t.end ()) + return EXIT_FAILURE; + if (it->l.size () != 1 && check[i] != it->l.front()) + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +}