| 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(initializer_list<value_type> il, const key_compare& comp = key_compare()); |
| 14 | // template<class Alloc> |
| 15 | // flat_map(initializer_list<value_type> il, const Alloc& a); |
| 16 | // template<class Alloc> |
| 17 | // flat_map(initializer_list<value_type> il, const key_compare& comp, const Alloc& a); |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <deque> |
| 21 | #include <flat_map> |
| 22 | #include <functional> |
| 23 | #include <type_traits> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "test_macros.h" |
| 27 | #include "min_allocator.h" |
| 28 | #include "test_allocator.h" |
| 29 | |
| 30 | #include "../../../test_compare.h" |
| 31 | |
| 32 | struct DefaultCtableComp { |
| 33 | explicit DefaultCtableComp() { default_constructed_ = true; } |
| 34 | bool operator()(int, int) const { return false; } |
| 35 | bool default_constructed_ = false; |
| 36 | }; |
| 37 | |
| 38 | int main(int, char**) { |
| 39 | { |
| 40 | // The constructors in this subclause shall not participate in overload |
| 41 | // resolution unless uses_allocator_v<key_container_type, Alloc> is true |
| 42 | // and uses_allocator_v<mapped_container_type, Alloc> is true. |
| 43 | |
| 44 | using C = test_less<int>; |
| 45 | using A1 = test_allocator<int>; |
| 46 | using A2 = other_allocator<int>; |
| 47 | using V1 = std::vector<int, A1>; |
| 48 | using V2 = std::vector<int, A2>; |
| 49 | using M1 = std::flat_map<int, int, C, V1, V1>; |
| 50 | using M2 = std::flat_map<int, int, C, V1, V2>; |
| 51 | using M3 = std::flat_map<int, int, C, V2, V1>; |
| 52 | using IL = std::initializer_list<std::pair<int, int>>; |
| 53 | static_assert(std::is_constructible_v<M1, IL, const A1&>); |
| 54 | static_assert(!std::is_constructible_v<M1, IL, const A2&>); |
| 55 | static_assert(!std::is_constructible_v<M2, IL, const A2&>); |
| 56 | static_assert(!std::is_constructible_v<M3, IL, const A2&>); |
| 57 | |
| 58 | static_assert(std::is_constructible_v<M1, IL, const C&, const A1&>); |
| 59 | static_assert(!std::is_constructible_v<M1, IL, const C&, const A2&>); |
| 60 | static_assert(!std::is_constructible_v<M2, IL, const C&, const A2&>); |
| 61 | static_assert(!std::is_constructible_v<M3, IL, const C&, const A2&>); |
| 62 | } |
| 63 | |
| 64 | { |
| 65 | // initializer_list<value_type> needs to match exactly |
| 66 | using M = std::flat_map<int, short>; |
| 67 | using C = typename M::key_compare; |
| 68 | static_assert(std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>>); |
| 69 | static_assert(std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>, C>); |
| 70 | static_assert(std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>, C, std::allocator<int>>); |
| 71 | static_assert(std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>, std::allocator<int>>); |
| 72 | static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>>); |
| 73 | static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>, C>); |
| 74 | static_assert( |
| 75 | !std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>, C, std::allocator<int>>); |
| 76 | static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>, std::allocator<int>>); |
| 77 | static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>>); |
| 78 | static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>, C>); |
| 79 | static_assert( |
| 80 | !std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>, C, std::allocator<int>>); |
| 81 | static_assert( |
| 82 | !std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>, std::allocator<int>>); |
| 83 | } |
| 84 | |
| 85 | std::pair<int, short> expected[] = {{1, 1}, {2, 2}, {3, 3}, {5, 2}}; |
| 86 | { |
| 87 | // flat_map(initializer_list<value_type>); |
| 88 | using M = std::flat_map<int, short>; |
| 89 | std::initializer_list<std::pair<int, short>> il = {{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}; |
| 90 | M m(il); |
| 91 | assert(std::equal(m.begin(), m.end(), expected, expected + 4)); |
| 92 | } |
| 93 | { |
| 94 | // flat_map(initializer_list<value_type>); |
| 95 | // explicit(false) |
| 96 | using M = std::flat_map<int, short>; |
| 97 | M m = {{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}; |
| 98 | assert(std::equal(m.begin(), m.end(), expected, expected + 4)); |
| 99 | } |
| 100 | { |
| 101 | // flat_map(initializer_list<value_type>); |
| 102 | using M = std::flat_map<int, short, std::greater<int>, std::deque<int, min_allocator<int>>>; |
| 103 | M m = {{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}; |
| 104 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 105 | } |
| 106 | { |
| 107 | using A = explicit_allocator<int>; |
| 108 | { |
| 109 | // flat_map(initializer_list<value_type>); |
| 110 | // different comparator |
| 111 | using M = std::flat_map<int, int, DefaultCtableComp, std::vector<int, A>, std::deque<int, A>>; |
| 112 | M m = {{1, 1}, {2, 2}, {3, 3}}; |
| 113 | assert(m.size() == 1); |
| 114 | assert(m.begin()->first == m.begin()->second); |
| 115 | LIBCPP_ASSERT(*m.begin() == std::make_pair(x: 1, y: 1)); |
| 116 | assert(m.key_comp().default_constructed_); |
| 117 | } |
| 118 | { |
| 119 | // flat_map(initializer_list<value_type>, const Allocator&); |
| 120 | using M = std::flat_map<int, int, std::greater<int>, std::deque<int, A>, std::vector<int, A>>; |
| 121 | A a; |
| 122 | M m({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, a); |
| 123 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 124 | } |
| 125 | } |
| 126 | { |
| 127 | // flat_map(initializer_list<value_type>, const key_compare&); |
| 128 | using C = test_less<int>; |
| 129 | using M = std::flat_map<int, short, C>; |
| 130 | auto m = M({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, C(10)); |
| 131 | assert(std::equal(m.begin(), m.end(), expected, expected + 4)); |
| 132 | assert(m.key_comp() == C(10)); |
| 133 | |
| 134 | // explicit(false) |
| 135 | M m2 = {{{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, C(10)}; |
| 136 | assert(m2 == m); |
| 137 | assert(m2.key_comp() == C(10)); |
| 138 | } |
| 139 | { |
| 140 | // flat_map(initializer_list<value_type>, const key_compare&); |
| 141 | // Sorting uses the comparator that was passed in |
| 142 | using M = std::flat_map<int, short, std::function<bool(int, int)>, std::deque<int, min_allocator<int>>>; |
| 143 | auto m = M({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, std::greater<int>()); |
| 144 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 145 | assert(m.key_comp()(2, 1) == true); |
| 146 | } |
| 147 | { |
| 148 | // flat_map(initializer_list<value_type> il, const key_compare& comp, const Alloc& a); |
| 149 | using A = explicit_allocator<int>; |
| 150 | using M = std::flat_map<int, int, std::greater<int>, std::deque<int, A>, std::vector<int, A>>; |
| 151 | A a; |
| 152 | M m({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, {}, a); |
| 153 | assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |