278 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// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11// <flat_map>12 13// template <class InputIterator>14// flat_multimap(InputIterator first, InputIterator last, const key_compare& comp = key_compare());15// template<class InputIterator, class Allocator>16// flat_multimap(InputIterator first, InputIterator last, const Allocator& a);17// template<class InputIterator, class Allocator>18// flat_multimap(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);19 20#include <algorithm>21#include <deque>22#include <flat_map>23#include <functional>24#include <vector>25#include <ranges>26 27#include "MinSequenceContainer.h"28#include "min_allocator.h"29#include "test_allocator.h"30#include "test_iterators.h"31#include "test_macros.h"32#include "../helpers.h"33#include "../../../test_compare.h"34 35template <class KeyContainer, class ValueContainer>36constexpr void test() {37 using Key = typename KeyContainer::value_type;38 using Value = typename ValueContainer::value_type;39 using P = std::pair<Key, Value>;40 P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}};41 {42 // flat_multimap(InputIterator , InputIterator)43 // cpp17_input_iterator44 using M = std::flat_multimap<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;45 auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9));46 assert(std::ranges::equal(m.keys(), KeyContainer{1, 1, 1, 2, 2, 2, 3, 3, 3}));47 check_possible_values(48 m.values(),49 std::vector<std::vector<Value>>{50 {1, 2, 3},51 {1, 2, 3},52 {1, 2, 3},53 {4, 5, 7},54 {4, 5, 7},55 {4, 5, 7},56 {6, 8, 9},57 {6, 8, 9},58 {6, 8, 9},59 });60 61 // explicit(false)62 M m2 = {cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9)};63 assert(m2 == m);64 }65 {66 // flat_multimap(InputIterator , InputIterator)67 // greater68 using M = std::flat_multimap<Key, Value, std::greater<Key>, KeyContainer, ValueContainer>;69 auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9));70 assert(std::ranges::equal(m.keys(), KeyContainer{3, 3, 3, 2, 2, 2, 1, 1, 1}));71 check_possible_values(72 m.values(),73 std::vector<std::vector<Value>>{74 {6, 8, 9},75 {6, 8, 9},76 {6, 8, 9},77 {4, 5, 7},78 {4, 5, 7},79 {4, 5, 7},80 {1, 2, 3},81 {1, 2, 3},82 {1, 2, 3},83 });84 }85 {86 // flat_multimap(InputIterator , InputIterator)87 // Test when the operands are of array type (also contiguous iterator type)88 using M = std::flat_multimap<Key, Value, std::greater<Key>, KeyContainer, ValueContainer>;89 auto m = M(ar, ar);90 assert(m.empty());91 }92 {93 // flat_multimap(InputIterator , InputIterator, const key_compare&)94 using C = test_less<Key>;95 using M = std::flat_multimap<Key, Value, C, KeyContainer, ValueContainer>;96 auto m = M(ar, ar + 9, C(3));97 assert(std::ranges::equal(m.keys(), KeyContainer{1, 1, 1, 2, 2, 2, 3, 3, 3}));98 check_possible_values(99 m.values(),100 std::vector<std::vector<Value>>{101 {1, 2, 3},102 {1, 2, 3},103 {1, 2, 3},104 {4, 5, 7},105 {4, 5, 7},106 {4, 5, 7},107 {6, 8, 9},108 {6, 8, 9},109 {6, 8, 9},110 });111 assert(m.key_comp() == C(3));112 113 // explicit(false)114 M m2 = {ar, ar + 9, C(3)};115 assert(m2 == m);116 assert(m2.key_comp() == C(3));117 }118}119 120template <template <class...> class KeyContainer, template <class...> class ValueContainer>121constexpr void test_alloc() {122 using P = std::pair<int, short>;123 P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}};124 125 {126 // flat_multimap(InputIterator , InputIterator, const Allocator&)127 using A1 = test_allocator<int>;128 using A2 = test_allocator<short>;129 using M = std::flat_multimap<int, short, std::less<int>, KeyContainer<int, A1>, ValueContainer<short, A2>>;130 auto m = M(ar, ar + 9, A1(5));131 assert(std::ranges::equal(m.keys(), KeyContainer<int, A1>{1, 1, 1, 2, 2, 2, 3, 3, 3}));132 check_possible_values(133 m.values(),134 std::vector<std::vector<short>>{135 {1, 2, 3},136 {1, 2, 3},137 {1, 2, 3},138 {4, 5, 7},139 {4, 5, 7},140 {4, 5, 7},141 {6, 8, 9},142 {6, 8, 9},143 {6, 8, 9},144 });145 assert(m.keys().get_allocator() == A1(5));146 assert(m.values().get_allocator() == A2(5));147 }148 {149 // flat_multimap(InputIterator , InputIterator, const Allocator&)150 // explicit(false)151 using A1 = test_allocator<int>;152 using A2 = test_allocator<short>;153 using M = std::flat_multimap<int, short, std::less<int>, KeyContainer<int, A1>, ValueContainer<short, A2>>;154 M m = {ar, ar + 9, A1(5)}; // implicit ctor155 assert(std::ranges::equal(m.keys(), KeyContainer<int, A1>{1, 1, 1, 2, 2, 2, 3, 3, 3}));156 check_possible_values(157 m.values(),158 std::vector<std::vector<short>>{159 {1, 2, 3},160 {1, 2, 3},161 {1, 2, 3},162 {4, 5, 7},163 {4, 5, 7},164 {4, 5, 7},165 {6, 8, 9},166 {6, 8, 9},167 {6, 8, 9},168 });169 assert(m.keys().get_allocator() == A1(5));170 assert(m.values().get_allocator() == A2(5));171 }172 {173 // flat_multimap(InputIterator , InputIterator, const key_compare&, const Allocator&)174 using C = test_less<int>;175 using A1 = test_allocator<int>;176 using A2 = test_allocator<short>;177 using M = std::flat_multimap<int, short, C, KeyContainer<int, A1>, ValueContainer<short, A2>>;178 auto m = M(ar, ar + 9, C(3), A1(5));179 assert(std::ranges::equal(m.keys(), KeyContainer<int, A1>{1, 1, 1, 2, 2, 2, 3, 3, 3}));180 check_possible_values(181 m.values(),182 std::vector<std::vector<short>>{183 {1, 2, 3},184 {1, 2, 3},185 {1, 2, 3},186 {4, 5, 7},187 {4, 5, 7},188 {4, 5, 7},189 {6, 8, 9},190 {6, 8, 9},191 {6, 8, 9},192 });193 assert(m.key_comp() == C(3));194 assert(m.keys().get_allocator() == A1(5));195 assert(m.values().get_allocator() == A2(5));196 }197 {198 // flat_multimap(InputIterator , InputIterator, const key_compare&, const Allocator&)199 // explicit(false)200 using A1 = test_allocator<int>;201 using A2 = test_allocator<short>;202 using M = std::flat_multimap<int, short, std::less<int>, KeyContainer<int, A1>, ValueContainer<short, A2>>;203 M m = {ar, ar + 9, {}, A2(5)}; // implicit ctor204 assert(std::ranges::equal(m.keys(), KeyContainer<int, A1>{1, 1, 1, 2, 2, 2, 3, 3, 3}));205 check_possible_values(206 m.values(),207 std::vector<std::vector<short>>{208 {1, 2, 3},209 {1, 2, 3},210 {1, 2, 3},211 {4, 5, 7},212 {4, 5, 7},213 {4, 5, 7},214 {6, 8, 9},215 {6, 8, 9},216 {6, 8, 9},217 });218 assert(m.keys().get_allocator() == A1(5));219 assert(m.values().get_allocator() == A2(5));220 }221}222 223constexpr bool test() {224 {225 // The constructors in this subclause shall not participate in overload226 // resolution unless uses_allocator_v<key_container_type, Alloc> is true227 // and uses_allocator_v<mapped_container_type, Alloc> is true.228 229 using C = test_less<int>;230 using A1 = test_allocator<int>;231 using A2 = other_allocator<int>;232 using V1 = std::vector<int, A1>;233 using V2 = std::vector<int, A2>;234 using M1 = std::flat_multimap<int, int, C, V1, V1>;235 using M2 = std::flat_multimap<int, int, C, V1, V2>;236 using M3 = std::flat_multimap<int, int, C, V2, V1>;237 using Iter1 = typename M1::iterator;238 using Iter2 = typename M2::iterator;239 using Iter3 = typename M3::iterator;240 static_assert(std::is_constructible_v<M1, Iter1, Iter1, const A1&>);241 static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const A2&>);242 static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const A2&>);243 static_assert(!std::is_constructible_v<M3, Iter3, Iter3, const A2&>);244 245 static_assert(std::is_constructible_v<M1, Iter1, Iter1, const C&, const A1&>);246 static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const C&, const A2&>);247 static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const C&, const A2&>);248 static_assert(!std::is_constructible_v<M3, Iter3, Iter3, const C&, const A2&>);249 }250 251 test<std::vector<int>, std::vector<int>>();252 test<std::vector<int>, std::vector<double>>();253 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();254 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();255 test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();256 257 test_alloc<std::vector, std::vector>();258 259#ifndef __cpp_lib_constexpr_deque260 if (!TEST_IS_CONSTANT_EVALUATED)261#endif262 {263 test<std::deque<int>, std::vector<double>>();264 test_alloc<std::deque, std::deque>();265 }266 267 return true;268}269 270int main(int, char**) {271 test();272#if TEST_STD_VER >= 26273 static_assert(test());274#endif275 276 return 0;277}278