From 74dae8909660f09453b5adc469e59a704a526779 Mon Sep 17 00:00:00 2001 From: Hui Xie Date: Thu, 1 May 2025 10:16:21 +0100 Subject: [PATCH 1/3] [libc++] add test for flat_set::insert not creating temporaries --- .../flat.multiset/insert.temporary.pass.cpp | 46 +++++++++++++++++++ .../flat.set/insert.temporary.pass.cpp | 46 +++++++++++++++++++ .../container.adaptors/flat_helpers.h | 40 ++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp create mode 100644 libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp create mode 100644 libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp new file mode 100644 index 0000000000000..97a9bc3bdbb3d --- /dev/null +++ b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// https://github.com/llvm/llvm-project/issues/119016 + +#include + +#include + +#include "../flat_helpers.h" +#include "test_macros.h" + +bool test() { + using M = std::flat_multiset; + { + M m; + TrackCopyMove t; + m.insert(t); + assert(m.begin()->copy_count == 1); + assert(m.begin()->move_count == 0); + } + { + M m; + TrackCopyMove t; + m.emplace(t); + assert(m.begin()->copy_count == 1); + assert(m.begin()->move_count == 0); + } + + return true; +} + +int main(int, char**) { + test(); + + return 0; +} \ No newline at end of file diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp new file mode 100644 index 0000000000000..1642ec8bc89a1 --- /dev/null +++ b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// https://github.com/llvm/llvm-project/issues/119016 + +#include + +#include + +#include "../flat_helpers.h" +#include "test_macros.h" + +bool test() { + using M = std::flat_set; + { + M m; + TrackCopyMove t; + m.insert(t); + assert(m.begin()->copy_count == 1); + assert(m.begin()->move_count == 0); + } + { + M m; + TrackCopyMove t; + m.emplace(t); + assert(m.begin()->copy_count == 1); + assert(m.begin()->move_count == 0); + } + + return true; +} + +int main(int, char**) { + test(); + + return 0; +} \ No newline at end of file diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h b/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h new file mode 100644 index 0000000000000..6dbf55c659377 --- /dev/null +++ b/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H +#define TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H + +struct TrackCopyMove { + mutable int copy_count = 0; + int move_count = 0; + + constexpr TrackCopyMove() = default; + constexpr TrackCopyMove(const TrackCopyMove& other) : copy_count(other.copy_count), move_count(other.move_count) { + ++copy_count; + ++other.copy_count; + } + + constexpr TrackCopyMove(TrackCopyMove&& other) noexcept : copy_count(other.copy_count), move_count(other.move_count) { + ++move_count; + ++other.move_count; + } + constexpr TrackCopyMove& operator=(const TrackCopyMove& other) { + ++copy_count; + ++other.copy_count; + return *this; + } + constexpr TrackCopyMove& operator=(TrackCopyMove&& other) noexcept { + ++move_count; + ++other.move_count; + return *this; + } + constexpr bool operator==(const TrackCopyMove&) const { return true; } + constexpr bool operator<(const TrackCopyMove&) const { return false; } +}; + +#endif // TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H \ No newline at end of file From b1c63518939733e70d4d14120474c31fcaa1ef64 Mon Sep 17 00:00:00 2001 From: Hui Xie Date: Sun, 4 May 2025 13:50:17 +0100 Subject: [PATCH 2/3] ci --- .../container.adaptors/flat.multiset/insert.temporary.pass.cpp | 2 ++ .../container.adaptors/flat.set/insert.temporary.pass.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp index 97a9bc3bdbb3d..73d3ac2fff5c9 100644 --- a/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp +++ b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp @@ -15,6 +15,8 @@ #include #include +#include +#include #include "../flat_helpers.h" #include "test_macros.h" diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp index 1642ec8bc89a1..8fb1c6362e02c 100644 --- a/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp +++ b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp @@ -15,6 +15,8 @@ #include #include +#include +#include #include "../flat_helpers.h" #include "test_macros.h" From 776e96ba552122e2c5bced2280f9a1d095c26321 Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Thu, 8 May 2025 16:41:21 -0400 Subject: [PATCH 3/3] Apply suggestions from code review --- .../container.adaptors/flat.multiset/insert.temporary.pass.cpp | 2 +- .../container.adaptors/flat.set/insert.temporary.pass.cpp | 2 +- libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp index 73d3ac2fff5c9..c185d0c700ed2 100644 --- a/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp +++ b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp @@ -45,4 +45,4 @@ int main(int, char**) { test(); return 0; -} \ No newline at end of file +} diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp index 8fb1c6362e02c..e71f35129ddac 100644 --- a/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp +++ b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp @@ -45,4 +45,4 @@ int main(int, char**) { test(); return 0; -} \ No newline at end of file +} diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h b/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h index 6dbf55c659377..1242c29715daa 100644 --- a/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h +++ b/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h @@ -37,4 +37,4 @@ struct TrackCopyMove { constexpr bool operator<(const TrackCopyMove&) const { return false; } }; -#endif // TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H \ No newline at end of file +#endif // TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H