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>
52 if (valueDigits == 999999999)
84 BEAST_EXPECT(!res.
native());
88 BEAST_EXPECT(!cmp.
native());
109 BEAST_EXPECT(!prod1.
native());
120 fail(
"Multiplication result is not exact");
135 BEAST_EXPECT(amount.getText() == value);
222 unexpected(zeroSt != beast::zero,
"STAmount fail");
224 unexpected(hundred == beast::zero,
"STAmount fail");
225 unexpected((zeroSt < zeroSt),
"STAmount fail");
227 unexpected(!(zeroSt < hundred),
"STAmount fail");
231 unexpected((hundred < zeroSt),
"STAmount fail");
233 unexpected((hundred < hundred),
"STAmount fail");
234 unexpected((zeroSt > zeroSt),
"STAmount fail");
236 unexpected((zeroSt > hundred),
"STAmount fail");
240 unexpected(!(hundred > zeroSt),
"STAmount fail");
242 unexpected((hundred > hundred),
"STAmount fail");
243 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
245 unexpected(!(zeroSt <= hundred),
"STAmount fail");
249 unexpected((hundred <= zeroSt),
"STAmount fail");
251 unexpected(!(hundred <= hundred),
"STAmount fail");
252 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
254 unexpected((zeroSt >= hundred),
"STAmount fail");
258 unexpected(!(hundred >= zeroSt),
"STAmount fail");
260 unexpected(!(hundred >= hundred),
"STAmount fail");
261 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
263 unexpected((zeroSt == hundred),
"STAmount fail");
267 unexpected((hundred == zeroSt),
"STAmount fail");
269 unexpected(!(hundred == hundred),
"STAmount fail");
270 unexpected((zeroSt != zeroSt),
"STAmount fail");
272 unexpected(!(zeroSt != hundred),
"STAmount fail");
276 unexpected(!(hundred != zeroSt),
"STAmount fail");
278 unexpected((hundred != hundred),
"STAmount fail");
287 std::string const cur =
"015841551A748AD2C1F76FF6ECB0CCCD00000000";
305 unexpected(zeroSt != beast::zero,
"STAmount fail");
307 unexpected(hundred == beast::zero,
"STAmount fail");
308 unexpected((zeroSt < zeroSt),
"STAmount fail");
310 unexpected(!(zeroSt < hundred),
"STAmount fail");
314 unexpected((hundred < zeroSt),
"STAmount fail");
316 unexpected((hundred < hundred),
"STAmount fail");
317 unexpected((zeroSt > zeroSt),
"STAmount fail");
319 unexpected((zeroSt > hundred),
"STAmount fail");
323 unexpected(!(hundred > zeroSt),
"STAmount fail");
325 unexpected((hundred > hundred),
"STAmount fail");
326 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
328 unexpected(!(zeroSt <= hundred),
"STAmount fail");
332 unexpected((hundred <= zeroSt),
"STAmount fail");
334 unexpected(!(hundred <= hundred),
"STAmount fail");
335 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
337 unexpected((zeroSt >= hundred),
"STAmount fail");
341 unexpected(!(hundred >= zeroSt),
"STAmount fail");
343 unexpected(!(hundred >= hundred),
"STAmount fail");
344 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
346 unexpected((zeroSt == hundred),
"STAmount fail");
350 unexpected((hundred == zeroSt),
"STAmount fail");
352 unexpected(!(hundred == hundred),
"STAmount fail");
353 unexpected((zeroSt != zeroSt),
"STAmount fail");
355 unexpected(!(zeroSt != hundred),
"STAmount fail");
359 unexpected(!(hundred != zeroSt),
"STAmount fail");
361 unexpected((hundred != hundred),
"STAmount fail");
373 "STAmount multiply fail 1");
377 "STAmount multiply fail 2");
380 "STAmount multiply fail 3");
383 "STAmount multiply fail 4");
391 fail(
"STAmount divide fail");
401 "STAmount divide fail");
406 "STAmount divide fail");
411 "STAmount divide fail");
417 "STAmount setRate(getRate) fail");
421 "STAmount setRate(getRate) fail");
437 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
438 "STAmount getRate fail 1");
442 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
443 "STAmount getRate fail 2");
447 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
448 "STAmount getRate fail 3");
452 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
453 "STAmount getRate fail 4");
457 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
458 "STAmount getRate fail 5");
462 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
463 "STAmount getRate fail 6");
467 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
468 "STAmount getRate fail 7");
472 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
473 "STAmount getRate fail 8");
482 for (
int i = 0; i <= 100000; ++i)
508 BEAST_EXPECT(smallXsmall == beast::zero);
512 BEAST_EXPECT(bigDsmall == beast::zero);
514 BEAST_EXPECT(bigDsmall == beast::zero);
518 BEAST_EXPECT(bigDsmall == beast::zero);
522 BEAST_EXPECT(bigDsmall == beast::zero);
527 BEAST_EXPECT(r == 0);
530 r =
getRate(bigValue, smallValue);
532 BEAST_EXPECT(r == 0);
544 beginTestCase (
"rounding ");
575 STAmount fourThirdsB = twoThird2 + twoThird2;
596 BEAST_EXPECT(stnum.getSType() == STI_AMOUNT);
597 BEAST_EXPECT(stnum.getText() ==
"0");
598 BEAST_EXPECT(stnum.isDefault() ==
true);
599 BEAST_EXPECT(stnum.value() ==
Number{0});
650 "XRP and MPT must be specified as integral amount.";
651 BEAST_EXPECT(e.
what() == expected);
663 "XRP and MPT must be specified as integral amount.";
664 BEAST_EXPECT(e.
what() == expected);
676 BEAST_EXPECT(e.
what() == expected);
686 std::string const expected =
"'e' is not a number";
687 BEAST_EXPECT(e.
what() == expected);
697 std::string const expected =
"'1e' is not a number";
698 BEAST_EXPECT(e.
what() == expected);
708 std::string const expected =
"'e2' is not a number";
709 BEAST_EXPECT(e.
what() == expected);
720 "XRP may not be specified with a null Json value";
721 BEAST_EXPECT(e.
what() == expected);
728 "123456789012345678901234567890123456789012345678901234"
730 "901234567890123456789012345678901234567890123456789012"
732 "78901234567890123456789012345678901234567890");
748 std::string const expected =
"'001' is not a number";
749 BEAST_EXPECT(e.
what() == expected);
759 std::string const expected =
"'000.0' is not a number";
760 BEAST_EXPECT(e.
what() == expected);
771 std::string const expected =
"'.1' is not a number";
772 BEAST_EXPECT(e.
what() == expected);
782 std::string const expected =
"'1.' is not a number";
783 BEAST_EXPECT(e.
what() == expected);
793 std::string const expected =
"'1.e3' is not a number";
794 BEAST_EXPECT(e.
what() == expected);
802 testcase(
"STAmount to XRPAmount conversions");
811 auto const s = t.xrp();
812 BEAST_EXPECT(s.drops() == drops);
828 fail(
"wrong exception");
835 testcase(
"STAmount to IOUAmount conversions");
841 dollars = dollars / 10)
844 auto const s = t.iou();
845 BEAST_EXPECT(t ==
STAmount(s, usd));
846 BEAST_EXPECT(s.mantissa() == t.mantissa());
847 BEAST_EXPECT(s.exponent() == t.exponent());
861 fail(
"wrong exception");
874 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
881 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
888 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
895 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
902 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
909 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
917 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
933 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
940 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
947 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
954 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
961 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
968 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
975 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
991 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
998 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
1005 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
1012 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
1019 BEAST_EXPECT(
canAdd(amt1, amt2) ==
true);
1027 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
1038 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
1045 BEAST_EXPECT(
canAdd(amt1, amt2) ==
false);
1052 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.
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)