File: Test.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (396 lines) | stat: -rw-r--r-- 11,156 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#pragma once

#include "Utils/Utils.h"
#include "Utils/Exception.h"
#include "Utils/Object.h"

// The main header for the test framework. Gives the macros:
// VERIFY(condition) - aborts if not true
// CHECK(condition)
// CHECK_TITLE(condition, title)
// CHECK_EQ(expr, val)
// CHECK_NEQ(expr, val)
// CHECK_EQ_TITLE(expr, val, title)
// CHECK_NEQ_TITLE(expr, val, title)
// CHECK_ERROR(expr, exception)
// BEGIN_TEST(name)
// END_TEST
// And the types:
// TestResult
// Test

class TestResult {
public:
	TestResult() : total(0), failed(0), crashed(0), aborted(false) {}

	nat total;
	nat failed;
	nat crashed;
	bool aborted;

	inline nat passed() const {
		return total - failed - crashed;
	}

	inline TestResult &operator +=(const TestResult &other) {
		total += other.total;
		failed += other.failed;
		crashed += other.crashed;
		aborted |= other.aborted;
		return *this;
	}

	inline bool ok() const {
		return failed == 0 && crashed == 0 && !aborted;
	}

	friend std::wostream &operator <<(std::wostream &to, const TestResult &r);
};

inline std::wostream &operator <<(std::wostream &to, const TestResult &r) {
	to << L"Passed ";
	if (r.passed() == r.total)
		to << L"all ";
	else
		to << r.passed() << L" of ";
	to << r.total << L" tests";
	if (r.failed == 1)
		to << L"\nFailed 1 test!";
	else if (r.failed > 0)
		to << L"\nFailed " << r.failed << L" tests!";
	if (r.crashed == 1)
		to << L"\nCrashed 1 test!";
	else if (r.crashed > 0)
		to << L"\nCrashed " << r.crashed << L" tests!";
	if (r.aborted)
		to << L"\nABORTED";
	return to;
}

class Test;
class Suite;

class Tests : Singleton {
public:
	inline Tests() : singleSuite(null), singleTest(null) {}

	// Run all tests, returns the statistics.
	static TestResult run(int argc, const wchar_t *const *argv);

private:
	typedef map<int, Suite *> SuiteMap;
	SuiteMap suites;

	typedef map<String, Test *> TestMap;
	TestMap tests;

	bool singleSuite;
	bool singleTest;

	static Tests &instance();
	static void addTest(Test *t, bool single);
	static void addSuite(Suite *t, bool single);

	friend class Test;
	friend class Suite;

	void runTests(TestResult &result, bool runAll);
	void runSuite(Suite *s, TestResult &result, bool runAll);
	int countSuite(Suite *s);
};

// Base class for all test suites.
class Suite : NoCopy {
public:
	const String name;
	const int order;
	const bool single;
	const bool ignore;

protected:
	Suite(const String &name, int order, bool single, bool ignore, bool onDemand)
		: name(name), order(order), single(single), ignore(ignore) {
		if (!onDemand)
			Tests::instance().addSuite(this, single);
	}
};

// Base class for all the tests. Keeps track of instances.
class Test : NoCopy {
public:
	virtual TestResult run() const = 0;

	const String name;
	Suite *const suite;
	const bool single;

protected:
	Test(const String &name, Suite *suite = null, bool single = false) : name(name), suite(suite), single(single) {
		Tests::instance().addTest(this, single);
	}
};

// Error thrown when we shall abort.
class AbortError : public ::Exception {
public:
	String what() const { return L"Aborted"; }
};

// Generic test error.
class TestError : public ::Exception {
public:
	TestError(const String &msg) : msg(msg) {}
	virtual String what() const { return msg; }
private:
	String msg;
};

//////////////////////////////////////////////////////////////////////////
// Helper functions
//////////////////////////////////////////////////////////////////////////

template <class T, class U>
void verifyEq(TestResult &r, const T &lhs, const U &rhs, const String &expr) {
	if (!(lhs == rhs)) {
		r.failed++;
		std::wcout << L"Failed: " << expr << L" == " << lhs << L" != " << rhs << std::endl;
	}
}

template <class T, class U>
void verifyNeq(TestResult &r, const T &lhs, const U &rhs, const String &expr) {
	if (lhs == rhs) {
		r.failed++;
		std::wcout << L"Failed: " << expr << L" != " << lhs << L" == " << rhs << std::endl;
	}
}

template <class T, class U>
void verifyLt(TestResult &r, const T &lhs, const U &rhs, const String &expr) {
	if (!(lhs < rhs)) {
		r.failed++;
		std::wcout << L"Failed: " << expr << L" == " << lhs << L" !< " << rhs << std::endl;
	}
}

template <class T, class U>
void verifyLte(TestResult &r, const T &lhs, const U &rhs, const String &expr) {
	if (!(lhs <= rhs)) {
		r.failed++;
		std::wcout << L"Failed: " << expr << L" == " << lhs << L" !<= " << rhs << std::endl;
	}
}

template <class T, class U>
void verifyGt(TestResult &r, const T &lhs, const U &rhs, const String &expr) {
	if (!(lhs > rhs)) {
		r.failed++;
		std::wcout << L"Failed: " << expr << L" == " << lhs << L" !> " << rhs << std::endl;
	}
}

template <class T, class U>
void verifyGte(TestResult &r, const T &lhs, const U &rhs, const String &expr) {
	if (!(lhs >= rhs)) {
		r.failed++;
		std::wcout << L"Failed: " << expr << L" == " << lhs << L" !>= " << rhs << std::endl;
	}
}

#define OUTPUT_ERROR(expr, error)									  \
	std::wcout << L"Crashed " << expr << L":\n" << error << std::endl; \
	__result__.crashed++

#define DEFINE_SUITE(name, order, single, disable, demand)				\
	class Suite_##name : public Suite {									\
	private:															\
	Suite_##name() : Suite(WIDEN(#name), order, single, disable, demand) {}	\
	public:																\
	static Suite &instance() {											\
		static Suite_##name s;											\
		return s;														\
	}																	\
	}

#define EXPAND(x) x // Hack for msvc
#define PICK_FIRST(a, ...) a
#define PICK_NO_3(a, b, c, ...) c
#define NUM_ARGS(...) EXPAND(PICK_NO_3(__VA_ARGS__, 1, 0))

#define SUITE_OR_NULL_0(dummy)     null
#define SUITE_OR_NULL_1(dummy, type) &Suite_##type::instance()

#define SUITE_OR_NULL3(cond, ...) EXPAND(SUITE_OR_NULL_ ## cond (__VA_ARGS__))
#define SUITE_OR_NULL2(cond, ...) SUITE_OR_NULL3(cond, __VA_ARGS__)
#define SUITE_OR_NULL(...) SUITE_OR_NULL2(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)

// Name is the first parameter. It has to be inside of VA_ARGS for SUITE_OR_NULL to work...
#define DEFINE_TEST(name, suite, single)			\
	class name : public Test {						\
	public:											\
	virtual TestResult run() const;					\
	private:										\
	name() : Test(STRING(name), suite, single) {}	\
	static name instance;							\
	}


//////////////////////////////////////////////////////////////////////////
// Macros
//////////////////////////////////////////////////////////////////////////

#define CHECK_TITLE(expr, title)								\
	do {														\
		try {													\
			__result__.total++;									\
			if (!(expr)) {										\
				__result__.failed++;							\
				std::wcout << "Failed: " << title << std::endl; \
			}													\
		} catch (const storm::Exception *e) {					\
			OUTPUT_ERROR(title, e);								\
		} catch (const ::Exception &e) {						\
			OUTPUT_ERROR(title, e);								\
		} catch (...) {											\
			OUTPUT_ERROR(title, "unknown crash");				\
		}														\
	} while (false)

#define CHECK_PRED_TITLE(pred, expr, eq, title)				\
	do {													\
		try {												\
			__result__.total++;								\
			std::wostringstream __stream__;					\
			__stream__ << title;							\
			pred(__result__, expr, eq, __stream__.str());	\
		} catch (const storm::Exception *e) {				\
			OUTPUT_ERROR(title, e);							\
		} catch (const ::Exception &e) {					\
			OUTPUT_ERROR(title, e);							\
		} catch (...) {										\
			OUTPUT_ERROR(title, "unknown crash");			\
		}													\
	} while (false)

#define CHECK_EQ_TITLE(expr, eq, title)			\
	CHECK_PRED_TITLE(verifyEq, expr, eq, title)

#define CHECK_NEQ_TITLE(expr, eq, title)		\
	CHECK_PRED_TITLE(verifyNeq, expr, eq, title)

#define CHECK_LT_TITLE(expr, eq, title)			\
	CHECK_PRED_TITLE(verifyLt, expr, eq, title)

#define CHECK_LTE_TITLE(expr, eq, title)		\
	CHECK_PRED_TITLE(verifyLte, expr, eq, title)

#define CHECK_GT_TITLE(expr, eq, title)			\
	CHECK_PRED_TITLE(verifyGt, expr, eq, title)

#define CHECK_GTE_TITLE(expr, eq, title)		\
	CHECK_PRED_TITLE(verifyGte, expr, eq, title)

#define CHECK(expr) CHECK_TITLE(expr, #expr)

#define CHECK_EQ(expr, eq) CHECK_EQ_TITLE(expr, eq, #expr)

#define CHECK_NEQ(expr, eq) CHECK_NEQ_TITLE(expr, eq, #expr)

#define CHECK_LT(expr, eq) CHECK_LT_TITLE(expr, eq, #expr)

#define CHECK_LTE(expr, eq) CHECK_LTE_TITLE(expr, eq, #expr)

#define CHECK_GT(expr, eq) CHECK_GT_TITLE(expr, eq, #expr)

#define CHECK_GTE(expr, eq) CHECK_GTE_TITLE(expr, eq, #expr)

#define CHECK_ERROR(expr, type)											\
	do {																\
		try {															\
			__result__.total++;											\
			expr;														\
			__result__.failed++;										\
			std::wcout << L"Failed: " << #expr << L", did not fail as expected" << std::endl; \
		} catch (const type &) {										\
		} catch (const type *) {										\
		} catch (const storm::Exception *e) {							\
			std::wcout << L"Failed: " << #expr << L", did not throw " << #type << L" as expected." << std::endl; \
			std::wcout << e << std::endl;								\
			__result__.failed++;										\
		} catch (const ::Exception &e) {								\
			std::wcout << L"Failed: " << #expr << L", did not throw " << #type << L" as expected." << std::endl; \
			std::wcout << e << std::endl;								\
			__result__.failed++;										\
		} catch (...) {													\
			OUTPUT_ERROR(#expr, "unknown error");						\
		}																\
	} while (false)

#define CHECK_RUNS(expr)							\
	do {											\
		try {										\
			__result__.total++;						\
			expr;									\
		} catch (const storm::Exception *e) {		\
			OUTPUT_ERROR(#expr, e);					\
		} catch (const ::Exception &e) {			\
			OUTPUT_ERROR(#expr, e);					\
		} catch (...) {								\
			OUTPUT_ERROR(#expr, "unknown crash");	\
		}											\
	} while (false)

#define VERIFY(expr)													\
	do {																\
		__result__.total++;												\
		if (!(expr)) {													\
			__result__.crashed++;										\
			std::wcout << L"Failed: " << #expr << L", aborting...\n";	\
			throw AbortError();											\
		}																\
	} while (false)

#define SUITE(name, order)						\
	DEFINE_SUITE(name, order, false, false, false)

#define SUITE_(name, order)						\
	DEFINE_SUITE(name, order, true, false, false)

#define SUITEX(name, order)						\
	DEFINE_SUITE(name, order, false, true, false)

#define SUITED(name, order)						\
	DEFINE_SUITE(name, order, false, true, true)

#define BEGIN_TEST_HELP(name, suite, single)	\
	DEFINE_TEST(name, suite, single);			\
	name name::instance;						\
	TestResult name::run() const {				\
	TestResult __result__; do

// Parameters: name, suite (suite is optional).
#define BEGIN_TEST(...)											\
	BEGIN_TEST_HELP(PICK_FIRST(__VA_ARGS__), SUITE_OR_NULL(__VA_ARGS__), false)

// Parameters: name, suite (suite is optional).
#define BEGIN_TEST_(...)										\
	BEGIN_TEST_HELP(PICK_FIRST(__VA_ARGS__), SUITE_OR_NULL(__VA_ARGS__), true)

#define BEGIN_TESTX(name, ...)					\
	TestResult name() {							\
	TestResult __result__; do

#define END_TEST	   \
	while (false);	   \
	return __result__; \
	}

#define BEGIN_TEST_FN(name, ...)				\
	void name(TestResult &__result__, __VA_ARGS__) { do

#define END_TEST_FN								\
	while (false);								\
	}

#define CALL_TEST_FN(name, ...)					\
	name(__result__, __VA_ARGS__)