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