| 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 | // mapped_type& operator[](const key_type& k); |
| 14 | |
| 15 | #include <cassert> |
| 16 | #include <deque> |
| 17 | #include <flat_map> |
| 18 | #include <functional> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "MinSequenceContainer.h" |
| 22 | #include "../helpers.h" |
| 23 | #include "min_allocator.h" |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | // Constraints: is_constructible_v<mapped_type> is true. |
| 27 | template <class M, class Input> |
| 28 | concept CanIndex = requires(M m, Input k) { m[k]; }; |
| 29 | |
| 30 | static_assert(CanIndex<std::flat_map<int, double>, const int&>); |
| 31 | static_assert(!CanIndex<std::flat_map<int, NoDefaultCtr>, const int&>); |
| 32 | |
| 33 | template <class KeyContainer, class ValueContainer> |
| 34 | void test() { |
| 35 | using P = std::pair<int, double>; |
| 36 | P ar[] = { |
| 37 | P(1, 1.5), |
| 38 | P(2, 2.5), |
| 39 | P(3, 3.5), |
| 40 | P(4, 4.5), |
| 41 | P(5, 5.5), |
| 42 | P(7, 7.5), |
| 43 | P(8, 8.5), |
| 44 | }; |
| 45 | const int one = 1; |
| 46 | std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer> m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 47 | ASSERT_SAME_TYPE(decltype(m[one]), double&); |
| 48 | assert(m.size() == 7); |
| 49 | assert(m[one] == 1.5); |
| 50 | assert(m.size() == 7); |
| 51 | m[1] = -1.5; |
| 52 | assert(m[1] == -1.5); |
| 53 | assert(m.size() == 7); |
| 54 | assert(m[6] == 0); |
| 55 | assert(m.size() == 8); |
| 56 | m[6] = 6.5; |
| 57 | assert(m[6] == 6.5); |
| 58 | assert(m.size() == 8); |
| 59 | } |
| 60 | |
| 61 | int main(int, char**) { |
| 62 | test<std::vector<int>, std::vector<double>>(); |
| 63 | test<std::deque<int>, std::vector<double>>(); |
| 64 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 65 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 66 | |
| 67 | { |
| 68 | auto index_func = [](auto& m, auto key_arg, auto value_arg) { |
| 69 | using FlatMap = std::decay_t<decltype(m)>; |
| 70 | const typename FlatMap::key_type key = key_arg; |
| 71 | const typename FlatMap::mapped_type value = value_arg; |
| 72 | m[key] = value; |
| 73 | }; |
| 74 | test_emplace_exception_guarantee(emplace_function&: index_func); |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |