xref: /llvm-project/libcxx/test/std/containers/associative/map/map.access/index_rv_key.pass.cpp (revision 31cbe0f240f660f15602c96b787c58a26f17e179)
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 
main(int,char **)26 int main(int, char**)
27 {
28     {
29     std::map<MoveOnly, double> m;
30     assert(m.size() == 0);
31     assert(m[1] == 0.0);
32     assert(m.size() == 1);
33     m[1] = -1.5;
34     assert(m[1] == -1.5);
35     assert(m.size() == 1);
36     assert(m[6] == 0);
37     assert(m.size() == 2);
38     m[6] = 6.5;
39     assert(m[6] == 6.5);
40     assert(m.size() == 2);
41     }
42     {
43     typedef std::pair<const MoveOnly, double> V;
44     std::map<MoveOnly, double, std::less<MoveOnly>, min_allocator<V>> m;
45     assert(m.size() == 0);
46     assert(m[1] == 0.0);
47     assert(m.size() == 1);
48     m[1] = -1.5;
49     assert(m[1] == -1.5);
50     assert(m.size() == 1);
51     assert(m[6] == 0);
52     assert(m.size() == 2);
53     m[6] = 6.5;
54     assert(m[6] == 6.5);
55     assert(m.size() == 2);
56     }
57     {
58         // Use "container_test_types.h" to check what arguments get passed
59         // to the allocator for operator[]
60         using Container = TCT::map<>;
61         using Key = Container::key_type;
62         using MappedType = Container::mapped_type;
63         ConstructController* cc = getConstructController();
64         cc->reset();
65         {
66             Container c;
67             Key k(1);
68             cc->expect<std::piecewise_construct_t const&, std::tuple<Key &&>&&, std::tuple<>&&>();
69             MappedType& mref = c[std::move(k)];
70             assert(!cc->unchecked());
71             {
72                 Key k2(1);
73                 DisableAllocationGuard g;
74                 MappedType& mref2 = c[std::move(k2)];
75                 assert(&mref == &mref2);
76             }
77         }
78     }
79 
80   return 0;
81 }
82