[wallet] tests: Don't use floating point

This commit is contained in:
MarcoFalke 2016-06-07 11:22:21 +02:00
parent 66db2d62d5
commit faa91b12f7

View File

@ -27,7 +27,7 @@ typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup) BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
static CWallet wallet; static const CWallet wallet;
static vector<COutput> vCoins; static vector<COutput> vCoins;
static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0) 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 the wallet and start again, now with fractions of a cent, to test small change avoidance
empty_wallet(); empty_wallet();
add_coin(0.1*MIN_CHANGE); add_coin(MIN_CHANGE * 1 / 10);
add_coin(0.2*MIN_CHANGE); add_coin(MIN_CHANGE * 2 / 10);
add_coin(0.3*MIN_CHANGE); add_coin(MIN_CHANGE * 3 / 10);
add_coin(0.4*MIN_CHANGE); add_coin(MIN_CHANGE * 4 / 10);
add_coin(0.5*MIN_CHANGE); add_coin(MIN_CHANGE * 5 / 10);
// try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE // 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 // 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 BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
// if we add more small coins: // if we add more small coins:
add_coin(0.6*MIN_CHANGE); add_coin(MIN_CHANGE * 6 / 10);
add_coin(0.7*MIN_CHANGE); add_coin(MIN_CHANGE * 7 / 10);
// and try again to make 1.0 * MIN_CHANGE // and try again to make 1.0 * MIN_CHANGE
BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet)); BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
@ -229,9 +229,9 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
// sometimes it will fail, and so we use the next biggest coin: // sometimes it will fail, and so we use the next biggest coin:
empty_wallet(); empty_wallet();
add_coin(0.5 * MIN_CHANGE); add_coin(MIN_CHANGE * 5 / 10);
add_coin(0.6 * MIN_CHANGE); add_coin(MIN_CHANGE * 6 / 10);
add_coin(0.7 * MIN_CHANGE); add_coin(MIN_CHANGE * 7 / 10);
add_coin(1111 * MIN_CHANGE); add_coin(1111 * MIN_CHANGE);
BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet)); 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(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
@ -239,9 +239,9 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
// but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0) // but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
empty_wallet(); empty_wallet();
add_coin(0.4 * MIN_CHANGE); add_coin(MIN_CHANGE * 4 / 10);
add_coin(0.6 * MIN_CHANGE); add_coin(MIN_CHANGE * 6 / 10);
add_coin(0.8 * MIN_CHANGE); add_coin(MIN_CHANGE * 8 / 10);
add_coin(1111 * MIN_CHANGE); add_coin(1111 * MIN_CHANGE);
BOOST_CHECK( wallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet)); 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(nValueRet, MIN_CHANGE); // we should get the exact amount
@ -249,17 +249,17 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
// test avoiding small change // test avoiding small change
empty_wallet(); empty_wallet();
add_coin(0.05 * MIN_CHANGE); add_coin(MIN_CHANGE * 5 / 100);
add_coin(1 * MIN_CHANGE); add_coin(MIN_CHANGE * 1);
add_coin(100 * MIN_CHANGE); add_coin(MIN_CHANGE * 100);
// trying to make 100.01 from these three coins // trying to make 100.01 from these three coins
BOOST_CHECK( wallet.SelectCoinsMinConf(100.01 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet)); BOOST_CHECK(wallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, 1, 1, vCoins, setCoinsRet, nValueRet));
BOOST_CHECK_EQUAL(nValueRet, 101.05 * MIN_CHANGE); // we should get all coins BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U); 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 // 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(nValueRet, 101 * MIN_CHANGE);
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); 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, // add 75 cents in small change. not enough to make 90 cents,
// then try making 90 cents. there are multiple competing "smallest bigger" coins, // then try making 90 cents. there are multiple competing "smallest bigger" coins,
// one of which should be picked at random // 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; fails = 0;
for (int i = 0; i < RANDOM_REPEATS; i++) for (int i = 0; i < RANDOM_REPEATS; i++)