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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <map> 10 // UNSUPPORTED: c++03, c++11, c++14 11 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 #include <algorithm> // std::equal 29 #include <cassert> 30 #include <climits> // INT_MAX 31 #include <functional> 32 #include <map> 33 #include <type_traits> 34 35 #include "test_allocator.h" 36 37 using P = std::pair<int, long>; 38 using PC = std::pair<const int, long>; 39 40 int main(int, char**) 41 { 42 { 43 const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} }; 44 std::map m(std::begin(arr), std::end(arr)); 45 46 ASSERT_SAME_TYPE(decltype(m), std::map<int, long>); 47 const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} }; 48 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 49 } 50 51 { 52 const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} }; 53 std::map m(std::begin(arr), std::end(arr), std::greater<int>()); 54 55 ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>>); 56 const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} }; 57 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 58 } 59 60 { 61 const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} }; 62 std::map m(std::begin(arr), std::end(arr), std::greater<int>(), test_allocator<PC>(0, 42)); 63 64 ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>, test_allocator<PC>>); 65 const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} }; 66 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 67 assert(m.get_allocator().get_id() == 42); 68 } 69 70 { 71 std::map<int, long> source; 72 std::map m(source); 73 ASSERT_SAME_TYPE(decltype(m), decltype(source)); 74 assert(m.size() == 0); 75 } 76 77 { 78 std::map<int, long> source; 79 std::map m{source}; // braces instead of parens 80 ASSERT_SAME_TYPE(decltype(m), decltype(source)); 81 assert(m.size() == 0); 82 } 83 84 { 85 std::map<int, long> source; 86 std::map m(source, std::map<int, long>::allocator_type()); 87 ASSERT_SAME_TYPE(decltype(m), decltype(source)); 88 assert(m.size() == 0); 89 } 90 91 { 92 std::map m{ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }; 93 94 ASSERT_SAME_TYPE(decltype(m), std::map<int, long>); 95 const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} }; 96 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 97 } 98 99 { 100 std::map m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, std::greater<int>()); 101 102 ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>>); 103 const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} }; 104 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 105 } 106 107 { 108 std::map m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, std::greater<int>(), test_allocator<PC>(0, 43)); 109 110 ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>, test_allocator<PC>>); 111 const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} }; 112 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 113 assert(m.get_allocator().get_id() == 43); 114 } 115 116 { 117 const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} }; 118 std::map m(std::begin(arr), std::end(arr), test_allocator<PC>(0, 44)); 119 120 ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::less<int>, test_allocator<PC>>); 121 const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} }; 122 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 123 assert(m.get_allocator().get_id() == 44); 124 } 125 126 { 127 std::map m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, test_allocator<PC>(0, 45)); 128 129 ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::less<int>, test_allocator<PC>>); 130 const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} }; 131 assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m))); 132 assert(m.get_allocator().get_id() == 45); 133 } 134 135 { 136 // Examples from LWG3025 137 std::map m{std::pair{1, 1}, {2, 2}, {3, 3}}; 138 ASSERT_SAME_TYPE(decltype(m), std::map<int, int>); 139 140 std::map m2{m.begin(), m.end()}; 141 ASSERT_SAME_TYPE(decltype(m2), std::map<int, int>); 142 } 143 144 { 145 // Examples from LWG3531 146 std::map m1{{std::pair{1, 2}, {3, 4}}, std::less<int>()}; 147 ASSERT_SAME_TYPE(decltype(m1), std::map<int, int>); 148 149 using value_type = std::pair<const int, int>; 150 std::map m2{{value_type{1, 2}, {3, 4}}, std::less<int>()}; 151 ASSERT_SAME_TYPE(decltype(m2), std::map<int, int>); 152 } 153 154 return 0; 155 } 156