/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// #include #include // for not_null, operator<, operator<=, operator> #include // for addressof #include // for uint16_t #include // for shared_ptr, make_shared, operator<, opera... #include // for operator<<, ostringstream, basic_ostream:... #include // for basic_string, operator==, string, operator<< #include // for declval #include // for type_info #include // for variant, monostate, get #include "deathTestCommon.h" using namespace gsl; #if __cplusplus >= 201703l using std::void_t; #else // __cplusplus >= 201703l template using void_t = void; #endif // __cplusplus < 201703l struct MyBase { }; struct MyDerived : public MyBase { }; struct Unrelated { }; // stand-in for a user-defined ref-counted class template struct RefCounted { RefCounted(T* p) : p_(p) {} operator T*() { return p_; } T* p_; }; // user defined smart pointer with comparison operators returning non bool value template struct CustomPtr { CustomPtr(T* p) : p_(p) {} operator T*() const { return p_; } bool operator!=(std::nullptr_t) const { return p_ != nullptr; } T* p_ = nullptr; }; template std::string operator==(CustomPtr const& lhs, CustomPtr const& rhs) { // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on return reinterpret_cast(lhs.p_) == reinterpret_cast(rhs.p_) ? "true" : "false"; } template std::string operator!=(CustomPtr const& lhs, CustomPtr const& rhs) { // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on return reinterpret_cast(lhs.p_) != reinterpret_cast(rhs.p_) ? "true" : "false"; } template std::string operator<(CustomPtr const& lhs, CustomPtr const& rhs) { // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on return reinterpret_cast(lhs.p_) < reinterpret_cast(rhs.p_) ? "true" : "false"; } template std::string operator>(CustomPtr const& lhs, CustomPtr const& rhs) { // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on return reinterpret_cast(lhs.p_) > reinterpret_cast(rhs.p_) ? "true" : "false"; } template std::string operator<=(CustomPtr const& lhs, CustomPtr const& rhs) { // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on return reinterpret_cast(lhs.p_) <= reinterpret_cast(rhs.p_) ? "true" : "false"; } template std::string operator>=(CustomPtr const& lhs, CustomPtr const& rhs) { // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on return reinterpret_cast(lhs.p_) >= reinterpret_cast(rhs.p_) ? "true" : "false"; } struct NonCopyableNonMovable { NonCopyableNonMovable() = default; NonCopyableNonMovable(const NonCopyableNonMovable&) = delete; NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete; NonCopyableNonMovable(NonCopyableNonMovable&&) = delete; NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete; }; namespace { // clang-format off GSL_SUPPRESS(f .4) // NO-FORMAT: attribute // clang-format on bool helper(not_null p) { return *p == 12; } // clang-format off GSL_SUPPRESS(f .4) // NO-FORMAT: attribute // clang-format on bool helper_const(not_null p) { return *p == 12; } int* return_pointer() { return nullptr; } } // namespace template static constexpr bool CtorCompilesFor_A = false; template static constexpr bool CtorCompilesFor_A{std::declval()})>> = true; template static constexpr bool CtorCompilesFor_B = false; template static constexpr bool CtorCompilesFor_B{N})>> = true; template static constexpr bool DefaultCtorCompilesFor = false; template static constexpr bool DefaultCtorCompilesFor{})>> = true; template static constexpr bool CtorCompilesFor_C = false; template static constexpr bool CtorCompilesFor_C{std::declval>()})>> = true; TEST(notnull_tests, TestNotNullConstructors) { { static_assert(CtorCompilesFor_A, "CtorCompilesFor_A"); static_assert(!CtorCompilesFor_A, "!CtorCompilesFor_A"); static_assert(!CtorCompilesFor_B, "!CtorCompilesFor_B"); static_assert(!DefaultCtorCompilesFor, "!DefaultCtorCompilesFor"); static_assert(!CtorCompilesFor_C, "CtorCompilesFor_C"); #ifdef CONFIRM_COMPILATION_ERRORS // Forbid non-nullptr assignable types not_null> f(std::vector{1}); not_null z(10); not_null> y({1, 2}); #endif } const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestNotNullConstructors"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { // from shared pointer int i = 12; auto rp = RefCounted(&i); not_null p(rp); EXPECT_TRUE(p.get() == &i); not_null> x( std::make_shared(10)); // shared_ptr is nullptr assignable int* pi = nullptr; EXPECT_DEATH((not_null(pi)), expected); } { // from unique pointer not_null> x( std::make_unique(10)); // unique_ptr is nullptr assignable EXPECT_DEATH((not_null>(std::unique_ptr{})), expected); } { // from pointer to local int t = 42; not_null x = &t; helper(&t); helper_const(&t); EXPECT_TRUE(*x == 42); } { // from raw pointer // from not_null pointer int t = 42; int* p = &t; not_null x = p; helper(p); helper_const(p); helper(x); helper_const(x); EXPECT_TRUE(*x == 42); } { // from raw const pointer // from not_null const pointer int t = 42; const int* cp = &t; not_null x = cp; helper_const(cp); helper_const(x); EXPECT_TRUE(*x == 42); } { // from not_null const pointer, using auto int t = 42; const int* cp = &t; auto x = not_null{cp}; EXPECT_TRUE(*x == 42); } { // from returned pointer EXPECT_DEATH(helper(return_pointer()), expected); EXPECT_DEATH(helper_const(return_pointer()), expected); } } template void ostream_helper(T v) { not_null p(&v); { std::ostringstream os; std::ostringstream ref; os << static_cast(p); ref << static_cast(&v); EXPECT_TRUE(os.str() == ref.str()); } { std::ostringstream os; std::ostringstream ref; os << *p; ref << v; EXPECT_TRUE(os.str() == ref.str()); } } TEST(notnull_tests, TestNotNullostream) { ostream_helper(17); ostream_helper(21.5f); ostream_helper(3.4566e-7); ostream_helper('c'); ostream_helper(0x0123u); ostream_helper("cstring"); ostream_helper("string"); } template static constexpr bool AssignmentCompilesFor = false; template static constexpr bool AssignmentCompilesFor&>().operator=( std::declval&>()))>> = true; template static constexpr bool SCastCompilesFor = false; template static constexpr bool SCastCompilesFor(std::declval&>()))>> = true; template static constexpr bool RCastCompilesFor = false; template static constexpr bool RCastCompilesFor< U, V, void_t(std::declval&>()))>> = true; TEST(notnull_tests, TestNotNullCasting) { MyBase base; MyDerived derived; Unrelated unrelated; not_null u{&unrelated}; (void) u; not_null p{&derived}; not_null q(&base); q = p; // allowed with heterogeneous copy ctor EXPECT_TRUE(q == p); static_assert(AssignmentCompilesFor, "AssignmentCompilesFor"); static_assert(!AssignmentCompilesFor, "!AssignmentCompilesFor"); static_assert(!AssignmentCompilesFor, "!AssignmentCompilesFor"); static_assert(!AssignmentCompilesFor, "!AssignmentCompilesFor"); static_assert(SCastCompilesFor, "SCastCompilesFor"); static_assert(SCastCompilesFor, "SCastCompilesFor"); static_assert(!SCastCompilesFor, "!SCastCompilesFor"); static_assert(!SCastCompilesFor, "!SCastCompilesFor"); static_assert(!RCastCompilesFor, "!SCastCompilesFor"); static_assert(!RCastCompilesFor, "!SCastCompilesFor"); not_null t(reinterpret_cast(p.get())); EXPECT_TRUE(reinterpret_cast(p.get()) == reinterpret_cast(t.get())); (void) static_cast(p); (void) static_cast(p); } TEST(notnull_tests, TestNotNullAssignment) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestNotNullAssignmentd"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int i = 12; not_null p(&i); EXPECT_TRUE(helper(p)); int* q = nullptr; EXPECT_DEATH(p = not_null(q), expected); } TEST(notnull_tests, TestNotNullRawPointerComparison) { int ints[2] = {42, 43}; int* p1 = &ints[0]; const int* p2 = &ints[1]; using NotNull1 = not_null; using NotNull2 = not_null; EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == true); EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == false); EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == false); EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == true); EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == false); EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2)); EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1)); EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == false); EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2)); EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1)); EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == true); EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2)); EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1)); } TEST(notnull_tests, TestNotNullDereferenceOperator) { { auto sp1 = std::make_shared(); using NotNullSp1 = not_null; EXPECT_TRUE(typeid(*sp1) == typeid(*NotNullSp1(sp1))); EXPECT_TRUE(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1)); } { int ints[1] = {42}; CustomPtr p1(&ints[0]); using NotNull1 = not_null; EXPECT_TRUE(typeid(*NotNull1(p1)) == typeid(*p1)); EXPECT_TRUE(*NotNull1(p1) == 42); *NotNull1(p1) = 43; EXPECT_TRUE(ints[0] == 43); } { int v = 42; gsl::not_null p(&v); EXPECT_TRUE(typeid(*p) == typeid(*(&v))); *p = 43; EXPECT_TRUE(v == 43); } } TEST(notnull_tests, TestNotNullSharedPtrComparison) { auto sp1 = std::make_shared(42); auto sp2 = std::make_shared(43); using NotNullSp1 = not_null; using NotNullSp2 = not_null; EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp1(sp1)) == true); EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp2(sp2)) == false); EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp1(sp1)) == false); EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp2(sp2)) == true); EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp1(sp1)) == false); EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2)); EXPECT_TRUE((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1)); EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp1(sp1)) == false); EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2)); EXPECT_TRUE((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1)); EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true); EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2)); EXPECT_TRUE((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1)); EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true); EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2)); EXPECT_TRUE((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1)); } TEST(notnull_tests, TestNotNullCustomPtrComparison) { int ints[2] = {42, 43}; CustomPtr p1(&ints[0]); CustomPtr p2(&ints[1]); using NotNull1 = not_null; using NotNull2 = not_null; EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == "true"); EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == "false"); EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == "false"); EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == "true"); EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == "false"); EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2)); EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1)); EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == "false"); EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2)); EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1)); EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == "true"); EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2)); EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1)); EXPECT_TRUE((NotNull1(p1) >= NotNull1(p1)) == "true"); EXPECT_TRUE((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2)); EXPECT_TRUE((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); } #if defined(__cplusplus) && (__cplusplus >= 201703L) template static constexpr bool TypeDeductionCtorCompilesFor = false; template static constexpr bool TypeDeductionCtorCompilesFor()})>> = true; template static constexpr bool TypeDeductionHelperCompilesFor = false; template static constexpr bool TypeDeductionHelperCompilesFor()}))>> = true; TEST(notnull_tests, TestNotNullConstructorTypeDeduction) { { int i = 42; not_null x{&i}; helper(not_null{&i}); helper_const(not_null{&i}); EXPECT_TRUE(*x == 42); } { const int i = 42; not_null x{&i}; static_assert(TypeDeductionHelperCompilesFor, "TypeDeductionHelperCompilesFor"); static_assert(!TypeDeductionHelperCompilesFor, "!TypeDeductionHelperCompilesFor"); helper_const(not_null{&i}); EXPECT_TRUE(*x == 42); } { int i = 42; int* p = &i; not_null x{p}; helper(not_null{p}); helper_const(not_null{p}); EXPECT_TRUE(*x == 42); } { const int i = 42; const int* p = &i; not_null x{p}; helper_const(not_null{p}); EXPECT_TRUE(*x == 42); } const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { auto workaround_macro = []() { int* p1 = nullptr; const not_null x{p1}; }; EXPECT_DEATH(workaround_macro(), expected); } { auto workaround_macro = []() { const int* p1 = nullptr; const not_null x{p1}; }; EXPECT_DEATH(workaround_macro(), expected); } { int* p = nullptr; EXPECT_DEATH(helper(not_null{p}), expected); EXPECT_DEATH(helper_const(not_null{p}), expected); } static_assert(TypeDeductionCtorCompilesFor, "TypeDeductionCtorCompilesFor"); #if defined(_MSC_VER) && !defined(__clang__) // Fails on gcc, clang, xcode, VS clang with // "error : no type named 'type' in 'std::enable_if'; 'enable_if' cannot be used to // disable this declaration" static_assert(!TypeDeductionCtorCompilesFor, "!TypeDeductionCtorCompilesFor"); static_assert(!TypeDeductionHelperCompilesFor, "!TypeDeductionHelperCompilesFor"); #endif } TEST(notnull_tests, TestVariantEmplace) { int i = 0; std::variant> v; v.emplace>(&i); EXPECT_FALSE(v.valueless_by_exception()); EXPECT_TRUE(v.index() == 1); EXPECT_TRUE(std::get>(v) == &i); } #endif // #if defined(__cplusplus) && (__cplusplus >= 201703L) template static constexpr bool HelperCompilesFor = false; template static constexpr bool HelperCompilesFor()))>> = true; TEST(notnull_tests, TestMakeNotNull) { { int i = 42; const auto x = make_not_null(&i); helper(make_not_null(&i)); helper_const(make_not_null(&i)); EXPECT_TRUE(*x == 42); } { const int i = 42; const auto x = make_not_null(&i); static_assert(HelperCompilesFor>, "HelperCompilesFor>"); helper_const(make_not_null(&i)); EXPECT_TRUE(*x == 42); } { int i = 42; int* p = &i; const auto x = make_not_null(p); helper(make_not_null(p)); helper_const(make_not_null(p)); EXPECT_TRUE(*x == 42); } { const int i = 42; const int* p = &i; const auto x = make_not_null(p); static_assert(!HelperCompilesFor>, "!HelperCompilesFor>"); helper_const(make_not_null(p)); EXPECT_TRUE(*x == 42); } const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestMakeNotNull"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { const auto workaround_macro = []() { int* p1 = nullptr; const auto x = make_not_null(p1); EXPECT_TRUE(*x == 42); }; EXPECT_DEATH(workaround_macro(), expected); } { const auto workaround_macro = []() { const int* p1 = nullptr; const auto x = make_not_null(p1); EXPECT_TRUE(*x == 42); }; EXPECT_DEATH(workaround_macro(), expected); } { int* p = nullptr; EXPECT_DEATH(helper(make_not_null(p)), expected); EXPECT_DEATH(helper_const(make_not_null(p)), expected); } #ifdef CONFIRM_COMPILATION_ERRORS { EXPECT_DEATH(make_not_null(nullptr), expected); EXPECT_DEATH(helper(make_not_null(nullptr)), expected); EXPECT_DEATH(helper_const(make_not_null(nullptr)), expected); } #endif } TEST(notnull_tests, TestStdHash) { { int x = 42; int y = 99; not_null nn{&x}; const not_null cnn{&x}; std::hash> hash_nn; std::hash hash_intptr; EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr)); } { const int x = 42; const int y = 99; not_null nn{&x}; const not_null cnn{&x}; std::hash> hash_nn; std::hash hash_intptr; EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr)); } }