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