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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/time/timevalue.hpp"
22 #include "lib/error.hpp"
23 #include "lib/util-foreach.hpp"
24 #include "lib/format-cout.hpp"
25 
26 #include <boost/algorithm/string.hpp>
27 #include <functional>
28 #include <string>
29 
30 using util::for_each;
31 using lumiera::Error;
32 using LERR_(EXCEPTION);
33 using LERR_(ASSERTION);
34 using lib::time::TimeVar;
35 using lib::time::Time;
36 
37 using boost::algorithm::is_lower;
38 using boost::algorithm::is_digit;
39 using std::function;
40 using std::string;
41 
42 
43 namespace lib {
44 namespace test{
45 namespace test{
46 
47  template<class T>
48  class Wrmrmpft
49  {
50  T tt_;
51  };
52 
53  struct Murpf { };
54 
55 
56  void doThrow() { throw Error("because I feel like it"); }
57  int dontThrow() { return 2+2; }
58 
59 
60  /*********************************************/
65  class TestHelper_test : public Test
66  {
67  void
68  run (Arg)
69  {
70  checkGarbageStr();
71  checkTypeDisplay();
72  checkThrowChecker();
73  checkLocalManipulation();
74  }
75 
76 
78  void
80  {
81  std::cout << "Displaying types and sizes....\n";
82 
83  typedef Wrmrmpft<Murpf> Wrmpf1;
84  typedef Wrmrmpft<char[2]> Wrmpf2;
85  typedef Wrmrmpft<char[3]> Wrmpf3;
86 
87  Wrmpf1 rmpf1;
88  Wrmpf2 rmpf2;
89  Wrmpf3 rmpf3;
90  Murpf murpf;
91 
92  CHECK (1 == sizeof (rmpf1));
93  CHECK (2 == sizeof (rmpf2));
94  CHECK (3 == sizeof (rmpf3));
95 
96  cout << showSizeof<char>("just a char") << endl;
97  cout << showSizeof(murpf) << endl;
98  cout << showSizeof(rmpf1) << endl;
99  cout << showSizeof(rmpf2) << endl;
100  cout << showSizeof<Wrmpf3>() << endl;
101  cout << showSizeof(size_t(42),
102  string{"Universe"}) << endl;
103 
104  // all the following ways to refer to an object are equivalent...
105  Wrmpf1 *p1 = &rmpf1;
106  Wrmpf1 *p2 = 0;
107  cout << showSizeof(p1) << endl;
108  cout << showSizeof(p2) << endl;
109 
110  Wrmpf1 & r = rmpf1;
111  Wrmpf1 const& cr = r;
112  Wrmpf1 const* cp = &r;
113 
114  cout << showSizeof(r) << endl;
115  cout << showSizeof(cr) << endl;
116  cout << showSizeof(cp) << endl;
117  }
118 
119 
120 
121 
122  void
123  checkGarbageStr()
124  {
125  string garN = randStr(0);
126  CHECK (0 == garN.size());
127 
128  typedef function<bool(string::value_type)> ChPredicate;
129  ChPredicate is_OK (is_lower() || is_digit());
130 
131  string garM = randStr(1000000);
132  for_each (garM, is_OK);
133 
134  cout << randStr(80) << endl;
135  }
136 
137 
141  void
143  {
144  // verify the exception is indeed raised
145  VERIFY_ERROR (EXCEPTION, doThrow() );
146 
147 #if false
148  // and when actually no exception is raised, this is an ASSERTION failure
149  VERIFY_ERROR (ASSERTION, VERIFY_ERROR (EXCEPTION, dontThrow() ));
150 #endif
151  }
152 
153 
157  void
159  {
160  int equilibrium = 42;
161  {
162  // manipulate the value temporarily...
163  TRANSIENTLY(equilibrium) = 49;
164 
165  CHECK (49 == equilibrium);
166  }
167  CHECK (42 == equilibrium);
168 
169 
170  TimeVar day_of_reckoning{Time{555,5}};
171  try
172  {
173  TRANSIENTLY(equilibrium) = 55;
174  TRANSIENTLY(day_of_reckoning) = Time::ANYTIME;
175 
176  CHECK (55 == equilibrium);
177  CHECK (Time::ANYTIME == day_of_reckoning);
178  throw "RRRrrevenge!!!!!!!!!!!!!!!!1!!11!!";
179  }
180  catch(...) { }
181  CHECK (42 == equilibrium);
182  CHECK (Time(555,5) == day_of_reckoning);
183 
184 
185  { // can also use λ for manipulation and clean-up
186  TRANSIENTLY ([&]{ day_of_reckoning *= 2; })
187  .cleanUp ([&]{ equilibrium /= 2; });
188 
189  CHECK (42 == equilibrium); // not yet touched...
190  CHECK (Time(110,11) == day_of_reckoning);
191  }
192  CHECK (Time(110,11) == day_of_reckoning);
193  CHECK (21 == equilibrium);
194  }
195  };
196 
197  LAUNCHER (TestHelper_test, "unit common");
198 
199 
200 }}} // namespace lib::test::test
201 
static const Time ANYTIME
border condition marker value. ANYTIME <= any time value
Definition: timevalue.hpp:313
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:232
Automatically use custom string conversion in C++ stream output.
#define TRANSIENTLY(_OO_)
Macro to simplify capturing assignments.
Definition: run.hpp:40
#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:49
Implementation namespace for support and library code.
string randStr(size_t len)
create garbage string of given length
Definition: test-helper.cpp:61
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:299
verifies the proper working of helper functions frequently used within the Lumiera testsuite...
Simplistic 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:62