207 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_set>12 13// template<container-compatible-range<value_type> R>14// flat_set(from_range_t, R&&)15// template<container-compatible-range<value_type> R>16// flat_set(from_range_t, R&&, const key_compare&)17// template<container-compatible-range<value_type> R, class Alloc>18// flat_set(from_range_t, R&&, const Alloc&);19// template<container-compatible-range<value_type> R, class Alloc>20// flat_set(from_range_t, R&&, const key_compare&, const Alloc&);21 22#include <algorithm>23#include <deque>24#include <flat_set>25#include <functional>26#include <string>27#include <vector>28 29#include "MinSequenceContainer.h"30#include "min_allocator.h"31#include "test_allocator.h"32#include "test_iterators.h"33#include "test_macros.h"34#include "../../../test_compare.h"35 36// test constraint container-compatible-range37 38template <class V>39using RangeOf = std::ranges::subrange<V*>;40using Set = std::flat_set<int>;41 42static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>>);43static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>>);44static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>>);45 46static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>>);47static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::less<int>>);48static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>>);49 50static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::allocator<int>>);51static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<short>, std::allocator<int>>);52static_assert(!std::is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::allocator<int>>);53 54static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>);55static_assert(std::is_constructible_v<Set, std::from_range_t, RangeOf<int>, std::less<int>, std::allocator<int>>);56static_assert(57 !std::58 is_constructible_v<Set, std::from_range_t, RangeOf<std::pair<int, int>>, std::less<int>, std::allocator<int>>);59 60template <class KeyContainer>61constexpr void test() {62 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};63 int expected[] = {1, 2, 3};64 {65 // flat_set(from_range_t, R&&)66 // input_range && !common67 using M = std::flat_set<int, std::less<int>, KeyContainer>;68 using Iter = cpp20_input_iterator<const int*>;69 using Sent = sentinel_wrapper<Iter>;70 using R = std::ranges::subrange<Iter, Sent>;71 auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));72 assert(std::ranges::equal(m, expected));73 LIBCPP_ASSERT(std::ranges::equal(m, expected));74 75 // explicit(false)76 M m2 = {std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))};77 assert(m2 == m);78 }79 {80 // flat_set(from_range_t, R&&)81 // greater82 using M = std::flat_set<int, std::greater<int>, KeyContainer>;83 using Iter = cpp20_input_iterator<const int*>;84 using Sent = sentinel_wrapper<Iter>;85 using R = std::ranges::subrange<Iter, Sent>;86 auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));87 assert(std::ranges::equal(m, KeyContainer{3, 2, 1}));88 }89 {90 // flat_set(from_range_t, R&&)91 // contiguous range92 using M = std::flat_set<int, std::less<int>, KeyContainer>;93 using R = std::ranges::subrange<const int*>;94 auto m = M(std::from_range, R(ar, ar + 9));95 assert(std::ranges::equal(m, expected));96 }97 {98 // flat_set(from_range_t, R&&, const key_compare&)99 using C = test_less<int>;100 using M = std::flat_set<int, C, KeyContainer>;101 using R = std::ranges::subrange<const int*>;102 auto m = M(std::from_range, R(ar, ar + 9), C(3));103 assert(std::ranges::equal(m, expected));104 assert(m.key_comp() == C(3));105 106 // explicit(false)107 M m2 = {std::from_range, R(ar, ar + 9), C(3)};108 assert(m2 == m);109 assert(m2.key_comp() == C(3));110 }111}112 113template <template <class...> class KeyContainer>114constexpr void test_alloc() {115 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};116 int expected[] = {1, 2, 3};117 {118 // flat_set(from_range_t, R&&, const Allocator&)119 using A1 = test_allocator<int>;120 using M = std::flat_set<int, std::less<int>, KeyContainer<int, A1>>;121 using R = std::ranges::subrange<const int*>;122 auto m = M(std::from_range, R(ar, ar + 9), A1(5));123 assert(std::ranges::equal(m, expected));124 assert(std::move(m).extract().get_allocator() == A1(5));125 }126 {127 // flat_set(from_range_t, R&&, const Allocator&)128 // explicit(false)129 using A1 = test_allocator<int>;130 using M = std::flat_set<int, std::less<int>, KeyContainer<int, A1>>;131 using R = std::ranges::subrange<const int*>;132 M m = {std::from_range, R(ar, ar + 9), A1(5)}; // implicit ctor133 assert(std::ranges::equal(m, expected));134 assert(std::move(m).extract().get_allocator() == A1(5));135 }136 {137 // flat_set(from_range_t, R&&, const key_compare&, const Allocator&)138 using C = test_less<int>;139 using A1 = test_allocator<int>;140 using M = std::flat_set<int, C, KeyContainer<int, A1>>;141 using R = std::ranges::subrange<const int*>;142 auto m = M(std::from_range, R(ar, ar + 9), C(3), A1(5));143 assert(std::ranges::equal(m, expected));144 assert(m.key_comp() == C(3));145 assert(std::move(m).extract().get_allocator() == A1(5));146 }147 {148 // flat_set(from_range_t, R&&, const key_compare&, const Allocator&)149 // explicit(false)150 using A1 = test_allocator<int>;151 using M = std::flat_set<int, std::less<int>, KeyContainer<int, A1>>;152 using R = std::ranges::subrange<const int*>;153 M m = {std::from_range, R(ar, ar + 9), {}, A1(5)}; // implicit ctor154 assert(std::ranges::equal(m, expected));155 assert(std::move(m).extract().get_allocator() == A1(5));156 }157}158 159constexpr bool test() {160 {161 // The constructors in this subclause shall not participate in overload162 // resolution unless uses_allocator_v<container_type, Alloc> is true.163 164 using C = test_less<int>;165 using A1 = test_allocator<int>;166 using A2 = other_allocator<int>;167 using V1 = std::vector<int, A1>;168 using V2 = std::vector<int, A2>;169 using M1 = std::flat_set<int, C, V1>;170 using M2 = std::flat_set<int, C, V2>;171 static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const A1&>);172 static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const A2&>);173 static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const A2&>);174 static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const A1&>);175 176 static_assert(std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A1&>);177 static_assert(std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A2&>);178 static_assert(!std::is_constructible_v<M1, std::from_range_t, M1, const C&, const A2&>);179 static_assert(!std::is_constructible_v<M2, std::from_range_t, M2, const C&, const A1&>);180 }181 182 test<std::vector<int>>();183 test<std::vector<int, min_allocator<int>>>();184 test<MinSequenceContainer<int>>();185 186 test_alloc<std::vector>();187 188#ifndef __cpp_lib_constexpr_deque189 if (!TEST_IS_CONSTANT_EVALUATED)190#endif191 {192 test<std::deque<int>>();193 test_alloc<std::deque>();194 }195 196 return true;197}198 199int main(int, char**) {200 test();201#if TEST_STD_VER >= 26202 static_assert(test());203#endif204 205 return 0;206}207