134 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// explicit flat_map(const key_compare& comp);14// template <class Alloc>15// flat_map(const key_compare& comp, const Alloc& a);16 17#include <deque>18#include <flat_map>19#include <functional>20#include <type_traits>21#include <vector>22 23#include "MinSequenceContainer.h"24#include "min_allocator.h"25#include "test_macros.h"26#include "../../../test_compare.h"27#include "test_allocator.h"28 29// explicit flat_map(const key_compare& comp);30template <class KeyContainer, class ValueContainer>31constexpr void test_compare() {32 using Key = typename KeyContainer::value_type;33 using Value = typename ValueContainer::value_type;34 {35 // The one-argument ctor is explicit.36 using C = test_less<Key>;37 static_assert(std::is_constructible_v<std::flat_map<Key, Value, C>, C>);38 static_assert(!std::is_convertible_v<C, std::flat_map<Key, Value, C>>);39 40 static_assert(std::is_constructible_v<std::flat_map<Key, Value>, std::less<Key>>);41 static_assert(!std::is_convertible_v<std::less<Key>, std::flat_map<Key, Value>>);42 }43 {44 using C = test_less<Key>;45 auto m = std::flat_map<Key, Value, C>(C(3));46 assert(m.empty());47 assert(m.begin() == m.end());48 assert(m.key_comp() == C(3));49 }50}51 52// template <class Alloc>53// flat_map(const key_compare& comp, const Alloc& a);54template <template <class...> class KeyContainer, template <class...> class ValueContainer>55constexpr void test_compare_alloc() {56 {57 // If an allocator is given, it must be usable by both containers.58 using A = test_allocator<int>;59 using M = std::flat_map<int, int, std::less<>, KeyContainer<int>, ValueContainer<int, A>>;60 static_assert(std::is_constructible_v<M, std::less<>>);61 static_assert(!std::is_constructible_v<M, std::less<>, std::allocator<int>>);62 static_assert(!std::is_constructible_v<M, std::less<>, A>);63 }64 {65 using C = test_less<int>;66 using A1 = test_allocator<int>;67 using A2 = test_allocator<short>;68 auto m = std::flat_map<int, short, C, KeyContainer<int, A1>, ValueContainer<short, A2>>(C(4), A1(5));69 assert(m.empty());70 assert(m.begin() == m.end());71 assert(m.key_comp() == C(4));72 assert(m.keys().get_allocator() == A1(5));73 assert(m.values().get_allocator() == A2(5));74 }75 {76 // explicit(false)77 using C = test_less<int>;78 using A1 = test_allocator<int>;79 using A2 = test_allocator<short>;80 std::flat_map<int, short, C, KeyContainer<int, A1>, ValueContainer<short, A2>> m = {C(4), A1(5)};81 assert(m.empty());82 assert(m.begin() == m.end());83 assert(m.key_comp() == C(4));84 assert(m.keys().get_allocator() == A1(5));85 assert(m.values().get_allocator() == A2(5));86 }87}88 89constexpr bool test() {90 {91 // The constructors in this subclause shall not participate in overload92 // resolution unless uses_allocator_v<key_container_type, Alloc> is true93 // and uses_allocator_v<mapped_container_type, Alloc> is true.94 95 using C = test_less<int>;96 using A1 = test_allocator<int>;97 using A2 = other_allocator<int>;98 using M1 = std::flat_map<int, int, C, std::vector<int, A1>, std::vector<int, A1>>;99 using M2 = std::flat_map<int, int, C, std::vector<int, A1>, std::vector<int, A2>>;100 using M3 = std::flat_map<int, int, C, std::vector<int, A2>, std::vector<int, A1>>;101 static_assert(std::is_constructible_v<M1, const C&, const A1&>);102 static_assert(!std::is_constructible_v<M1, const C&, const A2&>);103 static_assert(!std::is_constructible_v<M2, const C&, const A2&>);104 static_assert(!std::is_constructible_v<M3, const C&, const A2&>);105 }106 107 test_compare<std::vector<int>, std::vector<int>>();108 test_compare<std::vector<int>, std::vector<double>>();109 test_compare<MinSequenceContainer<int>, MinSequenceContainer<double>>();110 test_compare<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();111 test_compare<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();112 113 test_compare_alloc<std::vector, std::vector>();114 115#ifndef __cpp_lib_constexpr_deque116 if (!TEST_IS_CONSTANT_EVALUATED)117#endif118 {119 test_compare<std::deque<int>, std::vector<double>>();120 test_compare_alloc<std::deque, std::deque>();121 }122 123 return true;124}125 126int main(int, char**) {127 test();128#if TEST_STD_VER >= 26129 static_assert(test());130#endif131 132 return 0;133}134