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 // <map> 11 12 // class map 13 14 // mapped_type& operator[](const key_type& k); 15 16 #include <map> 17 #include <cassert> 18 19 #include "test_macros.h" 20 #include "count_new.hpp" 21 #include "min_allocator.h" 22 #include "private_constructor.hpp" 23 #if TEST_STD_VER >= 11 24 #include "container_test_types.h" 25 #endif 26 27 int main() 28 { 29 { 30 typedef std::pair<const int, double> V; 31 V ar[] = 32 { 33 V(1, 1.5), 34 V(2, 2.5), 35 V(3, 3.5), 36 V(4, 4.5), 37 V(5, 5.5), 38 V(7, 7.5), 39 V(8, 8.5), 40 }; 41 std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 42 assert(m.size() == 7); 43 assert(m[1] == 1.5); 44 assert(m.size() == 7); 45 m[1] = -1.5; 46 assert(m[1] == -1.5); 47 assert(m.size() == 7); 48 assert(m[6] == 0); 49 assert(m.size() == 8); 50 m[6] = 6.5; 51 assert(m[6] == 6.5); 52 assert(m.size() == 8); 53 } 54 #if TEST_STD_VER >= 11 55 { 56 typedef std::pair<const int, double> V; 57 V ar[] = 58 { 59 V(1, 1.5), 60 V(2, 2.5), 61 V(3, 3.5), 62 V(4, 4.5), 63 V(5, 5.5), 64 V(7, 7.5), 65 V(8, 8.5), 66 }; 67 std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 68 assert(m.size() == 7); 69 assert(m[1] == 1.5); 70 assert(m.size() == 7); 71 const int i = 1; 72 m[i] = -1.5; 73 assert(m[1] == -1.5); 74 assert(m.size() == 7); 75 assert(m[6] == 0); 76 assert(m.size() == 8); 77 m[6] = 6.5; 78 assert(m[6] == 6.5); 79 assert(m.size() == 8); 80 } 81 { 82 // Use "container_test_types.h" to check what arguments get passed 83 // to the allocator for operator[] 84 using Container = TCT::map<>; 85 using Key = Container::key_type; 86 using MappedType = Container::mapped_type; 87 using ValueTp = Container::value_type; 88 ConstructController* cc = getConstructController(); 89 cc->reset(); 90 { 91 Container c; 92 const Key k(1); 93 cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>(); 94 MappedType& mref = c[k]; 95 assert(!cc->unchecked()); 96 { 97 DisableAllocationGuard g; 98 MappedType& mref2 = c[k]; 99 assert(&mref == &mref2); 100 } 101 } 102 { 103 Container c; 104 Key k(1); 105 cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>(); 106 MappedType& mref = c[k]; 107 assert(!cc->unchecked()); 108 { 109 DisableAllocationGuard g; 110 MappedType& mref2 = c[k]; 111 assert(&mref == &mref2); 112 } 113 } 114 } 115 #endif 116 #if TEST_STD_VER > 11 117 { 118 typedef std::pair<const int, double> V; 119 V ar[] = 120 { 121 V(1, 1.5), 122 V(2, 2.5), 123 V(3, 3.5), 124 V(4, 4.5), 125 V(5, 5.5), 126 V(7, 7.5), 127 V(8, 8.5), 128 }; 129 std::map<int, double, std::less<>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); 130 131 assert(m.size() == 7); 132 assert(m[1] == 1.5); 133 assert(m.size() == 7); 134 m[1] = -1.5; 135 assert(m[1] == -1.5); 136 assert(m.size() == 7); 137 assert(m[6] == 0); 138 assert(m.size() == 8); 139 m[6] = 6.5; 140 assert(m[6] == 6.5); 141 assert(m.size() == 8); 142 } 143 #endif 144 } 145