Skip to content

Commit b6718e3

Browse files
tests: Use MakeUnique to construct objects owned by unique_ptrs
1 parent 8f46454 commit b6718e3

File tree

9 files changed

+20
-15
lines changed

9 files changed

+20
-15
lines changed

doc/developer-notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ General C++
439439

440440
- *Rationale*: This avoids memory and resource leaks, and ensures exception safety
441441

442+
- Use `MakeUnique()` to construct objects owned by `unique_ptr`s
443+
444+
- *Rationale*: `MakeUnique` is concise and ensures exception safety in complex expressions.
445+
`MakeUnique` is a temporary project local implementation of `std::make_unique` (C++14).
446+
442447
C++ data structures
443448
--------------------
444449

src/bench/bench_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int main(int argc, char** argv)
8282
return EXIT_FAILURE;
8383
}
8484

85-
std::unique_ptr<benchmark::Printer> printer(new benchmark::ConsolePrinter());
85+
std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
8686
std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
8787
if ("plot" == printer_arg) {
8888
printer.reset(new benchmark::PlotlyPrinter(

src/bench/coin_selection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>
6666
CMutableTransaction tx;
6767
tx.vout.resize(nInput + 1);
6868
tx.vout[nInput].nValue = nValue;
69-
std::unique_ptr<CWalletTx> wtx(new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
69+
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
7070
set.emplace_back(COutput(wtx.get(), nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0);
7171
wtxn.emplace_back(std::move(wtx));
7272
}

src/test/allocator_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class TestLockedPageAllocator: public LockedPageAllocator
163163
BOOST_AUTO_TEST_CASE(lockedpool_tests_mock)
164164
{
165165
// Test over three virtual arenas, of which one will succeed being locked
166-
std::unique_ptr<LockedPageAllocator> x(new TestLockedPageAllocator(3, 1));
166+
std::unique_ptr<LockedPageAllocator> x = MakeUnique<TestLockedPageAllocator>(3, 1);
167167
LockedPool pool(std::move(x));
168168
BOOST_CHECK(pool.stats().total == 0);
169169
BOOST_CHECK(pool.stats().locked == 0);

src/test/checkqueue_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ typedef CCheckQueue<FrozenCleanupCheck> FrozenCleanup_Queue;
148148
*/
149149
static void Correct_Queue_range(std::vector<size_t> range)
150150
{
151-
auto small_queue = std::unique_ptr<Correct_Queue>(new Correct_Queue {QUEUE_BATCH_SIZE});
151+
auto small_queue = MakeUnique<Correct_Queue>(QUEUE_BATCH_SIZE);
152152
boost::thread_group tg;
153153
for (auto x = 0; x < nScriptCheckThreads; ++x) {
154154
tg.create_thread([&]{small_queue->Thread();});
@@ -213,7 +213,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Random)
213213
/** Test that failing checks are caught */
214214
BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
215215
{
216-
auto fail_queue = std::unique_ptr<Failing_Queue>(new Failing_Queue {QUEUE_BATCH_SIZE});
216+
auto fail_queue = MakeUnique<Failing_Queue>(QUEUE_BATCH_SIZE);
217217

218218
boost::thread_group tg;
219219
for (auto x = 0; x < nScriptCheckThreads; ++x) {
@@ -246,7 +246,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
246246
// future blocks, ie, the bad state is cleared.
247247
BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
248248
{
249-
auto fail_queue = std::unique_ptr<Failing_Queue>(new Failing_Queue {QUEUE_BATCH_SIZE});
249+
auto fail_queue = MakeUnique<Failing_Queue>(QUEUE_BATCH_SIZE);
250250
boost::thread_group tg;
251251
for (auto x = 0; x < nScriptCheckThreads; ++x) {
252252
tg.create_thread([&]{fail_queue->Thread();});
@@ -274,7 +274,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
274274
// more than once as well
275275
BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
276276
{
277-
auto queue = std::unique_ptr<Unique_Queue>(new Unique_Queue {QUEUE_BATCH_SIZE});
277+
auto queue = MakeUnique<Unique_Queue>(QUEUE_BATCH_SIZE);
278278
boost::thread_group tg;
279279
for (auto x = 0; x < nScriptCheckThreads; ++x) {
280280
tg.create_thread([&]{queue->Thread();});
@@ -310,7 +310,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
310310
// time could leave the data hanging across a sequence of blocks.
311311
BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
312312
{
313-
auto queue = std::unique_ptr<Memory_Queue>(new Memory_Queue {QUEUE_BATCH_SIZE});
313+
auto queue = MakeUnique<Memory_Queue>(QUEUE_BATCH_SIZE);
314314
boost::thread_group tg;
315315
for (auto x = 0; x < nScriptCheckThreads; ++x) {
316316
tg.create_thread([&]{queue->Thread();});
@@ -341,7 +341,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
341341
// have been destructed
342342
BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
343343
{
344-
auto queue = std::unique_ptr<FrozenCleanup_Queue>(new FrozenCleanup_Queue {QUEUE_BATCH_SIZE});
344+
auto queue = MakeUnique<FrozenCleanup_Queue>(QUEUE_BATCH_SIZE);
345345
boost::thread_group tg;
346346
bool fails = false;
347347
for (auto x = 0; x < nScriptCheckThreads; ++x) {
@@ -384,7 +384,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
384384
/** Test that CCheckQueueControl is threadsafe */
385385
BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
386386
{
387-
auto queue = std::unique_ptr<Standard_Queue>(new Standard_Queue{QUEUE_BATCH_SIZE});
387+
auto queue = MakeUnique<Standard_Queue>(QUEUE_BATCH_SIZE);
388388
{
389389
boost::thread_group tg;
390390
std::atomic<int> nThreads {0};

src/test/net_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
179179
bool fInboundIn = false;
180180

181181
// Test that fFeeler is false by default.
182-
std::unique_ptr<CNode> pnode1(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn));
182+
std::unique_ptr<CNode> pnode1 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn);
183183
BOOST_CHECK(pnode1->fInbound == false);
184184
BOOST_CHECK(pnode1->fFeeler == false);
185185

186186
fInboundIn = true;
187-
std::unique_ptr<CNode> pnode2(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn));
187+
std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn);
188188
BOOST_CHECK(pnode2->fInbound == true);
189189
BOOST_CHECK(pnode2->fFeeler == false);
190190
}

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
107107
nScriptCheckThreads = 3;
108108
for (int i=0; i < nScriptCheckThreads-1; i++)
109109
threadGroup.create_thread(&ThreadScriptCheck);
110-
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
110+
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
111111
connman = g_connman.get();
112112
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
113113
}

src/test/test_bitcoin_fuzzy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
279279

280280
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
281281
void initialize() {
282-
globalVerifyHandle = std::unique_ptr<ECCVerifyHandle>(new ECCVerifyHandle());
282+
globalVerifyHandle = MakeUnique<ECCVerifyHandle>();
283283
}
284284

285285
// This function is used by libFuzzer

src/wallet/test/coinselector_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = fa
6565
// so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
6666
tx.vin.resize(1);
6767
}
68-
std::unique_ptr<CWalletTx> wtx(new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
68+
std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
6969
if (fIsFromMe)
7070
{
7171
wtx->fDebitCached = true;

0 commit comments

Comments
 (0)