monotone

monotone Mtn Source Tree

Root/unit_tests.hh

1#ifndef __UNIT_TESTS__
2#define __UNIT_TESTS__
3
4// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13// Log a success/failure message, and set the test state to 'fail' if needed
14#define UNIT_TEST_CHECK(expression) \
15 unit_test::do_check(expression, __FILE__, __LINE__, #expression)
16
17// Like UNIT_TEST_CHECK, but you get to specify what is logged.
18// MSG should be an FL("...") % ... construct.
19#define UNIT_TEST_CHECK_MSG(expression, msg) \
20 unit_test::do_check(expression, __FILE__, __LINE__, (msg).str().c_str())
21
22// Like UNIT_TEST_CHECK, but abort the test immediately on failure
23#define UNIT_TEST_REQUIRE(expression) \
24 unit_test::do_require(expression, __FILE__, __LINE__, #expression)
25
26#define UNIT_TEST_CHECK_THROW(statement, exception) \
27 do \
28 { \
29 bool fnord_unit_test_checkval = false; \
30 try \
31 { \
32 statement; \
33 } \
34 catch(exception const &) \
35 { \
36 fnord_unit_test_checkval = true; \
37 } \
38 unit_test::do_check(fnord_unit_test_checkval, __FILE__, __LINE__, \
39 #statement " throws " #exception); \
40 } while (0)
41
42#define UNIT_TEST_CHECK_NOT_THROW(statement, exception) \
43 do \
44 { \
45 bool fnord_unit_test_checkval = true; \
46 try \
47 { \
48 statement; \
49 } \
50 catch(exception const &) \
51 { \
52 fnord_unit_test_checkval = false; \
53 } \
54 unit_test::do_check(fnord_unit_test_checkval, __FILE__, __LINE__, \
55 #statement " does not throw " #exception); \
56 } while (0)
57
58#define UNIT_TEST_CHECKPOINT(message) \
59 unit_test::do_checkpoint(__FILE__, __LINE__, message);
60
61
62namespace unit_test {
63 void do_check(bool checkval, char const * file,
64 int line, char const * message);
65
66 void do_require(bool checkval, char const * file,
67 int line, char const * message);
68
69 void do_checkpoint(char const * file, int line, char const * message);
70
71 // Declarative mechanism for specifying unit tests, similar to
72 // auto_unit_test in boost, but more suited to our needs.
73 struct unit_test_case
74 {
75 char const *group;
76 char const *name;
77 void (*func)();
78 bool failure_is_success;
79 unit_test_case(char const * group,
80 char const * name,
81 void (*func)(),
82 bool fis);
83 unit_test_case();
84 };
85}
86
87// The names of the test functions must not collide with each other or with
88// names of symbols in the code being tested, despite their being in a
89// separate namespace, so that references _from_ the test functions _to_ the
90// code under test resolve correctly.
91#define UNIT_TEST(GROUP, TEST) \
92 namespace unit_test { \
93 static void t_##GROUP##_##TEST(); \
94 static unit_test_case r_##GROUP##_##TEST \
95 (#GROUP, #TEST, t_##GROUP##_##TEST, false); \
96 } \
97 static void unit_test::t_##GROUP##_##TEST()
98
99// Local Variables:
100// mode: C++
101// fill-column: 76
102// c-file-style: "gnu"
103// indent-tabs-mode: nil
104// End:
105// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
106
107#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status