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