rippled
Loading...
Searching...
No Matches
Units_test.cpp
1#include <xrpl/beast/unit_test.h>
2#include <xrpl/protocol/SystemParameters.h>
3#include <xrpl/protocol/Units.h>
4
5namespace xrpl {
6namespace test {
7
9{
10private:
11 void
13 {
14 using FeeLevel32 = FeeLevel<std::uint32_t>;
15
16 {
17 XRPAmount x{100};
18 BEAST_EXPECT(x.drops() == 100);
19 BEAST_EXPECT((std::is_same_v<decltype(x)::unit_type, unit::dropTag>));
20 auto y = 4u * x;
21 BEAST_EXPECT(y.value() == 400);
22 BEAST_EXPECT((std::is_same_v<decltype(y)::unit_type, unit::dropTag>));
23
24 auto z = 4 * y;
25 BEAST_EXPECT(z.value() == 1600);
26 BEAST_EXPECT((std::is_same_v<decltype(z)::unit_type, unit::dropTag>));
27
28 FeeLevel32 f{10};
29 FeeLevel32 baseFee{100};
30
31 auto drops = mulDiv(baseFee, x, f);
32
33 BEAST_EXPECT(drops);
34 BEAST_EXPECT(drops.value() == 1000);
35 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>::unit_type, unit::dropTag>));
36
37 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
38 }
39 {
40 XRPAmount x{100};
41 BEAST_EXPECT(x.value() == 100);
42 BEAST_EXPECT((std::is_same_v<decltype(x)::unit_type, unit::dropTag>));
43 auto y = 4u * x;
44 BEAST_EXPECT(y.value() == 400);
45 BEAST_EXPECT((std::is_same_v<decltype(y)::unit_type, unit::dropTag>));
46
47 FeeLevel64 f{10};
48 FeeLevel64 baseFee{100};
49
50 auto drops = mulDiv(baseFee, x, f);
51
52 BEAST_EXPECT(drops);
53 BEAST_EXPECT(drops.value() == 1000);
54 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>::unit_type, unit::dropTag>));
55 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
56 }
57 {
58 FeeLevel64 x{1024};
59 BEAST_EXPECT(x.value() == 1024);
60 BEAST_EXPECT((std::is_same_v<decltype(x)::unit_type, unit::feelevelTag>));
61 std::uint64_t m = 4;
62 auto y = m * x;
63 BEAST_EXPECT(y.value() == 4096);
64 BEAST_EXPECT((std::is_same_v<decltype(y)::unit_type, unit::feelevelTag>));
65
66 XRPAmount basefee{10};
67 FeeLevel64 referencefee{256};
68
69 auto drops = mulDiv(x, basefee, referencefee);
70
71 BEAST_EXPECT(drops);
72 BEAST_EXPECT(drops.value() == 40);
73 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>::unit_type, unit::dropTag>));
74 BEAST_EXPECT((std::is_same_v<std::remove_reference_t<decltype(*drops)>, XRPAmount>));
75 }
76 }
77
78 void
80 {
81 // Json value functionality
82 using FeeLevel32 = FeeLevel<std::uint32_t>;
83
84 {
86 auto y = x.jsonClipped();
87 BEAST_EXPECT(y.type() == Json::uintValue);
88 BEAST_EXPECT(y == Json::Value{x.fee()});
89 }
90
91 {
93 auto y = x.jsonClipped();
94 BEAST_EXPECT(y.type() == Json::uintValue);
95 BEAST_EXPECT(y == Json::Value{x.fee()});
96 }
97
98 {
100 auto y = x.jsonClipped();
101 BEAST_EXPECT(y.type() == Json::uintValue);
103 }
104
105 {
107 auto y = x.jsonClipped();
108 BEAST_EXPECT(y.type() == Json::uintValue);
109 BEAST_EXPECT(y == Json::Value{0});
110 }
111
112 {
114 auto y = x.jsonClipped();
115 BEAST_EXPECT(y.type() == Json::realValue);
116 BEAST_EXPECT(y == Json::Value{std::numeric_limits<double>::max()});
117 }
118
119 {
121 auto y = x.jsonClipped();
122 BEAST_EXPECT(y.type() == Json::realValue);
123 BEAST_EXPECT(y == Json::Value{std::numeric_limits<double>::min()});
124 }
125
126 {
128 auto y = x.jsonClipped();
129 BEAST_EXPECT(y.type() == Json::intValue);
131 }
132
133 {
135 auto y = x.jsonClipped();
136 BEAST_EXPECT(y.type() == Json::intValue);
138 }
139 }
140
141 void
143 {
144 // Explicitly test every defined function for the ValueUnit class
145 // since some of them are templated, but not used anywhere else.
146 using FeeLevel32 = FeeLevel<std::uint32_t>;
147
148 {
149 auto make = [&](auto x) -> FeeLevel64 { return x; };
150 auto explicitmake = [&](auto x) -> FeeLevel64 { return FeeLevel64{x}; };
151
152 [[maybe_unused]]
153 FeeLevel64 defaulted;
154 FeeLevel64 test{0};
155 BEAST_EXPECT(test.fee() == 0);
156
157 test = explicitmake(beast::zero);
158 BEAST_EXPECT(test.fee() == 0);
159
160 test = beast::zero;
161 BEAST_EXPECT(test.fee() == 0);
162
163 test = explicitmake(100u);
164 BEAST_EXPECT(test.fee() == 100);
165
166 FeeLevel64 const targetSame{200u};
167 FeeLevel32 const targetOther{300u};
168 test = make(targetSame);
169 BEAST_EXPECT(test.fee() == 200);
170 BEAST_EXPECT(test == targetSame);
171 BEAST_EXPECT(test < FeeLevel64{1000});
172 BEAST_EXPECT(test > FeeLevel64{100});
173 test = make(targetOther);
174 BEAST_EXPECT(test.fee() == 300);
175 BEAST_EXPECT(test == targetOther);
176
177 test = std::uint64_t(200);
178 BEAST_EXPECT(test.fee() == 200);
179 test = std::uint32_t(300);
180 BEAST_EXPECT(test.fee() == 300);
181
182 test = targetSame;
183 BEAST_EXPECT(test.fee() == 200);
184 test = targetOther.fee();
185 BEAST_EXPECT(test.fee() == 300);
186 BEAST_EXPECT(test == targetOther);
187
188 test = targetSame * 2;
189 BEAST_EXPECT(test.fee() == 400);
190 test = 3 * targetSame;
191 BEAST_EXPECT(test.fee() == 600);
192 test = targetSame / 10;
193 BEAST_EXPECT(test.fee() == 20);
194
195 test += targetSame;
196 BEAST_EXPECT(test.fee() == 220);
197
198 test -= targetSame;
199 BEAST_EXPECT(test.fee() == 20);
200
201 test++;
202 BEAST_EXPECT(test.fee() == 21);
203 ++test;
204 BEAST_EXPECT(test.fee() == 22);
205 test--;
206 BEAST_EXPECT(test.fee() == 21);
207 --test;
208 BEAST_EXPECT(test.fee() == 20);
209
210 test *= 5;
211 BEAST_EXPECT(test.fee() == 100);
212 test /= 2;
213 BEAST_EXPECT(test.fee() == 50);
214 test %= 13;
215 BEAST_EXPECT(test.fee() == 11);
216
217 /*
218 // illegal with unsigned
219 test = -test;
220 BEAST_EXPECT(test.fee() == -11);
221 BEAST_EXPECT(test.signum() == -1);
222 BEAST_EXPECT(to_string(test) == "-11");
223 */
224
225 BEAST_EXPECT(test);
226 test = 0;
227 BEAST_EXPECT(!test);
228 BEAST_EXPECT(test.signum() == 0);
229 test = targetSame;
230 BEAST_EXPECT(test.signum() == 1);
231 BEAST_EXPECT(to_string(test) == "200");
232 }
233 {
234 auto make = [&](auto x) -> FeeLevelDouble { return x; };
235 auto explicitmake = [&](auto x) -> FeeLevelDouble { return FeeLevelDouble{x}; };
236
237 [[maybe_unused]]
238 FeeLevelDouble defaulted;
239 FeeLevelDouble test{0};
240 BEAST_EXPECT(test.fee() == 0);
241
242 test = explicitmake(beast::zero);
243 BEAST_EXPECT(test.fee() == 0);
244
245 test = beast::zero;
246 BEAST_EXPECT(test.fee() == 0);
247
248 test = explicitmake(100.0);
249 BEAST_EXPECT(test.fee() == 100);
250
251 FeeLevelDouble const targetSame{200.0};
252 FeeLevel64 const targetOther{300};
253 test = make(targetSame);
254 BEAST_EXPECT(test.fee() == 200);
255 BEAST_EXPECT(test == targetSame);
256 BEAST_EXPECT(test < FeeLevelDouble{1000.0});
257 BEAST_EXPECT(test > FeeLevelDouble{100.0});
258 test = targetOther.fee();
259 BEAST_EXPECT(test.fee() == 300);
260 BEAST_EXPECT(test == targetOther);
261
262 test = 200.0;
263 BEAST_EXPECT(test.fee() == 200);
264 test = std::uint64_t(300);
265 BEAST_EXPECT(test.fee() == 300);
266
267 test = targetSame;
268 BEAST_EXPECT(test.fee() == 200);
269
270 test = targetSame * 2;
271 BEAST_EXPECT(test.fee() == 400);
272 test = 3 * targetSame;
273 BEAST_EXPECT(test.fee() == 600);
274 test = targetSame / 10;
275 BEAST_EXPECT(test.fee() == 20);
276
277 test += targetSame;
278 BEAST_EXPECT(test.fee() == 220);
279
280 test -= targetSame;
281 BEAST_EXPECT(test.fee() == 20);
282
283 test++;
284 BEAST_EXPECT(test.fee() == 21);
285 ++test;
286 BEAST_EXPECT(test.fee() == 22);
287 test--;
288 BEAST_EXPECT(test.fee() == 21);
289 --test;
290 BEAST_EXPECT(test.fee() == 20);
291
292 test *= 5;
293 BEAST_EXPECT(test.fee() == 100);
294 test /= 2;
295 BEAST_EXPECT(test.fee() == 50);
296 /* illegal with floating
297 test %= 13;
298 BEAST_EXPECT(test.fee() == 11);
299 */
300
301 // legal with signed
302 test = -test;
303 BEAST_EXPECT(test.fee() == -50);
304 BEAST_EXPECT(test.signum() == -1);
305 BEAST_EXPECT(to_string(test) == "-50.000000");
306
307 BEAST_EXPECT(test);
308 test = 0;
309 BEAST_EXPECT(!test);
310 BEAST_EXPECT(test.signum() == 0);
311 test = targetSame;
312 BEAST_EXPECT(test.signum() == 1);
313 BEAST_EXPECT(to_string(test) == "200.000000");
314 }
315 }
316
317public:
318 void
319 run() override
320 {
321 BEAST_EXPECT(INITIAL_XRP.drops() == 100'000'000'000'000'000);
322 BEAST_EXPECT(INITIAL_XRP == XRPAmount{100'000'000'000'000'000});
323
324 testTypes();
325 testJson();
327 }
328};
329
330BEAST_DEFINE_TESTSUITE(units, basics, xrpl);
331
332} // namespace test
333} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
A testsuite class.
Definition suite.h:52
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:158
Json::Value jsonClipped() const
Definition XRPAmount.h:197
void run() override
Runs the suite.
Json::Value jsonClipped() const
Definition Units.h:291
T is_same_v
T max(T... args)
T min(T... args)
@ realValue
double value
Definition json_value.h:23
@ intValue
signed integer value
Definition json_value.h:21
@ uintValue
unsigned integer value
Definition json_value.h:22
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
STAmount const & value() const