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_map>
12
13// flat_map& operator=(flat_map&&);
14// Preserves the class invariant for the moved-from flat_map.
15
16#include <algorithm>
17#include <cassert>
18#include <compare>
19#include <flat_map>
20#include <functional>
21#include <utility>
22#include <vector>
23
24#include "../helpers.h"
25#include "test_macros.h"
26
27struct MoveNegates {
28 int value_ = 0;
29 MoveNegates() = default;
30 MoveNegates(int v) : value_(v) {}
31 MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; }
32 MoveNegates& operator=(MoveNegates&& rhs) {
33 value_ = rhs.value_;
34 rhs.value_ = -rhs.value_;
35 return *this;
36 }
37 ~MoveNegates() = default;
38 auto operator<=>(const MoveNegates&) const = default;
39};
40
41struct MoveClears {
42 int value_ = 0;
43 MoveClears() = default;
44 MoveClears(int v) : value_(v) {}
45 MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; }
46 MoveClears& operator=(MoveClears&& rhs) {
47 value_ = rhs.value_;
48 rhs.value_ = 0;
49 return *this;
50 }
51 ~MoveClears() = default;
52 auto operator<=>(const MoveClears&) const = default;
53};
54
55int main(int, char**) {
56 auto value_eq = [](auto&& p, auto&& q) { return p.first == q.first; };
57 {
58 const std::pair<int, int> expected[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}};
59 using M = std::flat_map<MoveNegates, int, std::less<MoveNegates>, std::vector<MoveNegates>>;
60 M m = M(expected, expected + 8);
61 M m2 = M(expected, expected + 3);
62
63 m2 = std::move(m);
64
65 assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
66 LIBCPP_ASSERT(m.empty());
67 assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); // still sorted
68 assert(std::adjacent_find(m.begin(), m.end(), value_eq) == m.end()); // still contains no duplicates
69 m.insert({1, 1});
70 m.insert({2, 2});
71 assert(m.contains(1));
72 assert(m.find(2) != m.end());
73 }
74 {
75 const std::pair<int, int> expected[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}};
76 using M = std::flat_map<MoveClears, int, std::less<MoveClears>, std::vector<MoveClears>>;
77 M m = M(expected, expected + 8);
78 M m2 = M(expected, expected + 3);
79
80 m2 = std::move(m);
81
82 assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
83 LIBCPP_ASSERT(m.empty());
84 assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); // still sorted
85 assert(std::adjacent_find(m.begin(), m.end(), value_eq) == m.end()); // still contains no duplicates
86 m.insert({1, 1});
87 m.insert({2, 2});
88 assert(m.contains(1));
89 assert(m.find(2) != m.end());
90 }
91 {
92 // moved-from object maintains invariant if one of underlying container does not clear after move
93 using M = std::flat_map<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>;
94 M m1 = M({1, 2, 3}, {1, 2, 3});
95 M m2 = M({1, 2}, {1, 2});
96 m2 = std::move(m1);
97 assert(m2.size() == 3);
98 check_invariant(m1);
99 LIBCPP_ASSERT(m1.empty());
100 LIBCPP_ASSERT(m1.keys().size() == 0);
101 LIBCPP_ASSERT(m1.values().size() == 0);
102 }
103 return 0;
104}
105

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_clears.pass.cpp