Skip to content

Commit a679109

Browse files
committed
Speed up knapsack_solver_test by not recreating wallet 100 times.
Moved the code for creating the wallet out of the 100-times repetition loop, for the most time-consuming tests.
1 parent 48bf8ff commit a679109

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

src/wallet/test/coinselector_tests.cpp

+36-25
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,19 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
452452
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
453453
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
454454
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
455+
}
455456

456-
// test with many inputs
457-
for (CAmount amt=1500; amt < COIN; amt*=10) {
458-
empty_wallet();
459-
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
460-
for (uint16_t j = 0; j < 676; j++)
461-
add_coin(amt);
457+
// test with many inputs
458+
for (CAmount amt=1500; amt < COIN; amt*=10) {
459+
empty_wallet();
460+
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
461+
for (uint16_t j = 0; j < 676; j++)
462+
add_coin(amt);
463+
464+
// We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
465+
for (int i = 0; i < RUN_TESTS; i++) {
462466
BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
467+
463468
if (amt - 2000 < MIN_CHANGE) {
464469
// needs more than one input:
465470
uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
@@ -471,14 +476,17 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
471476
BOOST_CHECK_EQUAL(nValueRet, amt);
472477
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
473478
}
474-
}
479+
}
480+
}
475481

476-
// test randomness
477-
{
478-
empty_wallet();
479-
for (int i2 = 0; i2 < 100; i2++)
480-
add_coin(COIN);
482+
// test randomness
483+
{
484+
empty_wallet();
485+
for (int i2 = 0; i2 < 100; i2++)
486+
add_coin(COIN);
481487

488+
// Again, we only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
489+
for (int i = 0; i < RUN_TESTS; i++) {
482490
// picking 50 from 100 coins doesn't depend on the shuffle,
483491
// but does depend on randomness in the stochastic approximation code
484492
BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet , nValueRet, coin_selection_params, bnb_used));
@@ -496,17 +504,19 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
496504
fails++;
497505
}
498506
BOOST_CHECK_NE(fails, RANDOM_REPEATS);
499-
500-
// add 75 cents in small change. not enough to make 90 cents,
501-
// then try making 90 cents. there are multiple competing "smallest bigger" coins,
502-
// one of which should be picked at random
503-
add_coin(5 * CENT);
504-
add_coin(10 * CENT);
505-
add_coin(15 * CENT);
506-
add_coin(20 * CENT);
507-
add_coin(25 * CENT);
508-
509-
fails = 0;
507+
}
508+
509+
// add 75 cents in small change. not enough to make 90 cents,
510+
// then try making 90 cents. there are multiple competing "smallest bigger" coins,
511+
// one of which should be picked at random
512+
add_coin(5 * CENT);
513+
add_coin(10 * CENT);
514+
add_coin(15 * CENT);
515+
add_coin(20 * CENT);
516+
add_coin(25 * CENT);
517+
518+
for (int i = 0; i < RUN_TESTS; i++) {
519+
int fails = 0;
510520
for (int j = 0; j < RANDOM_REPEATS; j++)
511521
{
512522
// selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
@@ -517,8 +527,9 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
517527
fails++;
518528
}
519529
BOOST_CHECK_NE(fails, RANDOM_REPEATS);
520-
}
521-
}
530+
}
531+
}
532+
522533
empty_wallet();
523534
}
524535

0 commit comments

Comments
 (0)