brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.0 KiB · 5cefb6f Raw
218 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// <map>10// UNSUPPORTED: c++03, c++11, c++1411 12// template<class InputIterator,13//          class Compare = less<iter-value-type<InputIterator>>,14//          class Allocator = allocator<iter-value-type<InputIterator>>>15// map(InputIterator, InputIterator,16//          Compare = Compare(), Allocator = Allocator())17//   -> map<iter-value-type<InputIterator>, Compare, Allocator>;18// template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>19// map(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())20//   -> map<Key, Compare, Allocator>;21// template<class InputIterator, class Allocator>22// map(InputIterator, InputIterator, Allocator)23//   -> map<iter-value-type<InputIterator>, less<iter-value-type<InputIterator>>, Allocator>;24// template<class Key, class Allocator>25// map(initializer_list<Key>, Allocator)26//   -> map<Key, less<Key>, Allocator>;27//28// template<ranges::input_range R, class Compare = less<range-key-type<R>,29//          class Allocator = allocator<range-to-alloc-type<R>>>30//   map(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())31//     -> map<range-key-type<R>, range-mapped-type<R>, Compare, Allocator>; // C++2332//33// template<ranges::input_range R, class Allocator>34//   map(from_range_t, R&&, Allocator)35//     -> map<range-key-type<R>, range-mapped-type<R>, less<range-key-type<R>>, Allocator>; // C++2336 37#include <algorithm> // std::equal38#include <array>39#include <cassert>40#include <climits> // INT_MAX41#include <functional>42#include <map>43#include <utility>44#include <tuple>45#include <type_traits>46#include <vector>47 48#include "deduction_guides_sfinae_checks.h"49#include "test_allocator.h"50 51using P  = std::pair<int, long>;52using PC = std::pair<const int, long>;53 54int main(int, char**) {55  {56    const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};57    std::map m(std::begin(arr), std::end(arr));58 59    ASSERT_SAME_TYPE(decltype(m), std::map<int, long>);60    const PC expected_m[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};61    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));62  }63 64  {65    const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};66    std::map m(std::begin(arr), std::end(arr), std::greater<int>());67 68    ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>>);69    const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}};70    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));71  }72 73  {74    const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};75    std::map m(std::begin(arr), std::end(arr), std::greater<int>(), test_allocator<PC>(0, 42));76 77    ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>, test_allocator<PC>>);78    const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}};79    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));80    assert(m.get_allocator().get_id() == 42);81  }82 83  {84    std::map<int, long> source;85    std::map m(source);86    ASSERT_SAME_TYPE(decltype(m), decltype(source));87    assert(m.size() == 0);88  }89 90  {91    std::map<int, long> source;92    std::map m{source}; // braces instead of parens93    ASSERT_SAME_TYPE(decltype(m), decltype(source));94    assert(m.size() == 0);95  }96 97  {98    std::map<int, long> source;99    std::map m(source, std::map<int, long>::allocator_type());100    ASSERT_SAME_TYPE(decltype(m), decltype(source));101    assert(m.size() == 0);102  }103 104  {105    std::map m{P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}};106 107    ASSERT_SAME_TYPE(decltype(m), std::map<int, long>);108    const PC expected_m[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};109    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));110  }111 112  {113    std::map m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, std::greater<int>());114 115    ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>>);116    const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}};117    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));118  }119 120  {121    std::map m(122        {P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, std::greater<int>(), test_allocator<PC>(0, 43));123 124    ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>, test_allocator<PC>>);125    const PC expected_m[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}};126    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));127    assert(m.get_allocator().get_id() == 43);128  }129 130  {131    const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}};132    std::map m(std::begin(arr), std::end(arr), test_allocator<PC>(0, 44));133 134    ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::less<int>, test_allocator<PC>>);135    const PC expected_m[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};136    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));137    assert(m.get_allocator().get_id() == 44);138  }139 140  {141    std::map m({P{1, 1L}, P{2, 2L}, P{1, 1L}, P{INT_MAX, 1L}, P{3, 1L}}, test_allocator<PC>(0, 45));142 143    ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::less<int>, test_allocator<PC>>);144    const PC expected_m[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}};145    assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));146    assert(m.get_allocator().get_id() == 45);147  }148 149  {150    // Examples from LWG3025151    std::map m{std::pair{1, 1}, {2, 2}, {3, 3}};152    ASSERT_SAME_TYPE(decltype(m), std::map<int, int>);153 154    std::map m2{m.begin(), m.end()};155    ASSERT_SAME_TYPE(decltype(m2), std::map<int, int>);156  }157 158  {159    // Examples from LWG3531160    std::map m1{{std::pair{1, 2}, {3, 4}}, std::less<int>()};161    ASSERT_SAME_TYPE(decltype(m1), std::map<int, int>);162 163    using value_type = std::pair<const int, int>;164    std::map m2{{value_type{1, 2}, {3, 4}}, std::less<int>()};165    ASSERT_SAME_TYPE(decltype(m2), std::map<int, int>);166  }167 168#if TEST_STD_VER >= 23169  {170    using Range       = std::array<P, 0>;171    using Comp        = std::greater<int>;172    using DefaultComp = std::less<int>;173    using Alloc       = test_allocator<PC>;174 175    { // (from_range, range)176      std::map c(std::from_range, Range());177      static_assert(std::is_same_v<decltype(c), std::map<int, long>>);178    }179 180    { // (from_range, range, comp)181      std::map c(std::from_range, Range(), Comp());182      static_assert(std::is_same_v<decltype(c), std::map<int, long, Comp>>);183    }184 185    { // (from_range, range, comp, alloc)186      std::map c(std::from_range, Range(), Comp(), Alloc());187      static_assert(std::is_same_v<decltype(c), std::map<int, long, Comp, Alloc>>);188    }189 190    { // (from_range, range, alloc)191      std::map c(std::from_range, Range(), Alloc());192      static_assert(std::is_same_v<decltype(c), std::map<int, long, DefaultComp, Alloc>>);193    }194  }195  {196    std::vector<std::pair<const int, float>> pair_vec = {{1, 1.1f}, {2, 2.2f}, {3, 3.3f}};197    std::map m1(pair_vec.begin(), pair_vec.end());198    ASSERT_SAME_TYPE(decltype(m1), std::map<int, float>);199 200    std::vector<std::tuple<int, double>> tuple_vec = {{10, 1.1}, {20, 2.2}, {30, 3.3}};201    std::map m2(tuple_vec.begin(), tuple_vec.end());202    ASSERT_SAME_TYPE(decltype(m2), std::map<int, double>);203 204    std::vector<std::array<long, 2>> array_vec = {{100L, 101L}, {200L, 201L}, {300L, 301L}};205    std::map m3(array_vec.begin(), array_vec.end());206    ASSERT_SAME_TYPE(decltype(m3), std::map<long, long>);207 208    std::vector<std::pair<int, char>> non_const_key_pair_vec = {{5, 'a'}, {6, 'b'}};209    std::map m4(non_const_key_pair_vec.begin(), non_const_key_pair_vec.end());210    ASSERT_SAME_TYPE(decltype(m4), std::map<int, char>);211  }212#endif213 214  AssociativeContainerDeductionGuidesSfinaeAway<std::map, std::map<int, long>>();215 216  return 0;217}218