-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.cpp
189 lines (185 loc) · 5.65 KB
/
tests.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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "BigDecimal.h"
#include <concepts>
namespace {
template<std::integral T>
void checkConstructorI() {
INFO("Type: ",typeid(T).name());
//These constructors should copy the value exactly
CHECK(BigDecimal{std::numeric_limits<T>::max()}.toInt64() == std::numeric_limits<T>::max());
CHECK(BigDecimal{std::numeric_limits<T>::min()}.toInt64() == std::numeric_limits<T>::min());
}
template<std::floating_point T>
void checkConstructorF() {
INFO("Type: ",typeid(T).name());
//Floating point numbers are not copied exactly; however, ones that store an integer should be
CHECK(BigDecimal{static_cast<T>(50)}.toString() == "5e+1");
CHECK(BigDecimal{static_cast<T>(12345)}.toString() == "12345");
}
}
TEST_SUITE("Constructors") {
TEST_CASE("Numeric") {
CHECK(BigDecimal{}.toInt64() == 0);
checkConstructorI<long long>();
checkConstructorI<long>();
checkConstructorI<int>();
checkConstructorI<short>();
checkConstructorI<char>();
checkConstructorF<long double>();
checkConstructorF<double>();
checkConstructorF<float>();
}
TEST_CASE("String") {
CHECK(BigDecimal{"123456789.7e+50"}.toString() == "1234567897e+49");
CHECK(BigDecimal{""} == 0);
CHECK(BigDecimal{std::string("123456789.7e50")}.toString() == "1234567897e+49");
CHECK(BigDecimal{"01000.000e5"} == BigDecimal{"1000e5"});
CHECK(BigDecimal{".5e1"} == 5);
CHECK_THROWS_AS(BigDecimal{"1.0ea"}, std::invalid_argument);
CHECK_THROWS_AS(BigDecimal{"1e."}, std::invalid_argument);
CHECK_THROWS_AS(BigDecimal{"a"}, std::invalid_argument);
CHECK_THROWS_AS(BigDecimal{"1.b"}, std::invalid_argument);
}
}
TEST_SUITE("Conversions") {
TEST_CASE("toInt64") {
CHECK(BigDecimal{"1.7e10"}.toInt64() == 17e9);
CHECK_THROWS_AS(BigDecimal{"9e19"}.toInt64(), std::overflow_error);
}
TEST_CASE("toLongDouble") {
CHECK(BigDecimal{}.toLongDouble() == 0);
//Small integers can be precisely represented as doubles
CHECK(BigDecimal{12345}.toLongDouble() == 12345);
}
TEST_CASE("toString") {
CHECK(BigDecimal{}.toString() == "0");
CHECK(BigDecimal{"-50001e-2"}.toString() == "-500.01");
CHECK(BigDecimal{"100e-4"}.toString() == "1e-2");
}
}
TEST_SUITE("Operators") {
TEST_CASE("+=") {
BigDecimal bd{1};
bd += BigDecimal{"1e10"};
CHECK(bd == 10000000001);
bd = BigDecimal{"1e10"};
bd += 1;
CHECK(bd == 10000000001);
bd = BigDecimal{"233465.76894e-50"};
bd += BigDecimal{"-233465.76894e-50"};
CHECK(bd == 0);
}
TEST_CASE("-=") {
BigDecimal bd{1234};
bd -= bd;
CHECK(bd == 0);
bd = -1234;
bd -= 1234;
CHECK(bd == -2468);
}
TEST_CASE("*=") {
BigDecimal bd{"1e50"};
bd *= 5;
CHECK(bd == BigDecimal{"5e50"});
bd *= bd;
CHECK(bd == BigDecimal{"25e100"});
bd *= BigDecimal{"0.2"};
CHECK(bd == BigDecimal{"5e100"});
}
TEST_CASE("/=") {
BigDecimal bd{1000};
bd /= BigDecimal{"0.2"};
CHECK(bd == 5000);
bd /= 200;
CHECK(bd == 25);
bd /= BigDecimal{"5e-5"};
CHECK(bd == 500000);
bd /= BigDecimal{"1e7"};
CHECK(bd == BigDecimal{".05"});
}
TEST_CASE("%=") {
BigDecimal bd{1000};
bd %= 7;
CHECK(bd == 6);
bd %= 0.7;
CHECK(bd == BigDecimal{"0.4"});
bd %= BigDecimal{"2e50"};
CHECK(bd == BigDecimal{"0.4"});
bd %= bd;
CHECK(bd == 0);
bd = -13;
bd %= 3;
CHECK(bd == -1);
bd = -13;
bd %= -3;
CHECK(bd == -1);
}
TEST_CASE("++ prefix") {
BigDecimal bd = 5;
CHECK(++bd == 6);
bd = -5;
CHECK(++bd == -4);
}
TEST_CASE("++ postfix") {
BigDecimal bd = 5;
CHECK(bd++ == 5);
CHECK(bd == 6);
bd = -5;
CHECK(bd++ == -5);
CHECK(bd == -4);
}
TEST_CASE("-- prefix") {
BigDecimal bd = 5;
CHECK(--bd == 4);
bd = -5;
CHECK(--bd == -6);
}
TEST_CASE("-- postfix") {
BigDecimal bd = 5;
CHECK(bd-- == 5);
CHECK(bd == 4);
bd = -5;
CHECK(bd-- == -5);
CHECK(bd == -6);
}
TEST_CASE("<<") {
std::stringstream ss{};
ss << BigDecimal{"-50001e-2"};
CHECK(ss.str() == "-500.01");
}
TEST_CASE(">>") {
BigDecimal bd;
std::stringstream ss{"-5.7e+10 7.1ea\n12345"};
ss >> bd;
CHECK(bd.toString() == "-57e+9");
CHECK_FALSE(ss.fail());
ss >> bd;
CHECK(ss.fail());
ss.clear();
ss >> bd;
CHECK(bd == 12345);
CHECK_FALSE(ss.fail());
}
//Non-assignment operators are all defined in terms of the assignment ones; if these fail, the corresponding assignment one should too
TEST_CASE("+") {
CHECK(BigDecimal{5} + 10 == 15);
CHECK(1 + BigDecimal{"1e+5"} == 100001);
}
TEST_CASE("-") {
CHECK(BigDecimal{5} - 10 == -5);
CHECK(1 - BigDecimal{"1e+5"} == -99999);
}
TEST_CASE("*") {
CHECK(BigDecimal{5} * 10 == 50);
CHECK(2 * BigDecimal{"1e+5"} == 200000);
}
TEST_CASE("/") {
CHECK(BigDecimal{5} / 10 == BigDecimal{"0.5"});
CHECK(1 / BigDecimal{"1e+5"} == 1e-5);
}
TEST_CASE("%") {
CHECK(BigDecimal{56} % 11 == 1);
CHECK(10 % BigDecimal{"7e-5"} == BigDecimal{"1e-5"});
}
}