Skip to content

Commit a072d1a

Browse files
author
MarcoFalke
committed
Merge #8215: [wallet] tests: Don't use floating point
faa91b1 [wallet] tests: Don't use floating point (MarcoFalke)
2 parents ed2cd59 + faa91b1 commit a072d1a

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;
2727

2828
BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
2929

30-
static CWallet wallet;
30+
static const CWallet wallet;
3131
static vector<COutput> vCoins;
3232

3333
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)
188188
// empty the wallet and start again, now with fractions of a cent, to test small change avoidance
189189

190190
empty_wallet();
191-
add_coin(0.1*MIN_CHANGE);
192-
add_coin(0.2*MIN_CHANGE);
193-
add_coin(0.3*MIN_CHANGE);
194-
add_coin(0.4*MIN_CHANGE);
195-
add_coin(0.5*MIN_CHANGE);
191+
add_coin(MIN_CHANGE * 1 / 10);
192+
add_coin(MIN_CHANGE * 2 / 10);
193+
add_coin(MIN_CHANGE * 3 / 10);
194+
add_coin(MIN_CHANGE * 4 / 10);
195+
add_coin(MIN_CHANGE * 5 / 10);
196196

197197
// try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
198198
// 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)
207207
BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
208208

209209
// if we add more small coins:
210-
add_coin(0.6*MIN_CHANGE);
211-
add_coin(0.7*MIN_CHANGE);
210+
add_coin(MIN_CHANGE * 6 / 10);
211+
add_coin(MIN_CHANGE * 7 / 10);
212212

213213
// and try again to make 1.0 * MIN_CHANGE
214214
BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
@@ -229,37 +229,37 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
229229

230230
// sometimes it will fail, and so we use the next biggest coin:
231231
empty_wallet();
232-
add_coin(0.5 * MIN_CHANGE);
233-
add_coin(0.6 * MIN_CHANGE);
234-
add_coin(0.7 * MIN_CHANGE);
232+
add_coin(MIN_CHANGE * 5 / 10);
233+
add_coin(MIN_CHANGE * 6 / 10);
234+
add_coin(MIN_CHANGE * 7 / 10);
235235
add_coin(1111 * MIN_CHANGE);
236236
BOOST_CHECK( wallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
237237
BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
238238
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
239239

240240
// but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
241241
empty_wallet();
242-
add_coin(0.4 * MIN_CHANGE);
243-
add_coin(0.6 * MIN_CHANGE);
244-
add_coin(0.8 * MIN_CHANGE);
242+
add_coin(MIN_CHANGE * 4 / 10);
243+
add_coin(MIN_CHANGE * 6 / 10);
244+
add_coin(MIN_CHANGE * 8 / 10);
245245
add_coin(1111 * MIN_CHANGE);
246246
BOOST_CHECK( wallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
247247
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
248248
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
249249

250250
// test avoiding small change
251251
empty_wallet();
252-
add_coin(0.05 * MIN_CHANGE);
253-
add_coin(1 * MIN_CHANGE);
254-
add_coin(100 * MIN_CHANGE);
252+
add_coin(MIN_CHANGE * 5 / 100);
253+
add_coin(MIN_CHANGE * 1);
254+
add_coin(MIN_CHANGE * 100);
255255

256256
// trying to make 100.01 from these three coins
257-
BOOST_CHECK( wallet.SelectCoinsMinConf(100.01 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
258-
BOOST_CHECK_EQUAL(nValueRet, 101.05 * MIN_CHANGE); // we should get all coins
257+
BOOST_CHECK(wallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, 1, 1, vCoins, setCoinsRet, nValueRet));
258+
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
259259
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
260260

261261
// but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
262-
BOOST_CHECK( wallet.SelectCoinsMinConf(99.9 * MIN_CHANGE, 1, 1, vCoins, setCoinsRet, nValueRet));
262+
BOOST_CHECK(wallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, 1, 1, vCoins, setCoinsRet, nValueRet));
263263
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
264264
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
265265

@@ -310,7 +310,11 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
310310
// add 75 cents in small change. not enough to make 90 cents,
311311
// then try making 90 cents. there are multiple competing "smallest bigger" coins,
312312
// one of which should be picked at random
313-
add_coin( 5*CENT); add_coin(10*CENT); add_coin(15*CENT); add_coin(20*CENT); add_coin(25*CENT);
313+
add_coin(5 * CENT);
314+
add_coin(10 * CENT);
315+
add_coin(15 * CENT);
316+
add_coin(20 * CENT);
317+
add_coin(25 * CENT);
314318

315319
fails = 0;
316320
for (int i = 0; i < RANDOM_REPEATS; i++)

0 commit comments

Comments
 (0)