Merge bitcoin/bitcoin#27004: test: Use std::unique_ptr over manual delete in coins_tests

fab9f7d1bd48198d3e0d3c3a08e404ea73a2bc8d 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 fab9f7d1bd48198d3e0d3c3a08e404ea73a2bc8d
  john-moffett:
    ACK fab9f7d1bd48198d3e0d3c3a08e404ea73a2bc8d

Tree-SHA512: 30d2d2097906e61fdef47a52fc6a0c5ce2417bc41c3c82dafc1b216c655f31dabf9c1c13759575a696f61bbdfdba3f442be032d5e5145b7a54fae2a927824621
This commit is contained in:
fanquake 2023-02-02 16:51:35 +00:00 committed by pasta
parent 2ab1989a39
commit 44e6c9e902
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38

View File

@ -132,8 +132,8 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
std::map<COutPoint, Coin> result;
// The cache stack.
std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
stack.push_back(new CCoinsViewCacheTest(base)); // Start with one cache.
std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
// Use a limited set of random transaction ids, so we do test overwriting entries.
std::vector<uint256> txids;
@ -219,7 +219,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
found_an_entry = true;
}
}
for (const CCoinsViewCacheTest *test : stack) {
for (const auto& test : stack) {
test->SelfTest();
}
}
@ -242,18 +242,17 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
bool should_erase = InsecureRandRange(4) < 3;
BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
flushed_without_erase |= !should_erase;
delete stack.back();
stack.pop_back();
}
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
//Add a new cache
CCoinsView* tip = base;
if (stack.size() > 0) {
tip = stack.back();
tip = stack.back().get();
} else {
removed_all_caches = true;
}
stack.push_back(new CCoinsViewCacheTest(tip));
stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
if (stack.size() == 4) {
reached_4_caches = true;
}
@ -261,12 +260,6 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
}
}
// Clean up the stack.
while (stack.size() > 0) {
delete stack.back();
stack.pop_back();
}
// Verify coverage.
BOOST_CHECK(removed_all_caches);
BOOST_CHECK(reached_4_caches);
@ -322,8 +315,8 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
// The cache stack.
CCoinsViewTest base; // A CCoinsViewTest at the bottom.
std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache.
std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
// Track the txids we've used in various sets
std::set<COutPoint> coinbase_coins;
@ -488,25 +481,18 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
// Every 100 iterations, change the cache stack.
if (stack.size() > 0 && InsecureRandBool() == 0) {
BOOST_CHECK(stack.back()->Flush());
delete stack.back();
stack.pop_back();
}
if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
CCoinsView* tip = &base;
if (stack.size() > 0) {
tip = stack.back();
tip = stack.back().get();
}
stack.push_back(new CCoinsViewCacheTest(tip));
stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
}
}
}
// Clean up the stack.
while (stack.size() > 0) {
delete stack.back();
stack.pop_back();
}
// Verify coverage.
BOOST_CHECK(spent_a_duplicate_coinbase);
@ -920,7 +906,7 @@ Coin MakeCoin()
void TestFlushBehavior(
CCoinsViewCacheTest* view,
CCoinsViewDB& base,
std::vector<CCoinsViewCacheTest*>& all_caches,
std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
bool do_erasing_flush)
{
CAmount value;
@ -931,7 +917,7 @@ void TestFlushBehavior(
auto flush_all = [&all_caches](bool erase) {
// Flush in reverse order to ensure that flushes happen from children up.
for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
auto cache = *i;
auto& cache = *i;
// hashBlock must be filled before flushing to disk; value is
// unimportant here. This is normally done during connect/disconnect block.
cache->SetBestBlock(InsecureRand256());
@ -1087,19 +1073,13 @@ BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
{
// Create two in-memory caches atop a leveldb view.
CCoinsViewDB base{"test", /*nCacheSize=*/ 1 << 23, /*fMemory=*/ true, /*fWipe=*/ false};
std::vector<CCoinsViewCacheTest*> caches;
caches.push_back(new CCoinsViewCacheTest(&base));
caches.push_back(new CCoinsViewCacheTest(caches.back()));
std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
for (CCoinsViewCacheTest* view : caches) {
TestFlushBehavior(view, base, caches, /*do_erasing_flush=*/ false);
TestFlushBehavior(view, base, caches, /*do_erasing_flush=*/ true);
}
// Clean up the caches.
while (caches.size() > 0) {
delete caches.back();
caches.pop_back();
for (const auto& view : caches) {
TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
}
}