Skip to content

Commit 7753efc

Browse files
committed
Merge bitcoin/bitcoin#27004: test: Use std::unique_ptr over manual delete in coins_tests
fab9f7d test: Use std::unique_ptr over manual delete in coins_tests (MarcoFalke) Pull request description: Makes the code smaller and easier to read ACKs for top commit: stickies-v: ACK fab9f7d john-moffett: ACK fab9f7d Tree-SHA512: 30d2d2097906e61fdef47a52fc6a0c5ce2417bc41c3c82dafc1b216c655f31dabf9c1c13759575a696f61bbdfdba3f442be032d5e5145b7a54fae2a927824621
2 parents c2028f9 + fab9f7d commit 7753efc

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/test/coins_tests.cpp

+17-37
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
131131
std::map<COutPoint, Coin> result;
132132

133133
// The cache stack.
134-
std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
135-
stack.push_back(new CCoinsViewCacheTest(base)); // Start with one cache.
134+
std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
135+
stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
136136

137137
// Use a limited set of random transaction ids, so we do test overwriting entries.
138138
std::vector<uint256> txids;
@@ -218,7 +218,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
218218
found_an_entry = true;
219219
}
220220
}
221-
for (const CCoinsViewCacheTest *test : stack) {
221+
for (const auto& test : stack) {
222222
test->SelfTest();
223223
}
224224
}
@@ -241,31 +241,24 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
241241
bool should_erase = InsecureRandRange(4) < 3;
242242
BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
243243
flushed_without_erase |= !should_erase;
244-
delete stack.back();
245244
stack.pop_back();
246245
}
247246
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
248247
//Add a new cache
249248
CCoinsView* tip = base;
250249
if (stack.size() > 0) {
251-
tip = stack.back();
250+
tip = stack.back().get();
252251
} else {
253252
removed_all_caches = true;
254253
}
255-
stack.push_back(new CCoinsViewCacheTest(tip));
254+
stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
256255
if (stack.size() == 4) {
257256
reached_4_caches = true;
258257
}
259258
}
260259
}
261260
}
262261

263-
// Clean up the stack.
264-
while (stack.size() > 0) {
265-
delete stack.back();
266-
stack.pop_back();
267-
}
268-
269262
// Verify coverage.
270263
BOOST_CHECK(removed_all_caches);
271264
BOOST_CHECK(reached_4_caches);
@@ -321,8 +314,8 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
321314

322315
// The cache stack.
323316
CCoinsViewTest base; // A CCoinsViewTest at the bottom.
324-
std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
325-
stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache.
317+
std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
318+
stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
326319

327320
// Track the txids we've used in various sets
328321
std::set<COutPoint> coinbase_coins;
@@ -487,25 +480,18 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
487480
// Every 100 iterations, change the cache stack.
488481
if (stack.size() > 0 && InsecureRandBool() == 0) {
489482
BOOST_CHECK(stack.back()->Flush());
490-
delete stack.back();
491483
stack.pop_back();
492484
}
493485
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
494486
CCoinsView* tip = &base;
495487
if (stack.size() > 0) {
496-
tip = stack.back();
488+
tip = stack.back().get();
497489
}
498-
stack.push_back(new CCoinsViewCacheTest(tip));
490+
stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
499491
}
500492
}
501493
}
502494

503-
// Clean up the stack.
504-
while (stack.size() > 0) {
505-
delete stack.back();
506-
stack.pop_back();
507-
}
508-
509495
// Verify coverage.
510496
BOOST_CHECK(spent_a_duplicate_coinbase);
511497

@@ -918,7 +904,7 @@ Coin MakeCoin()
918904
void TestFlushBehavior(
919905
CCoinsViewCacheTest* view,
920906
CCoinsViewDB& base,
921-
std::vector<CCoinsViewCacheTest*>& all_caches,
907+
std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
922908
bool do_erasing_flush)
923909
{
924910
CAmount value;
@@ -928,7 +914,7 @@ void TestFlushBehavior(
928914
auto flush_all = [&all_caches](bool erase) {
929915
// Flush in reverse order to ensure that flushes happen from children up.
930916
for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
931-
auto cache = *i;
917+
auto& cache = *i;
932918
// hashBlock must be filled before flushing to disk; value is
933919
// unimportant here. This is normally done during connect/disconnect block.
934920
cache->SetBestBlock(InsecureRand256());
@@ -1079,19 +1065,13 @@ BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
10791065
{
10801066
// Create two in-memory caches atop a leveldb view.
10811067
CCoinsViewDB base{"test", /*nCacheSize=*/ 1 << 23, /*fMemory=*/ true, /*fWipe=*/ false};
1082-
std::vector<CCoinsViewCacheTest*> caches;
1083-
caches.push_back(new CCoinsViewCacheTest(&base));
1084-
caches.push_back(new CCoinsViewCacheTest(caches.back()));
1085-
1086-
for (CCoinsViewCacheTest* view : caches) {
1087-
TestFlushBehavior(view, base, caches, /*do_erasing_flush=*/ false);
1088-
TestFlushBehavior(view, base, caches, /*do_erasing_flush=*/ true);
1089-
}
1068+
std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1069+
caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1070+
caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
10901071

1091-
// Clean up the caches.
1092-
while (caches.size() > 0) {
1093-
delete caches.back();
1094-
caches.pop_back();
1072+
for (const auto& view : caches) {
1073+
TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
1074+
TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
10951075
}
10961076
}
10971077

0 commit comments

Comments
 (0)