1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // XFAIL: libcpp-no-exceptions 11 // <map> 12 13 // class map 14 15 // mapped_type& at(const key_type& k); 16 // const mapped_type& at(const key_type& k) const; 17 18 #include <map> 19 #include <cassert> 20 21 #include "min_allocator.h" 22 23 int main() 24 { 25 { 26 typedef std::pair<const int, double> V; 27 V ar[] = 28 { 29 V(1, 1.5), 30 V(2, 2.5), 31 V(3, 3.5), 32 V(4, 4.5), 33 V(5, 5.5), 34 V(7, 7.5), 35 V(8, 8.5), 36 }; 37 std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 38 assert(m.size() == 7); 39 assert(m.at(1) == 1.5); 40 m.at(1) = -1.5; 41 assert(m.at(1) == -1.5); 42 assert(m.at(2) == 2.5); 43 assert(m.at(3) == 3.5); 44 assert(m.at(4) == 4.5); 45 assert(m.at(5) == 5.5); 46 try 47 { 48 m.at(6); 49 assert(false); 50 } 51 catch (std::out_of_range&) 52 { 53 } 54 assert(m.at(7) == 7.5); 55 assert(m.at(8) == 8.5); 56 assert(m.size() == 7); 57 } 58 { 59 typedef std::pair<const int, double> V; 60 V ar[] = 61 { 62 V(1, 1.5), 63 V(2, 2.5), 64 V(3, 3.5), 65 V(4, 4.5), 66 V(5, 5.5), 67 V(7, 7.5), 68 V(8, 8.5), 69 }; 70 const std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 71 assert(m.size() == 7); 72 assert(m.at(1) == 1.5); 73 assert(m.at(2) == 2.5); 74 assert(m.at(3) == 3.5); 75 assert(m.at(4) == 4.5); 76 assert(m.at(5) == 5.5); 77 try 78 { 79 m.at(6); 80 assert(false); 81 } 82 catch (std::out_of_range&) 83 { 84 } 85 assert(m.at(7) == 7.5); 86 assert(m.at(8) == 8.5); 87 assert(m.size() == 7); 88 } 89 #if __cplusplus >= 201103L 90 { 91 typedef std::pair<const int, double> V; 92 V ar[] = 93 { 94 V(1, 1.5), 95 V(2, 2.5), 96 V(3, 3.5), 97 V(4, 4.5), 98 V(5, 5.5), 99 V(7, 7.5), 100 V(8, 8.5), 101 }; 102 std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 103 assert(m.size() == 7); 104 assert(m.at(1) == 1.5); 105 m.at(1) = -1.5; 106 assert(m.at(1) == -1.5); 107 assert(m.at(2) == 2.5); 108 assert(m.at(3) == 3.5); 109 assert(m.at(4) == 4.5); 110 assert(m.at(5) == 5.5); 111 try 112 { 113 m.at(6); 114 assert(false); 115 } 116 catch (std::out_of_range&) 117 { 118 } 119 assert(m.at(7) == 7.5); 120 assert(m.at(8) == 8.5); 121 assert(m.size() == 7); 122 } 123 { 124 typedef std::pair<const int, double> V; 125 V ar[] = 126 { 127 V(1, 1.5), 128 V(2, 2.5), 129 V(3, 3.5), 130 V(4, 4.5), 131 V(5, 5.5), 132 V(7, 7.5), 133 V(8, 8.5), 134 }; 135 const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 136 assert(m.size() == 7); 137 assert(m.at(1) == 1.5); 138 assert(m.at(2) == 2.5); 139 assert(m.at(3) == 3.5); 140 assert(m.at(4) == 4.5); 141 assert(m.at(5) == 5.5); 142 try 143 { 144 m.at(6); 145 assert(false); 146 } 147 catch (std::out_of_range&) 148 { 149 } 150 assert(m.at(7) == 7.5); 151 assert(m.at(8) == 8.5); 152 assert(m.size() == 7); 153 } 154 #endif 155 } 156