| 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 |
| 10 | |
| 11 | // <map> |
| 12 | |
| 13 | // class map |
| 14 | |
| 15 | // mapped_type& operator[](key_type&& k); |
| 16 | |
| 17 | #include <map> |
| 18 | #include <cassert> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | #include "count_new.h" |
| 22 | #include "MoveOnly.h" |
| 23 | #include "min_allocator.h" |
| 24 | #include "container_test_types.h" |
| 25 | |
| 26 | int main(int, char**) { |
| 27 | { |
| 28 | std::map<MoveOnly, double> m; |
| 29 | assert(m.size() == 0); |
| 30 | assert(m[1] == 0.0); |
| 31 | assert(m.size() == 1); |
| 32 | m[1] = -1.5; |
| 33 | assert(m[1] == -1.5); |
| 34 | assert(m.size() == 1); |
| 35 | assert(m[6] == 0); |
| 36 | assert(m.size() == 2); |
| 37 | m[6] = 6.5; |
| 38 | assert(m[6] == 6.5); |
| 39 | assert(m.size() == 2); |
| 40 | } |
| 41 | { |
| 42 | typedef std::pair<const MoveOnly, double> V; |
| 43 | std::map<MoveOnly, double, std::less<MoveOnly>, min_allocator<V>> m; |
| 44 | assert(m.size() == 0); |
| 45 | assert(m[1] == 0.0); |
| 46 | assert(m.size() == 1); |
| 47 | m[1] = -1.5; |
| 48 | assert(m[1] == -1.5); |
| 49 | assert(m.size() == 1); |
| 50 | assert(m[6] == 0); |
| 51 | assert(m.size() == 2); |
| 52 | m[6] = 6.5; |
| 53 | assert(m[6] == 6.5); |
| 54 | assert(m.size() == 2); |
| 55 | } |
| 56 | { |
| 57 | // Use "container_test_types.h" to check what arguments get passed |
| 58 | // to the allocator for operator[] |
| 59 | using Container = TCT::map<>; |
| 60 | using Key = Container::key_type; |
| 61 | using MappedType = Container::mapped_type; |
| 62 | ConstructController* cc = getConstructController(); |
| 63 | cc->reset(); |
| 64 | { |
| 65 | Container c; |
| 66 | Key k(1); |
| 67 | cc->expect<std::piecewise_construct_t const&, std::tuple<Key&&>&&, std::tuple<>&&>(); |
| 68 | MappedType& mref = c[std::move(k)]; |
| 69 | assert(!cc->unchecked()); |
| 70 | { |
| 71 | Key k2(1); |
| 72 | DisableAllocationGuard g; |
| 73 | MappedType& mref2 = c[std::move(k2)]; |
| 74 | assert(&mref == &mref2); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |