mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
New types Account, Currency, Directory:
* New tagged uint types. * Extract to_string functions from header to hide dependencies. * Include what you use and C++11 for cleanups.
This commit is contained in:
committed by
Vinnie Falco
parent
adce6ae851
commit
55222dc5d1
@@ -19,77 +19,16 @@
|
||||
|
||||
#include <beast/cxx14/iterator.h>
|
||||
|
||||
#include <ripple/types/api/UintTypes.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
std::uint64_t STAmount::uRateOne = STAmount::getRate (STAmount (1), STAmount (1));
|
||||
|
||||
bool STAmount::issuerFromString (uint160& uDstIssuer, const std::string& sIssuer)
|
||||
{
|
||||
bool bSuccess = true;
|
||||
|
||||
if (sIssuer.size () == (160 / 4))
|
||||
{
|
||||
uDstIssuer.SetHex (sIssuer);
|
||||
}
|
||||
else
|
||||
{
|
||||
RippleAddress raIssuer;
|
||||
|
||||
if (raIssuer.setAccountID (sIssuer))
|
||||
{
|
||||
uDstIssuer = raIssuer.getAccountID ();
|
||||
}
|
||||
else
|
||||
{
|
||||
bSuccess = false;
|
||||
}
|
||||
}
|
||||
|
||||
return bSuccess;
|
||||
}
|
||||
|
||||
// --> sCurrency: "", "XRP", or three letter ISO code.
|
||||
bool STAmount::currencyFromString (uint160& uDstCurrency, const std::string& sCurrency)
|
||||
{
|
||||
bool bSuccess = true;
|
||||
|
||||
if (sCurrency.empty () || !sCurrency.compare (SYSTEM_CURRENCY_CODE))
|
||||
{
|
||||
uDstCurrency.zero ();
|
||||
}
|
||||
else if (3 == sCurrency.size ())
|
||||
{
|
||||
Blob vucIso (3);
|
||||
|
||||
std::transform (sCurrency.begin (), sCurrency.end (), vucIso.begin (), ::toupper);
|
||||
|
||||
// std::string sIso;
|
||||
// sIso.assign(vucIso.begin(), vucIso.end());
|
||||
|
||||
Serializer s;
|
||||
|
||||
s.addZeros (96 / 8);
|
||||
s.addRaw (vucIso);
|
||||
s.addZeros (16 / 8);
|
||||
s.addZeros (24 / 8);
|
||||
|
||||
s.get160 (uDstCurrency, 0);
|
||||
}
|
||||
else if (40 == sCurrency.size ())
|
||||
{
|
||||
bSuccess = uDstCurrency.SetHex (sCurrency);
|
||||
}
|
||||
else
|
||||
{
|
||||
bSuccess = false;
|
||||
}
|
||||
|
||||
return bSuccess;
|
||||
}
|
||||
std::uint64_t STAmount::uRateOne =
|
||||
STAmount::getRate (STAmount (1), STAmount (1));
|
||||
|
||||
std::string STAmount::getHumanCurrency () const
|
||||
{
|
||||
return createHumanCurrency (mCurrency);
|
||||
return to_string (mCurrency);
|
||||
}
|
||||
|
||||
bool STAmount::bSetJson (const Json::Value& jvSource)
|
||||
@@ -152,7 +91,7 @@ STAmount::STAmount (SField::ref n, const Json::Value& v)
|
||||
else
|
||||
value = v;
|
||||
|
||||
mIsNative = !currency.isString () || currency.asString ().empty () || (currency.asString () == SYSTEM_CURRENCY_CODE);
|
||||
mIsNative = !currency.isString () || currency.asString ().empty () || (currency.asString () == systemCurrencyCode());
|
||||
|
||||
if (mIsNative)
|
||||
{
|
||||
@@ -162,11 +101,11 @@ STAmount::STAmount (SField::ref n, const Json::Value& v)
|
||||
else
|
||||
{
|
||||
// non-XRP
|
||||
if (!currencyFromString (mCurrency, currency.asString ()))
|
||||
if (!to_currency (mCurrency, currency.asString ()))
|
||||
throw std::runtime_error ("invalid currency");
|
||||
|
||||
if (!issuer.isString ()
|
||||
|| !issuerFromString (mIssuer, issuer.asString ()))
|
||||
|| !to_issuer (mIssuer, issuer.asString ()))
|
||||
throw std::runtime_error ("invalid issuer");
|
||||
|
||||
if (mIssuer.isZero ())
|
||||
@@ -216,53 +155,30 @@ STAmount::STAmount (SField::ref n, const Json::Value& v)
|
||||
throw std::runtime_error ("invalid amount type");
|
||||
}
|
||||
|
||||
std::string STAmount::createHumanCurrency (const uint160& uCurrency)
|
||||
{
|
||||
static uint160 const sIsoBits ("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF");
|
||||
|
||||
if (uCurrency.isZero ())
|
||||
{
|
||||
return SYSTEM_CURRENCY_CODE;
|
||||
}
|
||||
|
||||
if (CURRENCY_ONE == uCurrency)
|
||||
{
|
||||
return "1";
|
||||
}
|
||||
|
||||
if ((uCurrency & sIsoBits).isZero ())
|
||||
{
|
||||
// The offset of the 3 character ISO code in the currency descriptor
|
||||
int const isoOffset = 12;
|
||||
|
||||
std::string const iso(
|
||||
uCurrency.data () + isoOffset,
|
||||
uCurrency.data () + isoOffset + 3);
|
||||
|
||||
// Specifying the system currency code using ISO-style representation
|
||||
// is not allowed.
|
||||
if (iso != SYSTEM_CURRENCY_CODE)
|
||||
return iso;
|
||||
}
|
||||
|
||||
return to_string (uCurrency);
|
||||
}
|
||||
|
||||
bool STAmount::setValue (const std::string& sAmount)
|
||||
{
|
||||
// Note: mIsNative and mCurrency must be set already!
|
||||
|
||||
static boost::regex reNumber ("\\`([+-]?)(\\d*)(\\.(\\d*))?([eE]([+-]?)(\\d+))?\\'");
|
||||
static boost::regex reNumber (
|
||||
"\\`([+-]?)(\\d*)(\\.(\\d*))?([eE]([+-]?)(\\d+))?\\'");
|
||||
boost::smatch smMatch;
|
||||
|
||||
if (!boost::regex_match (sAmount, smMatch, reNumber))
|
||||
{
|
||||
WriteLog (lsWARNING, STAmount) << "Number not valid: \"" << sAmount << "\"";
|
||||
WriteLog (lsWARNING, STAmount)
|
||||
<< "Number not valid: \"" << sAmount << "\"";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Match fields: 0 = whole input, 1 = sign, 2 = integer portion, 3 = whole fraction (with '.')
|
||||
// 4 = fraction (without '.'), 5 = whole exponent (with 'e'), 6 = exponent sign, 7 = exponent number
|
||||
// Match fields:
|
||||
// 0 = whole input
|
||||
// 1 = sign
|
||||
// 2 = integer portion
|
||||
// 3 = whole fraction (with '.')
|
||||
// 4 = fraction (without '.')
|
||||
// 5 = whole exponent (with 'e')
|
||||
// 6 = exponent sign
|
||||
// 7 = exponent number
|
||||
|
||||
try
|
||||
{
|
||||
@@ -337,7 +253,7 @@ bool STAmount::setFullValue (const std::string& sAmount, const std::string& sCur
|
||||
//
|
||||
// Figure out the currency.
|
||||
//
|
||||
if (!currencyFromString (mCurrency, sCurrency))
|
||||
if (!to_currency (mCurrency, sCurrency))
|
||||
{
|
||||
WriteLog (lsINFO, STAmount) << "Currency malformed: " << sCurrency;
|
||||
|
||||
@@ -530,14 +446,18 @@ STAmount* STAmount::construct (SerializerIterator& sit, SField::ref name)
|
||||
return new STAmount (name, value, true); // negative
|
||||
}
|
||||
|
||||
uint160 currency = sit.get160 ();
|
||||
Currency currency;
|
||||
currency.copyFrom (sit.get160 ());
|
||||
|
||||
if (!currency)
|
||||
throw std::runtime_error ("invalid non-native currency");
|
||||
|
||||
uint160 issuer = sit.get160 ();
|
||||
Account issuer;
|
||||
issuer.copyFrom (sit.get160 ());
|
||||
|
||||
// 10 bits for the offset, sign and "not native" flag
|
||||
int offset = static_cast<int> (value >> (64 - 10));
|
||||
|
||||
int offset = static_cast<int> (value >> (64 - 10)); // 10 bits for the offset, sign and "not native" flag
|
||||
value &= ~ (1023ull << (64 - 10));
|
||||
|
||||
if (value)
|
||||
@@ -545,8 +465,13 @@ STAmount* STAmount::construct (SerializerIterator& sit, SField::ref name)
|
||||
bool isNegative = (offset & 256) == 0;
|
||||
offset = (offset & 255) - 97; // center the range
|
||||
|
||||
if ((value < cMinValue) || (value > cMaxValue) || (offset < cMinOffset) || (offset > cMaxOffset))
|
||||
if (value < cMinValue ||
|
||||
value > cMaxValue ||
|
||||
offset < cMinOffset ||
|
||||
offset > cMaxOffset)
|
||||
{
|
||||
throw std::runtime_error ("invalid currency value");
|
||||
}
|
||||
|
||||
return new STAmount (name, currency, issuer, value, offset, isNegative);
|
||||
}
|
||||
@@ -919,8 +844,8 @@ STAmount operator- (const STAmount& v1, const STAmount& v2)
|
||||
|
||||
// NIKB TODO Make Amount::divide skip math if den == QUALITY_ONE
|
||||
STAmount STAmount::divide (
|
||||
const STAmount& num, const STAmount& den, const uint160& currency,
|
||||
const uint160& issuer)
|
||||
const STAmount& num, const STAmount& den, Currency const& currency,
|
||||
Account const& issuer)
|
||||
{
|
||||
if (den == zero)
|
||||
throw std::runtime_error ("division by zero");
|
||||
@@ -964,8 +889,8 @@ STAmount STAmount::divide (
|
||||
}
|
||||
|
||||
STAmount STAmount::multiply (
|
||||
const STAmount& v1, const STAmount& v2, const uint160& currency,
|
||||
const uint160& issuer)
|
||||
const STAmount& v1, const STAmount& v2, Currency const& currency,
|
||||
Account const& issuer)
|
||||
{
|
||||
if (v1 == zero || v2 == zero)
|
||||
return STAmount (currency, issuer);
|
||||
@@ -1039,7 +964,7 @@ std::uint64_t STAmount::getRate (const STAmount& offerOut, const STAmount& offer
|
||||
|
||||
try
|
||||
{
|
||||
STAmount r = divide (offerIn, offerOut, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount r = divide (offerIn, offerOut, noCurrency(), noAccount());
|
||||
|
||||
if (r == zero) // offer is too good
|
||||
return 0;
|
||||
@@ -1060,12 +985,12 @@ std::uint64_t STAmount::getRate (const STAmount& offerOut, const STAmount& offer
|
||||
STAmount STAmount::setRate (std::uint64_t rate)
|
||||
{
|
||||
if (rate == 0)
|
||||
return STAmount (CURRENCY_ONE, ACCOUNT_ONE);
|
||||
return STAmount (noCurrency(), noAccount());
|
||||
|
||||
std::uint64_t mantissa = rate & ~ (255ull << (64 - 8));
|
||||
int exponent = static_cast<int> (rate >> (64 - 8)) - 100;
|
||||
|
||||
return STAmount (CURRENCY_ONE, ACCOUNT_ONE, mantissa, exponent);
|
||||
return STAmount (noCurrency(), noAccount(), mantissa, exponent);
|
||||
}
|
||||
|
||||
STAmount STAmount::getPay (const STAmount& offerOut, const STAmount& offerIn, const STAmount& needed)
|
||||
@@ -1080,7 +1005,7 @@ STAmount STAmount::getPay (const STAmount& offerOut, const STAmount& offerIn, co
|
||||
return needed;
|
||||
}
|
||||
|
||||
STAmount ret = divide (multiply (needed, offerIn, CURRENCY_ONE, ACCOUNT_ONE), offerOut, offerIn.getCurrency (), offerIn.getIssuer ());
|
||||
STAmount ret = divide (multiply (needed, offerIn, noCurrency(), noAccount()), offerOut, offerIn.getCurrency (), offerIn.getIssuer ());
|
||||
|
||||
return (ret > offerIn) ? offerIn : ret;
|
||||
}
|
||||
@@ -1105,10 +1030,10 @@ std::string STAmount::getFullText () const
|
||||
|
||||
if (!mIssuer)
|
||||
ret += "0";
|
||||
else if (mIssuer == ACCOUNT_ONE)
|
||||
else if (mIssuer == noAccount())
|
||||
ret += "1";
|
||||
else
|
||||
ret += RippleAddress::createHumanAccountID (mIssuer);
|
||||
ret += to_string (mIssuer);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -1162,7 +1087,7 @@ void STAmount::setJson (Json::Value& elem) const
|
||||
|
||||
elem[jss::value] = getText ();
|
||||
elem[jss::currency] = getHumanCurrency ();
|
||||
elem[jss::issuer] = RippleAddress::createHumanAccountID (mIssuer);
|
||||
elem[jss::issuer] = to_string (mIssuer);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1198,17 +1123,17 @@ public:
|
||||
bool roundTest (int n, int d, int m)
|
||||
{
|
||||
// check STAmount rounding
|
||||
STAmount num (CURRENCY_ONE, ACCOUNT_ONE, n);
|
||||
STAmount den (CURRENCY_ONE, ACCOUNT_ONE, d);
|
||||
STAmount mul (CURRENCY_ONE, ACCOUNT_ONE, m);
|
||||
STAmount quot = STAmount::divide (n, d, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount res = STAmount::multiply (quot, mul, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount num (noCurrency(), noAccount(), n);
|
||||
STAmount den (noCurrency(), noAccount(), d);
|
||||
STAmount mul (noCurrency(), noAccount(), m);
|
||||
STAmount quot = STAmount::divide (n, d, noCurrency(), noAccount());
|
||||
STAmount res = STAmount::multiply (quot, mul, noCurrency(), noAccount());
|
||||
|
||||
expect (! res.isNative (), "Product should not be native");
|
||||
|
||||
res.roundSelf ();
|
||||
|
||||
STAmount cmp (CURRENCY_ONE, ACCOUNT_ONE, (n * m) / d);
|
||||
STAmount cmp (noCurrency(), noAccount(), (n * m) / d);
|
||||
|
||||
expect (! cmp.isNative (), "Comparison amount should not be native");
|
||||
|
||||
@@ -1233,13 +1158,13 @@ public:
|
||||
|
||||
void mulTest (int a, int b)
|
||||
{
|
||||
STAmount aa (CURRENCY_ONE, ACCOUNT_ONE, a);
|
||||
STAmount bb (CURRENCY_ONE, ACCOUNT_ONE, b);
|
||||
STAmount prod1 (STAmount::multiply (aa, bb, CURRENCY_ONE, ACCOUNT_ONE));
|
||||
STAmount aa (noCurrency(), noAccount(), a);
|
||||
STAmount bb (noCurrency(), noAccount(), b);
|
||||
STAmount prod1 (STAmount::multiply (aa, bb, noCurrency(), noAccount()));
|
||||
|
||||
expect (! prod1.isNative ());
|
||||
|
||||
STAmount prod2 (CURRENCY_ONE, ACCOUNT_ONE, static_cast<std::uint64_t> (a) * static_cast<std::uint64_t> (b));
|
||||
STAmount prod2 (noCurrency(), noAccount(), static_cast<std::uint64_t> (a) * static_cast<std::uint64_t> (b));
|
||||
|
||||
if (prod1 != prod2)
|
||||
{
|
||||
@@ -1254,7 +1179,7 @@ public:
|
||||
}
|
||||
|
||||
aa = a;
|
||||
prod1 = STAmount::multiply (aa, bb, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
prod1 = STAmount::multiply (aa, bb, noCurrency(), noAccount());
|
||||
|
||||
if (prod1 != prod2)
|
||||
{
|
||||
@@ -1431,16 +1356,16 @@ public:
|
||||
|
||||
unexpected (STAmount (310).getText () != "310", "STAmount fail");
|
||||
|
||||
unexpected (STAmount::createHumanCurrency (uint160 ()) != "XRP", "cHC(XRP)");
|
||||
unexpected (to_string (Currency ()) != "XRP", "cHC(XRP)");
|
||||
|
||||
uint160 c;
|
||||
unexpected (!STAmount::currencyFromString (c, "USD"), "create USD currency");
|
||||
unexpected (STAmount::createHumanCurrency (c) != "USD", "check USD currency");
|
||||
Currency c;
|
||||
unexpected (!to_currency (c, "USD"), "create USD currency");
|
||||
unexpected (to_string (c) != "USD", "check USD currency");
|
||||
|
||||
const std::string cur = "015841551A748AD2C1F76FF6ECB0CCCD00000000";
|
||||
unexpected (!STAmount::currencyFromString (c, cur), "create custom currency");
|
||||
unexpected (STAmount::createHumanCurrency (c) != cur, "check custom currency");
|
||||
unexpected (c != uint160 (cur), "check custom currency");
|
||||
unexpected (!to_currency (c, cur), "create custom currency");
|
||||
unexpected (to_string (c) != cur, "check custom currency");
|
||||
unexpected (c != Currency (cur), "check custom currency");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
@@ -1449,7 +1374,7 @@ public:
|
||||
{
|
||||
testcase ("custom currency");
|
||||
|
||||
STAmount zeroSt (CURRENCY_ONE, ACCOUNT_ONE), one (CURRENCY_ONE, ACCOUNT_ONE, 1), hundred (CURRENCY_ONE, ACCOUNT_ONE, 100);
|
||||
STAmount zeroSt (noCurrency(), noAccount()), one (noCurrency(), noAccount(), 1), hundred (noCurrency(), noAccount(), 100);
|
||||
|
||||
unexpected (serializeAndDeserialize (zeroSt) != zeroSt, "STAmount fail");
|
||||
|
||||
@@ -1575,33 +1500,33 @@ public:
|
||||
|
||||
unexpected ((hundred != hundred), "STAmount fail");
|
||||
|
||||
unexpected (STAmount (CURRENCY_ONE, ACCOUNT_ONE).getText () != "0", "STAmount fail");
|
||||
unexpected (STAmount (noCurrency(), noAccount()).getText () != "0", "STAmount fail");
|
||||
|
||||
unexpected (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 31).getText () != "31", "STAmount fail");
|
||||
unexpected (STAmount (noCurrency(), noAccount(), 31).getText () != "31", "STAmount fail");
|
||||
|
||||
unexpected (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 31, 1).getText () != "310", "STAmount fail");
|
||||
unexpected (STAmount (noCurrency(), noAccount(), 31, 1).getText () != "310", "STAmount fail");
|
||||
|
||||
unexpected (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 31, -1).getText () != "3.1", "STAmount fail");
|
||||
unexpected (STAmount (noCurrency(), noAccount(), 31, -1).getText () != "3.1", "STAmount fail");
|
||||
|
||||
unexpected (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 31, -2).getText () != "0.31", "STAmount fail");
|
||||
unexpected (STAmount (noCurrency(), noAccount(), 31, -2).getText () != "0.31", "STAmount fail");
|
||||
|
||||
unexpected (STAmount::multiply (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 20), STAmount (3), CURRENCY_ONE, ACCOUNT_ONE).getText () != "60",
|
||||
unexpected (STAmount::multiply (STAmount (noCurrency(), noAccount(), 20), STAmount (3), noCurrency(), noAccount()).getText () != "60",
|
||||
"STAmount multiply fail 1");
|
||||
|
||||
unexpected (STAmount::multiply (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 20), STAmount (3), uint160 (), ACCOUNT_XRP).getText () != "60",
|
||||
unexpected (STAmount::multiply (STAmount (noCurrency(), noAccount(), 20), STAmount (3), xrpCurrency (), xrpIssuer()).getText () != "60",
|
||||
"STAmount multiply fail 2");
|
||||
|
||||
unexpected (STAmount::multiply (STAmount (20), STAmount (3), CURRENCY_ONE, ACCOUNT_ONE).getText () != "60",
|
||||
unexpected (STAmount::multiply (STAmount (20), STAmount (3), noCurrency(), noAccount()).getText () != "60",
|
||||
"STAmount multiply fail 3");
|
||||
|
||||
unexpected (STAmount::multiply (STAmount (20), STAmount (3), uint160 (), ACCOUNT_XRP).getText () != "60",
|
||||
unexpected (STAmount::multiply (STAmount (20), STAmount (3), xrpCurrency (), xrpIssuer()).getText () != "60",
|
||||
"STAmount multiply fail 4");
|
||||
|
||||
if (STAmount::divide (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 60), STAmount (3), CURRENCY_ONE, ACCOUNT_ONE).getText () != "20")
|
||||
if (STAmount::divide (STAmount (noCurrency(), noAccount(), 60), STAmount (3), noCurrency(), noAccount()).getText () != "20")
|
||||
{
|
||||
WriteLog (lsFATAL, STAmount) << "60/3 = " <<
|
||||
STAmount::divide (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 60),
|
||||
STAmount (3), CURRENCY_ONE, ACCOUNT_ONE).getText ();
|
||||
STAmount::divide (STAmount (noCurrency(), noAccount(), 60),
|
||||
STAmount (3), noCurrency(), noAccount()).getText ();
|
||||
fail ("STAmount divide fail");
|
||||
}
|
||||
else
|
||||
@@ -1609,21 +1534,21 @@ public:
|
||||
pass ();
|
||||
}
|
||||
|
||||
unexpected (STAmount::divide (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 60), STAmount (3), uint160 (), ACCOUNT_XRP).getText () != "20",
|
||||
unexpected (STAmount::divide (STAmount (noCurrency(), noAccount(), 60), STAmount (3), xrpCurrency (), xrpIssuer()).getText () != "20",
|
||||
"STAmount divide fail");
|
||||
|
||||
unexpected (STAmount::divide (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 60), STAmount (CURRENCY_ONE, ACCOUNT_ONE, 3), CURRENCY_ONE, ACCOUNT_ONE).getText () != "20",
|
||||
unexpected (STAmount::divide (STAmount (noCurrency(), noAccount(), 60), STAmount (noCurrency(), noAccount(), 3), noCurrency(), noAccount()).getText () != "20",
|
||||
"STAmount divide fail");
|
||||
|
||||
unexpected (STAmount::divide (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 60), STAmount (CURRENCY_ONE, ACCOUNT_ONE, 3), uint160 (), ACCOUNT_XRP).getText () != "20",
|
||||
unexpected (STAmount::divide (STAmount (noCurrency(), noAccount(), 60), STAmount (noCurrency(), noAccount(), 3), xrpCurrency (), xrpIssuer()).getText () != "20",
|
||||
"STAmount divide fail");
|
||||
|
||||
STAmount a1 (CURRENCY_ONE, ACCOUNT_ONE, 60), a2 (CURRENCY_ONE, ACCOUNT_ONE, 10, -1);
|
||||
STAmount a1 (noCurrency(), noAccount(), 60), a2 (noCurrency(), noAccount(), 10, -1);
|
||||
|
||||
unexpected (STAmount::divide (a2, a1, CURRENCY_ONE, ACCOUNT_ONE) != STAmount::setRate (STAmount::getRate (a1, a2)),
|
||||
unexpected (STAmount::divide (a2, a1, noCurrency(), noAccount()) != STAmount::setRate (STAmount::getRate (a1, a2)),
|
||||
"STAmount setRate(getRate) fail");
|
||||
|
||||
unexpected (STAmount::divide (a1, a2, CURRENCY_ONE, ACCOUNT_ONE) != STAmount::setRate (STAmount::getRate (a2, a1)),
|
||||
unexpected (STAmount::divide (a1, a2, noCurrency(), noAccount()) != STAmount::setRate (STAmount::getRate (a2, a1)),
|
||||
"STAmount setRate(getRate) fail");
|
||||
}
|
||||
|
||||
@@ -1662,22 +1587,22 @@ public:
|
||||
unexpected (STAmount::getRate (STAmount (10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 2");
|
||||
|
||||
unexpected (STAmount::getRate (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 1), STAmount (CURRENCY_ONE, ACCOUNT_ONE, 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
|
||||
unexpected (STAmount::getRate (STAmount (noCurrency(), noAccount(), 1), STAmount (noCurrency(), noAccount(), 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 3");
|
||||
|
||||
unexpected (STAmount::getRate (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 10), STAmount (CURRENCY_ONE, ACCOUNT_ONE, 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
unexpected (STAmount::getRate (STAmount (noCurrency(), noAccount(), 10), STAmount (noCurrency(), noAccount(), 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 4");
|
||||
|
||||
unexpected (STAmount::getRate (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 1), STAmount (10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
|
||||
unexpected (STAmount::getRate (STAmount (noCurrency(), noAccount(), 1), STAmount (10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 5");
|
||||
|
||||
unexpected (STAmount::getRate (STAmount (CURRENCY_ONE, ACCOUNT_ONE, 10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
unexpected (STAmount::getRate (STAmount (noCurrency(), noAccount(), 10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 6");
|
||||
|
||||
unexpected (STAmount::getRate (STAmount (1), STAmount (CURRENCY_ONE, ACCOUNT_ONE, 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
|
||||
unexpected (STAmount::getRate (STAmount (1), STAmount (noCurrency(), noAccount(), 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 7");
|
||||
|
||||
unexpected (STAmount::getRate (STAmount (10), STAmount (CURRENCY_ONE, ACCOUNT_ONE, 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
unexpected (STAmount::getRate (STAmount (10), STAmount (noCurrency(), noAccount(), 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
|
||||
"STAmount getRate fail 8");
|
||||
|
||||
roundTest (1, 3, 3);
|
||||
@@ -1712,29 +1637,29 @@ public:
|
||||
testcase ("underflow");
|
||||
|
||||
STAmount bigNative (STAmount::cMaxNative / 2);
|
||||
STAmount bigValue (CURRENCY_ONE, ACCOUNT_ONE,
|
||||
STAmount bigValue (noCurrency(), noAccount(),
|
||||
(STAmount::cMinValue + STAmount::cMaxValue) / 2, STAmount::cMaxOffset - 1);
|
||||
STAmount smallValue (CURRENCY_ONE, ACCOUNT_ONE,
|
||||
STAmount smallValue (noCurrency(), noAccount(),
|
||||
(STAmount::cMinValue + STAmount::cMaxValue) / 2, STAmount::cMinOffset + 1);
|
||||
STAmount zeroSt (CURRENCY_ONE, ACCOUNT_ONE, 0);
|
||||
STAmount zeroSt (noCurrency(), noAccount(), 0);
|
||||
|
||||
STAmount smallXsmall = STAmount::multiply (smallValue, smallValue, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount smallXsmall = STAmount::multiply (smallValue, smallValue, noCurrency(), noAccount());
|
||||
|
||||
expect (smallXsmall == zero, "smallXsmall != 0");
|
||||
|
||||
STAmount bigDsmall = STAmount::divide (smallValue, bigValue, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount bigDsmall = STAmount::divide (smallValue, bigValue, noCurrency(), noAccount());
|
||||
|
||||
expect (bigDsmall == zero, beast::String ("small/big != 0: ") + bigDsmall.getText ());
|
||||
|
||||
bigDsmall = STAmount::divide (smallValue, bigNative, CURRENCY_ONE, uint160 ());
|
||||
bigDsmall = STAmount::divide (smallValue, bigNative, noCurrency(), xrpIssuer ());
|
||||
|
||||
expect (bigDsmall == zero, beast::String ("small/bigNative != 0: ") + bigDsmall.getText ());
|
||||
|
||||
bigDsmall = STAmount::divide (smallValue, bigValue, uint160 (), uint160 ());
|
||||
bigDsmall = STAmount::divide (smallValue, bigValue, xrpCurrency (), xrpIssuer ());
|
||||
|
||||
expect (bigDsmall == zero, beast::String ("(small/big)->N != 0: ") + bigDsmall.getText ());
|
||||
|
||||
bigDsmall = STAmount::divide (smallValue, bigNative, uint160 (), uint160 ());
|
||||
bigDsmall = STAmount::divide (smallValue, bigNative, xrpCurrency (), xrpIssuer ());
|
||||
|
||||
expect (bigDsmall == zero, beast::String ("(small/bigNative)->N != 0: ") + bigDsmall.getText ());
|
||||
|
||||
@@ -1763,27 +1688,27 @@ public:
|
||||
int offset = -14;
|
||||
STAmount::canonicalizeRound (false, value, offset, true);
|
||||
|
||||
STAmount one (CURRENCY_ONE, ACCOUNT_ONE, 1);
|
||||
STAmount two (CURRENCY_ONE, ACCOUNT_ONE, 2);
|
||||
STAmount three (CURRENCY_ONE, ACCOUNT_ONE, 3);
|
||||
STAmount one (noCurrency(), noAccount(), 1);
|
||||
STAmount two (noCurrency(), noAccount(), 2);
|
||||
STAmount three (noCurrency(), noAccount(), 3);
|
||||
|
||||
STAmount oneThird1 = STAmount::divRound (one, three, CURRENCY_ONE, ACCOUNT_ONE, false);
|
||||
STAmount oneThird2 = STAmount::divide (one, three, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount oneThird3 = STAmount::divRound (one, three, CURRENCY_ONE, ACCOUNT_ONE, true);
|
||||
STAmount oneThird1 = STAmount::divRound (one, three, noCurrency(), noAccount(), false);
|
||||
STAmount oneThird2 = STAmount::divide (one, three, noCurrency(), noAccount());
|
||||
STAmount oneThird3 = STAmount::divRound (one, three, noCurrency(), noAccount(), true);
|
||||
WriteLog (lsINFO, STAmount) << oneThird1;
|
||||
WriteLog (lsINFO, STAmount) << oneThird2;
|
||||
WriteLog (lsINFO, STAmount) << oneThird3;
|
||||
|
||||
STAmount twoThird1 = STAmount::divRound (two, three, CURRENCY_ONE, ACCOUNT_ONE, false);
|
||||
STAmount twoThird2 = STAmount::divide (two, three, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount twoThird3 = STAmount::divRound (two, three, CURRENCY_ONE, ACCOUNT_ONE, true);
|
||||
STAmount twoThird1 = STAmount::divRound (two, three, noCurrency(), noAccount(), false);
|
||||
STAmount twoThird2 = STAmount::divide (two, three, noCurrency(), noAccount());
|
||||
STAmount twoThird3 = STAmount::divRound (two, three, noCurrency(), noAccount(), true);
|
||||
WriteLog (lsINFO, STAmount) << twoThird1;
|
||||
WriteLog (lsINFO, STAmount) << twoThird2;
|
||||
WriteLog (lsINFO, STAmount) << twoThird3;
|
||||
|
||||
STAmount oneA = STAmount::mulRound (oneThird1, three, CURRENCY_ONE, ACCOUNT_ONE, false);
|
||||
STAmount oneB = STAmount::multiply (oneThird2, three, CURRENCY_ONE, ACCOUNT_ONE);
|
||||
STAmount oneC = STAmount::mulRound (oneThird3, three, CURRENCY_ONE, ACCOUNT_ONE, true);
|
||||
STAmount oneA = STAmount::mulRound (oneThird1, three, noCurrency(), noAccount(), false);
|
||||
STAmount oneB = STAmount::multiply (oneThird2, three, noCurrency(), noAccount());
|
||||
STAmount oneC = STAmount::mulRound (oneThird3, three, noCurrency(), noAccount(), true);
|
||||
WriteLog (lsINFO, STAmount) << oneA;
|
||||
WriteLog (lsINFO, STAmount) << oneB;
|
||||
WriteLog (lsINFO, STAmount) << oneC;
|
||||
@@ -1795,9 +1720,9 @@ public:
|
||||
WriteLog (lsINFO, STAmount) << fourThirdsB;
|
||||
WriteLog (lsINFO, STAmount) << fourThirdsC;
|
||||
|
||||
STAmount dripTest1 = STAmount::mulRound (twoThird2, two, uint160 (), uint160 (), false);
|
||||
STAmount dripTest2 = STAmount::multiply (twoThird2, two, uint160 (), uint160 ());
|
||||
STAmount dripTest3 = STAmount::mulRound (twoThird2, two, uint160 (), uint160 (), true);
|
||||
STAmount dripTest1 = STAmount::mulRound (twoThird2, two, xrpCurrency (), xrpIssuer (), false);
|
||||
STAmount dripTest2 = STAmount::multiply (twoThird2, two, xrpCurrency (), xrpIssuer ());
|
||||
STAmount dripTest3 = STAmount::mulRound (twoThird2, two, xrpCurrency (), xrpIssuer (), true);
|
||||
WriteLog (lsINFO, STAmount) << dripTest1;
|
||||
WriteLog (lsINFO, STAmount) << dripTest2;
|
||||
WriteLog (lsINFO, STAmount) << dripTest3;
|
||||
|
||||
Reference in New Issue
Block a user