From 5f3f78822b5d43e9b02082da5268d186ba7101c0 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 5 Feb 2022 08:15:07 -0500 Subject: Improve use of std::unique_ptr * Use unique_ptr in place of shared_ptr in some cases * unique_ptr for arrays does not require a custom deleter * use std::make_unique (c++14) where possible --- libtests/qtest/qutil/qutil.out | 1 + libtests/qutil.cc | 13 +++++++++++-- libtests/rc4.cc | 3 +-- 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'libtests') diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index 58cc2334..8bab099b 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -23,6 +23,7 @@ one 7 compare okay compare okay +compare okay -2147483648 to int: PASSED 2147483647 to int: PASSED 2147483648 to int threw (integer out of range converting 2147483648 from a 8-byte signed type to a 4-byte signed type): PASSED diff --git a/libtests/qutil.cc b/libtests/qutil.cc index a74ae4fe..dcea9fd6 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -150,7 +150,7 @@ void string_conversion_test() std::cout << "compare failed" << std::endl; } delete [] tmp; - // Also test with make_shared_cstr + // Also test with make_shared_cstr and make_unique_cstr auto tmp2 = QUtil::make_shared_cstr(embedded_null); if (memcmp(tmp2.get(), embedded_null.c_str(), 7) == 0) { @@ -160,6 +160,15 @@ void string_conversion_test() { std::cout << "compare failed" << std::endl; } + auto tmp3 = QUtil::make_unique_cstr(embedded_null); + if (memcmp(tmp3.get(), embedded_null.c_str(), 7) == 0) + { + std::cout << "compare okay" << std::endl; + } + else + { + std::cout << "compare failed" << std::endl; + } std::string int_max_str = QUtil::int_to_string(INT_MAX); std::string int_min_str = QUtil::int_to_string(INT_MIN); @@ -417,7 +426,7 @@ void transcoding_test() void print_whoami(char const* str) { - auto dup = QUtil::make_shared_cstr(str); + auto dup = QUtil::make_unique_cstr(str); std::cout << QUtil::getWhoami(dup.get()) << std::endl; } diff --git a/libtests/rc4.cc b/libtests/rc4.cc index d3f1c7fa..b8abce88 100644 --- a/libtests/rc4.cc +++ b/libtests/rc4.cc @@ -14,8 +14,7 @@ static void other_tests() // Test cases not covered by the pipeline: string as key, convert // in place RC4 r(reinterpret_cast("quack")); - auto data = std::unique_ptr( - new unsigned char[6], std::default_delete()); + auto data = std::make_unique(6); memcpy(data.get(), "potato", 6); r.process(data.get(), 6); assert(memcmp(data.get(), "\xa5\x6f\xe7\x27\x2b\x5c", 6) == 0); -- cgit v1.2.3-54-g00ecf