Skip to content

Commit f39ac3f

Browse files
authored
[libc++] Add test for flat_set::insert not creating temporaries (#138387)
Fixes #119016
1 parent dbe320e commit f39ac3f

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <flat_set>
12+
13+
// https://github.com/llvm/llvm-project/issues/119016
14+
15+
#include <flat_set>
16+
17+
#include <cassert>
18+
#include <utility>
19+
#include <vector>
20+
21+
#include "../flat_helpers.h"
22+
#include "test_macros.h"
23+
24+
bool test() {
25+
using M = std::flat_multiset<TrackCopyMove>;
26+
{
27+
M m;
28+
TrackCopyMove t;
29+
m.insert(t);
30+
assert(m.begin()->copy_count == 1);
31+
assert(m.begin()->move_count == 0);
32+
}
33+
{
34+
M m;
35+
TrackCopyMove t;
36+
m.emplace(t);
37+
assert(m.begin()->copy_count == 1);
38+
assert(m.begin()->move_count == 0);
39+
}
40+
41+
return true;
42+
}
43+
44+
int main(int, char**) {
45+
test();
46+
47+
return 0;
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <flat_set>
12+
13+
// https://github.com/llvm/llvm-project/issues/119016
14+
15+
#include <flat_set>
16+
17+
#include <cassert>
18+
#include <utility>
19+
#include <vector>
20+
21+
#include "../flat_helpers.h"
22+
#include "test_macros.h"
23+
24+
bool test() {
25+
using M = std::flat_set<TrackCopyMove>;
26+
{
27+
M m;
28+
TrackCopyMove t;
29+
m.insert(t);
30+
assert(m.begin()->copy_count == 1);
31+
assert(m.begin()->move_count == 0);
32+
}
33+
{
34+
M m;
35+
TrackCopyMove t;
36+
m.emplace(t);
37+
assert(m.begin()->copy_count == 1);
38+
assert(m.begin()->move_count == 0);
39+
}
40+
41+
return true;
42+
}
43+
44+
int main(int, char**) {
45+
test();
46+
47+
return 0;
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
10+
#define TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
11+
12+
struct TrackCopyMove {
13+
mutable int copy_count = 0;
14+
int move_count = 0;
15+
16+
constexpr TrackCopyMove() = default;
17+
constexpr TrackCopyMove(const TrackCopyMove& other) : copy_count(other.copy_count), move_count(other.move_count) {
18+
++copy_count;
19+
++other.copy_count;
20+
}
21+
22+
constexpr TrackCopyMove(TrackCopyMove&& other) noexcept : copy_count(other.copy_count), move_count(other.move_count) {
23+
++move_count;
24+
++other.move_count;
25+
}
26+
constexpr TrackCopyMove& operator=(const TrackCopyMove& other) {
27+
++copy_count;
28+
++other.copy_count;
29+
return *this;
30+
}
31+
constexpr TrackCopyMove& operator=(TrackCopyMove&& other) noexcept {
32+
++move_count;
33+
++other.move_count;
34+
return *this;
35+
}
36+
constexpr bool operator==(const TrackCopyMove&) const { return true; }
37+
constexpr bool operator<(const TrackCopyMove&) const { return false; }
38+
};
39+
40+
#endif // TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H

0 commit comments

Comments
 (0)