brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.4 KiB · c7503bb Raw
234 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// flat_map(key_container_type key_cont, mapped_container_type mapped_cont,14//           const key_compare& comp = key_compare());15// template<class Allocator>16//   flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont,17//            const Allocator& a);18// template<class Alloc>19//   flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont,20//            const key_compare& comp, const Alloc& a);21 22#include <algorithm>23#include <deque>24#include <flat_map>25#include <functional>26#include <vector>27 28#include "min_allocator.h"29#include "MoveOnly.h"30#include "test_allocator.h"31#include "test_iterators.h"32#include "test_macros.h"33#include "../helpers.h"34#include "../../../test_compare.h"35 36struct P {37  int first;38  int second;39  template <class T, class U>40  constexpr bool operator==(const std::pair<T, U>& rhs) const {41    return MoveOnly(first) == rhs.first && MoveOnly(second) == rhs.second;42  }43};44 45template <template <class...> class KeyContainer, template <class...> class ValueContainer>46constexpr void test() {47  {48    // flat_map(key_container_type , mapped_container_type)49    using M                  = std::flat_map<int, short, std::less<int>, KeyContainer<int>, ValueContainer<short>>;50    KeyContainer<int> ks     = {1, 1, 1, 2, 2, 3, 2, 3, 3};51    ValueContainer<short> vs = {1, 2, 3, 4, 5, 6, 7, 8, 9};52    auto m                   = M(ks, vs);53    assert((m.keys() == KeyContainer<int>{1, 2, 3}));54    check_possible_values(55        m.values(),56        std::vector<std::vector<short>>{57            {1, 2, 3},58            {4, 5, 7},59            {6, 8, 9},60        });61 62    // explicit(false)63    M m2 = {ks, vs};64    assert(m2 == m);65 66    m = M(std::move(ks), std::move(vs));67    assert(ks.empty()); // it was moved-from68    assert(vs.empty()); // it was moved-from69    assert((m.keys() == KeyContainer<int>{1, 2, 3}));70    check_possible_values(71        m.values(),72        std::vector<std::vector<short>>{73            {1, 2, 3},74            {4, 5, 7},75            {6, 8, 9},76        });77  }78  {79    // flat_map(key_container_type , mapped_container_type)80    // move-only81    P expected[] = {{3, 2}, {2, 1}, {1, 3}};82    using Ks     = KeyContainer<int, min_allocator<int>>;83    using Vs     = ValueContainer<MoveOnly, min_allocator<MoveOnly>>;84    using M      = std::flat_map<int, MoveOnly, std::greater<int>, Ks, Vs>;85    Ks ks        = {1, 3, 2};86    Vs vs;87    vs.push_back(3);88    vs.push_back(2);89    vs.push_back(1);90    auto m = M(std::move(ks), std::move(vs));91    assert(ks.empty()); // it was moved-from92    assert(vs.empty()); // it was moved-from93    assert(std::ranges::equal(m, expected, std::equal_to<>()));94  }95  {96    // flat_map(key_container_type , mapped_container_type)97    // container's allocators are used98    using A = test_allocator<int>;99    using M = std::flat_map<int, int, std::less<int>, KeyContainer<int, A>, ValueContainer<int, A>>;100    auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));101    auto vs = ValueContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6));102    auto m  = M(std::move(ks), std::move(vs));103    assert(ks.empty()); // it was moved-from104    assert(vs.empty()); // it was moved-from105    assert((m == M{{1, 1}, {2, 2}, {3, 3}}));106    assert(m.keys().get_allocator() == A(5));107    assert(m.values().get_allocator() == A(6));108  }109  {110    // flat_map(key_container_type , mapped_container_type, key_compare)111    using C                 = test_less<int>;112    using M                 = std::flat_map<int, char, C, KeyContainer<int>, ValueContainer<char>>;113    KeyContainer<int> ks    = {1, 1, 1, 2, 2, 3, 2, 3, 3};114    ValueContainer<char> vs = {1, 2, 3, 4, 5, 6, 7, 8, 9};115    auto m                  = M(ks, vs, C(4));116    assert((m.keys() == KeyContainer<int>{1, 2, 3}));117    check_possible_values(118        m.values(),119        std::vector<std::vector<char>>{120            {1, 2, 3},121            {4, 5, 7},122            {6, 8, 9},123        });124    assert(m.key_comp() == C(4));125 126    // explicit(false)127    M m2 = {ks, vs, C(4)};128    assert(m2 == m);129    assert(m2.key_comp() == C(4));130  }131  {132    // flat_map(key_container_type , mapped_container_type, const Allocator&)133    using A = test_allocator<int>;134    using M = std::flat_map<int, int, std::less<int>, KeyContainer<int, A>, ValueContainer<int, A>>;135    auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));136    auto vs = ValueContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6));137    auto m  = M(ks, vs, A(4)); // replaces the allocators138    assert(!ks.empty());       // it was an lvalue above139    assert(!vs.empty());       // it was an lvalue above140    assert((m == M{{1, 1}, {2, 2}, {3, 3}}));141    assert(m.keys().get_allocator() == A(4));142    assert(m.values().get_allocator() == A(4));143  }144  {145    // flat_map(key_container_type , mapped_container_type, const Allocator&)146    // explicit(false)147    using A = test_allocator<int>;148    using M = std::flat_map<int, int, std::less<int>, KeyContainer<int, A>, ValueContainer<int, A>>;149    auto ks = KeyContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));150    auto vs = ValueContainer<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6));151    M m     = {ks, vs, A(4)}; // implicit ctor152    assert(!ks.empty());      // it was an lvalue above153    assert(!vs.empty());      // it was an lvalue above154    assert((m == M{{1, 1}, {2, 2}, {3, 3}}));155    assert(m.keys().get_allocator() == A(4));156    assert(m.values().get_allocator() == A(4));157  }158 159  {160    // flat_map(key_container_type , mapped_container_type, key_compare, const Allocator&)161    using C                = test_less<int>;162    using A                = test_allocator<int>;163    using M                = std::flat_map<int, int, C, std::vector<int, A>, std::vector<int, A>>;164    std::vector<int, A> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};165    std::vector<int, A> vs = {1, 2, 3, 4, 5, 6, 7, 8, 9};166    auto m                 = M(ks, vs, C(4), A(5));167    assert((m.keys() == std::vector<int, A>{1, 2, 3}));168    check_possible_values(169        m.values(),170        std::vector<std::vector<int>>{171            {1, 2, 3},172            {4, 5, 7},173            {6, 8, 9},174        });175    assert(m.key_comp() == C(4));176    assert(m.keys().get_allocator() == A(5));177    assert(m.values().get_allocator() == A(5));178 179    // explicit(false)180    M m2 = {ks, vs, C(4), A(5)};181    assert(m2 == m);182    assert(m2.key_comp() == C(4));183    assert(m2.keys().get_allocator() == A(5));184    assert(m2.values().get_allocator() == A(5));185  }186}187 188bool constexpr test() {189  {190    // The constructors in this subclause shall not participate in overload191    // resolution unless uses_allocator_v<key_container_type, Alloc> is true192    // and uses_allocator_v<mapped_container_type, Alloc> is true.193 194    using C  = test_less<int>;195    using A1 = test_allocator<int>;196    using A2 = other_allocator<int>;197    using V1 = std::vector<int, A1>;198    using V2 = std::vector<int, A2>;199    using M1 = std::flat_map<int, int, C, V1, V1>;200    using M2 = std::flat_map<int, int, C, V1, V2>;201    using M3 = std::flat_map<int, int, C, V2, V1>;202    static_assert(std::is_constructible_v<M1, const V1&, const V1&, const A1&>);203    static_assert(!std::is_constructible_v<M1, const V1&, const V1&, const A2&>);204    static_assert(!std::is_constructible_v<M2, const V1&, const V2&, const A2&>);205    static_assert(!std::is_constructible_v<M3, const V2&, const V1&, const A2&>);206 207    static_assert(std::is_constructible_v<M1, const V1&, const V1&, const C&, const A1&>);208    static_assert(!std::is_constructible_v<M1, const V1&, const V1&, const C&, const A2&>);209    static_assert(!std::is_constructible_v<M2, const V1&, const V2&, const C&, const A2&>);210    static_assert(!std::is_constructible_v<M3, const V2&, const V1&, const C&, const A2&>);211  }212 213  test<std::vector, std::vector>();214 215#ifndef __cpp_lib_constexpr_deque216  if (!TEST_IS_CONSTANT_EVALUATED)217#endif218  {219    test<std::deque, std::vector>();220    test<std::deque, std::deque>();221  }222 223  return true;224}225 226int main(int, char**) {227  test();228#if TEST_STD_VER >= 26229  static_assert(test());230#endif231 232  return 0;233}234