-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtest.h
More file actions
143 lines (120 loc) · 7.51 KB
/
rtest.h
File metadata and controls
143 lines (120 loc) · 7.51 KB
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
//
// Copyright (c) 2013 Sean Farrell <sean.farrell@rioki.org>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#ifndef _RTEST_H_
#define _RTEST_H_
#include <stdexcept>
#include <sstream>
#include <cmath>
namespace rtest
{
int run();
namespace impl
{
struct Failure
{
const char* file;
unsigned int line;
std::string msg;
Failure(const char* file, unsigned int line, const std::string& msg);
};
struct Test
{
const char* name;
const char* file;
unsigned int line;
Test(const char* name, const char* file, unsigned int line);
virtual void run() = 0;
};
void check(bool cond, const char* scond, const char* file, unsigned int line);
template <typename T1, typename T2>
void check_equal(T1 a, T2 b, const char* file, unsigned int line)
{
if (a != b)
{
std::stringstream buff;
buff << "Expected " << a << " but got " << b << ".";
throw ::rtest::impl::Failure(file, line, buff.str());
}
}
template <>
void check_equal(const char* a, const char* b, const char* file, unsigned int line);
template <>
void check_equal(const std::string& a, const char* b, const char* file, unsigned int line);
template <>
void check_equal(const char* a, const std::string& b, const char* file, unsigned int line);
template <typename T1, typename T2, typename T3>
void check_close(T1 a, T2 b, T3 eps, const char* file, unsigned int line)
{
if (std::abs(a - b) >= eps)
{
std::stringstream buff;
buff << "Expected " << a << " +- " << eps << " but got " << b << ".";
throw ::rtest::impl::Failure(file, line, buff.str());
}
}
}
}
#define SUITE(NAME) namespace NAME ## _suite
#define TEST(NAME) \
struct Test_ ## NAME : public ::rtest::impl::Test \
{ \
Test_ ## NAME() \
: Test(#NAME, __FILE__, __LINE__) {} \
\
void run(); \
\
} test_ ## NAME; \
\
void Test_ ## NAME ::run()
#define TEST_FIXTURE(FIXTURE, NAME) \
struct Helper_ ## NAME : public FIXTURE \
{ \
void run(); \
}; \
\
struct Test_ ## NAME : public ::rtest::impl::Test \
{ \
Test_ ## NAME() \
: Test(#NAME, __FILE__, __LINE__) {} \
\
void run() \
{ \
Helper_ ## NAME fixture; \
fixture.run(); \
} \
} test_ ## NAME; \
\
void Helper_ ## NAME ::run()
#define CHECK(COND) ::rtest::impl::check(COND, #COND, __FILE__, __LINE__)
#define CHECK_EQUAL(A, B) ::rtest::impl::check_equal(A, B, __FILE__, __LINE__)
#define CHECK_CLOSE(A, B, EPS) ::rtest::impl::check_close(A, B, EPS, __FILE__, __LINE__)
#define CHECK_THROW(EXPR, OBJ) \
try \
{ \
EXPR; \
throw ::rtest::impl::Failure(__FILE__, __LINE__, "'" #EXPR "' did not throw '" #OBJ "'."); \
} \
catch (OBJ) \
{ \
\
}
#endif