3#include <xrpl/basics/random.h>
4#include <xrpl/beast/unit_test.h>
5#include <xrpl/protocol/STAmount.h>
6#include <xrpl/protocol/XRPAmount.h>
38 return {amount.issue(), mantissa, amount.exponent(), amount.negative()};
48 if (valueDigits == 999999999)
53 return {amount.issue(), mantissa, amount.exponent(), amount.negative()};
76 BEAST_EXPECT(!res.
native());
80 BEAST_EXPECT(!cmp.
native());
100 BEAST_EXPECT(!prod1.
native());
109 fail(
"Multiplication result is not exact");
121 BEAST_EXPECT(amount.getText() == value);
211 unexpected(zeroSt != beast::zero,
"STAmount fail");
212 unexpected(one == beast::zero,
"STAmount fail");
213 unexpected(hundred == beast::zero,
"STAmount fail");
214 unexpected((zeroSt < zeroSt),
"STAmount fail");
216 unexpected(!(zeroSt < hundred),
"STAmount fail");
219 unexpected(!(one < hundred),
"STAmount fail");
220 unexpected((hundred < zeroSt),
"STAmount fail");
222 unexpected((hundred < hundred),
"STAmount fail");
223 unexpected((zeroSt > zeroSt),
"STAmount fail");
225 unexpected((zeroSt > hundred),
"STAmount fail");
229 unexpected(!(hundred > zeroSt),
"STAmount fail");
230 unexpected(!(hundred > one),
"STAmount fail");
231 unexpected((hundred > hundred),
"STAmount fail");
232 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
233 unexpected(!(zeroSt <= one),
"STAmount fail");
234 unexpected(!(zeroSt <= hundred),
"STAmount fail");
237 unexpected(!(one <= hundred),
"STAmount fail");
238 unexpected((hundred <= zeroSt),
"STAmount fail");
239 unexpected((hundred <= one),
"STAmount fail");
240 unexpected(!(hundred <= hundred),
"STAmount fail");
241 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
243 unexpected((zeroSt >= hundred),
"STAmount fail");
244 unexpected(!(one >= zeroSt),
"STAmount fail");
246 unexpected((one >= hundred),
"STAmount fail");
247 unexpected(!(hundred >= zeroSt),
"STAmount fail");
248 unexpected(!(hundred >= one),
"STAmount fail");
249 unexpected(!(hundred >= hundred),
"STAmount fail");
250 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
252 unexpected((zeroSt == hundred),
"STAmount fail");
255 unexpected((one == hundred),
"STAmount fail");
256 unexpected((hundred == zeroSt),
"STAmount fail");
257 unexpected((hundred == one),
"STAmount fail");
258 unexpected(!(hundred == hundred),
"STAmount fail");
259 unexpected((zeroSt != zeroSt),
"STAmount fail");
260 unexpected(!(zeroSt != one),
"STAmount fail");
261 unexpected(!(zeroSt != hundred),
"STAmount fail");
262 unexpected(!(one != zeroSt),
"STAmount fail");
264 unexpected(!(one != hundred),
"STAmount fail");
265 unexpected(!(hundred != zeroSt),
"STAmount fail");
266 unexpected(!(hundred != one),
"STAmount fail");
267 unexpected((hundred != hundred),
"STAmount fail");
276 std::string const cur =
"015841551A748AD2C1F76FF6ECB0CCCD00000000";
297 unexpected(zeroSt != beast::zero,
"STAmount fail");
298 unexpected(one == beast::zero,
"STAmount fail");
299 unexpected(hundred == beast::zero,
"STAmount fail");
300 unexpected((zeroSt < zeroSt),
"STAmount fail");
302 unexpected(!(zeroSt < hundred),
"STAmount fail");
305 unexpected(!(one < hundred),
"STAmount fail");
306 unexpected((hundred < zeroSt),
"STAmount fail");
308 unexpected((hundred < hundred),
"STAmount fail");
309 unexpected((zeroSt > zeroSt),
"STAmount fail");
311 unexpected((zeroSt > hundred),
"STAmount fail");
315 unexpected(!(hundred > zeroSt),
"STAmount fail");
316 unexpected(!(hundred > one),
"STAmount fail");
317 unexpected((hundred > hundred),
"STAmount fail");
318 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
319 unexpected(!(zeroSt <= one),
"STAmount fail");
320 unexpected(!(zeroSt <= hundred),
"STAmount fail");
323 unexpected(!(one <= hundred),
"STAmount fail");
324 unexpected((hundred <= zeroSt),
"STAmount fail");
325 unexpected((hundred <= one),
"STAmount fail");
326 unexpected(!(hundred <= hundred),
"STAmount fail");
327 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
329 unexpected((zeroSt >= hundred),
"STAmount fail");
330 unexpected(!(one >= zeroSt),
"STAmount fail");
332 unexpected((one >= hundred),
"STAmount fail");
333 unexpected(!(hundred >= zeroSt),
"STAmount fail");
334 unexpected(!(hundred >= one),
"STAmount fail");
335 unexpected(!(hundred >= hundred),
"STAmount fail");
336 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
338 unexpected((zeroSt == hundred),
"STAmount fail");
341 unexpected((one == hundred),
"STAmount fail");
342 unexpected((hundred == zeroSt),
"STAmount fail");
343 unexpected((hundred == one),
"STAmount fail");
344 unexpected(!(hundred == hundred),
"STAmount fail");
345 unexpected((zeroSt != zeroSt),
"STAmount fail");
346 unexpected(!(zeroSt != one),
"STAmount fail");
347 unexpected(!(zeroSt != hundred),
"STAmount fail");
348 unexpected(!(one != zeroSt),
"STAmount fail");
350 unexpected(!(one != hundred),
"STAmount fail");
351 unexpected(!(hundred != zeroSt),
"STAmount fail");
352 unexpected(!(hundred != one),
"STAmount fail");
353 unexpected((hundred != hundred),
"STAmount fail");
361 "STAmount multiply fail 1");
364 "STAmount multiply fail 2");
367 "STAmount multiply fail 3");
370 "STAmount multiply fail 4");
375 fail(
"STAmount divide fail");
384 "STAmount divide fail");
388 "STAmount divide fail");
392 "STAmount divide fail");
399 "STAmount setRate(getRate) fail");
403 "STAmount setRate(getRate) fail");
419 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
420 "STAmount getRate fail 1");
424 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
425 "STAmount getRate fail 2");
429 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
430 "STAmount getRate fail 3");
434 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
435 "STAmount getRate fail 4");
439 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
440 "STAmount getRate fail 5");
444 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
445 "STAmount getRate fail 6");
449 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
450 "STAmount getRate fail 7");
454 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
455 "STAmount getRate fail 8");
464 for (
int i = 0; i <= 100000; ++i)
486 BEAST_EXPECT(smallXSmall == beast::zero);
490 BEAST_EXPECT(bigDsmall == beast::zero);
492 BEAST_EXPECT(bigDsmall == beast::zero);
496 BEAST_EXPECT(bigDsmall == beast::zero);
500 BEAST_EXPECT(bigDsmall == beast::zero);
505 BEAST_EXPECT(r == 0);
508 r =
getRate(bigValue, smallValue);
510 BEAST_EXPECT(r == 0);
522 beginTestCase (
"rounding ");
553 STAmount fourThirdsB = twoThird2 + twoThird2;
574 BEAST_EXPECT(stnum.getSType() == STI_AMOUNT);
575 BEAST_EXPECT(stnum.getText() ==
"0");
576 BEAST_EXPECT(stnum.isDefault() ==
true);
577 BEAST_EXPECT(stnum.value() ==
Number{0});
617 std::string const expected =
"XRP and MPT must be specified as integral amount.";
618 BEAST_EXPECT(e.
what() == expected);
629 std::string const expected =
"XRP and MPT must be specified as integral amount.";
630 BEAST_EXPECT(e.
what() == expected);
642 BEAST_EXPECT(e.
what() == expected);
652 std::string const expected =
"'e' is not a number";
653 BEAST_EXPECT(e.
what() == expected);
663 std::string const expected =
"'1e' is not a number";
664 BEAST_EXPECT(e.
what() == expected);
674 std::string const expected =
"'e2' is not a number";
675 BEAST_EXPECT(e.
what() == expected);
685 std::string const expected =
"XRP may not be specified with a null Json value";
686 BEAST_EXPECT(e.
what() == expected);
693 "123456789012345678901234567890123456789012345678901234"
695 "901234567890123456789012345678901234567890123456789012"
697 "78901234567890123456789012345678901234567890");
713 std::string const expected =
"'001' is not a number";
714 BEAST_EXPECT(e.
what() == expected);
724 std::string const expected =
"'000.0' is not a number";
725 BEAST_EXPECT(e.
what() == expected);
736 std::string const expected =
"'.1' is not a number";
737 BEAST_EXPECT(e.
what() == expected);
747 std::string const expected =
"'1.' is not a number";
748 BEAST_EXPECT(e.
what() == expected);
758 std::string const expected =
"'1.e3' is not a number";
759 BEAST_EXPECT(e.
what() == expected);
767 testcase(
"STAmount to XRPAmount conversions");
772 for (
std::uint64_t drops = 100000000000000000; drops != 1; drops = drops / 10)
775 auto const s = t.xrp();
776 BEAST_EXPECT(s.drops() == drops);
792 fail(
"wrong exception");
799 testcase(
"STAmount to IOUAmount conversions");
804 for (
std::uint64_t dollars = 10000000000; dollars != 1; dollars = dollars / 10)
807 auto const s = t.iou();
808 BEAST_EXPECT(t ==
STAmount(s, usd));
809 BEAST_EXPECT(s.mantissa() == t.mantissa());
810 BEAST_EXPECT(s.exponent() == t.exponent());
824 fail(
"wrong exception");
837 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
844 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
851 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
858 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
865 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
872 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
880 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
896 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
903 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
910 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
917 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
924 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
931 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
938 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
954 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
961 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
968 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
975 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
982 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
989 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
1000 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
1007 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
1014 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
log_os< char > log
Logging output stream.
void pass()
Record a successful test condition.
bool unexpected(Condition shouldBeFalse, String const &reason)
testcase_t testcase
Memberspace for declaring test cases.
void fail(String const &reason, char const *file, int line)
Record a failure.
A currency issued by an account.
Number is a floating point type that can represent a wide range of values.
static STAmount roundSelf(STAmount const &amount)
void testSetValue(std::string const &value, Issue const &issue, bool success=true)
void testCanSubtractXRP()
void run() override
Runs the suite.
void testCanSubtractMPT()
void mulTest(int a, int b)
void testCanSubtractIOU()
static STAmount serializeAndDeserialize(STAmount const &s)
void testNativeCurrency()
void roundTest(int n, int d, int m)
void testCustomCurrency()
std::string getFullText() const override
static constexpr std::uint64_t cMaxValue
Issue const & issue() const
void add(Serializer &s) const override
std::string getText() const override
static int const cMaxOffset
bool native() const noexcept
static constexpr std::uint64_t cMinValue
static int const cMinOffset
static constexpr std::uint64_t cMaxNative
Slice slice() const noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount divide(STAmount const &amount, Rate const &rate)
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
static void canonicalizeRound(bool native, std::uint64_t &value, int &offset, bool)
STAmount amountFromJson(SField const &name, Json::Value const &v)
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount amountFromString(Asset const &asset, std::string const &amount)
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
STAmount divRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
STAmount multiply(STAmount const &amount, Rate const &rate)
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
STAmount amountFromQuality(std::uint64_t rate)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
bool canSubtract(STAmount const &amt1, STAmount const &amt2)
Determines if it is safe to subtract one STAmount from another.
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)