20#include <xrpl/basics/random.h>
21#include <xrpl/beast/unit_test.h>
22#include <xrpl/protocol/STAmount.h>
68 if (valueDigits == 999999999)
100 BEAST_EXPECT(!res.
native());
104 BEAST_EXPECT(!cmp.
native());
125 BEAST_EXPECT(!prod1.
native());
136 fail(
"Multiplication result is not exact");
151 BEAST_EXPECT(amount.
getText() == value);
238 unexpected(zeroSt != beast::zero,
"STAmount fail");
240 unexpected(hundred == beast::zero,
"STAmount fail");
241 unexpected((zeroSt < zeroSt),
"STAmount fail");
243 unexpected(!(zeroSt < hundred),
"STAmount fail");
247 unexpected((hundred < zeroSt),
"STAmount fail");
249 unexpected((hundred < hundred),
"STAmount fail");
250 unexpected((zeroSt > zeroSt),
"STAmount fail");
252 unexpected((zeroSt > hundred),
"STAmount fail");
256 unexpected(!(hundred > zeroSt),
"STAmount fail");
258 unexpected((hundred > hundred),
"STAmount fail");
259 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
261 unexpected(!(zeroSt <= hundred),
"STAmount fail");
265 unexpected((hundred <= zeroSt),
"STAmount fail");
267 unexpected(!(hundred <= hundred),
"STAmount fail");
268 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
270 unexpected((zeroSt >= hundred),
"STAmount fail");
274 unexpected(!(hundred >= zeroSt),
"STAmount fail");
276 unexpected(!(hundred >= hundred),
"STAmount fail");
277 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
279 unexpected((zeroSt == hundred),
"STAmount fail");
283 unexpected((hundred == zeroSt),
"STAmount fail");
285 unexpected(!(hundred == hundred),
"STAmount fail");
286 unexpected((zeroSt != zeroSt),
"STAmount fail");
288 unexpected(!(zeroSt != hundred),
"STAmount fail");
292 unexpected(!(hundred != zeroSt),
"STAmount fail");
294 unexpected((hundred != hundred),
"STAmount fail");
303 std::string const cur =
"015841551A748AD2C1F76FF6ECB0CCCD00000000";
321 unexpected(zeroSt != beast::zero,
"STAmount fail");
323 unexpected(hundred == beast::zero,
"STAmount fail");
324 unexpected((zeroSt < zeroSt),
"STAmount fail");
326 unexpected(!(zeroSt < hundred),
"STAmount fail");
330 unexpected((hundred < zeroSt),
"STAmount fail");
332 unexpected((hundred < hundred),
"STAmount fail");
333 unexpected((zeroSt > zeroSt),
"STAmount fail");
335 unexpected((zeroSt > hundred),
"STAmount fail");
339 unexpected(!(hundred > zeroSt),
"STAmount fail");
341 unexpected((hundred > hundred),
"STAmount fail");
342 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
344 unexpected(!(zeroSt <= hundred),
"STAmount fail");
348 unexpected((hundred <= zeroSt),
"STAmount fail");
350 unexpected(!(hundred <= hundred),
"STAmount fail");
351 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
353 unexpected((zeroSt >= hundred),
"STAmount fail");
357 unexpected(!(hundred >= zeroSt),
"STAmount fail");
359 unexpected(!(hundred >= hundred),
"STAmount fail");
360 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
362 unexpected((zeroSt == hundred),
"STAmount fail");
366 unexpected((hundred == zeroSt),
"STAmount fail");
368 unexpected(!(hundred == hundred),
"STAmount fail");
369 unexpected((zeroSt != zeroSt),
"STAmount fail");
371 unexpected(!(zeroSt != hundred),
"STAmount fail");
375 unexpected(!(hundred != zeroSt),
"STAmount fail");
377 unexpected((hundred != hundred),
"STAmount fail");
389 "STAmount multiply fail 1");
393 "STAmount multiply fail 2");
396 "STAmount multiply fail 3");
399 "STAmount multiply fail 4");
407 fail(
"STAmount divide fail");
417 "STAmount divide fail");
422 "STAmount divide fail");
427 "STAmount divide fail");
433 "STAmount setRate(getRate) fail");
437 "STAmount setRate(getRate) fail");
453 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
454 "STAmount getRate fail 1");
458 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
459 "STAmount getRate fail 2");
463 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
464 "STAmount getRate fail 3");
468 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
469 "STAmount getRate fail 4");
473 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
474 "STAmount getRate fail 5");
478 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
479 "STAmount getRate fail 6");
483 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
484 "STAmount getRate fail 7");
488 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
489 "STAmount getRate fail 8");
498 for (
int i = 0; i <= 100000; ++i)
524 BEAST_EXPECT(smallXsmall == beast::zero);
528 BEAST_EXPECT(bigDsmall == beast::zero);
530 BEAST_EXPECT(bigDsmall == beast::zero);
534 BEAST_EXPECT(bigDsmall == beast::zero);
538 BEAST_EXPECT(bigDsmall == beast::zero);
543 BEAST_EXPECT(r == 0);
546 r =
getRate(bigValue, smallValue);
548 BEAST_EXPECT(r == 0);
560 beginTestCase (
"rounding ");
591 STAmount fourThirdsB = twoThird2 + twoThird2;
608 testcase(
"STAmount to XRPAmount conversions");
617 auto const s = t.xrp();
618 BEAST_EXPECT(s.drops() == drops);
634 fail(
"wrong exception");
641 testcase(
"STAmount to IOUAmount conversions");
647 dollars = dollars / 10)
650 auto const s = t.iou();
651 BEAST_EXPECT(t ==
STAmount(s, usd));
652 BEAST_EXPECT(s.mantissa() == t.mantissa());
653 BEAST_EXPECT(s.exponent() == t.exponent());
667 fail(
"wrong exception");
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.
void roundTest(int n, int d, int m)
void testNativeCurrency()
STAmount roundSelf(STAmount const &amount)
static STAmount serializeAndDeserialize(STAmount const &s)
void testSetValue(std::string const &value, Issue const &issue, bool success=true)
void testCustomCurrency()
void mulTest(int a, int b)
void run() override
Runs the suite.
int exponent() const noexcept
static int const cMaxOffset
static int const cMinOffset
static std::uint64_t const cMinValue
void add(Serializer &s) const override
static std::uint64_t const cMaxValue
std::string getText() const override
bool negative() const noexcept
Issue const & issue() const
static std::uint64_t const cMaxNative
std::uint64_t mantissa() const noexcept
std::string getFullText() const override
bool native() const noexcept
Slice slice() const noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
STAmount divide(STAmount const &amount, Rate const &rate)
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
STAmount amountFromQuality(std::uint64_t rate)
STAmount multiply(STAmount const &amount, Rate const &rate)
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
STAmount divRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount amountFromString(Asset const &asset, std::string const &amount)
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
static void canonicalizeRound(bool native, std::uint64_t &value, int &offset, bool)