| 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 | // mapped_type& at(const key_type& k); |
| 14 | // const mapped_type& at(const key_type& k) const; |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <map> |
| 18 | #include <stdexcept> |
| 19 | |
| 20 | #include "min_allocator.h" |
| 21 | #include "test_macros.h" |
| 22 | |
| 23 | int main(int, char**) { |
| 24 | { |
| 25 | typedef std::pair<const int, double> V; |
| 26 | V ar[] = { |
| 27 | V(1, 1.5), |
| 28 | V(2, 2.5), |
| 29 | V(3, 3.5), |
| 30 | V(4, 4.5), |
| 31 | V(5, 5.5), |
| 32 | V(7, 7.5), |
| 33 | V(8, 8.5), |
| 34 | }; |
| 35 | std::map<int, double> m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 36 | assert(m.size() == 7); |
| 37 | assert(m.at(1) == 1.5); |
| 38 | m.at(k: 1) = -1.5; |
| 39 | assert(m.at(1) == -1.5); |
| 40 | assert(m.at(2) == 2.5); |
| 41 | assert(m.at(3) == 3.5); |
| 42 | assert(m.at(4) == 4.5); |
| 43 | assert(m.at(5) == 5.5); |
| 44 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 45 | try { |
| 46 | TEST_IGNORE_NODISCARD m.at(6); |
| 47 | assert(false); |
| 48 | } catch (std::out_of_range&) { |
| 49 | } |
| 50 | #endif |
| 51 | assert(m.at(7) == 7.5); |
| 52 | assert(m.at(8) == 8.5); |
| 53 | assert(m.size() == 7); |
| 54 | } |
| 55 | { |
| 56 | typedef std::pair<const int, double> V; |
| 57 | V ar[] = { |
| 58 | V(1, 1.5), |
| 59 | V(2, 2.5), |
| 60 | V(3, 3.5), |
| 61 | V(4, 4.5), |
| 62 | V(5, 5.5), |
| 63 | V(7, 7.5), |
| 64 | V(8, 8.5), |
| 65 | }; |
| 66 | const std::map<int, double> m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 67 | assert(m.size() == 7); |
| 68 | assert(m.at(1) == 1.5); |
| 69 | assert(m.at(2) == 2.5); |
| 70 | assert(m.at(3) == 3.5); |
| 71 | assert(m.at(4) == 4.5); |
| 72 | assert(m.at(5) == 5.5); |
| 73 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 74 | try { |
| 75 | TEST_IGNORE_NODISCARD m.at(6); |
| 76 | assert(false); |
| 77 | } catch (std::out_of_range&) { |
| 78 | } |
| 79 | #endif |
| 80 | assert(m.at(7) == 7.5); |
| 81 | assert(m.at(8) == 8.5); |
| 82 | assert(m.size() == 7); |
| 83 | } |
| 84 | #if TEST_STD_VER >= 11 |
| 85 | { |
| 86 | typedef std::pair<const int, double> V; |
| 87 | V ar[] = { |
| 88 | V(1, 1.5), |
| 89 | V(2, 2.5), |
| 90 | V(3, 3.5), |
| 91 | V(4, 4.5), |
| 92 | V(5, 5.5), |
| 93 | V(7, 7.5), |
| 94 | V(8, 8.5), |
| 95 | }; |
| 96 | std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 97 | assert(m.size() == 7); |
| 98 | assert(m.at(1) == 1.5); |
| 99 | m.at(1) = -1.5; |
| 100 | assert(m.at(1) == -1.5); |
| 101 | assert(m.at(2) == 2.5); |
| 102 | assert(m.at(3) == 3.5); |
| 103 | assert(m.at(4) == 4.5); |
| 104 | assert(m.at(5) == 5.5); |
| 105 | # ifndef TEST_HAS_NO_EXCEPTIONS |
| 106 | try { |
| 107 | TEST_IGNORE_NODISCARD m.at(6); |
| 108 | assert(false); |
| 109 | } catch (std::out_of_range&) { |
| 110 | } |
| 111 | # endif |
| 112 | assert(m.at(7) == 7.5); |
| 113 | assert(m.at(8) == 8.5); |
| 114 | assert(m.size() == 7); |
| 115 | } |
| 116 | { |
| 117 | typedef std::pair<const int, double> V; |
| 118 | V ar[] = { |
| 119 | V(1, 1.5), |
| 120 | V(2, 2.5), |
| 121 | V(3, 3.5), |
| 122 | V(4, 4.5), |
| 123 | V(5, 5.5), |
| 124 | V(7, 7.5), |
| 125 | V(8, 8.5), |
| 126 | }; |
| 127 | const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 128 | assert(m.size() == 7); |
| 129 | assert(m.at(1) == 1.5); |
| 130 | assert(m.at(2) == 2.5); |
| 131 | assert(m.at(3) == 3.5); |
| 132 | assert(m.at(4) == 4.5); |
| 133 | assert(m.at(5) == 5.5); |
| 134 | # ifndef TEST_HAS_NO_EXCEPTIONS |
| 135 | try { |
| 136 | TEST_IGNORE_NODISCARD m.at(6); |
| 137 | assert(false); |
| 138 | } catch (std::out_of_range&) { |
| 139 | } |
| 140 | # endif |
| 141 | assert(m.at(7) == 7.5); |
| 142 | assert(m.at(8) == 8.5); |
| 143 | assert(m.size() == 7); |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |