brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.9 KiB · ea775eb Raw
174 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// explicit flat_set(container_type key_cont, const key_compare& comp = key_compare());14// template<class Allocator>15//   flat_set(const container_type& key_cont, const Allocator& a);16// template<class Alloc>17//   flat_set(const container_type& key_cont, const key_compare& comp, const Alloc& a);18 19#include <algorithm>20#include <deque>21#include <flat_set>22#include <functional>23#include <type_traits>24#include <vector>25 26#include "min_allocator.h"27#include "MoveOnly.h"28#include "test_allocator.h"29#include "test_iterators.h"30#include "test_macros.h"31#include "../../../test_compare.h"32 33template <class T>34void conversion_test(T);35 36template <class T, class... Args>37concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); };38 39template <template <class...> class KeyContainer>40constexpr void test() {41  {42    // flat_set(container_type)43    using M              = std::flat_set<int, std::less<int>, KeyContainer<int>>;44    KeyContainer<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};45    auto m               = M(ks);46    assert(std::ranges::equal(m, std::vector<int>{1, 2, 3}));47 48    // explicit49    static_assert(std::is_constructible_v<M, const KeyContainer<int>&>);50    static_assert(!ImplicitlyConstructible<M, const KeyContainer<int>&>);51  }52  {53    // flat_set(container_type)54    // move-only55    MoveOnly expected[] = {3, 2, 1};56    using Ks            = KeyContainer<MoveOnly, min_allocator<MoveOnly>>;57    using M             = std::flat_set<MoveOnly, std::greater<MoveOnly>, Ks>;58    Ks ks;59    ks.push_back(1);60    ks.push_back(3);61    ks.push_back(2);62    auto m = M(std::move(ks));63    assert(ks.empty()); // it was moved-from64    assert(std::ranges::equal(m, expected));65  }66  {67    // flat_set(container_type)68    // container's allocator is used69    using A = test_allocator<int>;70    using M = std::flat_set<int, std::less<int>, KeyContainer<int, A>>;71    auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));72    auto m  = M(std::move(ks));73    assert(ks.empty()); // it was moved-from74    assert((m == M{1, 2, 3}));75    auto keys = std::move(m).extract();76    assert(keys.get_allocator() == A(5));77  }78  {79    // flat_set(container_type , key_compare)80    using C              = test_less<int>;81    using M              = std::flat_set<int, C, KeyContainer<int>>;82    KeyContainer<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};83    auto m               = M(ks, C(4));84    assert(std::ranges::equal(m, std::vector<int>{1, 2, 3}));85    assert(m.key_comp() == C(4));86 87    // explicit88    static_assert(std::is_constructible_v<M, const KeyContainer<int>&, const C&>);89    static_assert(!ImplicitlyConstructible<M, const KeyContainer<int>&, const C&>);90  }91  {92    // flat_set(container_type , const Allocator&)93    using A = test_allocator<int>;94    using M = std::flat_set<int, std::less<int>, KeyContainer<int, A>>;95    auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));96    auto m  = M(ks, A(4)); // replaces the allocators97    assert(!ks.empty());   // it was an lvalue above98    assert((m == M{1, 2, 3}));99    auto keys = M(m).extract();100    assert(keys.get_allocator() == A(4));101 102    // explicit(false)103    static_assert(ImplicitlyConstructible<M, const KeyContainer<int, A>&, const A&>);104    M m2 = {ks, A(4)};   // implicit ctor105    assert(!ks.empty()); // it was an lvalue above106    assert(m2 == m);107    auto keys2 = std::move(m).extract();108    assert(keys2.get_allocator() == A(4));109  }110  {111    // flat_set(container_type , key_compare, const Allocator&)112    using C                 = test_less<int>;113    using A                 = test_allocator<int>;114    using M                 = std::flat_set<int, C, KeyContainer<int, A>>;115    KeyContainer<int, A> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};116    auto m                  = M(ks, C(4), A(5));117    assert(std::ranges::equal(m, std::vector<int, A>{1, 2, 3}));118    assert(m.key_comp() == C(4));119    auto m_copy = m;120    auto keys   = std::move(m_copy).extract();121    assert(keys.get_allocator() == A(5));122 123    // explicit(false)124    static_assert(ImplicitlyConstructible<M, const KeyContainer<int, A>&, const A&>);125    M m2 = {ks, C(4), A(5)};126    assert(m2 == m);127    assert(m2.key_comp() == C(4));128    keys = std::move(m2).extract();129    assert(keys.get_allocator() == A(5));130  }131}132 133constexpr bool test() {134  {135    // The constructors in this subclause shall not participate in overload136    // resolution unless uses_allocator_v<container_type, Alloc> is true.137 138    using C  = test_less<int>;139    using A1 = test_allocator<int>;140    using A2 = other_allocator<int>;141    using V1 = std::vector<int, A1>;142    using V2 = std::vector<int, A2>;143    using M1 = std::flat_set<int, C, V1>;144    using M2 = std::flat_set<int, C, V2>;145    static_assert(std::is_constructible_v<M1, const V1&, const A1&>);146    static_assert(std::is_constructible_v<M2, const V2&, const A2&>);147    static_assert(!std::is_constructible_v<M1, const V1&, const A2&>);148    static_assert(!std::is_constructible_v<M2, const V2&, const A1&>);149 150    static_assert(std::is_constructible_v<M1, const V1&, const C&, const A1&>);151    static_assert(std::is_constructible_v<M2, const V2&, const C&, const A2&>);152    static_assert(!std::is_constructible_v<M1, const V1&, const C&, const A2&>);153    static_assert(!std::is_constructible_v<M2, const V2&, const C&, const A1&>);154  }155 156  test<std::vector>();157 158#ifndef __cpp_lib_constexpr_deque159  if (!TEST_IS_CONSTANT_EVALUATED)160#endif161    test<std::deque>();162 163  return true;164}165 166int main(int, char**) {167  test();168#if TEST_STD_VER >= 26169  static_assert(test());170#endif171 172  return 0;173}174