Skip to content

Commit 74dae89

Browse files
committed
[libc++] add test for flat_set::insert not creating temporaries
1 parent 2085119 commit 74dae89

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
19+
#include "../flat_helpers.h"
20+
#include "test_macros.h"
21+
22+
bool test() {
23+
using M = std::flat_multiset<TrackCopyMove>;
24+
{
25+
M m;
26+
TrackCopyMove t;
27+
m.insert(t);
28+
assert(m.begin()->copy_count == 1);
29+
assert(m.begin()->move_count == 0);
30+
}
31+
{
32+
M m;
33+
TrackCopyMove t;
34+
m.emplace(t);
35+
assert(m.begin()->copy_count == 1);
36+
assert(m.begin()->move_count == 0);
37+
}
38+
39+
return true;
40+
}
41+
42+
int main(int, char**) {
43+
test();
44+
45+
return 0;
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
19+
#include "../flat_helpers.h"
20+
#include "test_macros.h"
21+
22+
bool test() {
23+
using M = std::flat_set<TrackCopyMove>;
24+
{
25+
M m;
26+
TrackCopyMove t;
27+
m.insert(t);
28+
assert(m.begin()->copy_count == 1);
29+
assert(m.begin()->move_count == 0);
30+
}
31+
{
32+
M m;
33+
TrackCopyMove t;
34+
m.emplace(t);
35+
assert(m.begin()->copy_count == 1);
36+
assert(m.begin()->move_count == 0);
37+
}
38+
39+
return true;
40+
}
41+
42+
int main(int, char**) {
43+
test();
44+
45+
return 0;
46+
}
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)