Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14 |
| 10 | |
| 11 | #include <unordered_set> |
| 12 | #include <unordered_map> |
| 13 | #include <set> |
| 14 | #include <map> |
Marshall Clow | 7fc6a55 | 2019-05-31 18:35:30 +0000 | [diff] [blame] | 15 | #include "test_macros.h" |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 16 | #include "min_allocator.h" |
| 17 | |
| 18 | using namespace std; |
| 19 | |
| 20 | // [container.node.overview] Table 83. |
| 21 | template <class K, class T, class C1, class C2, class H1, class H2, class E1, class E2, class A_set, class A_map> |
| 22 | struct node_compatibility_table |
| 23 | { |
| 24 | static constexpr bool value = |
| 25 | is_same_v<typename map<K, T, C1, A_map>::node_type, typename map<K, T, C2, A_map>::node_type> && |
| 26 | is_same_v<typename map<K, T, C1, A_map>::node_type, typename multimap<K, T, C2, A_map>::node_type> && |
| 27 | is_same_v<typename set<K, C1, A_set>::node_type, typename set<K, C2, A_set>::node_type> && |
| 28 | is_same_v<typename set<K, C1, A_set>::node_type, typename multiset<K, C2, A_set>::node_type> && |
| 29 | is_same_v<typename unordered_map<K, T, H1, E1, A_map>::node_type, typename unordered_map<K, T, H2, E2, A_map>::node_type> && |
| 30 | is_same_v<typename unordered_map<K, T, H1, E1, A_map>::node_type, typename unordered_multimap<K, T, H2, E2, A_map>::node_type> && |
| 31 | is_same_v<typename unordered_set<K, H1, E1, A_set>::node_type, typename unordered_set<K, H2, E2, A_set>::node_type> && |
| 32 | is_same_v<typename unordered_set<K, H1, E1, A_set>::node_type, typename unordered_multiset<K, H2, E2, A_set>::node_type>; |
| 33 | }; |
| 34 | |
| 35 | template <class T> struct my_hash |
| 36 | { |
| 37 | using argument_type = T; |
| 38 | using result_type = size_t; |
| 39 | my_hash() = default; |
| 40 | size_t operator()(const T&) const {return 0;} |
| 41 | }; |
| 42 | |
| 43 | template <class T> struct my_compare |
| 44 | { |
| 45 | my_compare() = default; |
| 46 | bool operator()(const T&, const T&) const {return true;} |
| 47 | }; |
| 48 | |
| 49 | template <class T> struct my_equal |
| 50 | { |
| 51 | my_equal() = default; |
| 52 | bool operator()(const T&, const T&) const {return true;} |
| 53 | }; |
| 54 | |
| 55 | struct Static |
| 56 | { |
| 57 | Static() = default; |
| 58 | Static(const Static&) = delete; |
| 59 | Static(Static&&) = delete; |
| 60 | Static& operator=(const Static&) = delete; |
| 61 | Static& operator=(Static&&) = delete; |
| 62 | }; |
| 63 | |
| 64 | namespace std |
| 65 | { |
| 66 | template <> struct hash<Static> |
| 67 | { |
| 68 | using argument_type = Static; |
| 69 | using result_type = size_t; |
| 70 | hash() = default; |
| 71 | size_t operator()(const Static&) const; |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | static_assert(node_compatibility_table< |
| 76 | int, int, std::less<int>, std::less<int>, std::hash<int>, |
| 77 | std::hash<int>, std::equal_to<int>, std::equal_to<int>, |
| 78 | std::allocator<int>, |
| 79 | std::allocator<std::pair<const int, int>>>::value, |
| 80 | ""); |
| 81 | |
| 82 | static_assert( |
| 83 | node_compatibility_table<int, int, std::less<int>, my_compare<int>, |
| 84 | std::hash<int>, my_hash<int>, std::equal_to<int>, |
| 85 | my_equal<int>, allocator<int>, |
| 86 | allocator<std::pair<const int, int>>>::value, |
| 87 | ""); |
| 88 | |
| 89 | static_assert(node_compatibility_table< |
| 90 | Static, int, my_compare<Static>, std::less<Static>, |
| 91 | my_hash<Static>, std::hash<Static>, my_equal<Static>, |
| 92 | std::equal_to<Static>, min_allocator<Static>, |
| 93 | min_allocator<std::pair<const Static, int>>>::value, |
| 94 | ""); |
| 95 | |
| 96 | template <class Container> |
| 97 | void test_node_handle_operations() |
| 98 | { |
| 99 | Container c; |
| 100 | |
| 101 | typename Container::node_type nt1, nt2 = c.extract(c.emplace().first); |
| 102 | assert(nt2.get_allocator() == c.get_allocator()); |
| 103 | assert(!nt2.empty()); |
| 104 | assert(nt1.empty()); |
| 105 | std::swap(nt1, nt2); |
| 106 | assert(nt1.get_allocator() == c.get_allocator()); |
| 107 | assert(nt2.empty()); |
| 108 | } |
| 109 | |
| 110 | template <class Container> |
| 111 | void test_node_handle_operations_multi() |
| 112 | { |
| 113 | Container c; |
| 114 | |
| 115 | typename Container::node_type nt1, nt2 = c.extract(c.emplace()); |
| 116 | assert(nt2.get_allocator() == c.get_allocator()); |
| 117 | assert(!nt2.empty()); |
| 118 | assert(nt1.empty()); |
| 119 | std::swap(nt1, nt2); |
| 120 | assert(nt1.get_allocator() == c.get_allocator()); |
| 121 | assert(nt2.empty()); |
| 122 | } |
| 123 | |
Billy Robert O'Neal III | 77e898c | 2018-08-08 04:24:47 +0000 | [diff] [blame] | 124 | template <class> void test_typedef() {} |
| 125 | |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 126 | template <class Container> |
| 127 | void test_insert_return_type() |
| 128 | { |
Billy Robert O'Neal III | 77e898c | 2018-08-08 04:24:47 +0000 | [diff] [blame] | 129 | test_typedef<typename Container::insert_return_type>(); |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 130 | } |
| 131 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 132 | int main(int, char**) |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 133 | { |
| 134 | test_node_handle_operations<std::map<int, int>>(); |
| 135 | test_node_handle_operations_multi<std::multimap<int, int>>(); |
| 136 | test_node_handle_operations<std::set<int>>(); |
| 137 | test_node_handle_operations_multi<std::multiset<int>>(); |
| 138 | test_node_handle_operations<std::unordered_map<int, int>>(); |
| 139 | test_node_handle_operations_multi<std::unordered_multimap<int, int>>(); |
| 140 | test_node_handle_operations<std::unordered_set<int>>(); |
| 141 | test_node_handle_operations_multi<std::unordered_multiset<int>>(); |
| 142 | |
| 143 | test_insert_return_type<std::map<int, int>>(); |
| 144 | test_insert_return_type<std::set<int>>(); |
| 145 | test_insert_return_type<std::unordered_map<int, int>>(); |
| 146 | test_insert_return_type<std::unordered_set<int>>(); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 147 | |
| 148 | return 0; |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 149 | } |