Utils: Fix Statistics unit-test float compare glitch
g0dil [Wed, 30 Sep 2009 08:09:40 +0000 (08:09 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1466 270642c3-0616-0410-b53a-bc976706d245

senf/Utils/Statistics.cc
senf/Utils/Statistics.test.cc

index 6e6eb18..b9956fb 100644 (file)
@@ -305,8 +305,6 @@ prefix_ void senf::Collector::enter(float min, float avg, float max, float dev)
     accSumSq_ += avg*avg + dev*dev;
     if (min < accMin_) accMin_ = min;
     if (max > accMax_) accMax_ = max;
-//    std::cerr << "! " << i_ << ' ' << avg << ' ' << dev << ' ' << accSum_ << ' ' << accSumSq_ 
-//              << std::endl;
     if (++i_ >= rank_) {
         float accAvg (accSum_ / i_);
         float accDev (std::sqrt(accSumSq_ / i_ - accAvg*accAvg));
index 638783b..fb10b1f 100644 (file)
@@ -29,6 +29,9 @@
 // Custom includes
 #include "Statistics.hh"
 #include "StatisticsTargets.hh"
+#include <boost/tokenizer.hpp>
+#include <boost/iterator/transform_iterator.hpp>
+#include <boost/iterator/filter_iterator.hpp>
 
 #include "auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
@@ -47,6 +50,54 @@ namespace {
             { return arg.rank(); }
     };
 
+
+    struct splitFloats
+    {
+        typedef boost::char_separator<char> Separator;
+        typedef boost::tokenizer<Separator> Tokenizer;
+        struct Pred {
+            bool operator()(std::string const & s) const
+                { try { boost::lexical_cast<double>(s); return true; } 
+                    catch (std::bad_cast &) { return false; } }
+        };
+        typedef boost::filter_iterator<Pred,Tokenizer::iterator> FilterIterator;
+        struct Trafo {
+            typedef double result_type;
+            result_type operator()(std::string const & s) const
+                { return boost::lexical_cast<double>(s); }
+        };
+        typedef boost::transform_iterator<Trafo,FilterIterator> TransformIterator;
+
+        typedef TransformIterator iterator;
+        typedef TransformIterator const_iterator;
+        
+        splitFloats(std::string const & s) : s_ (s), sep_ (" \n"), tok_ (s_, sep_) {}
+        TransformIterator begin() const { return TransformIterator(FilterIterator(tok_.begin())); }
+        TransformIterator end() const { return TransformIterator(FilterIterator(tok_.end())); }
+
+        std::string s_;
+        Separator sep_;
+        Tokenizer tok_;
+    };
+
+    template <class R1, class R2>
+    void CHECK_CLOSE_RANGES(R1 const & r1, R2 const & r2, double delta)
+    {
+        typedef typename boost::range_const_iterator<R1>::type I1;
+        typedef typename boost::range_const_iterator<R2>::type I2;
+
+        I1 i1 (boost::begin(r1));
+        I1 i1_end (boost::end(r1));
+        I2 i2 (boost::begin(r2));
+        I2 i2_end (boost::end(r2));
+
+        for (; i1 != i1_end && i2 != i2_end; ++i1, ++i2) {
+            BOOST_CHECK_CLOSE(*i1, *i2, delta);
+        }
+
+        BOOST_CHECK( i1 == i1_end );
+        BOOST_CHECK( i2 == i2_end );
+    }
 }
 
 BOOST_AUTO_UNIT_TEST(statistics)
@@ -106,67 +157,67 @@ BOOST_AUTO_UNIT_TEST(statistics)
     for (unsigned i (0); i < sizeof(values)/sizeof(values[0]); ++i)
         stats(values[i][0], values[i][1], values[i][2]);
 
-    BOOST_CHECK_CLOSE( stats.min(), 0.4f, .1f );
-    BOOST_CHECK_CLOSE( stats.avg(), 1.2f, .1f );
-    BOOST_CHECK_CLOSE( stats.max(), 4.0f, .1f );
-    BOOST_CHECK_CLOSE( stats.dev(), 0.0f, .1f );
-
-    BOOST_CHECK_CLOSE( stats[4].min(), -2.0f, .1f );
-    BOOST_CHECK_CLOSE( stats[4].avg(), -0.05f, .1f );
-    BOOST_CHECK_CLOSE( stats[4].max(), 1.1f, .1f );
-    BOOST_CHECK_CLOSE( stats[4].dev(), 1.08282f, .1f );
-
-    BOOST_CHECK_CLOSE( stats[4][3].min(), -2.0f, .1f );
-    BOOST_CHECK_CLOSE( stats[4][3].avg(), 1.15f, .1f );
-    BOOST_CHECK_CLOSE( stats[4][3].max(),  3.8f, .1f );
-
-    BOOST_CHECK_EQUAL( statslog.str(), 
-                       "level0 -1 2.3 2.5 0\n"
-                       "level0 -0.35 2.35 3.15 0\n"
-                       "level0 -0.4 1.05 1.9 0\n"
-                       "level0 -0.7 1.45 1.65 0\n"
-                       "level1 -1.1 1.9 3.8 1.31719\n"
-                       "averaged1 -1.1 1.9 3.8 1.31719\n"
-                       "level0 0.35 2.15 2.2 0\n"
-                       "level0 0.75 0.8 0.8 0\n"
-                       "level0 0.25 0.25 0.25 0\n"
-                       "level0 -1 -0.9 -0.5 0\n"
-                       "level1 -2 -0.05 1.1 1.08282\n"
-                       "averaged1 -1.55 0.925 2.45 1.20001\n"
-                       "level0 -1 0.25 0.7 0\n"
-                       "level0 0.2 1.75 2.2 0\n"
-                       "level0 -0.3 1.75 2.25 0\n"
-                       "level0 -0.35 2.35 3.15 0\n"
-                       "level1 -1 2.05 3.8 0.492442\n"
-                       "averaged1 -1.36667 1.3 2.9 0.964152\n"
-                       "level0 -0.4 1.05 1.9 0\n"
-                       "level0 -0.7 1.45 1.65 0\n"
-                       "level0 0.35 2.15 2.2 0\n"
-                       "level0 0.75 0.8 0.8 0\n"
-                       "level1 -1.1 1.125 3.3 1.29687\n"
-                       "averaged1 -1.36667 1.04167 2.73333 0.957378\n"
-                       "level0 0.25 0.25 0.25 0\n"
-                       "level0 -1 -0.9 -0.5 0\n"
-                       "level0 -1 0.25 0.7 0\n"
-                       "level0 0.2 1.75 2.2 0\n"
-                       "level1 -2 0.425 2.4 1.52049\n"
-                       "averaged1 -1.36667 1.2 3.16667 1.10327\n"
-                       "level0 -0.3 1.75 2.25 0\n"
-                       "level0 -0.35 2.35 3.15 0\n"
-                       "level0 -0.4 1.05 1.9 0\n"
-                       "level0 -0.7 1.45 1.65 0\n"
-                       "level1 -1.1 1.9 3.8 1.31719\n"
-                       "averaged1 -1.4 1.15 3.16667 1.37818\n"
-                       "level3 -2 1.225 3.8 1.45752\n"
-                       "level0 0.35 2.15 2.2 0\n"
-                       "level0 0.75 0.8 0.8 0\n"
-                       "level0 0.25 0.25 0.25 0\n"
-                       "level0 -1 -0.9 -0.5 0\n"
-                       "level1 -2 -0.05 1.1 1.08282\n"
-                       "averaged1 -1.7 0.758333 2.43333 1.30683\n"
-                       "level0 -1 0.25 0.7 0\n"
-                       "level0 0.2 1.75 3.2 0\n" );
-
+    BOOST_CHECK_CLOSE( stats.min(), 0.4f, .001f );
+    BOOST_CHECK_CLOSE( stats.avg(), 1.2f, .001f );
+    BOOST_CHECK_CLOSE( stats.max(), 4.0f, .001f );
+    BOOST_CHECK_CLOSE( stats.dev(), 0.0f, .001f );
+
+    BOOST_CHECK_CLOSE( stats[4].min(), -2.0f, .001f );
+    BOOST_CHECK_CLOSE( stats[4].avg(), -0.05f, .001f );
+    BOOST_CHECK_CLOSE( stats[4].max(), 1.1f, .001f );
+    BOOST_CHECK_CLOSE( stats[4].dev(), 1.08282f, .001f );
+
+    BOOST_CHECK_CLOSE( stats[4][3].min(), -2.0f, .001f );
+    BOOST_CHECK_CLOSE( stats[4][3].avg(), 1.15f, .001f );
+    BOOST_CHECK_CLOSE( stats[4][3].max(),  3.8f, .001f );
+
+    CHECK_CLOSE_RANGES( splitFloats(statslog.str()),
+                        splitFloats("level0 -1 2.3 2.5 0\n"
+                                    "level0 -0.35 2.35 3.15 0\n"
+                                    "level0 -0.4 1.05 1.9 0\n"
+                                    "level0 -0.7 1.45 1.65 0\n"
+                                    "level1 -1.1 1.9 3.8 1.31719\n"
+                                    "averaged1 -1.1 1.9 3.8 1.31719\n"
+                                    "level0 0.35 2.15 2.2 0\n"
+                                    "level0 0.75 0.8 0.8 0\n"
+                                    "level0 0.25 0.25 0.25 0\n"
+                                    "level0 -1 -0.9 -0.5 0\n"
+                                    "level1 -2 -0.05 1.1 1.08282\n"
+                                    "averaged1 -1.55 0.925 2.45 1.20001\n"
+                                    "level0 -1 0.25 0.7 0\n"
+                                    "level0 0.2 1.75 2.2 0\n"
+                                    "level0 -0.3 1.75 2.25 0\n"
+                                    "level0 -0.35 2.35 3.15 0\n"
+                                    "level1 -1 2.05 3.8 0.492442\n"
+                                    "averaged1 -1.36667 1.3 2.9 0.964152\n"
+                                    "level0 -0.4 1.05 1.9 0\n"
+                                    "level0 -0.7 1.45 1.65 0\n"
+                                    "level0 0.35 2.15 2.2 0\n"
+                                    "level0 0.75 0.8 0.8 0\n"
+                                    "level1 -1.1 1.125 3.3 1.29687\n"
+                                    "averaged1 -1.36667 1.04167 2.73333 0.957378\n"
+                                    "level0 0.25 0.25 0.25 0\n"
+                                    "level0 -1 -0.9 -0.5 0\n"
+                                    "level0 -1 0.25 0.7 0\n"
+                                    "level0 0.2 1.75 2.2 0\n"
+                                    "level1 -2 0.425 2.4 1.52049\n"
+                                    "averaged1 -1.36667 1.2 3.16667 1.10327\n"
+                                    "level0 -0.3 1.75 2.25 0\n"
+                                    "level0 -0.35 2.35 3.15 0\n"
+                                    "level0 -0.4 1.05 1.9 0\n"
+                                    "level0 -0.7 1.45 1.65 0\n"
+                                    "level1 -1.1 1.9 3.8 1.31719\n"
+                                    "averaged1 -1.4 1.15 3.16667 1.37818\n"
+                                    "level3 -2 1.225 3.8 1.45752\n"
+                                    "level0 0.35 2.15 2.2 0\n"
+                                    "level0 0.75 0.8 0.8 0\n"
+                                    "level0 0.25 0.25 0.25 0\n"
+                                    "level0 -1 -0.9 -0.5 0\n"
+                                    "level1 -2 -0.05 1.1 1.08282\n"
+                                    "averaged1 -1.7 0.758333 2.43333 1.30683\n"
+                                    "level0 -1 0.25 0.7 0\n"
+                                    "level0 0.2 1.75 3.2 0\n"),
+                        0.001f );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////