nlib
Pred.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_TESTING_PRED_H_
4 #define INCLUDE_NN_NLIB_TESTING_PRED_H_
5 
7 
8 NLIB_NAMESPACE_BEGIN
9 namespace testing {
10 
11 template <class Pred, class T1>
12 AssertionResult EvalPred1(const char* pred_text, const char* e1, Pred pred, const T1& v1) {
13  return pred(v1) ? AssertionResult(true) : AssertionResult(false)
14  << pred_text << "(" << e1
15  << ") evaluates to false, where"
16  << "\n" << e1 << " evaluates to " << v1;
17 }
18 
19 template <class Pred, class T1, class T2>
20 AssertionResult EvalPred2(const char* pred_text, const char* e1, const char* e2, Pred pred,
21  const T1& v1, const T2& v2) {
22  return pred(v1, v2) ? AssertionResult(true) : AssertionResult(false)
23  << pred_text << "(" << e1 << ", " << e2
24  << ") evaluates to false, where"
25  << "\n" << e1 << " evaluates to " << v1
26  << "\n" << e2 << " evaluates to " << v2;
27 }
28 
29 template <class Pred, class T1, class T2, class T3>
30 AssertionResult EvalPred3(const char* pred_text, const char* e1, const char* e2, const char* e3,
31  Pred pred, const T1& v1, const T2& v2, const T3& v3) {
32  return pred(v1, v2, v3) ? AssertionResult(true)
33  : AssertionResult(false) << pred_text << "(" << e1 << ", " << e2 << ", "
34  << e3 << ") evaluates to false, where"
35  << "\n" << e1 << " evaluates to " << v1 << "\n"
36  << e2 << " evaluates to " << v2 << "\n" << e3
37  << " evaluates to " << v3;
38 }
39 
40 template <class Pred, class T1, class T2, class T3, class T4>
41 AssertionResult EvalPred4(const char* pred_text, const char* e1, const char* e2, const char* e3,
42  const char* e4, Pred pred, const T1& v1, const T2& v2, const T3& v3,
43  const T4& v4) {
44  return pred(v1, v2, v3, v4) ? AssertionResult(true)
45  : AssertionResult(false)
46  << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", "
47  << e4 << ") evaluates to false, where"
48  << "\n" << e1 << " evaluates to " << v1 << "\n" << e2
49  << " evaluates to " << v2 << "\n" << e3 << " evaluates to "
50  << v3 << "\n" << e4 << " evaluates to " << v4;
51 }
52 
53 template <class Pred, class T1, class T2, class T3, class T4, class T5>
54 AssertionResult EvalPred5(const char* pred_text, const char* e1, const char* e2, const char* e3,
55  const char* e4, const char* e5, Pred pred, const T1& v1, const T2& v2,
56  const T3& v3, const T4& v4, const T5& v5) {
57  return pred(v1, v2, v3, v4, v5)
58  ? AssertionResult(true)
59  : AssertionResult(false) << pred_text << "(" << e1 << ", " << e2 << ", " << e3
60  << ", " << e4 << ", " << e5 << ") evaluates to false, where"
61  << "\n" << e1 << " evaluates to " << v1 << "\n" << e2
62  << " evaluates to " << v2 << "\n" << e3 << " evaluates to "
63  << v3 << "\n" << e4 << " evaluates to " << v4 << "\n" << e5
64  << " evaluates to " << v5;
65 }
66 
67 } // namespace testing
68 NLIB_NAMESPACE_END
69 
70 #define NLIB_TESTING_PRED1_(pred, v1, iffail) \
71  switch (0) \
72  case 0: \
73  default: \
74  if (::nlib_ns::testing::AssertionResult ar = \
75  ::nlib_ns::testing::EvalPred1(#pred, #v1, pred, v1)) \
76  ; \
77  else \
78  iffail = ar
79 
80 #define NLIB_TESTING_PRED2_(pred, v1, v2, iffail) \
81  switch (0) \
82  case 0: \
83  default: \
84  if (::nlib_ns::testing::AssertionResult ar = \
85  ::nlib_ns::testing::EvalPred2(#pred, #v1, #v2, pred, v1, v2)) \
86  ; \
87  else \
88  iffail = ar
89 
90 #define NLIB_TESTING_PRED3_(pred, v1, v2, v3, iffail) \
91  switch (0) \
92  case 0: \
93  default: \
94  if (::nlib_ns::testing::AssertionResult ar = \
95  ::nlib_ns::testing::EvalPred3(#pred, #v1, #v2, #v3, pred, v1, v2, v3)) \
96  ; \
97  else \
98  iffail = ar
99 
100 #define NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, iffail) \
101  switch (0) \
102  case 0: \
103  default: \
104  if (::nlib_ns::testing::AssertionResult ar = \
105  ::nlib_ns::testing::EvalPred4(#pred, #v1, #v2, #v3, #v4, pred, v1, v2, v3, v4)) \
106  ; \
107  else \
108  iffail = ar
109 
110 #define NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, iffail) \
111  switch (0) \
112  case 0: \
113  default: \
114  if (::nlib_ns::testing::AssertionResult ar = ::nlib_ns::testing::EvalPred5( \
115  #pred, #v1, #v2, #v3, #v4, #v5, pred, v1, v2, v3, v4, v5)) \
116  ; \
117  else \
118  iffail = ar
119 
120 #define NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, iffail) \
121  switch (0) \
122  case 0: \
123  default: \
124  if (::nlib_ns::testing::AssertionResult ar = pred_format(#v1, v1)) \
125  ; \
126  else \
127  iffail = ar
128 
129 #define NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, iffail) \
130  switch (0) \
131  case 0: \
132  default: \
133  if (::nlib_ns::testing::AssertionResult ar = pred_format(#v1, #v2, v1, v2)) \
134  ; \
135  else \
136  iffail = ar
137 
138 #define NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, iffail) \
139  switch (0) \
140  case 0: \
141  default: \
142  if (::nlib_ns::testing::AssertionResult ar = pred_format(#v1, #v2, #v3, v1, v2, v3)) \
143  ; \
144  else \
145  iffail = ar
146 
147 #define NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, iffail) \
148  switch (0) \
149  case 0: \
150  default: \
151  if (::nlib_ns::testing::AssertionResult ar = \
152  pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4)) \
153  ; \
154  else \
155  iffail = ar
156 
157 #define NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, iffail) \
158  switch (0) \
159  case 0: \
160  default: \
161  if (::nlib_ns::testing::AssertionResult ar = \
162  pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5)) \
163  ; \
164  else \
165  iffail = ar
166 
167 #define ASSERT_PRED1(pred, v1) NLIB_TESTING_PRED1_(pred, v1, NLIB_TESTING_FATAL_)
168 #define EXPECT_PRED1(pred, v1) NLIB_TESTING_PRED1_(pred, v1, NLIB_TESTING_NONFATAL_)
169 #define ASSERT_PRED_FORMAT1(pred_format, v1) \
170  NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, NLIB_TESTING_FATAL_)
171 #define EXPECT_PRED_FORMAT1(pred_format, v1) \
172  NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, NLIB_TESTING_NONFATAL_)
173 
174 #define ASSERT_PRED2(pred, v1, v2) NLIB_TESTING_PRED2_(pred, v1, v2, NLIB_TESTING_FATAL_)
175 #define EXPECT_PRED2(pred, v1, v2) NLIB_TESTING_PRED2_(pred, v1, v2, NLIB_TESTING_NONFATAL_)
176 #define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \
177  NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, NLIB_TESTING_FATAL_)
178 #define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \
179  NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, NLIB_TESTING_NONFATAL_)
180 
181 #define ASSERT_PRED3(pred, v1, v2, v3) NLIB_TESTING_PRED3_(pred, v1, v2, v3, NLIB_TESTING_FATAL_)
182 #define EXPECT_PRED3(pred, v1, v2, v3) NLIB_TESTING_PRED3_(pred, v1, v2, v3, NLIB_TESTING_NONFATAL_)
183 #define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \
184  NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, NLIB_TESTING_FATAL_)
185 #define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \
186  NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, NLIB_TESTING_NONFATAL_)
187 
188 #define ASSERT_PRED4(pred, v1, v2, v3, v4) \
189  NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, NLIB_TESTING_FATAL_)
190 #define EXPECT_PRED4(pred, v1, v2, v3, v4) \
191  NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, NLIB_TESTING_NONFATAL_)
192 #define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
193  NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, NLIB_TESTING_FATAL_)
194 #define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
195  NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, NLIB_TESTING_NONFATAL_)
196 
197 #define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \
198  NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, NLIB_TESTING_FATAL_)
199 #define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \
200  NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, NLIB_TESTING_NONFATAL_)
201 #define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
202  NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, NLIB_TESTING_FATAL_)
203 #define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
204  NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, NLIB_TESTING_NONFATAL_)
205 
206 #endif // INCLUDE_NN_NLIB_TESTING_PRED_H_
Defines the macro for a basic, simple test.