Format first-party source according to .clang-format

This commit is contained in:
Pretty Printer
2020-04-17 09:56:34 -05:00
committed by manojsdoshi
parent 65dfc5d19e
commit 50760c6935
1076 changed files with 86161 additions and 77449 deletions

View File

@@ -19,30 +19,32 @@
#include <ripple/basics/Log.h>
#include <ripple/basics/random.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/beast/unit_test.h>
#include <ripple/protocol/STAmount.h>
namespace ripple {
class STAmount_test : public beast::unit_test::suite
{
public:
static STAmount serializeAndDeserialize (STAmount const& s)
static STAmount
serializeAndDeserialize(STAmount const& s)
{
Serializer ser;
s.add (ser);
s.add(ser);
SerialIter sit (ser.slice());
SerialIter sit(ser.slice());
return STAmount(sit, sfGeneric);
}
//--------------------------------------------------------------------------
STAmount roundSelf (STAmount const& amount)
STAmount
roundSelf(STAmount const& amount)
{
if (amount.native ())
if (amount.native())
return amount;
std::uint64_t mantissa = amount.mantissa ();
std::uint64_t mantissa = amount.mantissa();
std::uint64_t valueDigits = mantissa % 1000000000;
if (valueDigits == 1)
@@ -50,11 +52,19 @@ public:
mantissa--;
if (mantissa < STAmount::cMinValue)
return { amount.issue (), mantissa, amount.exponent (),
amount.negative () };
return {
amount.issue(),
mantissa,
amount.exponent(),
amount.negative()};
return { amount.issue (), mantissa, amount.exponent (),
amount.native(), amount.negative (), STAmount::unchecked {} };
return {
amount.issue(),
mantissa,
amount.exponent(),
amount.native(),
amount.negative(),
STAmount::unchecked{}};
}
if (valueDigits == 999999999)
@@ -62,72 +72,86 @@ public:
mantissa++;
if (mantissa > STAmount::cMaxValue)
return { amount.issue (), mantissa, amount.exponent (),
amount.negative () };
return {
amount.issue(),
mantissa,
amount.exponent(),
amount.negative()};
return { amount.issue (), mantissa, amount.exponent (),
amount.native(), amount.negative (), STAmount::unchecked {} };
return {
amount.issue(),
mantissa,
amount.exponent(),
amount.native(),
amount.negative(),
STAmount::unchecked{}};
}
return amount;
}
void roundTest (int n, int d, int m)
void
roundTest(int n, int d, int m)
{
// check STAmount rounding
STAmount num (noIssue(), n);
STAmount den (noIssue(), d);
STAmount mul (noIssue(), m);
STAmount num(noIssue(), n);
STAmount den(noIssue(), d);
STAmount mul(noIssue(), m);
STAmount quot = divide(STAmount(n), STAmount(d), noIssue());
STAmount res = roundSelf (multiply (quot, mul, noIssue()));
STAmount res = roundSelf(multiply(quot, mul, noIssue()));
BEAST_EXPECT(! res.native ());
BEAST_EXPECT(!res.native());
STAmount cmp (noIssue(), (n * m) / d);
STAmount cmp(noIssue(), (n * m) / d);
BEAST_EXPECT(! cmp.native ());
BEAST_EXPECT(!cmp.native());
BEAST_EXPECT(cmp.issue().currency == res.issue().currency);
if (res != cmp)
{
log <<
"(" << num.getText () << "/" << den.getText () <<
") X " << mul.getText () << " = " << res.getText () <<
" not " << cmp.getText ();
fail ("Rounding");
log << "(" << num.getText() << "/" << den.getText() << ") X "
<< mul.getText() << " = " << res.getText() << " not "
<< cmp.getText();
fail("Rounding");
return;
}
}
void mulTest (int a, int b)
void
mulTest(int a, int b)
{
STAmount aa (noIssue(), a);
STAmount bb (noIssue(), b);
STAmount prod1 (multiply (aa, bb, noIssue()));
STAmount aa(noIssue(), a);
STAmount bb(noIssue(), b);
STAmount prod1(multiply(aa, bb, noIssue()));
BEAST_EXPECT(! prod1.native ());
BEAST_EXPECT(!prod1.native());
STAmount prod2 (noIssue(), static_cast<std::uint64_t> (a) * static_cast<std::uint64_t> (b));
STAmount prod2(
noIssue(),
static_cast<std::uint64_t>(a) * static_cast<std::uint64_t>(b));
if (prod1 != prod2)
{
log <<
"nn(" << aa.getFullText () << " * " << bb.getFullText () <<
") = " << prod1.getFullText () << " not " << prod2.getFullText ();
fail ("Multiplication result is not exact");
log << "nn(" << aa.getFullText() << " * " << bb.getFullText()
<< ") = " << prod1.getFullText() << " not "
<< prod2.getFullText();
fail("Multiplication result is not exact");
}
}
//--------------------------------------------------------------------------
void testSetValue (
std::string const& value, Issue const& issue, bool success = true)
void
testSetValue(
std::string const& value,
Issue const& issue,
bool success = true)
{
try
{
STAmount const amount = amountFromString (issue, value);
BEAST_EXPECT(amount.getText () == value);
STAmount const amount = amountFromString(issue, value);
BEAST_EXPECT(amount.getText() == value);
}
catch (std::exception const&)
{
@@ -135,361 +159,405 @@ public:
}
}
void testSetValue ()
void
testSetValue()
{
{
testcase ("set value (native)");
testcase("set value (native)");
Issue const xrp (xrpIssue ());
Issue const xrp(xrpIssue());
// fractional XRP (i.e. drops)
testSetValue ("1", xrp);
testSetValue ("22", xrp);
testSetValue ("333", xrp);
testSetValue ("4444", xrp);
testSetValue ("55555", xrp);
testSetValue ("666666", xrp);
testSetValue("1", xrp);
testSetValue("22", xrp);
testSetValue("333", xrp);
testSetValue("4444", xrp);
testSetValue("55555", xrp);
testSetValue("666666", xrp);
// 1 XRP up to 100 billion, in powers of 10 (in drops)
testSetValue ("1000000", xrp);
testSetValue ("10000000", xrp);
testSetValue ("100000000", xrp);
testSetValue ("1000000000", xrp);
testSetValue ("10000000000", xrp);
testSetValue ("100000000000", xrp);
testSetValue ("1000000000000", xrp);
testSetValue ("10000000000000", xrp);
testSetValue ("100000000000000", xrp);
testSetValue ("1000000000000000", xrp);
testSetValue ("10000000000000000", xrp);
testSetValue ("100000000000000000", xrp);
testSetValue("1000000", xrp);
testSetValue("10000000", xrp);
testSetValue("100000000", xrp);
testSetValue("1000000000", xrp);
testSetValue("10000000000", xrp);
testSetValue("100000000000", xrp);
testSetValue("1000000000000", xrp);
testSetValue("10000000000000", xrp);
testSetValue("100000000000000", xrp);
testSetValue("1000000000000000", xrp);
testSetValue("10000000000000000", xrp);
testSetValue("100000000000000000", xrp);
// Invalid native values:
testSetValue ("1.1", xrp, false);
testSetValue ("100000000000000001", xrp, false);
testSetValue ("1000000000000000000", xrp, false);
testSetValue("1.1", xrp, false);
testSetValue("100000000000000001", xrp, false);
testSetValue("1000000000000000000", xrp, false);
}
{
testcase ("set value (iou)");
testcase("set value (iou)");
Issue const usd (Currency (0x5553440000000000), AccountID (0x4985601));
Issue const usd(Currency(0x5553440000000000), AccountID(0x4985601));
testSetValue ("1", usd);
testSetValue ("10", usd);
testSetValue ("100", usd);
testSetValue ("1000", usd);
testSetValue ("10000", usd);
testSetValue ("100000", usd);
testSetValue ("1000000", usd);
testSetValue ("10000000", usd);
testSetValue ("100000000", usd);
testSetValue ("1000000000", usd);
testSetValue ("10000000000", usd);
testSetValue("1", usd);
testSetValue("10", usd);
testSetValue("100", usd);
testSetValue("1000", usd);
testSetValue("10000", usd);
testSetValue("100000", usd);
testSetValue("1000000", usd);
testSetValue("10000000", usd);
testSetValue("100000000", usd);
testSetValue("1000000000", usd);
testSetValue("10000000000", usd);
testSetValue ("1234567.1", usd);
testSetValue ("1234567.12", usd);
testSetValue ("1234567.123", usd);
testSetValue ("1234567.1234", usd);
testSetValue ("1234567.12345", usd);
testSetValue ("1234567.123456", usd);
testSetValue ("1234567.1234567", usd);
testSetValue ("1234567.12345678", usd);
testSetValue ("1234567.123456789", usd);
testSetValue("1234567.1", usd);
testSetValue("1234567.12", usd);
testSetValue("1234567.123", usd);
testSetValue("1234567.1234", usd);
testSetValue("1234567.12345", usd);
testSetValue("1234567.123456", usd);
testSetValue("1234567.1234567", usd);
testSetValue("1234567.12345678", usd);
testSetValue("1234567.123456789", usd);
}
}
//--------------------------------------------------------------------------
void testNativeCurrency ()
void
testNativeCurrency()
{
testcase ("native currency");
STAmount zeroSt, one (1), hundred (100);
testcase("native currency");
STAmount zeroSt, one(1), hundred(100);
// VFALCO NOTE Why repeat "STAmount fail" so many times??
unexpected (serializeAndDeserialize (zeroSt) != zeroSt, "STAmount fail");
unexpected (serializeAndDeserialize (one) != one, "STAmount fail");
unexpected (serializeAndDeserialize (hundred) != hundred, "STAmount fail");
unexpected (!zeroSt.native (), "STAmount fail");
unexpected (!hundred.native (), "STAmount fail");
unexpected (zeroSt != beast::zero, "STAmount fail");
unexpected (one == beast::zero, "STAmount fail");
unexpected (hundred == beast::zero, "STAmount fail");
unexpected ((zeroSt < zeroSt), "STAmount fail");
unexpected (! (zeroSt < one), "STAmount fail");
unexpected (! (zeroSt < hundred), "STAmount fail");
unexpected ((one < zeroSt), "STAmount fail");
unexpected ((one < one), "STAmount fail");
unexpected (! (one < hundred), "STAmount fail");
unexpected ((hundred < zeroSt), "STAmount fail");
unexpected ((hundred < one), "STAmount fail");
unexpected ((hundred < hundred), "STAmount fail");
unexpected ((zeroSt > zeroSt), "STAmount fail");
unexpected ((zeroSt > one), "STAmount fail");
unexpected ((zeroSt > hundred), "STAmount fail");
unexpected (! (one > zeroSt), "STAmount fail");
unexpected ((one > one), "STAmount fail");
unexpected ((one > hundred), "STAmount fail");
unexpected (! (hundred > zeroSt), "STAmount fail");
unexpected (! (hundred > one), "STAmount fail");
unexpected ((hundred > hundred), "STAmount fail");
unexpected (! (zeroSt <= zeroSt), "STAmount fail");
unexpected (! (zeroSt <= one), "STAmount fail");
unexpected (! (zeroSt <= hundred), "STAmount fail");
unexpected ((one <= zeroSt), "STAmount fail");
unexpected (! (one <= one), "STAmount fail");
unexpected (! (one <= hundred), "STAmount fail");
unexpected ((hundred <= zeroSt), "STAmount fail");
unexpected ((hundred <= one), "STAmount fail");
unexpected (! (hundred <= hundred), "STAmount fail");
unexpected (! (zeroSt >= zeroSt), "STAmount fail");
unexpected ((zeroSt >= one), "STAmount fail");
unexpected ((zeroSt >= hundred), "STAmount fail");
unexpected (! (one >= zeroSt), "STAmount fail");
unexpected (! (one >= one), "STAmount fail");
unexpected ((one >= hundred), "STAmount fail");
unexpected (! (hundred >= zeroSt), "STAmount fail");
unexpected (! (hundred >= one), "STAmount fail");
unexpected (! (hundred >= hundred), "STAmount fail");
unexpected (! (zeroSt == zeroSt), "STAmount fail");
unexpected ((zeroSt == one), "STAmount fail");
unexpected ((zeroSt == hundred), "STAmount fail");
unexpected ((one == zeroSt), "STAmount fail");
unexpected (! (one == one), "STAmount fail");
unexpected ((one == hundred), "STAmount fail");
unexpected ((hundred == zeroSt), "STAmount fail");
unexpected ((hundred == one), "STAmount fail");
unexpected (! (hundred == hundred), "STAmount fail");
unexpected ((zeroSt != zeroSt), "STAmount fail");
unexpected (! (zeroSt != one), "STAmount fail");
unexpected (! (zeroSt != hundred), "STAmount fail");
unexpected (! (one != zeroSt), "STAmount fail");
unexpected ((one != one), "STAmount fail");
unexpected (! (one != hundred), "STAmount fail");
unexpected (! (hundred != zeroSt), "STAmount fail");
unexpected (! (hundred != one), "STAmount fail");
unexpected ((hundred != hundred), "STAmount fail");
unexpected (STAmount ().getText () != "0", "STAmount fail");
unexpected (STAmount (31).getText () != "31", "STAmount fail");
unexpected (STAmount (310).getText () != "310", "STAmount fail");
unexpected (to_string (Currency ()) != "XRP", "cHC(XRP)");
unexpected(serializeAndDeserialize(zeroSt) != zeroSt, "STAmount fail");
unexpected(serializeAndDeserialize(one) != one, "STAmount fail");
unexpected(
serializeAndDeserialize(hundred) != hundred, "STAmount fail");
unexpected(!zeroSt.native(), "STAmount fail");
unexpected(!hundred.native(), "STAmount fail");
unexpected(zeroSt != beast::zero, "STAmount fail");
unexpected(one == beast::zero, "STAmount fail");
unexpected(hundred == beast::zero, "STAmount fail");
unexpected((zeroSt < zeroSt), "STAmount fail");
unexpected(!(zeroSt < one), "STAmount fail");
unexpected(!(zeroSt < hundred), "STAmount fail");
unexpected((one < zeroSt), "STAmount fail");
unexpected((one < one), "STAmount fail");
unexpected(!(one < hundred), "STAmount fail");
unexpected((hundred < zeroSt), "STAmount fail");
unexpected((hundred < one), "STAmount fail");
unexpected((hundred < hundred), "STAmount fail");
unexpected((zeroSt > zeroSt), "STAmount fail");
unexpected((zeroSt > one), "STAmount fail");
unexpected((zeroSt > hundred), "STAmount fail");
unexpected(!(one > zeroSt), "STAmount fail");
unexpected((one > one), "STAmount fail");
unexpected((one > hundred), "STAmount fail");
unexpected(!(hundred > zeroSt), "STAmount fail");
unexpected(!(hundred > one), "STAmount fail");
unexpected((hundred > hundred), "STAmount fail");
unexpected(!(zeroSt <= zeroSt), "STAmount fail");
unexpected(!(zeroSt <= one), "STAmount fail");
unexpected(!(zeroSt <= hundred), "STAmount fail");
unexpected((one <= zeroSt), "STAmount fail");
unexpected(!(one <= one), "STAmount fail");
unexpected(!(one <= hundred), "STAmount fail");
unexpected((hundred <= zeroSt), "STAmount fail");
unexpected((hundred <= one), "STAmount fail");
unexpected(!(hundred <= hundred), "STAmount fail");
unexpected(!(zeroSt >= zeroSt), "STAmount fail");
unexpected((zeroSt >= one), "STAmount fail");
unexpected((zeroSt >= hundred), "STAmount fail");
unexpected(!(one >= zeroSt), "STAmount fail");
unexpected(!(one >= one), "STAmount fail");
unexpected((one >= hundred), "STAmount fail");
unexpected(!(hundred >= zeroSt), "STAmount fail");
unexpected(!(hundred >= one), "STAmount fail");
unexpected(!(hundred >= hundred), "STAmount fail");
unexpected(!(zeroSt == zeroSt), "STAmount fail");
unexpected((zeroSt == one), "STAmount fail");
unexpected((zeroSt == hundred), "STAmount fail");
unexpected((one == zeroSt), "STAmount fail");
unexpected(!(one == one), "STAmount fail");
unexpected((one == hundred), "STAmount fail");
unexpected((hundred == zeroSt), "STAmount fail");
unexpected((hundred == one), "STAmount fail");
unexpected(!(hundred == hundred), "STAmount fail");
unexpected((zeroSt != zeroSt), "STAmount fail");
unexpected(!(zeroSt != one), "STAmount fail");
unexpected(!(zeroSt != hundred), "STAmount fail");
unexpected(!(one != zeroSt), "STAmount fail");
unexpected((one != one), "STAmount fail");
unexpected(!(one != hundred), "STAmount fail");
unexpected(!(hundred != zeroSt), "STAmount fail");
unexpected(!(hundred != one), "STAmount fail");
unexpected((hundred != hundred), "STAmount fail");
unexpected(STAmount().getText() != "0", "STAmount fail");
unexpected(STAmount(31).getText() != "31", "STAmount fail");
unexpected(STAmount(310).getText() != "310", "STAmount fail");
unexpected(to_string(Currency()) != "XRP", "cHC(XRP)");
Currency c;
unexpected (!to_currency (c, "USD"), "create USD currency");
unexpected (to_string (c) != "USD", "check USD currency");
unexpected(!to_currency(c, "USD"), "create USD currency");
unexpected(to_string(c) != "USD", "check USD currency");
const std::string cur = "015841551A748AD2C1F76FF6ECB0CCCD00000000";
unexpected (!to_currency (c, cur), "create custom currency");
unexpected (to_string (c) != cur, "check custom currency");
unexpected (c != Currency (
from_hex_text<Currency>(cur)), "check custom currency");
unexpected(!to_currency(c, cur), "create custom currency");
unexpected(to_string(c) != cur, "check custom currency");
unexpected(
c != Currency(from_hex_text<Currency>(cur)),
"check custom currency");
}
//--------------------------------------------------------------------------
void testCustomCurrency ()
void
testCustomCurrency()
{
testcase ("custom currency");
STAmount zeroSt (noIssue()), one (noIssue(), 1), hundred (noIssue(), 100);
unexpected (serializeAndDeserialize (zeroSt) != zeroSt, "STAmount fail");
unexpected (serializeAndDeserialize (one) != one, "STAmount fail");
unexpected (serializeAndDeserialize (hundred) != hundred, "STAmount fail");
unexpected (zeroSt.native (), "STAmount fail");
unexpected (hundred.native (), "STAmount fail");
unexpected (zeroSt != beast::zero, "STAmount fail");
unexpected (one == beast::zero, "STAmount fail");
unexpected (hundred == beast::zero, "STAmount fail");
unexpected ((zeroSt < zeroSt), "STAmount fail");
unexpected (! (zeroSt < one), "STAmount fail");
unexpected (! (zeroSt < hundred), "STAmount fail");
unexpected ((one < zeroSt), "STAmount fail");
unexpected ((one < one), "STAmount fail");
unexpected (! (one < hundred), "STAmount fail");
unexpected ((hundred < zeroSt), "STAmount fail");
unexpected ((hundred < one), "STAmount fail");
unexpected ((hundred < hundred), "STAmount fail");
unexpected ((zeroSt > zeroSt), "STAmount fail");
unexpected ((zeroSt > one), "STAmount fail");
unexpected ((zeroSt > hundred), "STAmount fail");
unexpected (! (one > zeroSt), "STAmount fail");
unexpected ((one > one), "STAmount fail");
unexpected ((one > hundred), "STAmount fail");
unexpected (! (hundred > zeroSt), "STAmount fail");
unexpected (! (hundred > one), "STAmount fail");
unexpected ((hundred > hundred), "STAmount fail");
unexpected (! (zeroSt <= zeroSt), "STAmount fail");
unexpected (! (zeroSt <= one), "STAmount fail");
unexpected (! (zeroSt <= hundred), "STAmount fail");
unexpected ((one <= zeroSt), "STAmount fail");
unexpected (! (one <= one), "STAmount fail");
unexpected (! (one <= hundred), "STAmount fail");
unexpected ((hundred <= zeroSt), "STAmount fail");
unexpected ((hundred <= one), "STAmount fail");
unexpected (! (hundred <= hundred), "STAmount fail");
unexpected (! (zeroSt >= zeroSt), "STAmount fail");
unexpected ((zeroSt >= one), "STAmount fail");
unexpected ((zeroSt >= hundred), "STAmount fail");
unexpected (! (one >= zeroSt), "STAmount fail");
unexpected (! (one >= one), "STAmount fail");
unexpected ((one >= hundred), "STAmount fail");
unexpected (! (hundred >= zeroSt), "STAmount fail");
unexpected (! (hundred >= one), "STAmount fail");
unexpected (! (hundred >= hundred), "STAmount fail");
unexpected (! (zeroSt == zeroSt), "STAmount fail");
unexpected ((zeroSt == one), "STAmount fail");
unexpected ((zeroSt == hundred), "STAmount fail");
unexpected ((one == zeroSt), "STAmount fail");
unexpected (! (one == one), "STAmount fail");
unexpected ((one == hundred), "STAmount fail");
unexpected ((hundred == zeroSt), "STAmount fail");
unexpected ((hundred == one), "STAmount fail");
unexpected (! (hundred == hundred), "STAmount fail");
unexpected ((zeroSt != zeroSt), "STAmount fail");
unexpected (! (zeroSt != one), "STAmount fail");
unexpected (! (zeroSt != hundred), "STAmount fail");
unexpected (! (one != zeroSt), "STAmount fail");
unexpected ((one != one), "STAmount fail");
unexpected (! (one != hundred), "STAmount fail");
unexpected (! (hundred != zeroSt), "STAmount fail");
unexpected (! (hundred != one), "STAmount fail");
unexpected ((hundred != hundred), "STAmount fail");
unexpected (STAmount (noIssue()).getText () != "0", "STAmount fail");
unexpected (STAmount (noIssue(), 31).getText () != "31", "STAmount fail");
unexpected (STAmount (noIssue(), 31, 1).getText () != "310", "STAmount fail");
unexpected (STAmount (noIssue(), 31, -1).getText () != "3.1", "STAmount fail");
unexpected (STAmount (noIssue(), 31, -2).getText () != "0.31", "STAmount fail");
unexpected (multiply (STAmount (noIssue(), 20), STAmount (3), noIssue()).getText () != "60",
testcase("custom currency");
STAmount zeroSt(noIssue()), one(noIssue(), 1), hundred(noIssue(), 100);
unexpected(serializeAndDeserialize(zeroSt) != zeroSt, "STAmount fail");
unexpected(serializeAndDeserialize(one) != one, "STAmount fail");
unexpected(
serializeAndDeserialize(hundred) != hundred, "STAmount fail");
unexpected(zeroSt.native(), "STAmount fail");
unexpected(hundred.native(), "STAmount fail");
unexpected(zeroSt != beast::zero, "STAmount fail");
unexpected(one == beast::zero, "STAmount fail");
unexpected(hundred == beast::zero, "STAmount fail");
unexpected((zeroSt < zeroSt), "STAmount fail");
unexpected(!(zeroSt < one), "STAmount fail");
unexpected(!(zeroSt < hundred), "STAmount fail");
unexpected((one < zeroSt), "STAmount fail");
unexpected((one < one), "STAmount fail");
unexpected(!(one < hundred), "STAmount fail");
unexpected((hundred < zeroSt), "STAmount fail");
unexpected((hundred < one), "STAmount fail");
unexpected((hundred < hundred), "STAmount fail");
unexpected((zeroSt > zeroSt), "STAmount fail");
unexpected((zeroSt > one), "STAmount fail");
unexpected((zeroSt > hundred), "STAmount fail");
unexpected(!(one > zeroSt), "STAmount fail");
unexpected((one > one), "STAmount fail");
unexpected((one > hundred), "STAmount fail");
unexpected(!(hundred > zeroSt), "STAmount fail");
unexpected(!(hundred > one), "STAmount fail");
unexpected((hundred > hundred), "STAmount fail");
unexpected(!(zeroSt <= zeroSt), "STAmount fail");
unexpected(!(zeroSt <= one), "STAmount fail");
unexpected(!(zeroSt <= hundred), "STAmount fail");
unexpected((one <= zeroSt), "STAmount fail");
unexpected(!(one <= one), "STAmount fail");
unexpected(!(one <= hundred), "STAmount fail");
unexpected((hundred <= zeroSt), "STAmount fail");
unexpected((hundred <= one), "STAmount fail");
unexpected(!(hundred <= hundred), "STAmount fail");
unexpected(!(zeroSt >= zeroSt), "STAmount fail");
unexpected((zeroSt >= one), "STAmount fail");
unexpected((zeroSt >= hundred), "STAmount fail");
unexpected(!(one >= zeroSt), "STAmount fail");
unexpected(!(one >= one), "STAmount fail");
unexpected((one >= hundred), "STAmount fail");
unexpected(!(hundred >= zeroSt), "STAmount fail");
unexpected(!(hundred >= one), "STAmount fail");
unexpected(!(hundred >= hundred), "STAmount fail");
unexpected(!(zeroSt == zeroSt), "STAmount fail");
unexpected((zeroSt == one), "STAmount fail");
unexpected((zeroSt == hundred), "STAmount fail");
unexpected((one == zeroSt), "STAmount fail");
unexpected(!(one == one), "STAmount fail");
unexpected((one == hundred), "STAmount fail");
unexpected((hundred == zeroSt), "STAmount fail");
unexpected((hundred == one), "STAmount fail");
unexpected(!(hundred == hundred), "STAmount fail");
unexpected((zeroSt != zeroSt), "STAmount fail");
unexpected(!(zeroSt != one), "STAmount fail");
unexpected(!(zeroSt != hundred), "STAmount fail");
unexpected(!(one != zeroSt), "STAmount fail");
unexpected((one != one), "STAmount fail");
unexpected(!(one != hundred), "STAmount fail");
unexpected(!(hundred != zeroSt), "STAmount fail");
unexpected(!(hundred != one), "STAmount fail");
unexpected((hundred != hundred), "STAmount fail");
unexpected(STAmount(noIssue()).getText() != "0", "STAmount fail");
unexpected(STAmount(noIssue(), 31).getText() != "31", "STAmount fail");
unexpected(
STAmount(noIssue(), 31, 1).getText() != "310", "STAmount fail");
unexpected(
STAmount(noIssue(), 31, -1).getText() != "3.1", "STAmount fail");
unexpected(
STAmount(noIssue(), 31, -2).getText() != "0.31", "STAmount fail");
unexpected(
multiply(STAmount(noIssue(), 20), STAmount(3), noIssue())
.getText() != "60",
"STAmount multiply fail 1");
unexpected (multiply (STAmount (noIssue(), 20), STAmount (3), xrpIssue ()).getText () != "60",
unexpected(
multiply(STAmount(noIssue(), 20), STAmount(3), xrpIssue())
.getText() != "60",
"STAmount multiply fail 2");
unexpected (multiply (STAmount (20), STAmount (3), noIssue()).getText () != "60",
unexpected(
multiply(STAmount(20), STAmount(3), noIssue()).getText() != "60",
"STAmount multiply fail 3");
unexpected (multiply (STAmount (20), STAmount (3), xrpIssue ()).getText () != "60",
unexpected(
multiply(STAmount(20), STAmount(3), xrpIssue()).getText() != "60",
"STAmount multiply fail 4");
if (divide (STAmount (noIssue(), 60), STAmount (3), noIssue()).getText () != "20")
if (divide(STAmount(noIssue(), 60), STAmount(3), noIssue()).getText() !=
"20")
{
log << "60/3 = " <<
divide (STAmount (noIssue(), 60),
STAmount (3), noIssue()).getText ();
fail ("STAmount divide fail");
log << "60/3 = "
<< divide(STAmount(noIssue(), 60), STAmount(3), noIssue())
.getText();
fail("STAmount divide fail");
}
else
{
pass ();
pass();
}
unexpected (divide (STAmount (noIssue(), 60), STAmount (3), xrpIssue ()).getText () != "20",
unexpected(
divide(STAmount(noIssue(), 60), STAmount(3), xrpIssue())
.getText() != "20",
"STAmount divide fail");
unexpected (divide (STAmount (noIssue(), 60), STAmount (noIssue(), 3), noIssue()).getText () != "20",
unexpected(
divide(STAmount(noIssue(), 60), STAmount(noIssue(), 3), noIssue())
.getText() != "20",
"STAmount divide fail");
unexpected (divide (STAmount (noIssue(), 60), STAmount (noIssue(), 3), xrpIssue ()).getText () != "20",
unexpected(
divide(STAmount(noIssue(), 60), STAmount(noIssue(), 3), xrpIssue())
.getText() != "20",
"STAmount divide fail");
STAmount a1 (noIssue(), 60), a2 (noIssue(), 10, -1);
STAmount a1(noIssue(), 60), a2(noIssue(), 10, -1);
unexpected (divide (a2, a1, noIssue()) != amountFromQuality (getRate (a1, a2)),
unexpected(
divide(a2, a1, noIssue()) != amountFromQuality(getRate(a1, a2)),
"STAmount setRate(getRate) fail");
unexpected (divide (a1, a2, noIssue()) != amountFromQuality (getRate (a2, a1)),
unexpected(
divide(a1, a2, noIssue()) != amountFromQuality(getRate(a2, a1)),
"STAmount setRate(getRate) fail");
}
//--------------------------------------------------------------------------
void testArithmetic ()
void
testArithmetic()
{
testcase ("arithmetic");
testcase("arithmetic");
// Test currency multiplication and division operations such as
// convertToDisplayAmount, convertToInternalAmount, getRate, getClaimed, and getNeeded
// convertToDisplayAmount, convertToInternalAmount, getRate, getClaimed,
// and getNeeded
unexpected (getRate (STAmount (1), STAmount (10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(1), STAmount(10)) !=
(((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 1");
unexpected (getRate (STAmount (10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(10), STAmount(1)) !=
(((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 2");
unexpected (getRate (STAmount (noIssue(), 1), STAmount (noIssue(), 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(noIssue(), 1), STAmount(noIssue(), 10)) !=
(((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 3");
unexpected (getRate (STAmount (noIssue(), 10), STAmount (noIssue(), 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(noIssue(), 10), STAmount(noIssue(), 1)) !=
(((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 4");
unexpected (getRate (STAmount (noIssue(), 1), STAmount (10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(noIssue(), 1), STAmount(10)) !=
(((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 5");
unexpected (getRate (STAmount (noIssue(), 10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(noIssue(), 10), STAmount(1)) !=
(((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 6");
unexpected (getRate (STAmount (1), STAmount (noIssue(), 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(1), STAmount(noIssue(), 10)) !=
(((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 7");
unexpected (getRate (STAmount (10), STAmount (noIssue(), 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
unexpected(
getRate(STAmount(10), STAmount(noIssue(), 1)) !=
(((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 8");
roundTest (1, 3, 3);
roundTest (2, 3, 9);
roundTest (1, 7, 21);
roundTest (1, 2, 4);
roundTest (3, 9, 18);
roundTest (7, 11, 44);
roundTest(1, 3, 3);
roundTest(2, 3, 9);
roundTest(1, 7, 21);
roundTest(1, 2, 4);
roundTest(3, 9, 18);
roundTest(7, 11, 44);
for (int i = 0; i <= 100000; ++i)
{
mulTest (
rand_int(10000000),
rand_int(10000000));
mulTest(rand_int(10000000), rand_int(10000000));
}
}
//--------------------------------------------------------------------------
void testUnderflow ()
void
testUnderflow()
{
testcase ("underflow");
testcase("underflow");
STAmount bigNative (STAmount::cMaxNative / 2);
STAmount bigValue (noIssue(),
STAmount bigNative(STAmount::cMaxNative / 2);
STAmount bigValue(
noIssue(),
(STAmount::cMinValue + STAmount::cMaxValue) / 2,
STAmount::cMaxOffset - 1);
STAmount smallValue (noIssue(),
STAmount smallValue(
noIssue(),
(STAmount::cMinValue + STAmount::cMaxValue) / 2,
STAmount::cMinOffset + 1);
STAmount zeroSt (noIssue(), 0);
STAmount zeroSt(noIssue(), 0);
STAmount smallXsmall = multiply (smallValue, smallValue, noIssue());
STAmount smallXsmall = multiply(smallValue, smallValue, noIssue());
BEAST_EXPECT(smallXsmall == beast::zero);
STAmount bigDsmall = divide (smallValue, bigValue, noIssue());
STAmount bigDsmall = divide(smallValue, bigValue, noIssue());
BEAST_EXPECT(bigDsmall == beast::zero);
BEAST_EXPECT(bigDsmall == beast::zero);
bigDsmall = divide (smallValue, bigValue, xrpIssue ());
bigDsmall = divide(smallValue, bigValue, xrpIssue());
BEAST_EXPECT(bigDsmall == beast::zero);
bigDsmall = divide (smallValue, bigNative, xrpIssue ());
bigDsmall = divide(smallValue, bigNative, xrpIssue());
BEAST_EXPECT(bigDsmall == beast::zero);
// very bad offer
std::uint64_t r = getRate (smallValue, bigValue);
std::uint64_t r = getRate(smallValue, bigValue);
BEAST_EXPECT(r == 0);
// very good offer
r = getRate (bigValue, smallValue);
r = getRate(bigValue, smallValue);
BEAST_EXPECT(r == 0);
}
//--------------------------------------------------------------------------
void testRounding ()
void
testRounding()
{
// VFALCO TODO There are no actual tests here, just printed output?
// Change this to actually do something.
@@ -541,84 +609,87 @@ public:
}
void
testConvertXRP ()
testConvertXRP()
{
testcase ("STAmount to XRPAmount conversions");
testcase("STAmount to XRPAmount conversions");
Issue const usd { Currency (0x5553440000000000), AccountID (0x4985601) };
Issue const xrp { xrpIssue () };
Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
Issue const xrp{xrpIssue()};
for (std::uint64_t drops = 100000000000000000; drops != 1; drops = drops / 10)
for (std::uint64_t drops = 100000000000000000; drops != 1;
drops = drops / 10)
{
auto const t = amountFromString (xrp, std::to_string (drops));
auto const s = t.xrp ();
auto const t = amountFromString(xrp, std::to_string(drops));
auto const s = t.xrp();
BEAST_EXPECT(s.drops() == drops);
BEAST_EXPECT(t == STAmount (XRPAmount (drops)));
BEAST_EXPECT(s == XRPAmount (drops));
BEAST_EXPECT(t == STAmount(XRPAmount(drops)));
BEAST_EXPECT(s == XRPAmount(drops));
}
try
{
auto const t = amountFromString (usd, "136500");
fail (to_string (t.xrp ()));
auto const t = amountFromString(usd, "136500");
fail(to_string(t.xrp()));
}
catch (std::logic_error const&)
{
pass ();
pass();
}
catch (std::exception const&)
{
fail ("wrong exception");
fail("wrong exception");
}
}
void
testConvertIOU ()
testConvertIOU()
{
testcase ("STAmount to IOUAmount conversions");
testcase("STAmount to IOUAmount conversions");
Issue const usd { Currency (0x5553440000000000), AccountID (0x4985601) };
Issue const xrp { xrpIssue () };
Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
Issue const xrp{xrpIssue()};
for (std::uint64_t dollars = 10000000000; dollars != 1; dollars = dollars / 10)
for (std::uint64_t dollars = 10000000000; dollars != 1;
dollars = dollars / 10)
{
auto const t = amountFromString (usd, std::to_string (dollars));
auto const s = t.iou ();
BEAST_EXPECT(t == STAmount (s, usd));
BEAST_EXPECT(s.mantissa () == t.mantissa ());
BEAST_EXPECT(s.exponent () == t.exponent ());
auto const t = amountFromString(usd, std::to_string(dollars));
auto const s = t.iou();
BEAST_EXPECT(t == STAmount(s, usd));
BEAST_EXPECT(s.mantissa() == t.mantissa());
BEAST_EXPECT(s.exponent() == t.exponent());
}
try
{
auto const t = amountFromString (xrp, "136500");
fail (to_string (t.iou ()));
auto const t = amountFromString(xrp, "136500");
fail(to_string(t.iou()));
}
catch (std::logic_error const&)
{
pass ();
pass();
}
catch (std::exception const&)
{
fail ("wrong exception");
fail("wrong exception");
}
}
//--------------------------------------------------------------------------
void run () override
void
run() override
{
testSetValue ();
testNativeCurrency ();
testCustomCurrency ();
testArithmetic ();
testUnderflow ();
testRounding ();
testConvertXRP ();
testConvertIOU ();
testSetValue();
testNativeCurrency();
testCustomCurrency();
testArithmetic();
testUnderflow();
testRounding();
testConvertXRP();
testConvertIOU();
}
};
BEAST_DEFINE_TESTSUITE(STAmount,ripple_data,ripple);
BEAST_DEFINE_TESTSUITE(STAmount, ripple_data, ripple);
} // ripple
} // namespace ripple