20 #include <ripple/basics/XRPAmount.h>
21 #include <ripple/beast/unit_test.h>
32 for (
auto i : { -1, 0, 1})
37 BEAST_EXPECT(x.
signum () < 0);
39 BEAST_EXPECT(x.
signum () > 0);
41 BEAST_EXPECT(x.
signum () == 0);
47 testcase (
"beast::Zero Comparisons");
51 for (
auto i : { -1, 0, 1})
55 BEAST_EXPECT((i == 0) == (x == zero));
56 BEAST_EXPECT((i != 0) == (x != zero));
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));
62 BEAST_EXPECT((0 == i) == (zero == x));
63 BEAST_EXPECT((0 != i) == (zero != x));
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));
73 testcase (
"XRP Comparisons");
75 for (
auto i : { -1, 0, 1})
79 for (
auto j : { -1, 0, 1})
83 BEAST_EXPECT((i == j) == (x == y));
84 BEAST_EXPECT((i != j) == (x != y));
85 BEAST_EXPECT((i < j) == (x < y));
86 BEAST_EXPECT((i > j) == (x > y));
87 BEAST_EXPECT((i <= j) == (x <= y));
88 BEAST_EXPECT((i >= j) == (x >= y));
95 testcase (
"Addition & Subtraction");
97 for (
auto i : { -1, 0, 1})
101 for (
auto j : { -1, 0, 1})
105 BEAST_EXPECT(
XRPAmount(i + j) == (x + y));
106 BEAST_EXPECT(
XRPAmount(i - j) == (x - y));
108 BEAST_EXPECT((x + y) == (y + x));
119 BEAST_EXPECT(test.decimalXRP() == 0.000001);
122 BEAST_EXPECT(test.decimalXRP() == -0.000001);
125 BEAST_EXPECT(test.decimalXRP() == 100);
128 BEAST_EXPECT(test.decimalXRP() == -100);
140 BEAST_EXPECT(test.drops() == 0);
142 test = make(beast::zero);
143 BEAST_EXPECT(test.drops() == 0);
146 BEAST_EXPECT(test.drops() == 0);
149 BEAST_EXPECT(test.drops() == 100);
152 BEAST_EXPECT(test.drops() == 100);
155 test = make(targetSame);
156 BEAST_EXPECT(test.drops() == 200);
157 BEAST_EXPECT(test == targetSame);
162 BEAST_EXPECT(test.drops() == 200);
164 BEAST_EXPECT(test.drops() == 300);
167 BEAST_EXPECT(test.drops() == 200);
169 BEAST_EXPECT(testOther);
170 BEAST_EXPECT(*testOther == 200);
173 BEAST_EXPECT(!testOther);
176 BEAST_EXPECT(!testOther);
178 test = targetSame * 2;
179 BEAST_EXPECT(test.drops() == 400);
180 test = 3 * targetSame;
181 BEAST_EXPECT(test.drops() == 600);
183 BEAST_EXPECT(test.drops() == 20);
186 BEAST_EXPECT(test.drops() == 220);
189 BEAST_EXPECT(test.drops() == 20);
192 BEAST_EXPECT(test.drops() == 100);
194 BEAST_EXPECT(test.drops() == 50);
196 BEAST_EXPECT(test.drops() == 11);
200 BEAST_EXPECT(test.drops() == -11);
201 BEAST_EXPECT(test.signum() == -1);
207 BEAST_EXPECT(test.signum() == 0);
209 BEAST_EXPECT(test.signum() == 1);
215 testcase (
"mulRatio");
225 BEAST_EXPECT(big ==
mulRatio (big, maxUInt32, maxUInt32,
true));
227 BEAST_EXPECT(big ==
mulRatio (big, maxUInt32, maxUInt32,
false));
232 BEAST_EXPECT(
mulRatio(big, 3, 4,
false).value() == (big.value() / 4) * 3);
233 BEAST_EXPECT(
mulRatio(big, 3, 4,
true).value() == (big.value() / 4) * 3);
234 BEAST_EXPECT((big.value() * 3) / 4 != (big.value() / 4) * 3);
240 BEAST_EXPECT(big ==
mulRatio (big, maxUInt32, maxUInt32,
true));
242 BEAST_EXPECT(big ==
mulRatio (big, maxUInt32, maxUInt32,
false));
246 BEAST_EXPECT(
mulRatio(big, 3, 4,
false).value() == (big.value() / 4) * 3);
247 BEAST_EXPECT(
mulRatio(big, 3, 4,
true).value() == (big.value() / 4) * 3);
248 BEAST_EXPECT((big.value() * 3) / 4 != (big.value() / 4) * 3);
255 BEAST_EXPECT(tiny ==
mulRatio (tiny, 1, maxUInt32,
true));
257 BEAST_EXPECT(beast::zero ==
mulRatio (tiny, 1, maxUInt32,
false));
258 BEAST_EXPECT(beast::zero ==
259 mulRatio (tiny, maxUInt32 - 1, maxUInt32,
false));
264 BEAST_EXPECT(beast::zero ==
mulRatio (tinyNeg, 1, maxUInt32,
true));
265 BEAST_EXPECT(beast::zero ==
mulRatio (tinyNeg, maxUInt32 - 1, maxUInt32,
true));
267 BEAST_EXPECT(tinyNeg ==
mulRatio (tinyNeg, maxUInt32 - 1, maxUInt32,
false));
274 auto const rup =
mulRatio (one, maxUInt32 - 1, maxUInt32,
true);
275 auto const rdown =
mulRatio (one, maxUInt32 - 1, maxUInt32,
false);
276 BEAST_EXPECT(rup.drops () - rdown.drops () == 1);
281 auto const rup =
mulRatio (big, maxUInt32 - 1, maxUInt32,
true);
282 auto const rdown =
mulRatio (big, maxUInt32 - 1, maxUInt32,
false);
283 BEAST_EXPECT(rup.drops () - rdown.drops () == 1);
288 auto const rup =
mulRatio (negOne, maxUInt32 - 1, maxUInt32,
true);
289 auto const rdown =
mulRatio (negOne, maxUInt32 - 1, maxUInt32,
false);
290 BEAST_EXPECT(rup.drops () - rdown.drops () == 1);
297 except ([&] {
mulRatio (one, 1, 0,
true);});
303 except ([&] {
mulRatio (big, 2, 1,
true);});
309 BEAST_EXPECT(
mulRatio(bigNegative, 2, 1,
true) == minXRP);