171 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 <class InputIterator>14// flat_set(InputIterator first, InputIterator last, const key_compare& comp = key_compare());15// template<class InputIterator, class Allocator>16// flat_set(InputIterator first, InputIterator last, const Allocator& a);17// template<class InputIterator, class Allocator>18// flat_set(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);19 20#include <algorithm>21#include <deque>22#include <flat_set>23#include <functional>24#include <vector>25 26#include "MinSequenceContainer.h"27#include "min_allocator.h"28#include "test_allocator.h"29#include "test_iterators.h"30#include "test_macros.h"31#include "../../../test_compare.h"32 33template <class KeyContainer>34constexpr void test() {35 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};36 int expected[] = {1, 2, 3};37 {38 // flat_set(InputIterator , InputIterator)39 // cpp17_input_iterator40 using M = std::flat_set<int, std::less<int>, KeyContainer>;41 auto m = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));42 assert(std::ranges::equal(m, expected));43 44 // explicit(false)45 M m2 = {cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9)};46 assert(m2 == m);47 }48 {49 // flat_set(InputIterator , InputIterator)50 // greater51 using M = std::flat_set<int, std::greater<int>, KeyContainer>;52 auto m = M(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));53 assert(std::ranges::equal(m, KeyContainer{3, 2, 1}));54 }55 {56 // flat_set(InputIterator , InputIterator)57 // Test when the operands are of array type (also contiguous iterator type)58 using M = std::flat_set<int, std::greater<int>, KeyContainer>;59 auto m = M(ar, ar);60 assert(m.empty());61 }62 {63 // flat_set(InputIterator , InputIterator, const key_compare&)64 using C = test_less<int>;65 using M = std::flat_set<int, C, KeyContainer>;66 auto m = M(ar, ar + 9, C(3));67 assert(std::ranges::equal(m, expected));68 assert(m.key_comp() == C(3));69 70 // explicit(false)71 M m2 = {ar, ar + 9, C(3)};72 assert(m2 == m);73 assert(m2.key_comp() == C(3));74 }75}76 77template <template <class...> class KeyContainer>78constexpr void test_alloc() {79 int ar[] = {1, 1, 1, 2, 2, 3, 2, 3, 3};80 int expected[] = {1, 2, 3};81 82 {83 // flat_set(InputIterator , InputIterator, const Allocator&)84 using A1 = test_allocator<int>;85 using M = std::flat_set<int, std::less<int>, KeyContainer<int, A1>>;86 auto m = M(ar, ar + 9, A1(5));87 assert(std::ranges::equal(m, expected));88 assert(std::move(m).extract().get_allocator() == A1(5));89 }90 {91 // flat_set(InputIterator , InputIterator, const Allocator&)92 // explicit(false)93 using A1 = test_allocator<int>;94 using M = std::flat_set<int, std::less<int>, KeyContainer<int, A1>>;95 M m = {ar, ar + 9, A1(5)}; // implicit ctor96 assert(std::ranges::equal(m, expected));97 assert(std::move(m).extract().get_allocator() == A1(5));98 }99 {100 // flat_set(InputIterator , InputIterator, const key_compare&, const Allocator&)101 using C = test_less<int>;102 using A1 = test_allocator<int>;103 using M = std::flat_set<int, C, KeyContainer<int, A1>>;104 auto m = M(ar, ar + 9, C(3), A1(5));105 assert(std::ranges::equal(m, expected));106 assert(m.key_comp() == C(3));107 assert(std::move(m).extract().get_allocator() == A1(5));108 }109 {110 // flat_set(InputIterator , InputIterator, const key_compare&, const Allocator&)111 // explicit(false)112 using A1 = test_allocator<int>;113 using M = std::flat_set<int, std::less<int>, KeyContainer<int, A1>>;114 M m = {ar, ar + 9, {}, A1(5)}; // implicit ctor115 assert(std::ranges::equal(m, expected));116 LIBCPP_ASSERT(std::ranges::equal(m, expected));117 assert(std::move(m).extract().get_allocator() == A1(5));118 }119}120 121constexpr bool test() {122 {123 // The constructors in this subclause shall not participate in overload124 // resolution unless uses_allocator_v<container_type, Alloc> is true.125 126 using C = test_less<int>;127 using A1 = test_allocator<int>;128 using A2 = other_allocator<int>;129 using V1 = std::vector<int, A1>;130 using V2 = std::vector<int, A2>;131 using M1 = std::flat_set<int, C, V1>;132 using M2 = std::flat_set<int, C, V2>;133 using Iter1 = typename M1::iterator;134 using Iter2 = typename M2::iterator;135 static_assert(std::is_constructible_v<M1, Iter1, Iter1, const A1&>);136 static_assert(std::is_constructible_v<M2, Iter2, Iter2, const A2&>);137 static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const A2&>);138 static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const A1&>);139 140 static_assert(std::is_constructible_v<M1, Iter1, Iter1, const C&, const A1&>);141 static_assert(std::is_constructible_v<M2, Iter2, Iter2, const C&, const A2&>);142 static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const C&, const A2&>);143 static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const C&, const A1&>);144 }145 146 test<std::vector<int>>();147 test<MinSequenceContainer<int>>();148 test<std::vector<int, min_allocator<int>>>();149 150 test_alloc<std::vector>();151 152#ifndef __cpp_lib_constexpr_deque153 if (!TEST_IS_CONSTANT_EVALUATED)154#endif155 {156 test<std::deque<int>>();157 test_alloc<std::deque>();158 }159 160 return true;161}162 163int main(int, char**) {164 test();165#if TEST_STD_VER >= 26166 static_assert(test());167#endif168 169 return 0;170}171