113 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_multimap(flat_multimap&&, const allocator_type&);14 15#include <algorithm>16#include <deque>17#include <flat_map>18#include <functional>19#include <ranges>20#include <vector>21 22#include "../helpers.h"23#include "test_macros.h"24#include "../../../test_compare.h"25#include "test_allocator.h"26 27template <template <class...> class KeyContainer, template <class...> class ValueContainer>28constexpr void test() {29 {30 std::pair<int, int> expected[] = {{1, 1}, {1, 2}, {2, 3}, {2, 2}, {3, 1}};31 using C = test_less<int>;32 using A = test_allocator<int>;33 using M = std::flat_multimap<int, int, C, KeyContainer<int, A>, ValueContainer<int, A>>;34 auto mo = M(expected, expected + 5, C(5), A(7));35 auto m = M(std::move(mo), A(3));36 37 assert(m.key_comp() == C(5));38 assert(m.size() == 5);39 auto [keys, values] = std::move(m).extract();40 assert(keys.get_allocator() == A(3));41 assert(values.get_allocator() == A(3));42 assert(std::ranges::equal(keys, expected | std::views::elements<0>));43 check_possible_values(44 values,45 std::vector<std::vector<int>>{46 {1, 2},47 {1, 2},48 {2, 3},49 {2, 3},50 {1},51 });52 53 // The original flat_multimap is moved-from.54 assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));55 assert(mo.empty());56 assert(mo.key_comp() == C(5));57 assert(mo.keys().get_allocator() == A(7));58 assert(mo.values().get_allocator() == A(7));59 }60 {61 // moved-from object maintains invariant if one of underlying container does not clear after move62 using M = std::flat_multimap<int, int, std::less<>, KeyContainer<int>, CopyOnlyVector<int>>;63 M m1 = M({1, 2, 3}, {1, 2, 3});64 M m2(std::move(m1), std::allocator<int>{});65 assert(m2.size() == 3);66 check_invariant(m1);67 LIBCPP_ASSERT(m1.empty());68 LIBCPP_ASSERT(m1.keys().size() == 0);69 LIBCPP_ASSERT(m1.values().size() == 0);70 }71}72 73constexpr bool test() {74 {75 // The constructors in this subclause shall not participate in overload76 // resolution unless uses_allocator_v<key_container_type, Alloc> is true77 // and uses_allocator_v<mapped_container_type, Alloc> is true.78 79 using C = test_less<int>;80 using A1 = test_allocator<int>;81 using A2 = other_allocator<int>;82 using V1 = std::vector<int, A1>;83 using V2 = std::vector<int, A2>;84 using M1 = std::flat_multimap<int, int, C, V1, V1>;85 using M2 = std::flat_multimap<int, int, C, V1, V2>;86 using M3 = std::flat_multimap<int, int, C, V2, V1>;87 static_assert(std::is_constructible_v<M1, M1&&, const A1&>);88 static_assert(!std::is_constructible_v<M1, M1&&, const A2&>);89 static_assert(!std::is_constructible_v<M2, M2&&, const A2&>);90 static_assert(!std::is_constructible_v<M3, M3&&, const A2&>);91 }92 93 test<std::vector, std::vector>();94 95#ifndef __cpp_lib_constexpr_deque96 if (!TEST_IS_CONSTANT_EVALUATED)97#endif98 {99 test<std::deque, std::deque>();100 }101 102 return true;103}104 105int main(int, char**) {106 test();107#if TEST_STD_VER >= 26108 static_assert(test());109#endif110 111 return 0;112}113