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