| 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 | // template<class K> mapped_type& at(const K& x); |
| 14 | // template<class K> const mapped_type& at(const K& x) const; |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <deque> |
| 18 | #include <flat_map> |
| 19 | #include <functional> |
| 20 | #include <stdexcept> |
| 21 | |
| 22 | #include "../helpers.h" |
| 23 | #include "min_allocator.h" |
| 24 | #include "MinSequenceContainer.h" |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | // Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. |
| 28 | template <class M> |
| 29 | concept CanAt = requires(M m, Transparent<int> k) { m.at(k); }; |
| 30 | using TransparentMap = std::flat_map<int, double, TransparentComparator>; |
| 31 | using NonTransparentMap = std::flat_map<int, double, NonTransparentComparator>; |
| 32 | static_assert(CanAt<TransparentMap>); |
| 33 | static_assert(CanAt<const TransparentMap>); |
| 34 | static_assert(!CanAt<NonTransparentMap>); |
| 35 | static_assert(!CanAt<const NonTransparentMap>); |
| 36 | |
| 37 | template <class KeyContainer, class ValueContainer> |
| 38 | void test() { |
| 39 | using P = std::pair<int, double>; |
| 40 | P ar[] = { |
| 41 | P(1, 1.5), |
| 42 | P(2, 2.5), |
| 43 | P(3, 3.5), |
| 44 | P(4, 4.5), |
| 45 | P(5, 5.5), |
| 46 | P(7, 7.5), |
| 47 | P(8, 8.5), |
| 48 | }; |
| 49 | const Transparent<int> one{.t: 1}; |
| 50 | { |
| 51 | std::flat_map<int, double, TransparentComparator, KeyContainer, ValueContainer> m( |
| 52 | ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 53 | ASSERT_SAME_TYPE(decltype(m.at(one)), double&); |
| 54 | assert(m.size() == 7); |
| 55 | assert(m.at(one) == 1.5); |
| 56 | m.at(one) = -1.5; |
| 57 | assert(m.at(Transparent<int>{1}) == -1.5); |
| 58 | assert(m.at(Transparent<int>{2}) == 2.5); |
| 59 | assert(m.at(Transparent<int>{3}) == 3.5); |
| 60 | assert(m.at(Transparent<int>{4}) == 4.5); |
| 61 | assert(m.at(Transparent<int>{5}) == 5.5); |
| 62 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 63 | try { |
| 64 | TEST_IGNORE_NODISCARD m.at(Transparent<int>{6}); |
| 65 | assert(false); |
| 66 | } catch (std::out_of_range&) { |
| 67 | } |
| 68 | #endif |
| 69 | assert(m.at(Transparent<int>{7}) == 7.5); |
| 70 | assert(m.at(Transparent<int>{8}) == 8.5); |
| 71 | assert(m.size() == 7); |
| 72 | } |
| 73 | { |
| 74 | const std::flat_map<int, double, TransparentComparator, KeyContainer, ValueContainer> m( |
| 75 | ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 76 | ASSERT_SAME_TYPE(decltype(m.at(one)), const double&); |
| 77 | assert(m.size() == 7); |
| 78 | assert(m.at(Transparent<int>{1}) == 1.5); |
| 79 | assert(m.at(Transparent<int>{2}) == 2.5); |
| 80 | assert(m.at(Transparent<int>{3}) == 3.5); |
| 81 | assert(m.at(Transparent<int>{4}) == 4.5); |
| 82 | assert(m.at(Transparent<int>{5}) == 5.5); |
| 83 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 84 | try { |
| 85 | TEST_IGNORE_NODISCARD m.at(Transparent<int>{6}); |
| 86 | assert(false); |
| 87 | } catch (std::out_of_range&) { |
| 88 | } |
| 89 | #endif |
| 90 | assert(m.at(Transparent<int>{7}) == 7.5); |
| 91 | assert(m.at(Transparent<int>{8}) == 8.5); |
| 92 | assert(m.size() == 7); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int main(int, char**) { |
| 97 | test<std::vector<int>, std::vector<double>>(); |
| 98 | test<std::deque<int>, std::vector<double>>(); |
| 99 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 100 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 101 | { |
| 102 | bool transparent_used = false; |
| 103 | TransparentComparator c(transparent_used); |
| 104 | std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); |
| 105 | assert(!transparent_used); |
| 106 | TEST_IGNORE_NODISCARD m.at(Transparent<int>{3}); |
| 107 | assert(transparent_used); |
| 108 | } |
| 109 | { |
| 110 | // LWG4239 std::string and C string literal |
| 111 | using M = std::flat_map<std::string, int, std::less<>>; |
| 112 | M m{{"alpha" , 1}, {"beta" , 2}, {"epsilon" , 1}, {"eta" , 3}, {"gamma" , 3}}; |
| 113 | int& x = m.at("alpha" ); |
| 114 | assert(x == 1); |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |