From 97fe2568e59d777814562445231bb2ceeee7c36d Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 2 Jun 2012 17:12:57 -0700 Subject: [PATCH 1/2] Improve starting currency comments. --- src/Config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Config.h b/src/Config.h index 9cd34d22e0..ad737bd35b 100644 --- a/src/Config.h +++ b/src/Config.h @@ -13,7 +13,7 @@ #define SYSTEM_CURRENCY_GIFT 1000ull #define SYSTEM_CURRENCY_USERS 100000000ull -#define SYSTEM_CURRENCY_PARTS 1000000ull +#define SYSTEM_CURRENCY_PARTS 1000000ull // 10^SYSTEM_CURRENCY_PRECISION #define SYSTEM_CURRENCY_START (SYSTEM_CURRENCY_GIFT*SYSTEM_CURRENCY_USERS*SYSTEM_CURRENCY_PARTS) #define VALIDATORS_FILE_NAME "validators.txt" From 96face502ca59a1b7c186691d9c152d3bfe79c3b Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sun, 3 Jun 2012 01:54:51 -0700 Subject: [PATCH 2/2] Add unit tests for STAmount::setValue. --- src/Amount.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Amount.cpp b/src/Amount.cpp index b3cddd9809..cefa268af0 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -1,4 +1,3 @@ - #include #include #include @@ -107,18 +106,22 @@ bool STAmount::setValue(const std::string& sAmount, const std::string& sCurrency else { // Example size decimal size-decimal offset - // .1 2 0 2 -1 - // 123. 4 3 1 0 - // 1.23 4 1 3 -2 + // ^1 2 0 2 -1 + // 123^ 4 3 1 0 + // 1^23 4 1 3 -2 iOffset = -(sAmount.size()-uDecimal-1); + + // Issolate integer and fraction. uint64 uInteger = uDecimal ? boost::lexical_cast(sAmount.substr(0, uDecimal)) : 0; uint64 uFraction = iOffset ? boost::lexical_cast(sAmount.substr(uDecimal+1)) : 0; + // Scale the integer portion to the same offset as the fraction. uValue = uInteger; for (int i=-iOffset; i--;) uValue *= 10; + // Add in the fraction. uValue += uFraction; } @@ -801,6 +804,20 @@ static STAmount serdes(const STAmount &s) BOOST_AUTO_TEST_SUITE(amount) +BOOST_AUTO_TEST_CASE( setValue_test ) +{ + STAmount saTmp; + + // Check native floats + saTmp.setValue("1^0",""); BOOST_CHECK_MESSAGE(SYSTEM_CURRENCY_PARTS == saTmp.getNValue(), "float integer failed"); + saTmp.setValue("0^1",""); BOOST_CHECK_MESSAGE(SYSTEM_CURRENCY_PARTS/10 == saTmp.getNValue(), "float fraction failed"); + saTmp.setValue("0^12",""); BOOST_CHECK_MESSAGE(12*SYSTEM_CURRENCY_PARTS/100 == saTmp.getNValue(), "float fraction failed"); + saTmp.setValue("1^2",""); BOOST_CHECK_MESSAGE(SYSTEM_CURRENCY_PARTS+(2*SYSTEM_CURRENCY_PARTS/10) == saTmp.getNValue(), "float combined failed"); + + // Check native integer + saTmp.setValue("1",""); BOOST_CHECK_MESSAGE(1 == saTmp.getNValue(), "integer failed"); +} + BOOST_AUTO_TEST_CASE( NativeCurrency_test ) { STAmount zero, one(1), hundred(100);