20 #include <ripple/basics/XRPAmount.h>
21 #include <ripple/beast/unit_test.h>
33 for (
auto i : {-1, 0, 1})
38 BEAST_EXPECT(x.
signum() < 0);
40 BEAST_EXPECT(x.
signum() > 0);
42 BEAST_EXPECT(x.
signum() == 0);
49 testcase(
"beast::Zero Comparisons");
53 for (
auto i : {-1, 0, 1})
57 BEAST_EXPECT((i == 0) == (x == zero));
58 BEAST_EXPECT((i != 0) == (x != zero));
59 BEAST_EXPECT((i < 0) == (x < zero));
60 BEAST_EXPECT((i > 0) == (x > zero));
61 BEAST_EXPECT((i <= 0) == (x <= zero));
62 BEAST_EXPECT((i >= 0) == (x >= zero));
64 BEAST_EXPECT((0 == i) == (zero == x));
65 BEAST_EXPECT((0 != i) == (zero != x));
66 BEAST_EXPECT((0 < i) == (zero < x));
67 BEAST_EXPECT((0 > i) == (zero > x));
68 BEAST_EXPECT((0 <= i) == (zero <= x));
69 BEAST_EXPECT((0 >= i) == (zero >= x));
76 testcase(
"XRP Comparisons");
78 for (
auto i : {-1, 0, 1})
82 for (
auto j : {-1, 0, 1})
86 BEAST_EXPECT((i == j) == (x == y));
87 BEAST_EXPECT((i != j) == (x != y));
88 BEAST_EXPECT((i < j) == (x < y));
89 BEAST_EXPECT((i > j) == (x > y));
90 BEAST_EXPECT((i <= j) == (x <= y));
91 BEAST_EXPECT((i >= j) == (x >= y));
99 testcase(
"Addition & Subtraction");
101 for (
auto i : {-1, 0, 1})
105 for (
auto j : {-1, 0, 1})
109 BEAST_EXPECT(
XRPAmount(i + j) == (x + y));
110 BEAST_EXPECT(
XRPAmount(i - j) == (x - y));
112 BEAST_EXPECT((x + y) == (y + x));
124 BEAST_EXPECT(test.decimalXRP() == 0.000001);
127 BEAST_EXPECT(test.decimalXRP() == -0.000001);
130 BEAST_EXPECT(test.decimalXRP() == 100);
133 BEAST_EXPECT(test.decimalXRP() == -100);
146 BEAST_EXPECT(test.drops() == 0);
148 test = make(beast::zero);
149 BEAST_EXPECT(test.drops() == 0);
152 BEAST_EXPECT(test.drops() == 0);
155 BEAST_EXPECT(test.drops() == 100);
158 BEAST_EXPECT(test.drops() == 100);
161 test = make(targetSame);
162 BEAST_EXPECT(test.drops() == 200);
163 BEAST_EXPECT(test == targetSame);
168 BEAST_EXPECT(test.drops() == 200);
170 BEAST_EXPECT(test.drops() == 300);
173 BEAST_EXPECT(test.drops() == 200);
175 BEAST_EXPECT(testOther);
176 BEAST_EXPECT(*testOther == 200);
179 BEAST_EXPECT(!testOther);
182 BEAST_EXPECT(!testOther);
184 test = targetSame * 2;
185 BEAST_EXPECT(test.drops() == 400);
186 test = 3 * targetSame;
187 BEAST_EXPECT(test.drops() == 600);
189 BEAST_EXPECT(test.drops() == 20);
192 BEAST_EXPECT(test.drops() == 220);
195 BEAST_EXPECT(test.drops() == 20);
198 BEAST_EXPECT(test.drops() == 100);
200 BEAST_EXPECT(test.drops() == 50);
202 BEAST_EXPECT(test.drops() == 11);
206 BEAST_EXPECT(test.drops() == -11);
207 BEAST_EXPECT(test.signum() == -1);
213 BEAST_EXPECT(test.signum() == 0);
215 BEAST_EXPECT(test.signum() == 1);
222 testcase(
"mulRatio");
225 constexpr
auto maxXRP =
227 constexpr
auto minXRP =
234 BEAST_EXPECT(big ==
mulRatio(big, maxUInt32, maxUInt32,
true));
236 BEAST_EXPECT(big ==
mulRatio(big, maxUInt32, maxUInt32,
false));
242 mulRatio(big, 3, 4,
false).value() == (big.
value() / 4) * 3);
245 BEAST_EXPECT((big.
value() * 3) / 4 != (big.
value() / 4) * 3);
251 BEAST_EXPECT(big ==
mulRatio(big, maxUInt32, maxUInt32,
true));
253 BEAST_EXPECT(big ==
mulRatio(big, maxUInt32, maxUInt32,
false));
258 mulRatio(big, 3, 4,
false).value() == (big.
value() / 4) * 3);
261 BEAST_EXPECT((big.
value() * 3) / 4 != (big.
value() / 4) * 3);
268 BEAST_EXPECT(tiny ==
mulRatio(tiny, 1, maxUInt32,
true));
270 BEAST_EXPECT(beast::zero ==
mulRatio(tiny, 1, maxUInt32,
false));
272 beast::zero ==
mulRatio(tiny, maxUInt32 - 1, maxUInt32,
false));
277 BEAST_EXPECT(beast::zero ==
mulRatio(tinyNeg, 1, maxUInt32,
true));
280 mulRatio(tinyNeg, maxUInt32 - 1, maxUInt32,
true));
283 tinyNeg ==
mulRatio(tinyNeg, maxUInt32 - 1, maxUInt32,
false));
288 auto const rup =
mulRatio(
one, maxUInt32 - 1, maxUInt32,
true);
289 auto const rdown =
mulRatio(
one, maxUInt32 - 1, maxUInt32,
false);
290 BEAST_EXPECT(rup.drops() - rdown.drops() == 1);
295 auto const rup =
mulRatio(big, maxUInt32 - 1, maxUInt32,
true);
296 auto const rdown =
mulRatio(big, maxUInt32 - 1, maxUInt32,
false);
297 BEAST_EXPECT(rup.drops() - rdown.drops() == 1);
302 auto const rup =
mulRatio(negOne, maxUInt32 - 1, maxUInt32,
true);
303 auto const rdown =
mulRatio(negOne, maxUInt32 - 1, maxUInt32,
false);
304 BEAST_EXPECT(rup.drops() - rdown.drops() == 1);
317 except([&] {
mulRatio(big, 2, 1,
true); });
323 BEAST_EXPECT(
mulRatio(bigNegative, 2, 1,
true) == minXRP);