| 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& at(const key_type& k); |
| 14 | // const mapped_type& at(const key_type& k) const; |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <deque> |
| 18 | #include <flat_map> |
| 19 | #include <functional> |
| 20 | #include <stdexcept> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "MinSequenceContainer.h" |
| 24 | #include "min_allocator.h" |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | template <class KeyContainer, class ValueContainer> |
| 28 | void test() { |
| 29 | using P = std::pair<int, double>; |
| 30 | P ar[] = { |
| 31 | P(1, 1.5), |
| 32 | P(2, 2.5), |
| 33 | P(3, 3.5), |
| 34 | P(4, 4.5), |
| 35 | P(5, 5.5), |
| 36 | P(7, 7.5), |
| 37 | P(8, 8.5), |
| 38 | }; |
| 39 | const int one = 1; |
| 40 | { |
| 41 | std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer> m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 42 | ASSERT_SAME_TYPE(decltype(m.at(one)), double&); |
| 43 | assert(m.size() == 7); |
| 44 | assert(m.at(one) == 1.5); |
| 45 | m.at(1) = -1.5; |
| 46 | assert(m.at(1) == -1.5); |
| 47 | assert(m.at(2) == 2.5); |
| 48 | assert(m.at(3) == 3.5); |
| 49 | assert(m.at(4) == 4.5); |
| 50 | assert(m.at(5) == 5.5); |
| 51 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 52 | try { |
| 53 | TEST_IGNORE_NODISCARD m.at(6); |
| 54 | assert(false); |
| 55 | } catch (std::out_of_range&) { |
| 56 | } |
| 57 | #endif |
| 58 | assert(m.at(7) == 7.5); |
| 59 | assert(m.at(8) == 8.5); |
| 60 | assert(m.size() == 7); |
| 61 | } |
| 62 | { |
| 63 | const std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer> m( |
| 64 | ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 65 | ASSERT_SAME_TYPE(decltype(m.at(one)), const double&); |
| 66 | assert(m.size() == 7); |
| 67 | assert(m.at(one) == 1.5); |
| 68 | assert(m.at(2) == 2.5); |
| 69 | assert(m.at(3) == 3.5); |
| 70 | assert(m.at(4) == 4.5); |
| 71 | assert(m.at(5) == 5.5); |
| 72 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 73 | try { |
| 74 | TEST_IGNORE_NODISCARD m.at(6); |
| 75 | assert(false); |
| 76 | } catch (std::out_of_range&) { |
| 77 | } |
| 78 | #endif |
| 79 | assert(m.at(7) == 7.5); |
| 80 | assert(m.at(8) == 8.5); |
| 81 | assert(m.size() == 7); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | int main(int, char**) { |
| 86 | test<std::vector<int>, std::vector<double>>(); |
| 87 | test<std::deque<int>, std::vector<double>>(); |
| 88 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 89 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |