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, c++11, c++14, c++17, c++20
10
11// <flat_map>
12
13// template<class K> mapped_type& operator[](K&& x);
14
15#include <cassert>
16#include <deque>
17#include <flat_map>
18#include <functional>
19#include <vector>
20
21#include "MinSequenceContainer.h"
22#include "../helpers.h"
23#include "test_macros.h"
24#include "min_allocator.h"
25
26// Constraints:
27// The qualified-id Compare::is_transparent is valid and denotes a type.
28// is_constructible_v<key_type, K> is true.
29// is_constructible_v<mapped_type, Args...> is true.
30// is_convertible_v<K&&, const_iterator> and is_convertible_v<K&&, iterator> are both false
31template <class M, class Input>
32concept CanIndex = requires(M m, Input k) { m[k]; };
33using TransparentMap = std::flat_map<int, double, TransparentComparator>;
34using NonTransparentMap = std::flat_map<int, double, NonTransparentComparator>;
35using TransparentNoDefaultCtrValueMap = std::flat_map<int, NoDefaultCtr, TransparentComparator>;
36
37static_assert(CanIndex<TransparentMap, ConvertibleTransparent<int>>);
38static_assert(!CanIndex<const TransparentMap, ConvertibleTransparent<int>>);
39
40static_assert(!CanIndex<NonTransparentMap, NonConvertibleTransparent<int>>);
41static_assert(!CanIndex<const NonTransparentMap, NonConvertibleTransparent<int>>);
42
43static_assert(!CanIndex<TransparentMap, NonConvertibleTransparent<int>>);
44static_assert(!CanIndex<const TransparentMap, NonConvertibleTransparent<int>>);
45
46static_assert(!CanIndex<TransparentNoDefaultCtrValueMap, ConvertibleTransparent<int>>);
47static_assert(!CanIndex<const TransparentNoDefaultCtrValueMap, ConvertibleTransparent<int>>);
48
49static_assert(!CanIndex<TransparentMap, TransparentMap::iterator>);
50static_assert(!CanIndex<TransparentMap, TransparentMap::const_iterator>);
51
52template <class KeyContainer, class ValueContainer>
53void test() {
54 using P = std::pair<int, double>;
55 P ar[] = {
56 P(1, 1.5),
57 P(2, 2.5),
58 P(3, 3.5),
59 P(4, 4.5),
60 P(5, 5.5),
61 P(7, 7.5),
62 P(8, 8.5),
63 };
64 const ConvertibleTransparent<int> one{.t: 1};
65 const ConvertibleTransparent<int> six{.t: 6};
66 {
67 std::flat_map<int, double, TransparentComparator, KeyContainer, ValueContainer> m(
68 ar, ar + sizeof(ar) / sizeof(ar[0]));
69 ASSERT_SAME_TYPE(decltype(m[one]), double&);
70 assert(m.size() == 7);
71 assert(m[one] == 1.5);
72 assert(m.size() == 7);
73 m[one] = -1.5;
74 assert(m[one] == -1.5);
75 assert(m.size() == 7);
76 assert(m[six] == 0);
77 assert(m.size() == 8);
78 m[six] = 6.5;
79 assert(m[six] == 6.5);
80 assert(m.size() == 8);
81 }
82}
83
84int main(int, char**) {
85 test<std::vector<int>, std::vector<double>>();
86 test<std::deque<int>, std::vector<double>>();
87 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();
88 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
89 {
90 bool transparent_used = false;
91 TransparentComparator c(transparent_used);
92 std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);
93 assert(!transparent_used);
94 m[ConvertibleTransparent<int>{3}];
95 assert(transparent_used);
96 }
97 {
98 // LWG4239 std::string and C string literal
99 using M = std::flat_map<std::string, int, std::less<>>;
100 M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};
101 int& x = m["alpha"];
102 assert(x == 1);
103 }
104 {
105 auto index_func = [](auto& m, auto key_arg, auto value_arg) {
106 using FlatMap = std::decay_t<decltype(m)>;
107 using Key = typename FlatMap::key_type;
108 const typename FlatMap::mapped_type value = value_arg;
109 m[ConvertibleTransparent<Key>{key_arg}] = value;
110 };
111 test_emplace_exception_guarantee(emplace_function&: index_func);
112 }
113 return 0;
114}
115

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_transparent.pass.cpp