-
Notifications
You must be signed in to change notification settings - Fork 0
/
protest_test.cpp
359 lines (329 loc) · 9.39 KB
/
protest_test.cpp
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
/*!
* \file protest_test.cpp
* \brief
* \author Larry-o <nu11p0.6477@gmail.com>
* \date 2015/03/24
* C++ version: C++14
*/
#include <iostream>
#include <functional>
#include <tuple>
#include <limits>
#include <cmath>
#include <typeinfo>
#include <protest/generic_test.hpp>
#include <protest/condition.hpp>
//using namespace nu11p0::protest;
namespace protest = nu11p0::protest;
int fun(int a)
{
return a;
}
//template <typename T, typename Less=std::less<T>, typename Negate=std::negate<T>>
//T absolute_simple(const T &val)
//{
//const T negative = Negate()(val);
//return (Less()(val, negative) ? negative : val);
//}
template <typename T, typename Less=std::less<T>, typename Negate=std::negate<T>>
T absolute(const T &val, Less l=Less(), Negate n=Negate())
{
const T negative = n(val);
return (l(val, negative) ? negative : val);
}
struct AbsoluteWrapper {
template <typename T>
auto operator()(const T &arg) {
return absolute<T>(arg);
}
}; // struct TemplateFunctionWrapperAbsolute
template <typename F>
struct TemplateIdempotent {
template <typename T>
protest::CheckResult operator()(const T &arg)
{
protest::CheckResult result;
const auto ret1 = F()(arg);
if(!(ret1 == F()(ret1))) {
result.reason.emplace("given function is not idempotent.");
}
return result;
}
};
template <typename F>
struct TemplateFunctionPredicateWrapper {
template <typename T>
struct type {
protest::CheckResult operator()(const T &arg) {
return F()(arg);
}
};
};
int absoluteInt64Test(void)
{
{
protest::SimpleTest<int64_t> test(
"Idempotence test for absolute<int64_t>",
//protest::Idempotent<int64_t>(absolute_simple<int64_t>),
protest::Idempotent<int64_t>([](auto x){ return absolute<int64_t>(x); }),
[](int64_t arg) {
return (arg != std::numeric_limits<int64_t>::min());
}
);
auto result = test.runTest(
"random case",
protest::case_gen::Random<int64_t>(),
20,
std::cout);
printResult(std::cout, result);
if(result.isTestFailed()) {
std::cout << " | failed case: " << protest::ns_any::any_cast<int64_t>(result.failedCase) << std::endl;
return 1;
}
test.clearAll();
result = test.runTest(
"edge case",
protest::case_gen::Edge<int64_t>(),
20,
std::cout);
printResult(std::cout, result);
if(result.isTestFailed()) {
std::cout << " | failed case: " << protest::ns_any::any_cast<int64_t>(result.failedCase) << std::endl;
return 2;
}
}
{
protest::SimpleTest<int64_t> test(
"Positivity test for absolute<int64_t>",
[](int64_t arg) {
return protest::AssertResult(absolute<int64_t>(arg) >= 0, "return value is still negative");
},
[](int64_t arg) {
return (arg != std::numeric_limits<int64_t>::min());
}
);
auto result = test.runTest(
"random case",
protest::case_gen::Random<int64_t>(),
20,
std::cout);
printResult(std::cout, result);
if(result.isTestFailed()) {
std::cout << " | failed case: " << protest::ns_any::any_cast<int64_t>(result.failedCase) << std::endl;
return 3;
}
test.clearAll();
result = test.runTest(
"edge case",
protest::case_gen::Edge<int64_t>(),
20,
std::cout);
printResult(std::cout, result);
if(result.isTestFailed()) {
std::cout << " | failed case: " << protest::ns_any::any_cast<int64_t>(result.failedCase) << std::endl;
return 4;
}
}
return 0;
}
int absoluteTemplateNumTest(void)
{
//using Nums = protest::tuple_append_t<Integers, float, double, long double>;
using Nums = protest::tuple_concat_t<protest::Integers, protest::Floats>;
auto absolutePrecondition = protest::overload(
// signedな整数の場合は、最小値でないことを確認する。
[](auto x) -> std::enable_if_t<std::is_integral<decltype(x)>{} && std::is_signed<decltype(x)>{}, bool>
{
return (x != std::numeric_limits<decltype(x)>::min());
}
// 浮動小数点数の場合は、NaNでないことを確認する。
// (つまり、正規化数、非正規化数、ゼロ、無限大については処理を行う。)
, [](auto x) -> std::enable_if_t<std::is_floating_point<decltype(x)>{}, bool>
{
return !std::isnan(x);
}
, [](auto)
{
return true;
}
);
auto casePrinter = protest::overload(
[](auto x) -> std::enable_if_t<std::is_floating_point<decltype(x)>{}, void> {
std::cout << "(floating point)(" << x << ')';
}
, [](auto x) -> std::enable_if_t<std::is_integral<decltype(x)>{} && std::is_signed<decltype(x)>{}, void> {
std::cout << "(signed integral)(" << x << ')';
}
, [](auto x) {
std::cout << "(unsigned integral)(" << x << ')';
}
);
auto result = protest::generic::test<
protest::generic::Random
, Nums
>(
"absolute<>() template function positivity test"
, "random case"
, [](auto x) {
return protest::AssertResult((absolute(x) >= 0), "return value is still negative");
}
, absolutePrecondition
, 35, std::cout
// You can specify function to print result. protest::printResult() is used if omitted.
//, [](std::ostream &ost, const protest::TestResult &res) {
//ost << "(generic test) ";
//printResult(ost, res);
//}
);
// std::coutを指定した時点で、デフォルトでprintResult()による結果出力がされるので、テスト後のprintResult()は不要。
if(result.result.isTestFailed()) {
std::cout << " | failed case: ";
protest::passAsNthType<Nums>(
casePrinter
, result.result.failedCase
, result.failedIndex);
std::cout << std::endl;
return 2;
}
result.result.clearAll();
result = protest::generic::test<
protest::generic::Edge
, Nums
>(
"absolute<>() template function positivity test"
, "edge case"
, [](auto x) {
return protest::AssertResult((absolute(x) >= 0), "return value is still negative");
}
, absolutePrecondition
, 50, std::cout
);
if(result.result.isTestFailed()) {
std::cout << " | failed case: ";
protest::passAsNthType<Nums>(
casePrinter
, result.result.failedCase
, result.failedIndex);
std::cout << std::endl;
return 2;
}
result.result.clearAll();
return 0;
}
int protestGenericUtilTest(void)
{
auto fun1_fallback = [](const auto &) {
return "const auto &";
};
auto fun1 = protest::overload(
// #1
[](auto x) -> std::enable_if_t<std::is_integral<decltype(x)>{} && std::is_signed<decltype(x)>{}, const char *>
{
return "(signed integral)";
}
// #2
, [](int *x) {
return "int *";
}
// #3
, [](const char *x) {
return "const char *";
}
// #4
, [](auto x) -> std::enable_if_t<std::is_integral<decltype(x)>{}, const char *>
{
return "(integral, not signed)";
}
// #5
,[](std::string &&x) {
return "std::string &&";
}
// #6
// If you want only 'float', you should use 'auto' and SFINAE.
, [](auto x) -> std::enable_if_t<std::is_same<decltype(x), float>{}, const char *>
{
return "(auto, exactly float)";
}
// #7
// This function is used for double, long double, and
// all other types which can be converted implicitly to float.
// Note that NOT this function but the above function ([](auto x)) is called for float.
, [](float) {
return "float";
}
// #8
, fun1_fallback
// #9
// The below will never called because the priority is lower than
// fun1_fallback (which is [](auto){}).
, [](double) {
return "double";
}
);
// #3
if(fun1("cstr") != std::string("const char *")) {
std::cout<<"error:"<<fun1("cstr")<<" for const char *"<<std::endl;
return 1;
}
// #5
if(fun1(std::string("std::string")) != std::string("std::string &&")) {
std::cout<<"error:"<<fun1(std::string("std::string"))<<" for std::string"<<std::endl;
return 2;
}
// #7
if(fun1(3.14) != std::string("float")) {
std::cout<<"error:"<<fun1(3.14)<<" for double"<<std::endl;
return 4;
}
// #6
if(fun1(3.14f) != std::string("(auto, exactly float)")) {
std::cout<<"error:"<<fun1(3.14f)<<" for float"<<std::endl;
return 4;
}
// #1
if(fun1(3) != std::string("(signed integral)")) {
std::cout<<"error:"<<fun1(3)<<"for signed integer"<<std::endl;
return 5;
}
// You can convert overloaded functions to std::function.
std::function<const char *(unsigned long)> fun1_cchar_ulong(fun1);
// force #4 (with std::function cast).
if(fun1_cchar_ulong(-3) != std::string("(integral, not signed)")) {
std::cout<<"error:"<<fun1_cchar_ulong(-3)<<std::endl;
return 6;
}
return 0;
}
template <uint64_t num>
struct Factorial : std::integral_constant<uint64_t, num*Factorial<num-1>{}> {};
template <>
struct Factorial<0> : std::integral_constant<uint64_t, 1> {};
int mplTest(void)
{
std::cout << "fact(5) = " << Factorial<5>{} << std::endl;
std::cout << "fact(10) = " << Factorial<10>{} << std::endl;
std::cout << "fact(15) = " << Factorial<15>{} << std::endl;
std::cout << "fact(20) = " << Factorial<20>{} << std::endl;
std::cout << "max = " << std::numeric_limits<uint64_t>::max() << std::endl;
std::cout << "fact(21) = " << Factorial<21>{} << std::endl;
// FIXME: unimplemented.
// コンパイル時疑似乱数を使ってテストケース自動生成したい。
return 0;
}
int main(void)
{
int ret;
if(( ret = absoluteInt64Test() )) {
return ret;
}
if(( ret = absoluteTemplateNumTest() )) {
return ret;
}
if(( ret = protestGenericUtilTest() )) {
return ret;
}
if(( ret = mplTest() )) {
return 0;
}
return 0;
}