From 1272f513d0d4c7346527beecb9a6bb795f39d395 Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Mon, 13 May 2024 21:22:43 +0800 Subject: [PATCH 01/13] : Remove self swap check from pair. --- stl/inc/utility | 12 ++-- tests/std/tests/GH_004597_self_swap/env.lst | 4 ++ tests/std/tests/GH_004597_self_swap/test.cpp | 58 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 tests/std/tests/GH_004597_self_swap/env.lst create mode 100644 tests/std/tests/GH_004597_self_swap/test.cpp diff --git a/stl/inc/utility b/stl/inc/utility index 070899fb3fa..4c65253e318 100644 --- a/stl/inc/utility +++ b/stl/inc/utility @@ -441,10 +441,8 @@ struct pair { // store a pair of values _CONSTEXPR20 void swap(pair& _Right) noexcept( _Is_nothrow_swappable<_Ty1>::value && _Is_nothrow_swappable<_Ty2>::value) { using _STD swap; - if (this != _STD addressof(_Right)) { - swap(first, _Right.first); // intentional ADL - swap(second, _Right.second); // intentional ADL - } + swap(first, _Right.first); // intentional ADL + swap(second, _Right.second); // intentional ADL } #if _HAS_CXX23 @@ -452,10 +450,8 @@ struct pair { // store a pair of values constexpr void swap(const pair& _Right) const noexcept(is_nothrow_swappable_v && is_nothrow_swappable_v) { using _STD swap; - if (this != _STD addressof(_Right)) { - swap(first, _Right.first); // intentional ADL - swap(second, _Right.second); // intentional ADL - } + swap(first, _Right.first); // intentional ADL + swap(second, _Right.second); // intentional ADL } #endif // _HAS_CXX23 diff --git a/tests/std/tests/GH_004597_self_swap/env.lst b/tests/std/tests/GH_004597_self_swap/env.lst new file mode 100644 index 00000000000..351a8293d9d --- /dev/null +++ b/tests/std/tests/GH_004597_self_swap/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_20_matrix.lst diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp new file mode 100644 index 00000000000..a12caea8f6d --- /dev/null +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include + +// Test GH-4597 ": Side effects in self-swaps of pair are skipped" +struct swap_counter { + unsigned int* pcnt_ = nullptr; + + friend constexpr void swap(swap_counter& lhs, swap_counter& rhs) noexcept { + std::swap(lhs.pcnt_, rhs.pcnt_); + if (lhs.pcnt_ != nullptr) + ++(*lhs.pcnt_); + if (rhs.pcnt_ != nullptr) + ++(*rhs.pcnt_); + } + + constexpr bool operator==(unsigned int x) const { + return *pcnt_ == x; + } +}; + + +void test_gh_4595() { + { + static_assert([] { + unsigned int cnt{}; + std::pair pr{swap_counter{&cnt}, 0}; + pr.swap(pr); + return (cnt == 2u) && (pr.first == 2u) && (pr.second == 0); + }()); + } + + { + static_assert([] { + unsigned int cnt{}; + std::pair p1{swap_counter{&cnt}, 0}; + std::pair p2{swap_counter{&cnt}, 1}; + p1.swap(p2); + return (cnt == 2u) && (p1.first == 2u) && (p1.second == 1) && (p2.first == 2u) && (p2.second == 0); + }()); + } + + { + static_assert([] { + unsigned int c1{}; + unsigned int c2{2}; + std::pair p1{swap_counter{&c1}, 1}; + std::pair p2{swap_counter{&c2}, 3}; + p1.swap(p2); + return (c1 == 1u) && (c2==3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); + }()); + } +} + +int main() { + test_gh_4595(); +} From 8645c97dd0642d14c488e9f3cd390ca842cf4be4 Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Mon, 13 May 2024 21:33:44 +0800 Subject: [PATCH 02/13] Code format --- stl/inc/utility | 2 +- tests/std/tests/GH_004597_self_swap/test.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/stl/inc/utility b/stl/inc/utility index 4c65253e318..6dd9540db03 100644 --- a/stl/inc/utility +++ b/stl/inc/utility @@ -442,7 +442,7 @@ struct pair { // store a pair of values _Is_nothrow_swappable<_Ty1>::value && _Is_nothrow_swappable<_Ty2>::value) { using _STD swap; swap(first, _Right.first); // intentional ADL - swap(second, _Right.second); // intentional ADL + swap(second, _Right.second); // intentional ADL } #if _HAS_CXX23 diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index a12caea8f6d..e0b04a43871 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -9,10 +9,12 @@ struct swap_counter { friend constexpr void swap(swap_counter& lhs, swap_counter& rhs) noexcept { std::swap(lhs.pcnt_, rhs.pcnt_); - if (lhs.pcnt_ != nullptr) + if (lhs.pcnt_ != nullptr) { ++(*lhs.pcnt_); - if (rhs.pcnt_ != nullptr) + } + if (rhs.pcnt_ != nullptr) { ++(*rhs.pcnt_); + } } constexpr bool operator==(unsigned int x) const { @@ -48,7 +50,8 @@ void test_gh_4595() { std::pair p1{swap_counter{&c1}, 1}; std::pair p2{swap_counter{&c2}, 3}; p1.swap(p2); - return (c1 == 1u) && (c2==3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); + return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) + && (p2.second == 1); }()); } } From e0c464e0e6248ee6895fdad005c0db2e1122d3e0 Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Mon, 13 May 2024 22:01:43 +0800 Subject: [PATCH 03/13] Compatible with C++11 --- tests/std/tests/GH_004597_self_swap/env.lst | 2 +- tests/std/tests/GH_004597_self_swap/test.cpp | 72 +++++++++++--------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/env.lst b/tests/std/tests/GH_004597_self_swap/env.lst index 351a8293d9d..19f025bd0e6 100644 --- a/tests/std/tests/GH_004597_self_swap/env.lst +++ b/tests/std/tests/GH_004597_self_swap/env.lst @@ -1,4 +1,4 @@ # Copyright (c) Microsoft Corporation. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -RUNALL_INCLUDE ..\usual_20_matrix.lst +RUNALL_INCLUDE ..\usual_matrix.lst diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index e0b04a43871..203b3dbfadc 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -1,13 +1,20 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include #include +#if _HAS_CXX20 +#define TEST_CONSTEXPR20 constexpr +#else +#define TEST_CONSTEXPR20 inline +#endif + // Test GH-4597 ": Side effects in self-swaps of pair are skipped" struct swap_counter { unsigned int* pcnt_ = nullptr; - friend constexpr void swap(swap_counter& lhs, swap_counter& rhs) noexcept { + friend TEST_CONSTEXPR20 void swap(swap_counter& lhs, swap_counter& rhs) noexcept { std::swap(lhs.pcnt_, rhs.pcnt_); if (lhs.pcnt_ != nullptr) { ++(*lhs.pcnt_); @@ -17,45 +24,46 @@ struct swap_counter { } } - constexpr bool operator==(unsigned int x) const { + TEST_CONSTEXPR20 bool operator==(unsigned int x) const { return *pcnt_ == x; } }; -void test_gh_4595() { - { - static_assert([] { - unsigned int cnt{}; - std::pair pr{swap_counter{&cnt}, 0}; - pr.swap(pr); - return (cnt == 2u) && (pr.first == 2u) && (pr.second == 0); - }()); - } +TEST_CONSTEXPR20 bool test_gh_4595() { + auto res1 = [] { + unsigned int cnt{}; + std::pair pr{swap_counter{&cnt}, 0}; + pr.swap(pr); + return (cnt == 2u) && (pr.first == 2u) && (pr.second == 0); + }(); - { - static_assert([] { - unsigned int cnt{}; - std::pair p1{swap_counter{&cnt}, 0}; - std::pair p2{swap_counter{&cnt}, 1}; - p1.swap(p2); - return (cnt == 2u) && (p1.first == 2u) && (p1.second == 1) && (p2.first == 2u) && (p2.second == 0); - }()); - } - { - static_assert([] { - unsigned int c1{}; - unsigned int c2{2}; - std::pair p1{swap_counter{&c1}, 1}; - std::pair p2{swap_counter{&c2}, 3}; - p1.swap(p2); - return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) - && (p2.second == 1); - }()); - } + auto res2 = [] { + unsigned int cnt{}; + std::pair p1{swap_counter{&cnt}, 0}; + std::pair p2{swap_counter{&cnt}, 1}; + p1.swap(p2); + return (cnt == 2u) && (p1.first == 2u) && (p1.second == 1) && (p2.first == 2u) && (p2.second == 0); + }(); + + + auto res3 = [] { + unsigned int c1{}; + unsigned int c2{2}; + std::pair p1{swap_counter{&c1}, 1}; + std::pair p2{swap_counter{&c2}, 3}; + p1.swap(p2); + return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); + }(); + + return res1 && res2 && res3; } int main() { - test_gh_4595(); +#if _HAS_CXX20 + static_assert(test_gh_4595()); +#else + assert(test_gh_4595()); +#endif } From 63e474a77eb02fd20953b92025b6df211f6957b4 Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Mon, 13 May 2024 22:38:49 +0800 Subject: [PATCH 04/13] Use existed defs --- tests/std/tests/GH_004597_self_swap/test.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 203b3dbfadc..6d457fe0f49 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -4,17 +4,11 @@ #include #include -#if _HAS_CXX20 -#define TEST_CONSTEXPR20 constexpr -#else -#define TEST_CONSTEXPR20 inline -#endif - // Test GH-4597 ": Side effects in self-swaps of pair are skipped" struct swap_counter { unsigned int* pcnt_ = nullptr; - friend TEST_CONSTEXPR20 void swap(swap_counter& lhs, swap_counter& rhs) noexcept { + friend _CONSTEXPR20 void swap(swap_counter& lhs, swap_counter& rhs) noexcept { std::swap(lhs.pcnt_, rhs.pcnt_); if (lhs.pcnt_ != nullptr) { ++(*lhs.pcnt_); @@ -24,13 +18,13 @@ struct swap_counter { } } - TEST_CONSTEXPR20 bool operator==(unsigned int x) const { + _CONSTEXPR20 bool operator==(unsigned int x) const { return *pcnt_ == x; } }; -TEST_CONSTEXPR20 bool test_gh_4595() { +_CONSTEXPR20 bool test_gh_4595() { auto res1 = [] { unsigned int cnt{}; std::pair pr{swap_counter{&cnt}, 0}; From 706bb5704d2c9eb72e1086a51abf7e16bb10ff2b Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Mon, 13 May 2024 23:54:33 +0800 Subject: [PATCH 05/13] Add test for cpp23 --- tests/std/tests/GH_004597_self_swap/test.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 6d457fe0f49..204267ad83b 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -51,7 +51,24 @@ _CONSTEXPR20 bool test_gh_4595() { return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); }(); - return res1 && res2 && res3; + auto res4 = true; + +#if _HAS_CXX23 + res4 = [] { + unsigned int c1{}; + unsigned int c2{2}; + int i1 = 1; + int i2 = 3; + auto s1 = swap_counter{&c1}; + auto s2 = swap_counter{&c2}; + const std::pair p1{s1, i1}; + const std::pair p2{s2, i2}; + p1.swap(p2); + return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); + }(); + +#endif + return res1 && res2 && res3 && res4; } int main() { From fe0d85ec96bdc7f2e5168356d7bdeed5521f69ec Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Tue, 14 May 2024 02:04:32 +0800 Subject: [PATCH 06/13] Format code and add tests\GH_004597_self_swap into test.lst --- tests/std/test.lst | 1 + tests/std/tests/GH_004597_self_swap/test.cpp | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/std/test.lst b/tests/std/test.lst index adb3bb08e3a..12bb1b2816d 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -243,6 +243,7 @@ tests\GH_004201_chrono_formatter tests\GH_004275_seeking_fancy_iterators tests\GH_004388_unordered_meow_operator_equal tests\GH_004477_mdspan_warning_5246 +tests\GH_004597_self_swap tests\GH_004618_mixed_operator_usage_keeps_statistical_properties tests\GH_004618_normal_distribution_avoids_resets tests\LWG2381_num_get_floating_point diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 204267ad83b..6a2573a3e44 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -4,7 +4,6 @@ #include #include -// Test GH-4597 ": Side effects in self-swaps of pair are skipped" struct swap_counter { unsigned int* pcnt_ = nullptr; @@ -66,15 +65,13 @@ _CONSTEXPR20 bool test_gh_4595() { p1.swap(p2); return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); }(); - -#endif +#endif // _HAS_CXX23 return res1 && res2 && res3 && res4; } int main() { #if _HAS_CXX20 static_assert(test_gh_4595()); -#else +#endif // _HAS_CXX20 assert(test_gh_4595()); -#endif } From c04a6a70535ac1f9ab4ed6cd0f4a3da012bdc0a4 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:24:43 -0700 Subject: [PATCH 07/13] Remove unnecessary parentheses. --- tests/std/tests/GH_004597_self_swap/test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 6a2573a3e44..bcefc23c497 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -10,10 +10,10 @@ struct swap_counter { friend _CONSTEXPR20 void swap(swap_counter& lhs, swap_counter& rhs) noexcept { std::swap(lhs.pcnt_, rhs.pcnt_); if (lhs.pcnt_ != nullptr) { - ++(*lhs.pcnt_); + ++*lhs.pcnt_; } if (rhs.pcnt_ != nullptr) { - ++(*rhs.pcnt_); + ++*rhs.pcnt_; } } @@ -28,7 +28,7 @@ _CONSTEXPR20 bool test_gh_4595() { unsigned int cnt{}; std::pair pr{swap_counter{&cnt}, 0}; pr.swap(pr); - return (cnt == 2u) && (pr.first == 2u) && (pr.second == 0); + return cnt == 2u && pr.first == 2u && pr.second == 0; }(); @@ -37,7 +37,7 @@ _CONSTEXPR20 bool test_gh_4595() { std::pair p1{swap_counter{&cnt}, 0}; std::pair p2{swap_counter{&cnt}, 1}; p1.swap(p2); - return (cnt == 2u) && (p1.first == 2u) && (p1.second == 1) && (p2.first == 2u) && (p2.second == 0); + return cnt == 2u && p1.first == 2u && p1.second == 1 && p2.first == 2u && p2.second == 0; }(); @@ -47,7 +47,7 @@ _CONSTEXPR20 bool test_gh_4595() { std::pair p1{swap_counter{&c1}, 1}; std::pair p2{swap_counter{&c2}, 3}; p1.swap(p2); - return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); + return c1 == 1u && c2 == 3u && p1.first == 3u && p1.second == 3 && p2.first == 1u && p2.second == 1; }(); auto res4 = true; @@ -63,7 +63,7 @@ _CONSTEXPR20 bool test_gh_4595() { const std::pair p1{s1, i1}; const std::pair p2{s2, i2}; p1.swap(p2); - return (c1 == 1u) && (c2 == 3u) && (p1.first == 3u) && (p1.second == 3) && (p2.first == 1u) && (p2.second == 1); + return c1 == 1u && c2 == 3u && p1.first == 3u && p1.second == 3 && p2.first == 1u && p2.second == 1; }(); #endif // _HAS_CXX23 return res1 && res2 && res3 && res4; From f1aecccc6fd76ffe6a7e87be5d0982a3cfa1f5cd Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:25:37 -0700 Subject: [PATCH 08/13] Add newline. --- tests/std/tests/GH_004597_self_swap/test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index bcefc23c497..120b1d33337 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -12,6 +12,7 @@ struct swap_counter { if (lhs.pcnt_ != nullptr) { ++*lhs.pcnt_; } + if (rhs.pcnt_ != nullptr) { ++*rhs.pcnt_; } From 3e16611a2832de76ce8c9b47f55022e09336380b Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:27:20 -0700 Subject: [PATCH 09/13] Fix bug number, add bug title. --- tests/std/tests/GH_004597_self_swap/test.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 120b1d33337..d641675446f 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -24,7 +24,8 @@ struct swap_counter { }; -_CONSTEXPR20 bool test_gh_4595() { +// Test GH-4597 ": Side effects in self-swaps of pair are skipped" +_CONSTEXPR20 bool test_gh_4597() { auto res1 = [] { unsigned int cnt{}; std::pair pr{swap_counter{&cnt}, 0}; @@ -72,7 +73,7 @@ _CONSTEXPR20 bool test_gh_4595() { int main() { #if _HAS_CXX20 - static_assert(test_gh_4595()); + static_assert(test_gh_4597()); #endif // _HAS_CXX20 - assert(test_gh_4595()); + assert(test_gh_4597()); } From 43c8ad100ab79815f4cdba13e432873882998999 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:29:27 -0700 Subject: [PATCH 10/13] Direct-init swap_counters. --- tests/std/tests/GH_004597_self_swap/test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index d641675446f..4bbedaf4381 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -58,10 +58,10 @@ _CONSTEXPR20 bool test_gh_4597() { res4 = [] { unsigned int c1{}; unsigned int c2{2}; - int i1 = 1; - int i2 = 3; - auto s1 = swap_counter{&c1}; - auto s2 = swap_counter{&c2}; + int i1 = 1; + int i2 = 3; + swap_counter s1{&c1}; + swap_counter s2{&c2}; const std::pair p1{s1, i1}; const std::pair p2{s2, i2}; p1.swap(p2); From 41251c12335e09ddedf0b9fdbd3576652e98a109 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:33:26 -0700 Subject: [PATCH 11/13] Separately assert conditions instead of ANDing them. --- tests/std/tests/GH_004597_self_swap/test.cpp | 47 +++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 4bbedaf4381..fde49125a78 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -26,36 +26,43 @@ struct swap_counter { // Test GH-4597 ": Side effects in self-swaps of pair are skipped" _CONSTEXPR20 bool test_gh_4597() { - auto res1 = [] { + { unsigned int cnt{}; std::pair pr{swap_counter{&cnt}, 0}; pr.swap(pr); - return cnt == 2u && pr.first == 2u && pr.second == 0; - }(); - + assert(cnt == 2u); + assert(pr.first == 2u); + assert(pr.second == 0); + } - auto res2 = [] { + { unsigned int cnt{}; std::pair p1{swap_counter{&cnt}, 0}; std::pair p2{swap_counter{&cnt}, 1}; p1.swap(p2); - return cnt == 2u && p1.first == 2u && p1.second == 1 && p2.first == 2u && p2.second == 0; - }(); - + assert(cnt == 2u); + assert(p1.first == 2u); + assert(p1.second == 1); + assert(p2.first == 2u); + assert(p2.second == 0); + } - auto res3 = [] { + { unsigned int c1{}; unsigned int c2{2}; std::pair p1{swap_counter{&c1}, 1}; std::pair p2{swap_counter{&c2}, 3}; p1.swap(p2); - return c1 == 1u && c2 == 3u && p1.first == 3u && p1.second == 3 && p2.first == 1u && p2.second == 1; - }(); - - auto res4 = true; + assert(c1 == 1u); + assert(c2 == 3u); + assert(p1.first == 3u); + assert(p1.second == 3); + assert(p2.first == 1u); + assert(p2.second == 1); + } #if _HAS_CXX23 - res4 = [] { + { unsigned int c1{}; unsigned int c2{2}; int i1 = 1; @@ -65,10 +72,16 @@ _CONSTEXPR20 bool test_gh_4597() { const std::pair p1{s1, i1}; const std::pair p2{s2, i2}; p1.swap(p2); - return c1 == 1u && c2 == 3u && p1.first == 3u && p1.second == 3 && p2.first == 1u && p2.second == 1; - }(); + assert(c1 == 1u); + assert(c2 == 3u); + assert(p1.first == 3u); + assert(p1.second == 3); + assert(p2.first == 1u); + assert(p2.second == 1); + } #endif // _HAS_CXX23 - return res1 && res2 && res3 && res4; + + return true; } int main() { From 31fd40a71e71dc6f87b1559c18df151e857f667f Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:44:41 -0700 Subject: [PATCH 12/13] Consistently `assert` that `pcnt_` is non-null. --- tests/std/tests/GH_004597_self_swap/test.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index fde49125a78..3d0b6189539 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -8,17 +8,15 @@ struct swap_counter { unsigned int* pcnt_ = nullptr; friend _CONSTEXPR20 void swap(swap_counter& lhs, swap_counter& rhs) noexcept { + assert(lhs.pcnt_ != nullptr); + assert(rhs.pcnt_ != nullptr); std::swap(lhs.pcnt_, rhs.pcnt_); - if (lhs.pcnt_ != nullptr) { - ++*lhs.pcnt_; - } - - if (rhs.pcnt_ != nullptr) { - ++*rhs.pcnt_; - } + ++*lhs.pcnt_; + ++*rhs.pcnt_; } _CONSTEXPR20 bool operator==(unsigned int x) const { + assert(pcnt_ != nullptr); return *pcnt_ == x; } }; From 3e45a7db5cc3aa5b6e206bafbafd4dad9896d5b4 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 15 May 2024 23:52:02 -0700 Subject: [PATCH 13/13] Add 10 to the int values to make them distinctive. --- tests/std/tests/GH_004597_self_swap/test.cpp | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/std/tests/GH_004597_self_swap/test.cpp b/tests/std/tests/GH_004597_self_swap/test.cpp index 3d0b6189539..e9bdc3e78f5 100644 --- a/tests/std/tests/GH_004597_self_swap/test.cpp +++ b/tests/std/tests/GH_004597_self_swap/test.cpp @@ -26,45 +26,45 @@ struct swap_counter { _CONSTEXPR20 bool test_gh_4597() { { unsigned int cnt{}; - std::pair pr{swap_counter{&cnt}, 0}; + std::pair pr{swap_counter{&cnt}, 10}; pr.swap(pr); assert(cnt == 2u); assert(pr.first == 2u); - assert(pr.second == 0); + assert(pr.second == 10); } { unsigned int cnt{}; - std::pair p1{swap_counter{&cnt}, 0}; - std::pair p2{swap_counter{&cnt}, 1}; + std::pair p1{swap_counter{&cnt}, 10}; + std::pair p2{swap_counter{&cnt}, 11}; p1.swap(p2); assert(cnt == 2u); assert(p1.first == 2u); - assert(p1.second == 1); + assert(p1.second == 11); assert(p2.first == 2u); - assert(p2.second == 0); + assert(p2.second == 10); } { unsigned int c1{}; unsigned int c2{2}; - std::pair p1{swap_counter{&c1}, 1}; - std::pair p2{swap_counter{&c2}, 3}; + std::pair p1{swap_counter{&c1}, 11}; + std::pair p2{swap_counter{&c2}, 13}; p1.swap(p2); assert(c1 == 1u); assert(c2 == 3u); assert(p1.first == 3u); - assert(p1.second == 3); + assert(p1.second == 13); assert(p2.first == 1u); - assert(p2.second == 1); + assert(p2.second == 11); } #if _HAS_CXX23 { unsigned int c1{}; unsigned int c2{2}; - int i1 = 1; - int i2 = 3; + int i1 = 11; + int i2 = 13; swap_counter s1{&c1}; swap_counter s2{&c2}; const std::pair p1{s1, i1}; @@ -73,9 +73,9 @@ _CONSTEXPR20 bool test_gh_4597() { assert(c1 == 1u); assert(c2 == 3u); assert(p1.first == 3u); - assert(p1.second == 3); + assert(p1.second == 13); assert(p2.first == 1u); - assert(p2.second == 1); + assert(p2.second == 11); } #endif // _HAS_CXX23