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_multimap& operator=(flat_multimap&&);
14// Preserves the class invariant for the moved-from flat_multimap.
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 {
57 const std::pair<int, int> expected[] = {{1, 1}, {1, 2}, {3, 3}, {3, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}};
58 using M = std::flat_multimap<MoveNegates, int, std::less<MoveNegates>, std::vector<MoveNegates>>;
59 M m = M(expected, expected + 8);
60 M m2 = M(expected, expected + 3);
61
62 m2 = std::move(m);
63
64 assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
65 LIBCPP_ASSERT(m.empty());
66 check_invariant(m);
67 m.insert({1, 1});
68 m.insert({2, 2});
69 assert(m.contains(1));
70 assert(m.find(2) != m.end());
71 }
72 {
73 const std::pair<int, int> expected[] = {{1, 1}, {1, 2}, {3, 3}, {4, 4}, {5, 5}, {5, 6}, {7, 7}, {8, 8}};
74 using M = std::flat_multimap<MoveClears, int, std::less<MoveClears>, std::vector<MoveClears>>;
75 M m = M(expected, expected + 8);
76 M m2 = M(expected, expected + 3);
77
78 m2 = std::move(m);
79
80 assert(std::equal(m2.begin(), m2.end(), expected, expected + 8));
81 LIBCPP_ASSERT(m.empty());
82 check_invariant(m);
83 m.insert({1, 1});
84 m.insert({2, 2});
85 assert(m.contains(1));
86 assert(m.find(2) != m.end());
87 }
88 {
89 // moved-from object maintains invariant if one of underlying container does not clear after move
90 using M = std::flat_multimap<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>;
91 M m1 = M({1, 1, 3}, {1, 2, 3});
92 M m2 = M({1, 1}, {1, 2});
93 m2 = std::move(m1);
94 assert(m2.size() == 3);
95 check_invariant(m1);
96 LIBCPP_ASSERT(m1.empty());
97 LIBCPP_ASSERT(m1.keys().size() == 0);
98 LIBCPP_ASSERT(m1.values().size() == 0);
99 }
100 return 0;
101}
102

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