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
|
#include "testing/testing.hpp"
#include "base/newtype.hpp"
#include "std/sstream.hpp"
#include "std/type_traits.hpp"
namespace
{
NEWTYPE(int, Int);
string DebugPrint(Int const & i)
{
stringstream sstr;
sstr << "Int(" << i.Get() << ')';
return sstr.str();
}
UNIT_TEST(NewType_TypeChecks)
{
TEST((is_constructible<Int, int>::value), ());
TEST((is_constructible<Int, char>::value), ());
TEST(!(is_convertible<int, Int>::value), ());
TEST(!(is_convertible<Int, int>::value), ());
}
UNIT_TEST(NewType_Base)
{
Int a{10};
TEST_EQUAL(a.Get(), 10, ());
a.Set(100);
TEST_EQUAL(a.Get(), 100, ());
}
UNIT_TEST(NewType_Operations)
{
TEST(Int(10) == Int(10), ());
TEST(Int(20) != Int(30), ());
TEST(Int(10) < Int(20), ());
TEST(Int(10) <= Int(20), ());
TEST(Int(20) > Int(10), ());
TEST(Int(20) >= Int(10), ());
TEST_EQUAL(Int(10) + Int(20), Int(30), ());
TEST_EQUAL(Int(10) - Int(20), Int(-10), ());
TEST_EQUAL(Int(10) / Int(2), Int(5), ());
TEST_EQUAL(Int(10) * Int(2), Int(20), ());
TEST_EQUAL(Int(10) % Int(3), Int(1), ());
TEST_EQUAL(Int(10) | Int(7), Int(10 | 7), ());
TEST_EQUAL(Int(10) & Int(7), Int(10 & 7), ());
TEST_EQUAL(Int(10) ^ Int(7), Int(10 ^ 7), ());
}
UNIT_TEST(NewTypeMember_Operations)
{
Int a(10);
auto b = a--;
TEST_EQUAL(a, Int(9), ());
TEST_EQUAL(b, Int(10), ());
b = --a;
TEST_EQUAL(a, b, ());
TEST_EQUAL(a, Int(8), ());
b = ++a;
TEST_EQUAL(a, b, ());
TEST_EQUAL(a, Int(9), ());
b = a++;
TEST_EQUAL(a, Int(10), ());
TEST_EQUAL(b, Int(9), ());
a.Set(100);
b = Int(2);
a *= b;
TEST_EQUAL(a, Int(200), ());
a /= b;
TEST_EQUAL(a, Int(100), ());
b.Set(3);
a %= b;
TEST_EQUAL(a, Int(1), ());
a.Set(10);
a |= Int(7);
TEST_EQUAL(a, Int(10 | 7), ());
a.Set(10);
a &= Int(7);
TEST_EQUAL(a, Int(10 & 7), ());
a.Set(10);
a ^= Int(7);
TEST_EQUAL(a, Int(10 ^ 7), ());
}
} // namespace
|