Lumiera  0.pre.03
»edit your freedom«
util-floorwrap-test.cpp
Go to the documentation of this file.
1 /*
2  UtilFloorwrap(Test) - verify integer scale remainder wrapping
3 
4  Copyright (C) Lumiera.org
5  2011, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/format-string.hpp"
31 #include "lib/format-cout.hpp"
32 #include "lib/util-quant.hpp"
33 
34 #include <cmath>
35 #include <boost/lexical_cast.hpp>
36 
37 using ::Test;
38 using boost::lexical_cast;
39 using util::typeStr;
40 using util::floorwrap;
41 
42 
43 namespace util {
44 namespace test {
45 
46 
47 
48 
49 
50  /***********************************************************************/
61  class UtilFloorwrap_test : public Test
62  {
63 
64  virtual void
65  run (Arg arg)
66  {
67  int range = 0 < arg.size()? lexical_cast<int> (arg[0]) : 12;
68  int scale = 1 < arg.size()? lexical_cast<int> (arg[1]) : 4;
69 
70  checkWrap (range, scale);
71  checkWrap (range, -scale);
72  checkWrap<long> (range, scale);
73  checkWrap<long> (range, -scale);
74  }
75 
76 
77  template<typename I>
78  void
79  checkWrap (I range, I scale)
80  {
81  cout << "--------"<< typeStr<I>()
82  << "--------"<< range<<"/"<<scale<<endl;
83  for (I i=range; i >=-range; --i)
84  showWrap (i, scale);
85  }
86 
87  template<typename I>
88  void
89  showWrap (I val, I scale)
90  {
91  IDiv<I> wrap = floorwrap(val,scale);
92  cout << _Fmt ("% 3d /% 1d =% 1d %% =% d floor=% 4.1f wrap = (%2d,%2d)\n")
93  % val % scale % (val/scale)
94  % (val%scale) % floor(double(val)/scale)
95  % wrap.quot % wrap.rem;
96  }
97  };
98 
99 
100 
101 
102  LAUNCHER (UtilFloorwrap_test, "unit common");
103 
104 
105 }} // namespace util::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
helper to treat int or long division uniformly
Definition: util-quant.hpp:44
Front-end for printf-style string template interpolation.
IDiv< I > floorwrap(I num, I den)
scale wrapping operation.
Definition: util-quant.hpp:121
A front-end for using printf-style formatting.
Simple test class runner.
Utilities for quantisation (grid alignment) and comparisons.
A collection of frequently used helper functions to support unit testing.