tests: Use MakeUnique to construct objects owned by unique_ptrs
This commit is contained in:
parent
8f464549c4
commit
b6718e373e
9 changed files with 20 additions and 15 deletions
|
@ -439,6 +439,11 @@ General C++
|
||||||
|
|
||||||
- *Rationale*: This avoids memory and resource leaks, and ensures exception safety
|
- *Rationale*: This avoids memory and resource leaks, and ensures exception safety
|
||||||
|
|
||||||
|
- Use `MakeUnique()` to construct objects owned by `unique_ptr`s
|
||||||
|
|
||||||
|
- *Rationale*: `MakeUnique` is concise and ensures exception safety in complex expressions.
|
||||||
|
`MakeUnique` is a temporary project local implementation of `std::make_unique` (C++14).
|
||||||
|
|
||||||
C++ data structures
|
C++ data structures
|
||||||
--------------------
|
--------------------
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ int main(int argc, char** argv)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<benchmark::Printer> printer(new benchmark::ConsolePrinter());
|
std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
|
||||||
std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
|
std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
|
||||||
if ("plot" == printer_arg) {
|
if ("plot" == printer_arg) {
|
||||||
printer.reset(new benchmark::PlotlyPrinter(
|
printer.reset(new benchmark::PlotlyPrinter(
|
||||||
|
|
|
@ -66,7 +66,7 @@ static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
tx.vout.resize(nInput + 1);
|
tx.vout.resize(nInput + 1);
|
||||||
tx.vout[nInput].nValue = nValue;
|
tx.vout[nInput].nValue = nValue;
|
||||||
std::unique_ptr<CWalletTx> wtx(new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
|
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
|
||||||
set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
|
set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
|
||||||
wtxn.emplace_back(std::move(wtx));
|
wtxn.emplace_back(std::move(wtx));
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,7 +163,7 @@ private:
|
||||||
BOOST_AUTO_TEST_CASE(lockedpool_tests_mock)
|
BOOST_AUTO_TEST_CASE(lockedpool_tests_mock)
|
||||||
{
|
{
|
||||||
// Test over three virtual arenas, of which one will succeed being locked
|
// Test over three virtual arenas, of which one will succeed being locked
|
||||||
std::unique_ptr<LockedPageAllocator> x(new TestLockedPageAllocator(3, 1));
|
std::unique_ptr<LockedPageAllocator> x = MakeUnique<TestLockedPageAllocator>(3, 1);
|
||||||
LockedPool pool(std::move(x));
|
LockedPool pool(std::move(x));
|
||||||
BOOST_CHECK(pool.stats().total == 0);
|
BOOST_CHECK(pool.stats().total == 0);
|
||||||
BOOST_CHECK(pool.stats().locked == 0);
|
BOOST_CHECK(pool.stats().locked == 0);
|
||||||
|
|
|
@ -148,7 +148,7 @@ typedef CCheckQueue<FrozenCleanupCheck> FrozenCleanup_Queue;
|
||||||
*/
|
*/
|
||||||
static void Correct_Queue_range(std::vector<size_t> range)
|
static void Correct_Queue_range(std::vector<size_t> range)
|
||||||
{
|
{
|
||||||
auto small_queue = std::unique_ptr<Correct_Queue>(new Correct_Queue {QUEUE_BATCH_SIZE});
|
auto small_queue = MakeUnique<Correct_Queue>(QUEUE_BATCH_SIZE);
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
||||||
tg.create_thread([&]{small_queue->Thread();});
|
tg.create_thread([&]{small_queue->Thread();});
|
||||||
|
@ -213,7 +213,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
|
||||||
/** Test that failing checks are caught */
|
/** Test that failing checks are caught */
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
|
||||||
{
|
{
|
||||||
auto fail_queue = std::unique_ptr<Failing_Queue>(new Failing_Queue {QUEUE_BATCH_SIZE});
|
auto fail_queue = MakeUnique<Failing_Queue>(QUEUE_BATCH_SIZE);
|
||||||
|
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
||||||
|
@ -246,7 +246,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
|
||||||
// future blocks, ie, the bad state is cleared.
|
// future blocks, ie, the bad state is cleared.
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
||||||
{
|
{
|
||||||
auto fail_queue = std::unique_ptr<Failing_Queue>(new Failing_Queue {QUEUE_BATCH_SIZE});
|
auto fail_queue = MakeUnique<Failing_Queue>(QUEUE_BATCH_SIZE);
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
||||||
tg.create_thread([&]{fail_queue->Thread();});
|
tg.create_thread([&]{fail_queue->Thread();});
|
||||||
|
@ -274,7 +274,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
|
||||||
// more than once as well
|
// more than once as well
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
||||||
{
|
{
|
||||||
auto queue = std::unique_ptr<Unique_Queue>(new Unique_Queue {QUEUE_BATCH_SIZE});
|
auto queue = MakeUnique<Unique_Queue>(QUEUE_BATCH_SIZE);
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
||||||
tg.create_thread([&]{queue->Thread();});
|
tg.create_thread([&]{queue->Thread();});
|
||||||
|
@ -310,7 +310,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
|
||||||
// time could leave the data hanging across a sequence of blocks.
|
// time could leave the data hanging across a sequence of blocks.
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
|
||||||
{
|
{
|
||||||
auto queue = std::unique_ptr<Memory_Queue>(new Memory_Queue {QUEUE_BATCH_SIZE});
|
auto queue = MakeUnique<Memory_Queue>(QUEUE_BATCH_SIZE);
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
||||||
tg.create_thread([&]{queue->Thread();});
|
tg.create_thread([&]{queue->Thread();});
|
||||||
|
@ -341,7 +341,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
|
||||||
// have been destructed
|
// have been destructed
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
|
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
|
||||||
{
|
{
|
||||||
auto queue = std::unique_ptr<FrozenCleanup_Queue>(new FrozenCleanup_Queue {QUEUE_BATCH_SIZE});
|
auto queue = MakeUnique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE);
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
bool fails = false;
|
bool fails = false;
|
||||||
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
for (auto x = 0; x < nScriptCheckThreads; ++x) {
|
||||||
|
@ -384,7 +384,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
|
||||||
/** Test that CCheckQueueControl is threadsafe */
|
/** Test that CCheckQueueControl is threadsafe */
|
||||||
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
|
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
|
||||||
{
|
{
|
||||||
auto queue = std::unique_ptr<Standard_Queue>(new Standard_Queue{QUEUE_BATCH_SIZE});
|
auto queue = MakeUnique<Standard_Queue>(QUEUE_BATCH_SIZE);
|
||||||
{
|
{
|
||||||
boost::thread_group tg;
|
boost::thread_group tg;
|
||||||
std::atomic<int> nThreads {0};
|
std::atomic<int> nThreads {0};
|
||||||
|
|
|
@ -179,12 +179,12 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
|
||||||
bool fInboundIn = false;
|
bool fInboundIn = false;
|
||||||
|
|
||||||
// Test that fFeeler is false by default.
|
// Test that fFeeler is false by default.
|
||||||
std::unique_ptr<CNode> pnode1(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn));
|
std::unique_ptr<CNode> pnode1 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn);
|
||||||
BOOST_CHECK(pnode1->fInbound == false);
|
BOOST_CHECK(pnode1->fInbound == false);
|
||||||
BOOST_CHECK(pnode1->fFeeler == false);
|
BOOST_CHECK(pnode1->fFeeler == false);
|
||||||
|
|
||||||
fInboundIn = true;
|
fInboundIn = true;
|
||||||
std::unique_ptr<CNode> pnode2(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn));
|
std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn);
|
||||||
BOOST_CHECK(pnode2->fInbound == true);
|
BOOST_CHECK(pnode2->fInbound == true);
|
||||||
BOOST_CHECK(pnode2->fFeeler == false);
|
BOOST_CHECK(pnode2->fFeeler == false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,7 +107,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
|
||||||
nScriptCheckThreads = 3;
|
nScriptCheckThreads = 3;
|
||||||
for (int i=0; i < nScriptCheckThreads-1; i++)
|
for (int i=0; i < nScriptCheckThreads-1; i++)
|
||||||
threadGroup.create_thread(&ThreadScriptCheck);
|
threadGroup.create_thread(&ThreadScriptCheck);
|
||||||
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
|
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
|
||||||
connman = g_connman.get();
|
connman = g_connman.get();
|
||||||
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
|
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
|
||||||
}
|
}
|
||||||
|
|
|
@ -279,7 +279,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
|
||||||
|
|
||||||
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
|
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
|
||||||
void initialize() {
|
void initialize() {
|
||||||
globalVerifyHandle = std::unique_ptr<ECCVerifyHandle>(new ECCVerifyHandle());
|
globalVerifyHandle = MakeUnique<ECCVerifyHandle>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is used by libFuzzer
|
// This function is used by libFuzzer
|
||||||
|
|
|
@ -65,7 +65,7 @@ static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = fa
|
||||||
// so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
|
// so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
|
||||||
tx.vin.resize(1);
|
tx.vin.resize(1);
|
||||||
}
|
}
|
||||||
std::unique_ptr<CWalletTx> wtx(new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
|
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
|
||||||
if (fIsFromMe)
|
if (fIsFromMe)
|
||||||
{
|
{
|
||||||
wtx->fDebitCached = true;
|
wtx->fDebitCached = true;
|
||||||
|
|
Loading…
Reference in a new issue