Permalink
Browse files

Merge #8215: [wallet] tests: Don't use floating point

faa91b1 [wallet] tests: Don't use floating point (MarcoFalke)
  • Loading branch information...
MarcoFalke committed Jun 18, 2016
2 parents ed2cd59 + faa91b1 commit a072d1a83787e786d074a4b5871b0b961781f7c6
Showing with 25 additions and 21 deletions.
  1. +25 −21 src/wallet/test/wallet_tests.cpp
@@ -27,7 +27,7 @@ typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
static CWallet wallet;
static const CWallet wallet;
static vector<COutput> vCoins;
static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0)
@@ -188,11 +188,11 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
// empty the wallet and start again, now with fractions of a cent, to test small change avoidance
empty_wallet();
add_coin(0.1*MIN_CHANGE);
add_coin(0.2*MIN_CHANGE);
add_coin(0.3*MIN_CHANGE);
add_coin(0.4*MIN_CHANGE);
add_coin(0.5*MIN_CHANGE);
add_coin(MIN_CHANGE * 1 / 10);
add_coin(MIN_CHANGE * 2 / 10);
add_coin(MIN_CHANGE * 3 / 10);
add_coin(MIN_CHANGE * 4 / 10);
add_coin(MIN_CHANGE * 5 / 10);
// try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
// we'll get change smaller than MIN_CHANGE whatever happens, so can expect MIN_CHANGE exactly
@@ -207,8 +207,8 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
// if we add more small coins:
add_coin(0.6*MIN_CHANGE);
add_coin(0.7*MIN_CHANGE);
add_coin(MIN_CHANGE * 6 / 10);
add_coin(MIN_CHANGE * 7 / 10);
// and try again to make 1.0 * MIN_CHANGE
BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
@@ -229,37 +229,37 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
// sometimes it will fail, and so we use the next biggest coin:
empty_wallet();
add_coin(0.5 * MIN_CHANGE);
add_coin(0.6 * MIN_CHANGE);
add_coin(0.7 * MIN_CHANGE);
add_coin(MIN_CHANGE * 5 / 10);
add_coin(MIN_CHANGE * 6 / 10);
add_coin(MIN_CHANGE * 7 / 10);
add_coin(1111 * MIN_CHANGE);
BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
// but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
empty_wallet();
add_coin(0.4 * MIN_CHANGE);
add_coin(0.6 * MIN_CHANGE);
add_coin(0.8 * MIN_CHANGE);
add_coin(MIN_CHANGE * 4 / 10);
add_coin(MIN_CHANGE * 6 / 10);
add_coin(MIN_CHANGE * 8 / 10);
add_coin(1111 * MIN_CHANGE);
BOOST_CHECK( wallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
// test avoiding small change
empty_wallet();
add_coin(0.05 * MIN_CHANGE);
add_coin(1 * MIN_CHANGE);
add_coin(100 * MIN_CHANGE);
add_coin(MIN_CHANGE * 5 / 100);
add_coin(MIN_CHANGE * 1);
add_coin(MIN_CHANGE * 100);
// trying to make 100.01 from these three coins
BOOST_CHECK( wallet.SelectCoinsMinConf(100.01 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK_EQUAL(nValueRet, 101.05 * MIN_CHANGE); // we should get all coins
BOOST_CHECK(wallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
// but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
BOOST_CHECK( wallet.SelectCoinsMinConf(99.9 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK(wallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
@@ -310,7 +310,11 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
// add 75 cents in small change. not enough to make 90 cents,
// then try making 90 cents. there are multiple competing "smallest bigger" coins,
// one of which should be picked at random
add_coin( 5*CENT); add_coin(10*CENT); add_coin(15*CENT); add_coin(20*CENT); add_coin(25*CENT);
add_coin(5 * CENT);
add_coin(10 * CENT);
add_coin(15 * CENT);
add_coin(20 * CENT);
add_coin(25 * CENT);
fails = 0;
for (int i = 0; i < RANDOM_REPEATS; i++)

0 comments on commit a072d1a

Please sign in to comment.