Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/coinselector_tests.cpp
Show First 20 Lines • Show All 160 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(bnb_search_test) { | ||||
add_coin(3 * CENT, 3, utxo_pool); | add_coin(3 * CENT, 3, utxo_pool); | ||||
add_coin(4 * CENT, 4, utxo_pool); | add_coin(4 * CENT, 4, utxo_pool); | ||||
// Select 1 Cent | // Select 1 Cent | ||||
add_coin(1 * CENT, 1, actual_selection); | add_coin(1 * CENT, 1, actual_selection); | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, CENT / 2, | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, CENT / 2, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
BOOST_CHECK(equal_sets(selection, actual_selection)); | BOOST_CHECK(equal_sets(selection, actual_selection)); | ||||
BOOST_CHECK_EQUAL(value_ret, 1 * CENT); | |||||
actual_selection.clear(); | actual_selection.clear(); | ||||
selection.clear(); | selection.clear(); | ||||
// Select 2 Cent | // Select 2 Cent | ||||
add_coin(2 * CENT, 2, actual_selection); | add_coin(2 * CENT, 2, actual_selection); | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, CENT / 2, | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, CENT / 2, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
BOOST_CHECK(equal_sets(selection, actual_selection)); | BOOST_CHECK(equal_sets(selection, actual_selection)); | ||||
BOOST_CHECK_EQUAL(value_ret, 2 * CENT); | |||||
actual_selection.clear(); | actual_selection.clear(); | ||||
selection.clear(); | selection.clear(); | ||||
// Select 5 Cent | // Select 5 Cent | ||||
add_coin(3 * CENT, 3, actual_selection); | add_coin(3 * CENT, 3, actual_selection); | ||||
add_coin(2 * CENT, 2, actual_selection); | add_coin(2 * CENT, 2, actual_selection); | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, CENT / 2, | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, CENT / 2, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
BOOST_CHECK(equal_sets(selection, actual_selection)); | BOOST_CHECK(equal_sets(selection, actual_selection)); | ||||
BOOST_CHECK_EQUAL(value_ret, 5 * CENT); | |||||
actual_selection.clear(); | actual_selection.clear(); | ||||
selection.clear(); | selection.clear(); | ||||
// Select 11 Cent, not possible | // Select 11 Cent, not possible | ||||
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 11 * CENT, CENT / 2, | BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 11 * CENT, CENT / 2, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
actual_selection.clear(); | actual_selection.clear(); | ||||
selection.clear(); | selection.clear(); | ||||
// Select 10 Cent | // Select 10 Cent | ||||
add_coin(5 * CENT, 5, utxo_pool); | add_coin(5 * CENT, 5, utxo_pool); | ||||
add_coin(4 * CENT, 4, actual_selection); | add_coin(4 * CENT, 4, actual_selection); | ||||
add_coin(3 * CENT, 3, actual_selection); | add_coin(3 * CENT, 3, actual_selection); | ||||
add_coin(2 * CENT, 2, actual_selection); | add_coin(2 * CENT, 2, actual_selection); | ||||
add_coin(1 * CENT, 1, actual_selection); | add_coin(1 * CENT, 1, actual_selection); | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, CENT / 2, | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, CENT / 2, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
BOOST_CHECK(equal_sets(selection, actual_selection)); | BOOST_CHECK(equal_sets(selection, actual_selection)); | ||||
BOOST_CHECK_EQUAL(value_ret, 10 * CENT); | |||||
actual_selection.clear(); | actual_selection.clear(); | ||||
selection.clear(); | selection.clear(); | ||||
// Negative effective value | // Negative effective value | ||||
// Select 10 Cent but have 1 Cent not be possible because too small | // Select 10 Cent but have 1 Cent not be possible because too small | ||||
add_coin(5 * CENT, 5, actual_selection); | add_coin(5 * CENT, 5, actual_selection); | ||||
add_coin(3 * CENT, 3, actual_selection); | add_coin(3 * CENT, 3, actual_selection); | ||||
add_coin(2 * CENT, 2, actual_selection); | add_coin(2 * CENT, 2, actual_selection); | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 5000 * SATOSHI, | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 5000 * SATOSHI, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
BOOST_CHECK_EQUAL(value_ret, 10 * CENT); | |||||
// FIXME: this test is redundant with the above, because 1 Cent is selected, | |||||
// not "too small" BOOST_CHECK(equal_sets(selection, actual_selection)); | |||||
// Select 0.25 Cent, not possible | // Select 0.25 Cent, not possible | ||||
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), CENT / 4, CENT / 2, | BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), CENT / 4, CENT / 2, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
actual_selection.clear(); | actual_selection.clear(); | ||||
selection.clear(); | selection.clear(); | ||||
// Iteration exhaustion test | // Iteration exhaustion test | ||||
Amount target = make_hard_case(17, utxo_pool); | Amount target = make_hard_case(17, utxo_pool); | ||||
// Should exhaust | // Should exhaust | ||||
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), target, Amount::zero(), | BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), target, Amount::zero(), | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
target = make_hard_case(14, utxo_pool); | target = make_hard_case(14, utxo_pool); | ||||
// Should not exhaust | // Should not exhaust | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), target, Amount::zero(), | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), target, Amount::zero(), | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
// Test same value early bailout optimization | // Test same value early bailout optimization | ||||
utxo_pool.clear(); | |||||
add_coin(7 * CENT, 7, actual_selection); | add_coin(7 * CENT, 7, actual_selection); | ||||
add_coin(7 * CENT, 7, actual_selection); | add_coin(7 * CENT, 7, actual_selection); | ||||
add_coin(7 * CENT, 7, actual_selection); | add_coin(7 * CENT, 7, actual_selection); | ||||
add_coin(7 * CENT, 7, actual_selection); | add_coin(7 * CENT, 7, actual_selection); | ||||
add_coin(2 * CENT, 7, actual_selection); | add_coin(2 * CENT, 7, actual_selection); | ||||
add_coin(7 * CENT, 7, utxo_pool); | add_coin(7 * CENT, 7, utxo_pool); | ||||
add_coin(7 * CENT, 7, utxo_pool); | add_coin(7 * CENT, 7, utxo_pool); | ||||
add_coin(7 * CENT, 7, utxo_pool); | add_coin(7 * CENT, 7, utxo_pool); | ||||
add_coin(7 * CENT, 7, utxo_pool); | add_coin(7 * CENT, 7, utxo_pool); | ||||
add_coin(2 * CENT, 7, utxo_pool); | add_coin(2 * CENT, 7, utxo_pool); | ||||
for (int i = 0; i < 50000; ++i) { | for (int i = 0; i < 50000; ++i) { | ||||
add_coin(5 * CENT, 7, utxo_pool); | add_coin(5 * CENT, 7, utxo_pool); | ||||
} | } | ||||
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 30 * CENT, 5000 * SATOSHI, | BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 30 * CENT, 5000 * SATOSHI, | ||||
selection, value_ret, not_input_fees)); | selection, value_ret, not_input_fees)); | ||||
BOOST_CHECK_EQUAL(value_ret, 30 * CENT); | |||||
BOOST_CHECK(equal_sets(selection, actual_selection)); | |||||
//////////////////// | //////////////////// | ||||
// Behavior tests // | // Behavior tests // | ||||
//////////////////// | //////////////////// | ||||
// Select 1 Cent with pool of only greater than 5 Cent | // Select 1 Cent with pool of only greater than 5 Cent | ||||
utxo_pool.clear(); | utxo_pool.clear(); | ||||
for (int i = 5; i <= 20; ++i) { | for (int i = 5; i <= 20; ++i) { | ||||
add_coin(i * CENT, i, utxo_pool); | add_coin(i * CENT, i, utxo_pool); | ||||
▲ Show 20 Lines • Show All 511 Lines • Show Last 20 Lines |