Skip to content

Commit 935b3dd

Browse files
committed
scripted-diff: tests: Use KnapsackSolver directly
When doing the coin selector tests for KnapsackSolver, call it directly instead of using AttemptSelection and hoping/instructing it uses KnapsackSolver. -BEGIN VERIFY SCRIPT- sed -i 's/testWallet\.AttemptSelection( /KnapsackSolver(/g' src/wallet/test/coinselector_tests.cpp sed -i 's/testWallet\.AttemptSelection(/KnapsackSolver(/g' src/wallet/test/coinselector_tests.cpp sed -i 's/, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params)/, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet)/g' src/wallet/test/coinselector_tests.cpp sed -i 's/, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params)/, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet)/g' src/wallet/test/coinselector_tests.cpp sed -i 's/, filter_standard_extra, vCoins, setCoinsRet, nValueRet, coin_selection_params)/, KnapsackGroupOutputs(filter_standard_extra), setCoinsRet, nValueRet)/g' src/wallet/test/coinselector_tests.cpp sed -i 's/BOOST_CHECK( /BOOST_CHECK(/g' src/wallet/test/coinselector_tests.cpp -END VERIFY SCRIPT-
1 parent 6a023a6 commit 935b3dd

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/wallet/test/coinselector_tests.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -330,24 +330,24 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
330330
empty_wallet();
331331

332332
// with an empty wallet we can't even pay one cent
333-
BOOST_CHECK(!testWallet.AttemptSelection( 1 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params));
333+
BOOST_CHECK(!KnapsackSolver(1 * CENT, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet));
334334

335335
add_coin(1*CENT, 4); // add a new 1 cent coin
336336

337337
// with a new 1 cent coin, we still can't find a mature 1 cent
338-
BOOST_CHECK(!testWallet.AttemptSelection( 1 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params));
338+
BOOST_CHECK(!KnapsackSolver(1 * CENT, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet));
339339

340340
// but we can find a new 1 cent
341-
BOOST_CHECK( testWallet.AttemptSelection( 1 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
341+
BOOST_CHECK(KnapsackSolver(1 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
342342
BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
343343

344344
add_coin(2*CENT); // add a mature 2 cent coin
345345

346346
// we can't make 3 cents of mature coins
347-
BOOST_CHECK(!testWallet.AttemptSelection( 3 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params));
347+
BOOST_CHECK(!KnapsackSolver(3 * CENT, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet));
348348

349349
// we can make 3 cents of new coins
350-
BOOST_CHECK( testWallet.AttemptSelection( 3 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
350+
BOOST_CHECK(KnapsackSolver(3 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
351351
BOOST_CHECK_EQUAL(nValueRet, 3 * CENT);
352352

353353
add_coin(5*CENT); // add a mature 5 cent coin,
@@ -357,33 +357,33 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
357357
// now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27. total = 38
358358

359359
// we can't make 38 cents only if we disallow new coins:
360-
BOOST_CHECK(!testWallet.AttemptSelection(38 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params));
360+
BOOST_CHECK(!KnapsackSolver(38 * CENT, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet));
361361
// we can't even make 37 cents if we don't allow new coins even if they're from us
362-
BOOST_CHECK(!testWallet.AttemptSelection(38 * CENT, filter_standard_extra, vCoins, setCoinsRet, nValueRet, coin_selection_params));
362+
BOOST_CHECK(!KnapsackSolver(38 * CENT, KnapsackGroupOutputs(filter_standard_extra), setCoinsRet, nValueRet));
363363
// but we can make 37 cents if we accept new coins from ourself
364-
BOOST_CHECK( testWallet.AttemptSelection(37 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params));
364+
BOOST_CHECK(KnapsackSolver(37 * CENT, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet));
365365
BOOST_CHECK_EQUAL(nValueRet, 37 * CENT);
366366
// and we can make 38 cents if we accept all new coins
367-
BOOST_CHECK( testWallet.AttemptSelection(38 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
367+
BOOST_CHECK(KnapsackSolver(38 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
368368
BOOST_CHECK_EQUAL(nValueRet, 38 * CENT);
369369

370370
// try making 34 cents from 1,2,5,10,20 - we can't do it exactly
371-
BOOST_CHECK( testWallet.AttemptSelection(34 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
371+
BOOST_CHECK(KnapsackSolver(34 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
372372
BOOST_CHECK_EQUAL(nValueRet, 35 * CENT); // but 35 cents is closest
373373
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U); // the best should be 20+10+5. it's incredibly unlikely the 1 or 2 got included (but possible)
374374

375375
// when we try making 7 cents, the smaller coins (1,2,5) are enough. We should see just 2+5
376-
BOOST_CHECK( testWallet.AttemptSelection( 7 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
376+
BOOST_CHECK(KnapsackSolver(7 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
377377
BOOST_CHECK_EQUAL(nValueRet, 7 * CENT);
378378
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
379379

380380
// when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
381-
BOOST_CHECK( testWallet.AttemptSelection( 8 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
381+
BOOST_CHECK(KnapsackSolver(8 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
382382
BOOST_CHECK(nValueRet == 8 * CENT);
383383
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
384384

385385
// when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
386-
BOOST_CHECK( testWallet.AttemptSelection( 9 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
386+
BOOST_CHECK(KnapsackSolver(9 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
387387
BOOST_CHECK_EQUAL(nValueRet, 10 * CENT);
388388
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
389389

@@ -397,30 +397,30 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
397397
add_coin(30*CENT); // now we have 6+7+8+20+30 = 71 cents total
398398

399399
// check that we have 71 and not 72
400-
BOOST_CHECK( testWallet.AttemptSelection(71 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
401-
BOOST_CHECK(!testWallet.AttemptSelection(72 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
400+
BOOST_CHECK(KnapsackSolver(71 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
401+
BOOST_CHECK(!KnapsackSolver(72 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
402402

403403
// now try making 16 cents. the best smaller coins can do is 6+7+8 = 21; not as good at the next biggest coin, 20
404-
BOOST_CHECK( testWallet.AttemptSelection(16 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
404+
BOOST_CHECK(KnapsackSolver(16 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
405405
BOOST_CHECK_EQUAL(nValueRet, 20 * CENT); // we should get 20 in one coin
406406
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
407407

408408
add_coin( 5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
409409

410410
// now if we try making 16 cents again, the smaller coins can make 5+6+7 = 18 cents, better than the next biggest coin, 20
411-
BOOST_CHECK( testWallet.AttemptSelection(16 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
411+
BOOST_CHECK(KnapsackSolver(16 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
412412
BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 3 coins
413413
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
414414

415415
add_coin( 18*CENT); // now we have 5+6+7+8+18+20+30
416416

417417
// and now if we try making 16 cents again, the smaller coins can make 5+6+7 = 18 cents, the same as the next biggest coin, 18
418-
BOOST_CHECK( testWallet.AttemptSelection(16 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
418+
BOOST_CHECK(KnapsackSolver(16 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
419419
BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 1 coin
420420
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); // because in the event of a tie, the biggest coin wins
421421

422422
// now try making 11 cents. we should get 5+6
423-
BOOST_CHECK( testWallet.AttemptSelection(11 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
423+
BOOST_CHECK(KnapsackSolver(11 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
424424
BOOST_CHECK_EQUAL(nValueRet, 11 * CENT);
425425
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
426426

@@ -429,11 +429,11 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
429429
add_coin( 2*COIN);
430430
add_coin( 3*COIN);
431431
add_coin( 4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
432-
BOOST_CHECK( testWallet.AttemptSelection(95 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
432+
BOOST_CHECK(KnapsackSolver(95 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
433433
BOOST_CHECK_EQUAL(nValueRet, 1 * COIN); // we should get 1 BTC in 1 coin
434434
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
435435

436-
BOOST_CHECK( testWallet.AttemptSelection(195 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
436+
BOOST_CHECK(KnapsackSolver(195 * CENT, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
437437
BOOST_CHECK_EQUAL(nValueRet, 2 * COIN); // we should get 2 BTC in 1 coin
438438
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
439439

@@ -448,22 +448,22 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
448448

449449
// try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
450450
// we'll get change smaller than MIN_CHANGE whatever happens, so can expect MIN_CHANGE exactly
451-
BOOST_CHECK( testWallet.AttemptSelection(MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
451+
BOOST_CHECK(KnapsackSolver(MIN_CHANGE, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
452452
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE);
453453

454454
// but if we add a bigger coin, small change is avoided
455455
add_coin(1111*MIN_CHANGE);
456456

457457
// try making 1 from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5
458-
BOOST_CHECK( testWallet.AttemptSelection(1 * MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
458+
BOOST_CHECK(KnapsackSolver(1 * MIN_CHANGE, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
459459
BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
460460

461461
// if we add more small coins:
462462
add_coin(MIN_CHANGE * 6 / 10);
463463
add_coin(MIN_CHANGE * 7 / 10);
464464

465465
// and try again to make 1.0 * MIN_CHANGE
466-
BOOST_CHECK( testWallet.AttemptSelection(1 * MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
466+
BOOST_CHECK(KnapsackSolver(1 * MIN_CHANGE, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
467467
BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
468468

469469
// run the 'mtgox' test (see https://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
@@ -472,7 +472,7 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
472472
for (int j = 0; j < 20; j++)
473473
add_coin(50000 * COIN);
474474

475-
BOOST_CHECK( testWallet.AttemptSelection(500000 * COIN, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
475+
BOOST_CHECK(KnapsackSolver(500000 * COIN, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
476476
BOOST_CHECK_EQUAL(nValueRet, 500000 * COIN); // we should get the exact amount
477477
BOOST_CHECK_EQUAL(setCoinsRet.size(), 10U); // in ten coins
478478

@@ -485,7 +485,7 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
485485
add_coin(MIN_CHANGE * 6 / 10);
486486
add_coin(MIN_CHANGE * 7 / 10);
487487
add_coin(1111 * MIN_CHANGE);
488-
BOOST_CHECK( testWallet.AttemptSelection(1 * MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
488+
BOOST_CHECK(KnapsackSolver(1 * MIN_CHANGE, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
489489
BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
490490
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
491491

@@ -495,7 +495,7 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
495495
add_coin(MIN_CHANGE * 6 / 10);
496496
add_coin(MIN_CHANGE * 8 / 10);
497497
add_coin(1111 * MIN_CHANGE);
498-
BOOST_CHECK( testWallet.AttemptSelection(MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
498+
BOOST_CHECK(KnapsackSolver(MIN_CHANGE, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
499499
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
500500
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
501501

@@ -506,12 +506,12 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
506506
add_coin(MIN_CHANGE * 100);
507507

508508
// trying to make 100.01 from these three coins
509-
BOOST_CHECK(testWallet.AttemptSelection(MIN_CHANGE * 10001 / 100, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
509+
BOOST_CHECK(KnapsackSolver(MIN_CHANGE * 10001 / 100, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
510510
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
511511
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
512512

513513
// but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
514-
BOOST_CHECK(testWallet.AttemptSelection(MIN_CHANGE * 9990 / 100, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
514+
BOOST_CHECK(KnapsackSolver(MIN_CHANGE * 9990 / 100, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
515515
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
516516
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
517517
}
@@ -525,7 +525,7 @@ BOOST_AUTO_TEST_CASE(knapsack_solver_test)
525525

526526
// We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
527527
for (int i = 0; i < RUN_TESTS; i++) {
528-
BOOST_CHECK(testWallet.AttemptSelection(2000, filter_confirmed, vCoins, setCoinsRet, nValueRet, coin_selection_params));
528+
BOOST_CHECK(KnapsackSolver(2000, KnapsackGroupOutputs(filter_confirmed), setCoinsRet, nValueRet));
529529

530530
if (amt - 2000 < MIN_CHANGE) {
531531
// needs more than one input:
@@ -610,7 +610,7 @@ BOOST_AUTO_TEST_CASE(ApproximateBestSubset)
610610
add_coin(1000 * COIN);
611611
add_coin(3 * COIN);
612612

613-
BOOST_CHECK(testWallet.AttemptSelection(1003 * COIN, filter_standard, vCoins, setCoinsRet, nValueRet, coin_selection_params));
613+
BOOST_CHECK(KnapsackSolver(1003 * COIN, KnapsackGroupOutputs(filter_standard), setCoinsRet, nValueRet));
614614
BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN);
615615
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
616616

0 commit comments

Comments
 (0)