diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index 75b25143e2..6c8ab69bb5 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -132,8 +132,8 @@ void SimulationTest(CCoinsView* base, bool fake_best_block) std::map result; // The cache stack. - std::vector stack; // A stack of CCoinsViewCaches on top. - stack.push_back(new CCoinsViewCacheTest(base)); // Start with one cache. + std::vector> stack; // A stack of CCoinsViewCaches on top. + stack.push_back(std::make_unique(base)); // Start with one cache. // Use a limited set of random transaction ids, so we do test overwriting entries. std::vector 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(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 stack; // A stack of CCoinsViewCaches on top. - stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache. + std::vector> stack; // A stack of CCoinsViewCaches on top. + stack.push_back(std::make_unique(&base)); // Start with one cache. // Track the txids we've used in various sets std::set 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(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& all_caches, + std::vector>& 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 caches; - caches.push_back(new CCoinsViewCacheTest(&base)); - caches.push_back(new CCoinsViewCacheTest(caches.back())); + std::vector> caches; + caches.push_back(std::make_unique(&base)); + caches.push_back(std::make_unique(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); } }