Lumiera  0.pre.03
»edit your freedom«
test-helper-test.cpp
Go to the documentation of this file.
1 /*
2  TestHelper(Test) - validate the unittest support functions
3 
4  Copyright (C) Lumiera.org
5  2009, 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/time/timevalue.hpp"
31 #include "lib/error.hpp"
32 #include "lib/util-foreach.hpp"
33 #include "lib/format-cout.hpp"
34 
35 #include <boost/algorithm/string.hpp>
36 #include <functional>
37 #include <string>
38 
39 using util::for_each;
40 using lumiera::Error;
41 using LERR_(EXCEPTION);
42 using LERR_(ASSERTION);
43 using lib::time::TimeVar;
44 using lib::time::Time;
45 
46 using boost::algorithm::is_lower;
47 using boost::algorithm::is_digit;
48 using std::function;
49 using std::string;
50 
51 
52 namespace lib {
53 namespace test{
54 namespace test{
55 
56  template<class T>
57  class Wrmrmpft
58  {
59  T tt_;
60  };
61 
62  struct Murpf { };
63 
64 
65  void doThrow() { throw Error("because I feel like it"); }
66  int dontThrow() { return 2+2; }
67 
68 
69  /*********************************************/
74  class TestHelper_test : public Test
75  {
76  void
77  run (Arg)
78  {
79  checkGarbageStr();
80  checkTypeDisplay();
81  checkThrowChecker();
82  checkLocalManipulation();
83  }
84 
85 
87  void
89  {
90  std::cout << "Displaying types and sizes....\n";
91 
92  typedef Wrmrmpft<Murpf> Wrmpf1;
93  typedef Wrmrmpft<char[2]> Wrmpf2;
94  typedef Wrmrmpft<char[3]> Wrmpf3;
95 
96  Wrmpf1 rmpf1;
97  Wrmpf2 rmpf2;
98  Wrmpf3 rmpf3;
99  Murpf murpf;
100 
101  CHECK (1 == sizeof (rmpf1));
102  CHECK (2 == sizeof (rmpf2));
103  CHECK (3 == sizeof (rmpf3));
104 
105  cout << showSizeof<char>("just a char") << endl;
106  cout << showSizeof(murpf) << endl;
107  cout << showSizeof(rmpf1) << endl;
108  cout << showSizeof(rmpf2) << endl;
109  cout << showSizeof<Wrmpf3>() << endl;
110  cout << showSizeof(size_t(42),
111  string{"Universe"}) << endl;
112 
113  // all the following ways to refer to an object are equivalent...
114  Wrmpf1 *p1 = &rmpf1;
115  Wrmpf1 *p2 = 0;
116  cout << showSizeof(p1) << endl;
117  cout << showSizeof(p2) << endl;
118 
119  Wrmpf1 & r = rmpf1;
120  Wrmpf1 const& cr = r;
121  Wrmpf1 const* cp = &r;
122 
123  cout << showSizeof(r) << endl;
124  cout << showSizeof(cr) << endl;
125  cout << showSizeof(cp) << endl;
126  }
127 
128 
129 
130 
131  void
132  checkGarbageStr()
133  {
134  string garN = randStr(0);
135  CHECK (0 == garN.size());
136 
137  typedef function<bool(string::value_type)> ChPredicate;
138  ChPredicate is_OK (is_lower() || is_digit());
139 
140  string garM = randStr(1000000);
141  for_each (garM, is_OK);
142 
143  cout << randStr(80) << endl;
144  }
145 
146 
150  void
152  {
153  // verify the exception is indeed raised
154  VERIFY_ERROR (EXCEPTION, doThrow() );
155 
156 #if false
157  // and when actually no exception is raised, this is an ASSERTION failure
158  VERIFY_ERROR (ASSERTION, VERIFY_ERROR (EXCEPTION, dontThrow() ));
159 #endif
160  }
161 
162 
166  void
168  {
169  int equilibrium = 42;
170  {
171  // manipulate the value temporarily...
172  TRANSIENTLY(equilibrium) = 49;
173 
174  CHECK (49 == equilibrium);
175  }
176  CHECK (42 == equilibrium);
177 
178 
179  TimeVar day_of_reckoning{Time{555,5}};
180  try
181  {
182  TRANSIENTLY(equilibrium) = 55;
183  TRANSIENTLY(day_of_reckoning) = Time::ANYTIME;
184 
185  CHECK (55 == equilibrium);
186  CHECK (Time::ANYTIME == day_of_reckoning);
187  throw "RRRrrevenge!!!!!!!!!!!!!!!!1!!11!!";
188  }
189  catch(...) { }
190  CHECK (42 == equilibrium);
191  CHECK (Time(555,5) == day_of_reckoning);
192 
193 
194  { // can also use λ for manipulation and clean-up
195  TRANSIENTLY ([&]{ day_of_reckoning *= 2; })
196  .cleanUp ([&]{ equilibrium /= 2; });
197 
198  CHECK (42 == equilibrium); // not yet touched...
199  CHECK (Time(110,11) == day_of_reckoning);
200  }
201  CHECK (Time(110,11) == day_of_reckoning);
202  CHECK (21 == equilibrium);
203  }
204  };
205 
206  LAUNCHER (TestHelper_test, "unit common");
207 
208 
209 }}} // namespace lib::test::test
210 
static const Time ANYTIME
border condition marker value. ANYTIME <= any time value
Definition: timevalue.hpp:322
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:241
Automatically use custom string conversion in C++ stream output.
#define TRANSIENTLY(_OO_)
Macro to simplify capturing assignments.
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
string showSizeof(size_t siz, string name)
for printing sizeof().
Definition: test-helper.cpp:57
Implementation namespace for support and library code.
string randStr(size_t len)
create garbage string of given length
Definition: test-helper.cpp:69
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:308
verifies the proper working of helper functions frequently used within the Lumiera testsuite...
Simple test class runner.
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
A collection of frequently used helper functions to support unit testing.
Lumiera error handling (C++ interface).
a family of time value like entities and their relationships.
Perform operations "for each element" of a collection.
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:71