| 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 | // <map> |
| 10 | |
| 11 | // class map |
| 12 | |
| 13 | // iterator find(const key_type& k); |
| 14 | // const_iterator find(const key_type& k) const; |
| 15 | |
| 16 | #include <map> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "min_allocator.h" |
| 21 | #include "private_constructor.h" |
| 22 | #include "is_transparent.h" |
| 23 | |
| 24 | int main(int, char**) { |
| 25 | { |
| 26 | typedef std::pair<const int, double> V; |
| 27 | typedef std::map<int, double> M; |
| 28 | { |
| 29 | typedef M::iterator R; |
| 30 | V ar[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; |
| 31 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 32 | R r = m.find(x: 5); |
| 33 | assert(r == m.begin()); |
| 34 | r = m.find(x: 6); |
| 35 | assert(r == std::next(m.begin())); |
| 36 | r = m.find(x: 7); |
| 37 | assert(r == std::next(m.begin(), 2)); |
| 38 | r = m.find(x: 8); |
| 39 | assert(r == std::next(m.begin(), 3)); |
| 40 | r = m.find(x: 9); |
| 41 | assert(r == std::next(m.begin(), 4)); |
| 42 | r = m.find(x: 10); |
| 43 | assert(r == std::next(m.begin(), 5)); |
| 44 | r = m.find(x: 11); |
| 45 | assert(r == std::next(m.begin(), 6)); |
| 46 | r = m.find(x: 12); |
| 47 | assert(r == std::next(m.begin(), 7)); |
| 48 | r = m.find(x: 4); |
| 49 | assert(r == std::next(m.begin(), 8)); |
| 50 | } |
| 51 | { |
| 52 | typedef M::const_iterator R; |
| 53 | V ar[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; |
| 54 | const M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 55 | R r = m.find(x: 5); |
| 56 | assert(r == m.begin()); |
| 57 | r = m.find(x: 6); |
| 58 | assert(r == std::next(m.begin())); |
| 59 | r = m.find(x: 7); |
| 60 | assert(r == std::next(m.begin(), 2)); |
| 61 | r = m.find(x: 8); |
| 62 | assert(r == std::next(m.begin(), 3)); |
| 63 | r = m.find(x: 9); |
| 64 | assert(r == std::next(m.begin(), 4)); |
| 65 | r = m.find(x: 10); |
| 66 | assert(r == std::next(m.begin(), 5)); |
| 67 | r = m.find(x: 11); |
| 68 | assert(r == std::next(m.begin(), 6)); |
| 69 | r = m.find(x: 12); |
| 70 | assert(r == std::next(m.begin(), 7)); |
| 71 | r = m.find(x: 4); |
| 72 | assert(r == std::next(m.begin(), 8)); |
| 73 | } |
| 74 | } |
| 75 | #if TEST_STD_VER >= 11 |
| 76 | { |
| 77 | typedef std::pair<const int, double> V; |
| 78 | typedef std::map<int, double, std::less<int>, min_allocator<V>> M; |
| 79 | { |
| 80 | typedef M::iterator R; |
| 81 | V ar[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; |
| 82 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 83 | R r = m.find(5); |
| 84 | assert(r == m.begin()); |
| 85 | r = m.find(6); |
| 86 | assert(r == std::next(m.begin())); |
| 87 | r = m.find(7); |
| 88 | assert(r == std::next(m.begin(), 2)); |
| 89 | r = m.find(8); |
| 90 | assert(r == std::next(m.begin(), 3)); |
| 91 | r = m.find(9); |
| 92 | assert(r == std::next(m.begin(), 4)); |
| 93 | r = m.find(10); |
| 94 | assert(r == std::next(m.begin(), 5)); |
| 95 | r = m.find(11); |
| 96 | assert(r == std::next(m.begin(), 6)); |
| 97 | r = m.find(12); |
| 98 | assert(r == std::next(m.begin(), 7)); |
| 99 | r = m.find(4); |
| 100 | assert(r == std::next(m.begin(), 8)); |
| 101 | } |
| 102 | { |
| 103 | typedef M::const_iterator R; |
| 104 | V ar[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; |
| 105 | const M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 106 | R r = m.find(5); |
| 107 | assert(r == m.begin()); |
| 108 | r = m.find(6); |
| 109 | assert(r == std::next(m.begin())); |
| 110 | r = m.find(7); |
| 111 | assert(r == std::next(m.begin(), 2)); |
| 112 | r = m.find(8); |
| 113 | assert(r == std::next(m.begin(), 3)); |
| 114 | r = m.find(9); |
| 115 | assert(r == std::next(m.begin(), 4)); |
| 116 | r = m.find(10); |
| 117 | assert(r == std::next(m.begin(), 5)); |
| 118 | r = m.find(11); |
| 119 | assert(r == std::next(m.begin(), 6)); |
| 120 | r = m.find(12); |
| 121 | assert(r == std::next(m.begin(), 7)); |
| 122 | r = m.find(4); |
| 123 | assert(r == std::next(m.begin(), 8)); |
| 124 | } |
| 125 | } |
| 126 | #endif |
| 127 | #if TEST_STD_VER > 11 |
| 128 | { |
| 129 | typedef std::pair<const int, double> V; |
| 130 | typedef std::map<int, double, std::less<>> M; |
| 131 | typedef M::iterator R; |
| 132 | |
| 133 | V ar[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; |
| 134 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 135 | R r = m.find(5); |
| 136 | assert(r == m.begin()); |
| 137 | r = m.find(6); |
| 138 | assert(r == std::next(m.begin())); |
| 139 | r = m.find(7); |
| 140 | assert(r == std::next(m.begin(), 2)); |
| 141 | r = m.find(8); |
| 142 | assert(r == std::next(m.begin(), 3)); |
| 143 | r = m.find(9); |
| 144 | assert(r == std::next(m.begin(), 4)); |
| 145 | r = m.find(10); |
| 146 | assert(r == std::next(m.begin(), 5)); |
| 147 | r = m.find(11); |
| 148 | assert(r == std::next(m.begin(), 6)); |
| 149 | r = m.find(12); |
| 150 | assert(r == std::next(m.begin(), 7)); |
| 151 | r = m.find(4); |
| 152 | assert(r == std::next(m.begin(), 8)); |
| 153 | |
| 154 | r = m.find(C2Int(5)); |
| 155 | assert(r == m.begin()); |
| 156 | r = m.find(C2Int(6)); |
| 157 | assert(r == std::next(m.begin())); |
| 158 | r = m.find(C2Int(7)); |
| 159 | assert(r == std::next(m.begin(), 2)); |
| 160 | r = m.find(C2Int(8)); |
| 161 | assert(r == std::next(m.begin(), 3)); |
| 162 | r = m.find(C2Int(9)); |
| 163 | assert(r == std::next(m.begin(), 4)); |
| 164 | r = m.find(C2Int(10)); |
| 165 | assert(r == std::next(m.begin(), 5)); |
| 166 | r = m.find(C2Int(11)); |
| 167 | assert(r == std::next(m.begin(), 6)); |
| 168 | r = m.find(C2Int(12)); |
| 169 | assert(r == std::next(m.begin(), 7)); |
| 170 | r = m.find(C2Int(4)); |
| 171 | assert(r == std::next(m.begin(), 8)); |
| 172 | } |
| 173 | |
| 174 | { |
| 175 | typedef PrivateConstructor PC; |
| 176 | typedef std::map<PC, double, std::less<>> M; |
| 177 | typedef M::iterator R; |
| 178 | |
| 179 | M m; |
| 180 | m[PC::make(5)] = 5; |
| 181 | m[PC::make(6)] = 6; |
| 182 | m[PC::make(7)] = 7; |
| 183 | m[PC::make(8)] = 8; |
| 184 | m[PC::make(9)] = 9; |
| 185 | m[PC::make(10)] = 10; |
| 186 | m[PC::make(11)] = 11; |
| 187 | m[PC::make(12)] = 12; |
| 188 | |
| 189 | R r = m.find(5); |
| 190 | assert(r == m.begin()); |
| 191 | r = m.find(6); |
| 192 | assert(r == std::next(m.begin())); |
| 193 | r = m.find(7); |
| 194 | assert(r == std::next(m.begin(), 2)); |
| 195 | r = m.find(8); |
| 196 | assert(r == std::next(m.begin(), 3)); |
| 197 | r = m.find(9); |
| 198 | assert(r == std::next(m.begin(), 4)); |
| 199 | r = m.find(10); |
| 200 | assert(r == std::next(m.begin(), 5)); |
| 201 | r = m.find(11); |
| 202 | assert(r == std::next(m.begin(), 6)); |
| 203 | r = m.find(12); |
| 204 | assert(r == std::next(m.begin(), 7)); |
| 205 | r = m.find(4); |
| 206 | assert(r == std::next(m.begin(), 8)); |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |