Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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"
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
30using util::for_each;
31using lumiera::Error;
32using LERR_(EXCEPTION);
33using LERR_(ASSERTION);
35using lib::time::Time;
36
37using boost::algorithm::is_lower;
38using boost::algorithm::is_digit;
39using std::function;
40using std::string;
41
42
43namespace lib {
44namespace test{
45namespace test{
46
47 template<class T>
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
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
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() or 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
verifies the proper working of helper functions frequently used within the Lumiera testsuite.
a mutable time value, behaving like a plain number, allowing copy and re-accessing
Lumiera's internal time value datatype.
static const Time ANYTIME
border condition marker value. ANYTIME <= any time value
Interface and Base definition for all Lumiera Exceptions.
Definition error.hpp:65
Lumiera error handling (C++ interface).
#define LERR_(_NAME_)
Definition error.hpp:45
Automatically use custom string conversion in C++ stream output.
string showSizeof(size_t siz, string name)
for printing sizeof().
string randStr(size_t len)
create garbage string of given length
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
disable_if< can_IterForEach< Container >, FUN > for_each(Container const &coll, FUN doIt)
operate on all elements of a STL container.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
a family of time value like entities and their relationships.
#define TRANSIENTLY(_OO_)
Macro to simplify capturing assignments.
Perform operations "for each element" of a collection.