From 49c88f27bd642b8d80371cbd0b8cffbfe5367c44 Mon Sep 17 00:00:00 2001 From: Werner Henze <34543625+beinhaerter@users.noreply.github.com> Date: Mon, 6 Feb 2023 22:16:06 +0100 Subject: [PATCH] add not_null and strict_not_null tests for const (#1085) --- tests/notnull_tests.cpp | 84 +++++++++++++++++++++++--- tests/strict_notnull_tests.cpp | 107 +++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 9 deletions(-) diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index 420c87b..4c317d9 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -441,6 +441,18 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction) EXPECT_TRUE(*x == 42); } + { + const int i = 42; + + not_null x{&i}; +#ifdef CONFIRM_COMPILATION_ERRORS + helper(not_null{&i}); +#endif + helper_const(not_null{&i}); + + EXPECT_TRUE(*x == 42); + } + { int i = 42; int* p = &i; @@ -452,6 +464,19 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction) EXPECT_TRUE(*x == 42); } + { + const int i = 42; + const int* p = &i; + + not_null x{p}; +#ifdef CONFIRM_COMPILATION_ERRORS + helper(not_null{p}); +#endif + helper_const(not_null{p}); + + EXPECT_TRUE(*x == 42); + } + const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction"; std::abort(); @@ -503,6 +528,18 @@ TEST(notnull_tests, TestMakeNotNull) EXPECT_TRUE(*x == 42); } + { + const int i = 42; + + const auto x = make_not_null(&i); +#ifdef CONFIRM_COMPILATION_ERRORS + helper(make_not_null(&i)); +#endif + helper_const(make_not_null(&i)); + + EXPECT_TRUE(*x == 42); + } + { int i = 42; int* p = &i; @@ -514,6 +551,19 @@ TEST(notnull_tests, TestMakeNotNull) EXPECT_TRUE(*x == 42); } + { + const int i = 42; + const int* p = &i; + + const auto x = make_not_null(p); +#ifdef CONFIRM_COMPILATION_ERRORS + helper(make_not_null(p)); +#endif + helper_const(make_not_null(p)); + + EXPECT_TRUE(*x == 42); + } + const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. TestMakeNotNull"; std::abort(); @@ -556,15 +606,31 @@ TEST(notnull_tests, TestMakeNotNull) TEST(notnull_tests, TestStdHash) { - int x = 42; - int y = 99; - not_null nn{&x}; - const not_null cnn{&x}; + { + int x = 42; + int y = 99; + not_null nn{&x}; + const not_null cnn{&x}; - std::hash> hash_nn; - std::hash hash_intptr; + 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)); + 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)); + } } diff --git a/tests/strict_notnull_tests.cpp b/tests/strict_notnull_tests.cpp index 7edef43..c1b1f54 100644 --- a/tests/strict_notnull_tests.cpp +++ b/tests/strict_notnull_tests.cpp @@ -71,6 +71,28 @@ TEST(strict_notnull_tests, TestStrictNotNull) EXPECT_TRUE(*snn1 == 42); } + { + // raw ptr <-> strict_not_null + const int x = 42; + +#ifdef CONFIRM_COMPILATION_ERRORS + strict_not_null snn = &x; + strict_helper(&x); + strict_helper_const(&x); + strict_helper(return_pointer()); + strict_helper_const(return_pointer_const()); +#endif + + const strict_not_null snn1{&x}; + +#ifdef CONFIRM_COMPILATION_ERRORS + helper(snn1); +#endif + helper_const(snn1); + + EXPECT_TRUE(*snn1 == 42); + } + { // strict_not_null -> strict_not_null int x = 42; @@ -85,6 +107,22 @@ TEST(strict_notnull_tests, TestStrictNotNull) EXPECT_TRUE(snn1 == snn2); } + { + // strict_not_null -> strict_not_null + const int x = 42; + + strict_not_null snn1{&x}; + const strict_not_null snn2{&x}; + +#ifdef CONFIRM_COMPILATION_ERRORS + strict_helper(snn1); +#endif + strict_helper_const(snn1); + strict_helper_const(snn2); + + EXPECT_TRUE(snn1 == snn2); + } + { // strict_not_null -> not_null int x = 42; @@ -101,6 +139,24 @@ TEST(strict_notnull_tests, TestStrictNotNull) EXPECT_TRUE(snn == nn2); } + { + // strict_not_null -> not_null + const int x = 42; + + strict_not_null snn{&x}; + + const not_null nn1 = snn; + const not_null nn2{snn}; + +#ifdef CONFIRM_COMPILATION_ERRORS + helper(snn); +#endif + helper_const(snn); + + EXPECT_TRUE(snn == nn1); + EXPECT_TRUE(snn == nn2); + } + { // not_null -> strict_not_null int x = 42; @@ -125,6 +181,32 @@ TEST(strict_notnull_tests, TestStrictNotNull) EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn)); } + { + // not_null -> strict_not_null + const int x = 42; + + not_null nn{&x}; + + const strict_not_null snn1{nn}; + const strict_not_null snn2{nn}; + +#ifdef CONFIRM_COMPILATION_ERRORS + strict_helper(nn); +#endif + strict_helper_const(nn); + + EXPECT_TRUE(snn1 == nn); + EXPECT_TRUE(snn2 == nn); + + std::hash> hash_snn; + std::hash> hash_nn; + + EXPECT_TRUE(hash_nn(snn1) == hash_nn(nn)); + EXPECT_TRUE(hash_snn(snn1) == hash_nn(nn)); + EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2)); + EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn)); + } + #ifdef CONFIRM_COMPILATION_ERRORS { strict_not_null p{nullptr}; @@ -152,6 +234,18 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) EXPECT_TRUE(*x == 42); } + { + const int i = 42; + + strict_not_null x{&i}; +#ifdef CONFIRM_COMPILATION_ERRORS + helper(strict_not_null{&i}); +#endif + helper_const(strict_not_null{&i}); + + EXPECT_TRUE(*x == 42); + } + { int i = 42; int* p = &i; @@ -163,6 +257,19 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) EXPECT_TRUE(*x == 42); } + { + const int i = 42; + const int* p = &i; + + strict_not_null x{p}; +#ifdef CONFIRM_COMPILATION_ERRORS + helper(strict_not_null{p}); +#endif + helper_const(strict_not_null{p}); + + EXPECT_TRUE(*x == 42); + } + { auto workaround_macro = []() { int* p1 = nullptr;