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
|
// Copyright (C) 2001 Claus Dr�by
// Terms of use are in the file COPYING
#include "unit++.h"
#include "tester.h"
#include "main.h"
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <iostream>
#endif
using namespace std;
using namespace unitpp;
namespace {
// a test case that can fail with any exception
class test_test : public test
{
public:
enum result { succes, fail, error, exotic };
test_test(string name, result res = succes) : test(name), res(res) {}
virtual void operator()()
{
switch (res) {
case succes: break;
case fail: ::fail("test_test");
case error: throw out_of_range("ranged");
case exotic: throw 4711;
}
}
private:
result res;
};
// The test suite for the unit++ library
class Test : public suite
{
void create()
{
test_test a_loc_test("local");
}
void assert_ok()
{
string s("ok");
assert_true("assert_true(true)", true);
assert_eq("assert_eq(int)", 7, 7);
assert_eq("assert_eq(char*, string)", "ok", s);
}
void assert_fail()
{
string s("fejl");
bool ok = true;
try {
assert_true("assert_true(false)", false);
ok = false;
} catch (assertion_error e) {
#ifdef HAVE_SSTREAM
ostringstream oss;
oss << e;
assert_eq("assert_true(false) output",
"assert_true(false) [assertion failed]", oss.str());
#endif
}
if (!ok)
fail("no exception from assert_true(false)");
try {
assert_eq("assert_eq(int)", 5, 7);
ok = false;
} catch (assert_value_error<int,int> e) {
#ifdef HAVE_SSTREAM
ostringstream oss;
oss << e;
assert_eq("assert_eq(int) output",
"assert_eq(int) [expected: `5' got: `7']", oss.str());
#endif
}
if (!ok)
fail("no exception from assert_eq(int)");
try {
assert_eq("assert_eq(char*, string)", "ok", s);
ok = false;
} catch (assert_value_error<const char*, string> e) {
} catch (assert_value_error<char*, string> e) { // MSVC++ bug
}
if (!ok)
fail("no exception from assert_eq(const char*, string)");
}
void tester_visit()
{
out_of_range oor("negative");
assertion_error ae("test");
#ifdef HAVE_SSTREAM
ostringstream os;
tester tst(os);
#else
tester tst(cerr);
#endif
root.visit(&tst);
assert_eq("tests ok", 3, tst.res_tests().n_ok());
assert_eq("tests error", 2, tst.res_tests().n_err());
assert_eq("tests fail", 1, tst.res_tests().n_fail());
assert_eq("suites ok", 1, tst.res_suites().n_ok());
assert_eq("suites error", 2, tst.res_suites().n_err());
assert_eq("suites fail", 1, tst.res_suites().n_fail());
}
void ex_test()
{
throw out_of_range("expected");
}
void get_by_id()
{
test* p = root.get_child("s2");
assert_true("found s2", p != 0);
suite* sp = dynamic_cast<suite*>(p);
assert_true("s2 was suite", sp != 0);
assert_eq("right s2", "S2", sp->name());
p = sp->get_child("t20");
assert_true("found t20", p != 0);
assert_eq("not suite", static_cast<suite*>(0),dynamic_cast<suite*>(p));
}
void vec()
{
string s = "three.blind.mice";
vector<string> v(vectorize(s,'.'));
assert_eq("v[0]", string("three"), v[0]);
assert_eq("v[1]", string("blind"), v[1]);
assert_eq("v[2]", string("mice"), v[2]);
assert_eq("size", size_t(3), v.size());
v = vectorize(s,'-');
assert_eq("no match", s, v[0]);
assert_eq("no match size", size_t(1), v.size());
}
void empty_vec()
{
string s("");
vector<string> v(vectorize(s,'.'));
assert_eq("size", size_t(0), v.size());
s = "one..three";
v = vectorize(s,'.');
assert_eq("v[0]", string("one"), v[0]);
assert_eq("v[1]", string(""), v[1]);
assert_eq("v[2]", string("three"), v[2]);
assert_eq("size", size_t(3), v.size());
}
void find()
{
test* tp = root.find("s2.s21.t210");
assert_eq("t210", t210, tp);
tp = root.find("s1.s21");
assert_eq("bad mid", static_cast<test*>(0), tp);
}
suite root;
test* t210;
bool do_fail;
void fail_on_flag()
{
assert_true("Fail option not set", !do_fail);
}
public:
Test() : suite("Unit++ test suite"), root("The root")
{
do_fail = false;
options().add("f", new options_utils::opt_flag(do_fail));
options().alias("fail", "f");
suite* s1;
suite* s2;
suite* s21;
root.add("s1", s1 = new suite("S1"));
root.add("s2", s2 = new suite("S2"));
s2->add("s21", s21 = new suite("S21"));
s1->add("t10", new test_test("T10"));
s1->add("t11", new test_test("T11"));
s2->add("t20", new test_test("T20", test_test::error));
s2->add("t22", new test_test("T22", test_test::exotic));
s21->add("t210", t210 = new test_test("T210"));
s21->add("t211", new test_test("T211", test_test::fail));
//
// Adding testcases
suite::main().add("unitpp", this);
add("create", testcase(this, "Create a test", &Test::create));
add("assert_ok", testcase(this, "Assert ok", &Test::assert_ok));
add("assert_fail", testcase(this, "Assert fail", &Test::assert_fail));
add("tester_visit", testcase(this, "Visit", &Test::tester_visit));
add("exception", testcase(new exception_test<out_of_range>(
testcase(this, "gen ex", &Test::ex_test))));
add("id_get", testcase(this, "Get by id", &Test::get_by_id));
add("vec", testcase(this, "Vectorize", &Test::vec));
add("empty_vec", testcase(this, "Vectorize empty", &Test::empty_vec));
add("find", testcase(this, "find", &Test::find));
add("fail", testcase(this, "fail on option", &Test::fail_on_flag));
}
} * theTest = new Test();
}
|