-
Notifications
You must be signed in to change notification settings - Fork 1
/
cppFriendsClangTest.cpp
514 lines (423 loc) · 15.9 KB
/
cppFriendsClangTest.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <cstdlib>
#include <iostream>
#include <limits>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
#include "cppFriendsClang.hpp"
class TestSwitchCase : public ::testing::Test{};
TEST_F(TestSwitchCase, LookUpTable) {
struct TestCase {
SwitchCase::Shape shape;
double expected;
};
const TestCase testSet[] = {{SwitchCase::Shape::UNKNOWN, 0.0},
{SwitchCase::Shape::CIRCLE, 12.566370614359172},
{SwitchCase::Shape::RECTANGULAR, 6.0},
{SwitchCase::Shape::TRIANGLE, 24.0},
{SwitchCase::Shape::SQUARE, 49.0}};
for(auto& test : testSet) {
EXPECT_DOUBLE_EQ(test.expected, GetFixedTestValue(test.shape));
}
}
// Devirtualizationによって結果は変わらないが、コードは変わるので.sを確認する
class TestDevirtualization : public ::testing::Test{};
TEST_F(TestDevirtualization, Inline) {
auto actual = Devirtualization::GetStringInline();
std::string expected = "BaseInlineDerivedInline";
EXPECT_EQ(expected, actual);
}
TEST_F(TestDevirtualization, Outline) {
auto actual = Devirtualization::GetStringOutline();
std::string expected = "BaseOutlineDerivedOutline";
EXPECT_EQ(expected, actual);
}
class TestShiftTooManyLto : public ::testing::Test{};
TEST_F(TestShiftTooManyLto, All) {
EXPECT_EQ(8, ShiftManyFor1(3));
#ifdef CPPFRIENDS_ENABLE_LTO
EXPECT_EQ(0, ShiftManyFor1(35));
#else
EXPECT_EQ(8, ShiftManyFor1(35));
#endif
}
class TestNarrowingCast : public ::testing::Test{};
TEST_F(TestNarrowingCast, LongLongToDouble) {
// 0x7fffffffffff00ff
const std::string base = "9223372036854710527";
long long int llNumber = boost::lexical_cast<decltype(llNumber)>(base);
// g++もclang++も警告を出さないが、64-bitを仮数部52-bitに入れるので精度が失われる
double d = llNumber; // ビット数が減るので警告が出るが意図的
std::ostringstream os;
// 整数部と.の分だけ多く精度を取る
os << std::setprecision(3 + std::numeric_limits<decltype(llNumber)>::digits10) << d;
const std::string actual = os.str();
// 下位の桁は保存されない
#if !defined(__MINGW32__) || defined(__MINGW64__)
EXPECT_TRUE(std::numeric_limits<decltype(d)>::is_iec559);
#endif
EXPECT_NE(base, actual);
// 上位の桁は保存される
std::string expected = base;
expected.substr(0, base.size() - 3);
EXPECT_EQ(0, actual.find_first_of(expected));
}
// 敢えてdo-whileで囲まない
#define DEBUG_PRINT_COUT(osout, str) osout << str << "@" << __PRETTY_FUNCTION__ << "in " << __FILE__ << " : " << __LINE__;
// strをosoutとoserrの両方に書き出す
#define ILL_DEBUG_PRINT(osout, oserr, str) \
osout << str << "@" << __PRETTY_FUNCTION__ << ":"; \
oserr << str << "@" << __PRETTY_FUNCTION__ << ":"; \
#define WELL_DEBUG_PRINT(osout, oserr, str) \
do { \
osout << str << "@" << __PRETTY_FUNCTION__ << ":"; \
oserr << str << "@" << __PRETTY_FUNCTION__ << ":"; \
} while(0)
// ;が余計
#define BAD_DEBUG_PRINT1(osout, oserr, str) \
do { \
osout << str << "@" << __PRETTY_FUNCTION__ << ":"; \
oserr << str << "@" << __PRETTY_FUNCTION__ << ":"; \
} while(0); \
#define BAD_DEBUG_PRINT2(osout, oserr, str) \
{ \
osout << str << "@" << __PRETTY_FUNCTION__ << ":"; \
oserr << str << "@" << __PRETTY_FUNCTION__ << ":"; \
}
class TestMacroExpansion : public ::testing::Test {};
TEST_F(TestMacroExpansion, SingleStatement) {
std::ostringstream out;
std::string message {"Event"};
std::string log = message;
log += "@";
log += __PRETTY_FUNCTION__;
log += "in cppFriendsClangTest.cpp : 119";
bool cond = !out.tellp();
if (cond) DEBUG_PRINT_COUT(out, message);
EXPECT_EQ(log, out.str());
}
TEST_F(TestMacroExpansion, Ill) {
std::ostringstream out;
std::ostringstream err;
std::string message {"Event"};
std::string log = message;
log += "@";
log += __PRETTY_FUNCTION__;
log += ":";
EXPECT_FALSE(out.tellp());
ILL_DEBUG_PRINT(out, err, message);
EXPECT_EQ(log, out.str());
EXPECT_EQ(log, err.str());
// outには書いたので、条件はfalse
bool cond = !out.tellp();
if (cond) ILL_DEBUG_PRINT(out, err, message);
// outにはログが書かれないが、errには書かれてしまう
EXPECT_EQ(log, out.str());
EXPECT_NE(log, err.str());
}
TEST_F(TestMacroExpansion, Well) {
std::ostringstream out;
std::ostringstream err;
std::string message {"Event"};
std::string log = message;
log += "@";
log += __PRETTY_FUNCTION__;
log += ":";
WELL_DEBUG_PRINT(out, err, message);
EXPECT_EQ(log, out.str());
EXPECT_EQ(log, err.str());
bool cond = !out.tellp();
if (cond) WELL_DEBUG_PRINT(out, err, message);
EXPECT_EQ(log, out.str());
EXPECT_EQ(log, err.str());
#ifdef ONLY_PREPROCESSING
if (cond) BAD_DEBUG_PRINT1(out, err, message); else {}
if (cond) BAD_DEBUG_PRINT2(out, err, message); else {}
#endif
}
// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
// で規定されるGCC拡張
// fprintfはintを返す
#define FUNC_DEBUG_PRINTF(fdout, fderr, str) \
({ \
fprintf(fdout, "%s@%s", str, __PRETTY_FUNCTION__ ); \
fprintf(fderr, "%s@%s", str, __PRETTY_FUNCTION__ ); \
}) \
// oserrで終わると、oserrをコピーできないというコンパイルエラーが出るので
// 何か返す
#define FUNC_DEBUG_COUT(osout, oserr, str) \
({ \
osout << str << "@" << __PRETTY_FUNCTION__ << ":"; \
oserr << str << "@" << __PRETTY_FUNCTION__ << ":"; \
0; \
}) \
// Makefileからgrepで調べる
class TestCompoundStatement : public ::testing::Test{};
TEST_F(TestCompoundStatement, Printf) {
if (true) FUNC_DEBUG_PRINTF(stdout, stderr, "Printf in compound statements");
}
TEST_F(TestCompoundStatement, Cout) {
std::ostringstream out;
std::ostringstream err;
std::string message {"Cout in compound statements"};
std::string log = message;
log += "@";
log += __PRETTY_FUNCTION__;
log += ":";
bool cond = !out.tellp();
if (cond) FUNC_DEBUG_COUT(out, err, message);
EXPECT_EQ(log, out.str());
EXPECT_EQ(log, err.str());
}
TEST_F(TestCompoundStatement, Loop) {
std::ostringstream out;
std::ostringstream err;
std::string message;
std::string expected;
for(int i=1; i<=2; ++i, FUNC_DEBUG_COUT(out, err, message)) {
message = boost::lexical_cast<decltype(message)>(i);
std::string log = message;
log += "@";
log += __PRETTY_FUNCTION__;
log += ":";
expected += log;
};
EXPECT_EQ(expected, out.str());
EXPECT_EQ(expected, err.str());
}
class TestConditionalMove : public ::testing::Test{};
TEST_F(TestConditionalMove, All) {
ConditionalMove::SudokuCell cell;
ConditionalMove::SudokuCell other;
EXPECT_FALSE(cell.candidates_);
EXPECT_FALSE(other.candidates_);
cell.candidates_ = 3;
other.candidates_ = 0;
EXPECT_FALSE(cell.has_unique_candidate());
EXPECT_FALSE(cell.has_no_or_unique_candidates());
EXPECT_FALSE(other.has_unique_candidate());
EXPECT_TRUE(other.has_no_or_unique_candidates());
cell.filter_by_candidates_1(other);
EXPECT_EQ(3, cell.candidates_);
cell.candidates_ = 3;
other.candidates_ = 0;
cell.filter_by_candidates_2(other);
EXPECT_EQ(3, cell.candidates_);
cell.candidates_ = 3;
other.candidates_ = 2;
EXPECT_TRUE(other.has_unique_candidate());
EXPECT_TRUE(other.has_no_or_unique_candidates());
cell.filter_by_candidates_1(other);
EXPECT_EQ(1, cell.candidates_);
cell.candidates_ = 3;
other.candidates_ = 2;
cell.filter_by_candidates_2(other);
EXPECT_EQ(1, cell.candidates_);
}
namespace InferVariadicTemplate {
#if !defined(__clang__)
auto MyApply3(auto& f, auto&&... v) {
return f(std::forward<decltype(v)>(v)...);
};
#endif
}
class TestInferVariadicTemplate : public ::testing::Test{};
TEST_F(TestInferVariadicTemplate, All) {
auto sumTwo = [](const auto& lhs, const auto& rhs) {
return lhs + rhs;
};
EXPECT_EQ(5, InferVariadicTemplate::MyApply(sumTwo, 2, 3));
#if !defined(__clang__)
EXPECT_EQ(7, InferVariadicTemplate::MyApply2(sumTwo, 3, 4));
EXPECT_EQ(9, InferVariadicTemplate::MyApply3(sumTwo, 4, 5));
#endif
}
class TestMultiplier : public ::testing::Test{};
TEST_F(TestMultiplier, Int8) {
using Number = int8_t;
constexpr Number a = 13;
constexpr Number b = 3;
constexpr Number expected = 39;
EXPECT_EQ(expected, MulInt8(a, b));
EXPECT_EQ(-expected, MulInt8(-a, b));
EXPECT_EQ(-expected, MulInt8(a, -b));
EXPECT_EQ(expected, MulInt8(-a, -b));
}
TEST_F(TestMultiplier, Uint8) {
using Number = uint8_t;
constexpr Number a = 15;
constexpr Number b = 14;
constexpr Number expected = 210;
EXPECT_EQ(expected, MulUint8(a, b));
}
TEST_F(TestMultiplier, Int16) {
using Number = int16_t;
constexpr Number a = 270;
constexpr Number b = 5;
constexpr Number expected = 1350;
EXPECT_EQ(expected, MulInt16(a, b));
EXPECT_EQ(-expected, MulInt16(-a, b));
EXPECT_EQ(-expected, MulInt16(a, -b));
EXPECT_EQ(expected, MulInt16(-a, -b));
}
TEST_F(TestMultiplier, Uint16) {
using Number = uint16_t;
constexpr Number a = 251;
constexpr Number b = 231;
constexpr Number expected = 57981;
EXPECT_EQ(expected, MulUint16(a, b));
}
TEST_F(TestMultiplier, Int32) {
using Number = int32_t;
constexpr Number a = 160000;
constexpr Number b = 12345;
constexpr Number expected = 1975200000;
EXPECT_EQ(expected, MulInt32(a, b));
EXPECT_EQ(-expected, MulInt32(-a, b));
EXPECT_EQ(-expected, MulInt32(a, -b));
EXPECT_EQ(expected, MulInt32(-a, -b));
}
TEST_F(TestMultiplier, Uint32) {
using Number = uint32_t;
constexpr Number a = 320000u;
constexpr Number b = 12345u;
constexpr Number expected = 3950400000u;
EXPECT_EQ(expected, MulUint32(a, b));
}
TEST_F(TestMultiplier, Int64) {
using Number = int64_t;
constexpr Number a = 0x1fffffffffffffffll;
constexpr Number b = 2;
constexpr Number expected = 0x3ffffffffffffffell;
EXPECT_EQ(expected, MulInt64(a, b));
EXPECT_EQ(-expected, MulInt64(-a, b));
EXPECT_EQ(-expected, MulInt64(a, -b));
EXPECT_EQ(expected, MulInt64(-a, -b));
}
TEST_F(TestMultiplier, Uint64) {
using Number = uint64_t;
constexpr Number a = 0xfffffffffffffffull;
constexpr Number b = 16ull;
constexpr Number expected = 0xfffffffffffffff0ull;
EXPECT_EQ(expected, MulUint64(a, b));
}
class TestMyDivider : public ::testing::Test{};
TEST_F(TestMyDivider, Int8) {
using Number = int8_t;
constexpr Number dividend = 13;
constexpr Number divider = 3;
constexpr Number expectedQuo = 4;
constexpr Number expectedRem = 1;
EXPECT_EQ(expectedQuo, DivInt8(dividend, divider));
const auto actual = DivRemInt8(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
const auto actualNegative = DivRemInt8(-dividend, divider);
EXPECT_EQ(-expectedQuo, actualNegative.first);
EXPECT_EQ(-expectedRem, actualNegative.second);
}
TEST_F(TestMyDivider, Uint8) {
using Number = uint8_t;
constexpr Number dividend = 133;
constexpr Number divider = 33;
constexpr Number expectedQuo = 4;
constexpr Number expectedRem = 1;
EXPECT_EQ(expectedQuo, DivUint8(dividend, divider));
const auto actual = DivRemUint8(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
}
TEST_F(TestMyDivider, Int16) {
using Number = int16_t;
constexpr Number dividend = 23333;
constexpr Number divider = 7;
constexpr Number expectedQuo = 3333;
constexpr Number expectedRem = 2;
EXPECT_EQ(expectedQuo, DivInt16(dividend, divider));
const auto actual = DivRemInt16(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
const auto actualNegative = DivRemInt16(-dividend, divider);
EXPECT_EQ(-expectedQuo, actualNegative.first);
EXPECT_EQ(-expectedRem, actualNegative.second);
}
TEST_F(TestMyDivider, Uint16) {
using Number = uint16_t;
constexpr Number dividend = 53333;
constexpr Number divider = 17;
constexpr Number expectedQuo = 3137;
constexpr Number expectedRem = 4;
EXPECT_EQ(expectedQuo, DivUint16(dividend, divider));
const auto actual = DivRemUint16(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
}
TEST_F(TestMyDivider, Int32) {
using Number = int32_t;
constexpr Number dividend = 1333333333;
constexpr Number divider = 19;
constexpr Number expectedQuo = 70175438;
constexpr Number expectedRem = 11;
EXPECT_EQ(expectedQuo, DivInt32(dividend, divider));
const auto actual = DivRemInt32(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
const auto actualStd = std::div(dividend, divider);
EXPECT_EQ(expectedQuo, actualStd.quot);
EXPECT_EQ(expectedRem, actualStd.rem);
const auto actualNegative = DivRemInt32(-dividend, divider);
EXPECT_EQ(-expectedQuo, actualNegative.first);
EXPECT_EQ(-expectedRem, actualNegative.second);
const auto actualStdNegative = std::div(-dividend, divider);
EXPECT_EQ(-expectedQuo, actualStdNegative.quot);
EXPECT_EQ(-expectedRem, actualStdNegative.rem);
}
TEST_F(TestMyDivider, Uint32) {
using Number = uint32_t;
constexpr Number dividend = 3333333333u;
constexpr Number divider = 19u;
constexpr Number expectedQuo = 175438596u;
constexpr Number expectedRem = 9u;
EXPECT_EQ(expectedQuo, DivUint32(dividend, divider));
const auto actual = DivRemUint32(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
}
TEST_F(TestMyDivider, Int64) {
using Number = int64_t;
constexpr Number dividend = 8070450532247928832ll;
constexpr Number divider = 23ll;
constexpr Number expectedQuo = 350889153575996905ll;
constexpr Number expectedRem = 17ll;
EXPECT_EQ(expectedQuo, DivInt64(dividend, divider));
const auto actual = DivRemInt64(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
const auto actualStd = std::lldiv(dividend, divider);
EXPECT_EQ(expectedQuo, actualStd.quot);
EXPECT_EQ(expectedRem, actualStd.rem);
const auto actualNegative = DivRemInt64(-dividend, divider);
EXPECT_EQ(-expectedQuo, actualNegative.first);
EXPECT_EQ(-expectedRem, actualNegative.second);
const auto actualStdNegative = std::lldiv(-dividend, divider);
EXPECT_EQ(-expectedQuo, actualStdNegative.quot);
EXPECT_EQ(-expectedRem, actualStdNegative.rem);
}
TEST_F(TestMyDivider, Uint64) {
using Number = uint64_t;
constexpr Number dividend = 0xf0000000000000ffull;
constexpr Number divider = 256ull;
constexpr Number expectedQuo = 0xf0000000000000ull;
constexpr Number expectedRem = 255ull;
EXPECT_EQ(expectedQuo, DivUint64(dividend, divider));
const auto actual = DivRemUint64(dividend, divider);
EXPECT_EQ(expectedQuo, actual.first);
EXPECT_EQ(expectedRem, actual.second);
}
/*
Local Variables:
mode: c++
coding: utf-8-dos
tab-width: nil
c-file-style: "stroustrup"
End:
*/