From c90614e3f3875771b17417fe5b1052fd06ae093e Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Wed, 9 Oct 2024 20:07:09 +0800 Subject: [PATCH 1/3] GCC --- tests/proxy_lifetime_tests.cpp | 38 +++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index ffdf6d91..6fd1a918 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -805,24 +805,38 @@ TEST(ProxyLifetimeTests, TestMoveAssignment_FromNull_ToNull) { } TEST(ProxyLifetimeTests, TestHasValue) { - int foo = 123; - pro::proxy p1; - ASSERT_FALSE(p1.has_value()); - p1 = &foo; - ASSERT_TRUE(p1.has_value()); - ASSERT_EQ(ToString(*p1), "123"); + utils::LifetimeTracker tracker; + std::vector expected_ops; + { + pro::proxy p; + ASSERT_FALSE(p.has_value()); + p.emplace(&tracker); + ASSERT_TRUE(p.has_value()); + ASSERT_EQ(ToString(*p), "Session 1"); + expected_ops.emplace_back(1, utils::LifetimeOperationType::kValueConstruction); + ASSERT_TRUE(tracker.GetOperations() == expected_ops); + } + expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); + ASSERT_TRUE(tracker.GetOperations() == expected_ops); } TEST(ProxyLifetimeTests, TestOperatorBool) { // Implicit conversion to bool shall be prohibited. static_assert(!std::is_nothrow_convertible_v, bool>); - int foo = 123; - pro::proxy p1; - ASSERT_FALSE(static_cast(p1)); - p1 = &foo; - ASSERT_TRUE(static_cast(p1)); - ASSERT_EQ(ToString(*p1), "123"); + utils::LifetimeTracker tracker; + std::vector expected_ops; + { + pro::proxy p; + ASSERT_FALSE(static_cast(p)); + p.emplace(&tracker); + ASSERT_TRUE(static_cast(p)); + ASSERT_EQ(ToString(*p), "Session 1"); + expected_ops.emplace_back(1, utils::LifetimeOperationType::kValueConstruction); + ASSERT_TRUE(tracker.GetOperations() == expected_ops); + } + expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); + ASSERT_TRUE(tracker.GetOperations() == expected_ops); } TEST(ProxyLifetimeTests, TestEqualsToNullptr) { From f2e8a11f6f17555ded234e626e16984a210dec63 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Wed, 9 Oct 2024 20:09:53 +0800 Subject: [PATCH 2/3] Fix GCC --- tests/proxy_lifetime_tests.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index 6fd1a918..e77b0d76 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -840,14 +840,21 @@ TEST(ProxyLifetimeTests, TestOperatorBool) { } TEST(ProxyLifetimeTests, TestEqualsToNullptr) { - int foo = 123; - pro::proxy p1; - ASSERT_TRUE(p1 == nullptr); - ASSERT_TRUE(nullptr == p1); - p1 = &foo; - ASSERT_TRUE(p1 != nullptr); - ASSERT_TRUE(nullptr != p1); - ASSERT_EQ(ToString(*p1), "123"); + utils::LifetimeTracker tracker; + std::vector expected_ops; + { + pro::proxy p; + ASSERT_TRUE(p == nullptr); + ASSERT_TRUE(nullptr == p); + p.emplace(&tracker); + ASSERT_TRUE(p != nullptr); + ASSERT_TRUE(nullptr != p); + ASSERT_EQ(ToString(*p), "Session 1"); + expected_ops.emplace_back(1, utils::LifetimeOperationType::kValueConstruction); + ASSERT_TRUE(tracker.GetOperations() == expected_ops); + } + expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); + ASSERT_TRUE(tracker.GetOperations() == expected_ops); } TEST(ProxyLifetimeTests, TestReset_FromValue) { From df3c310d25e15d70c35f048e21435308d8752a66 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Wed, 9 Oct 2024 20:28:49 +0800 Subject: [PATCH 3/3] Resolve warning --- tests/CMakeLists.txt | 4 ++-- tests/proxy_invocation_tests.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8d91ed1d..1c4d5520 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,9 +30,9 @@ target_link_libraries(msft_proxy_tests PRIVATE msft_proxy) target_link_libraries(msft_proxy_tests PRIVATE gtest_main) if (MSVC) - target_compile_options(msft_proxy_tests PRIVATE /W4) + target_compile_options(msft_proxy_tests PRIVATE /W4 /WX) else() - target_compile_options(msft_proxy_tests PRIVATE -Wall -Wextra -Wpedantic) + target_compile_options(msft_proxy_tests PRIVATE -Wall -Wextra -Wpedantic -Werror) endif() include(GoogleTest) diff --git a/tests/proxy_invocation_tests.cpp b/tests/proxy_invocation_tests.cpp index 5388efcb..c8b5bffd 100644 --- a/tests/proxy_invocation_tests.cpp +++ b/tests/proxy_invocation_tests.cpp @@ -11,7 +11,14 @@ #include #include #include +#if defined(_MSC_VER) && !defined(__clang__) +#pragma warning(push) +#pragma warning(disable: 4702) // False alarm from MSVC: warning C4702: unreachable code +#endif // defined(_MSC_VER) && !defined(__clang__) #include "proxy.h" +#if defined(_MSC_VER) && !defined(__clang__) +#pragma warning(pop) +#endif // defined(_MSC_VER) && !defined(__clang__) #include "utils.h" namespace {