llvm-project
144 строки · 3.5 Кб
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 >= 1123#include "container_test_types.h"24#endif25
26int main(int, char**)27{
28{29typedef std::pair<const int, double> V;30V ar[] =31{32V(1, 1.5),33V(2, 2.5),34V(3, 3.5),35V(4, 4.5),36V(5, 5.5),37V(7, 7.5),38V(8, 8.5),39};40std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));41assert(m.size() == 7);42assert(m[1] == 1.5);43assert(m.size() == 7);44m[1] = -1.5;45assert(m[1] == -1.5);46assert(m.size() == 7);47assert(m[6] == 0);48assert(m.size() == 8);49m[6] = 6.5;50assert(m[6] == 6.5);51assert(m.size() == 8);52}53#if TEST_STD_VER >= 1154{55typedef std::pair<const int, double> V;56V ar[] =57{58V(1, 1.5),59V(2, 2.5),60V(3, 3.5),61V(4, 4.5),62V(5, 5.5),63V(7, 7.5),64V(8, 8.5),65};66std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));67assert(m.size() == 7);68assert(m[1] == 1.5);69assert(m.size() == 7);70const int i = 1;71m[i] = -1.5;72assert(m[1] == -1.5);73assert(m.size() == 7);74assert(m[6] == 0);75assert(m.size() == 8);76m[6] = 6.5;77assert(m[6] == 6.5);78assert(m.size() == 8);79}80{81// Use "container_test_types.h" to check what arguments get passed82// to the allocator for operator[]83using Container = TCT::map<>;84using Key = Container::key_type;85using MappedType = Container::mapped_type;86ConstructController* cc = getConstructController();87cc->reset();88{89Container c;90const Key k(1);91cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>();92MappedType& mref = c[k];93assert(!cc->unchecked());94{95DisableAllocationGuard g;96MappedType& mref2 = c[k];97assert(&mref == &mref2);98}99}100{101Container c;102Key k(1);103cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>();104MappedType& mref = c[k];105assert(!cc->unchecked());106{107DisableAllocationGuard g;108MappedType& mref2 = c[k];109assert(&mref == &mref2);110}111}112}113#endif114#if TEST_STD_VER > 11115{116typedef std::pair<const int, double> V;117V ar[] =118{119V(1, 1.5),120V(2, 2.5),121V(3, 3.5),122V(4, 4.5),123V(5, 5.5),124V(7, 7.5),125V(8, 8.5),126};127std::map<int, double, std::less<>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));128
129assert(m.size() == 7);130assert(m[1] == 1.5);131assert(m.size() == 7);132m[1] = -1.5;133assert(m[1] == -1.5);134assert(m.size() == 7);135assert(m[6] == 0);136assert(m.size() == 8);137m[6] = 6.5;138assert(m[6] == 6.5);139assert(m.size() == 8);140}141#endif142
143return 0;144}
145