20#include <xrpl/basics/Log.h>
21#include <xrpl/basics/random.h>
22#include <xrpl/beast/unit_test.h>
23#include <xrpl/protocol/STAmount.h>
69 if (valueDigits == 999999999)
101 BEAST_EXPECT(!res.
native());
105 BEAST_EXPECT(!cmp.
native());
126 BEAST_EXPECT(!prod1.
native());
137 fail(
"Multiplication result is not exact");
152 BEAST_EXPECT(amount.
getText() == value);
239 unexpected(zeroSt != beast::zero,
"STAmount fail");
241 unexpected(hundred == beast::zero,
"STAmount fail");
242 unexpected((zeroSt < zeroSt),
"STAmount fail");
244 unexpected(!(zeroSt < hundred),
"STAmount fail");
248 unexpected((hundred < zeroSt),
"STAmount fail");
250 unexpected((hundred < hundred),
"STAmount fail");
251 unexpected((zeroSt > zeroSt),
"STAmount fail");
253 unexpected((zeroSt > hundred),
"STAmount fail");
257 unexpected(!(hundred > zeroSt),
"STAmount fail");
259 unexpected((hundred > hundred),
"STAmount fail");
260 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
262 unexpected(!(zeroSt <= hundred),
"STAmount fail");
266 unexpected((hundred <= zeroSt),
"STAmount fail");
268 unexpected(!(hundred <= hundred),
"STAmount fail");
269 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
271 unexpected((zeroSt >= hundred),
"STAmount fail");
275 unexpected(!(hundred >= zeroSt),
"STAmount fail");
277 unexpected(!(hundred >= hundred),
"STAmount fail");
278 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
280 unexpected((zeroSt == hundred),
"STAmount fail");
284 unexpected((hundred == zeroSt),
"STAmount fail");
286 unexpected(!(hundred == hundred),
"STAmount fail");
287 unexpected((zeroSt != zeroSt),
"STAmount fail");
289 unexpected(!(zeroSt != hundred),
"STAmount fail");
293 unexpected(!(hundred != zeroSt),
"STAmount fail");
295 unexpected((hundred != hundred),
"STAmount fail");
304 const std::string cur =
"015841551A748AD2C1F76FF6ECB0CCCD00000000";
322 unexpected(zeroSt != beast::zero,
"STAmount fail");
324 unexpected(hundred == beast::zero,
"STAmount fail");
325 unexpected((zeroSt < zeroSt),
"STAmount fail");
327 unexpected(!(zeroSt < hundred),
"STAmount fail");
331 unexpected((hundred < zeroSt),
"STAmount fail");
333 unexpected((hundred < hundred),
"STAmount fail");
334 unexpected((zeroSt > zeroSt),
"STAmount fail");
336 unexpected((zeroSt > hundred),
"STAmount fail");
340 unexpected(!(hundred > zeroSt),
"STAmount fail");
342 unexpected((hundred > hundred),
"STAmount fail");
343 unexpected(!(zeroSt <= zeroSt),
"STAmount fail");
345 unexpected(!(zeroSt <= hundred),
"STAmount fail");
349 unexpected((hundred <= zeroSt),
"STAmount fail");
351 unexpected(!(hundred <= hundred),
"STAmount fail");
352 unexpected(!(zeroSt >= zeroSt),
"STAmount fail");
354 unexpected((zeroSt >= hundred),
"STAmount fail");
358 unexpected(!(hundred >= zeroSt),
"STAmount fail");
360 unexpected(!(hundred >= hundred),
"STAmount fail");
361 unexpected(!(zeroSt == zeroSt),
"STAmount fail");
363 unexpected((zeroSt == hundred),
"STAmount fail");
367 unexpected((hundred == zeroSt),
"STAmount fail");
369 unexpected(!(hundred == hundred),
"STAmount fail");
370 unexpected((zeroSt != zeroSt),
"STAmount fail");
372 unexpected(!(zeroSt != hundred),
"STAmount fail");
376 unexpected(!(hundred != zeroSt),
"STAmount fail");
378 unexpected((hundred != hundred),
"STAmount fail");
390 "STAmount multiply fail 1");
394 "STAmount multiply fail 2");
397 "STAmount multiply fail 3");
400 "STAmount multiply fail 4");
408 fail(
"STAmount divide fail");
418 "STAmount divide fail");
423 "STAmount divide fail");
428 "STAmount divide fail");
434 "STAmount setRate(getRate) fail");
438 "STAmount setRate(getRate) fail");
454 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
455 "STAmount getRate fail 1");
459 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
460 "STAmount getRate fail 2");
464 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
465 "STAmount getRate fail 3");
469 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
470 "STAmount getRate fail 4");
474 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
475 "STAmount getRate fail 5");
479 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
480 "STAmount getRate fail 6");
484 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
485 "STAmount getRate fail 7");
489 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
490 "STAmount getRate fail 8");
499 for (
int i = 0; i <= 100000; ++i)
525 BEAST_EXPECT(smallXsmall == beast::zero);
529 BEAST_EXPECT(bigDsmall == beast::zero);
531 BEAST_EXPECT(bigDsmall == beast::zero);
535 BEAST_EXPECT(bigDsmall == beast::zero);
539 BEAST_EXPECT(bigDsmall == beast::zero);
544 BEAST_EXPECT(r == 0);
547 r =
getRate(bigValue, smallValue);
549 BEAST_EXPECT(r == 0);
561 beginTestCase (
"rounding ");
592 STAmount fourThirdsB = twoThird2 + twoThird2;
609 testcase(
"STAmount to XRPAmount conversions");
618 auto const s = t.xrp();
619 BEAST_EXPECT(s.drops() == drops);
635 fail(
"wrong exception");
642 testcase(
"STAmount to IOUAmount conversions");
648 dollars = dollars / 10)
651 auto const s = t.iou();
652 BEAST_EXPECT(t ==
STAmount(s, usd));
653 BEAST_EXPECT(s.mantissa() == t.mantissa());
654 BEAST_EXPECT(s.exponent() == t.exponent());
668 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 const std::uint64_t cMaxNative
void add(Serializer &s) const override
static const int cMaxOffset
std::string getText() const override
bool negative() const noexcept
Issue const & issue() const
static const std::uint64_t cMinValue
static const int cMinOffset
std::uint64_t mantissa() const noexcept
std::string getFullText() const override
bool native() const noexcept
static const std::uint64_t cMaxValue
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 amountFromString(Asset const &issue, std::string const &amount)
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 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)