105 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(flat_map&&, 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}, {2, 2}, {3, 1}};31 using C = test_less<int>;32 using A = test_allocator<int>;33 using M = std::flat_map<int, int, C, KeyContainer<int, A>, ValueContainer<int, A>>;34 auto mo = M(expected, expected + 3, 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() == 3);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 assert(std::ranges::equal(values, expected | std::views::elements<1>));44 45 // The original flat_map is moved-from.46 assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));47 assert(mo.empty());48 assert(mo.key_comp() == C(5));49 assert(mo.keys().get_allocator() == A(7));50 assert(mo.values().get_allocator() == A(7));51 }52 {53 // moved-from object maintains invariant if one of underlying container does not clear after move54 using M = std::flat_map<int, int, std::less<>, KeyContainer<int>, CopyOnlyVector<int>>;55 M m1 = M({1, 2, 3}, {1, 2, 3});56 M m2(std::move(m1), std::allocator<int>{});57 assert(m2.size() == 3);58 check_invariant(m1);59 LIBCPP_ASSERT(m1.empty());60 LIBCPP_ASSERT(m1.keys().size() == 0);61 LIBCPP_ASSERT(m1.values().size() == 0);62 }63}64 65constexpr bool test() {66 {67 // The constructors in this subclause shall not participate in overload68 // resolution unless uses_allocator_v<key_container_type, Alloc> is true69 // and uses_allocator_v<mapped_container_type, Alloc> is true.70 71 using C = test_less<int>;72 using A1 = test_allocator<int>;73 using A2 = other_allocator<int>;74 using V1 = std::vector<int, A1>;75 using V2 = std::vector<int, A2>;76 using M1 = std::flat_map<int, int, C, V1, V1>;77 using M2 = std::flat_map<int, int, C, V1, V2>;78 using M3 = std::flat_map<int, int, C, V2, V1>;79 static_assert(std::is_constructible_v<M1, M1&&, const A1&>);80 static_assert(!std::is_constructible_v<M1, M1&&, const A2&>);81 static_assert(!std::is_constructible_v<M2, M2&&, const A2&>);82 static_assert(!std::is_constructible_v<M3, M3&&, const A2&>);83 }84 85 test<std::vector, std::vector>();86 87#ifndef __cpp_lib_constexpr_deque88 if (!TEST_IS_CONSTANT_EVALUATED)89#endif90 {91 test<std::deque, std::deque>();92 }93 94 return true;95}96 97int main(int, char**) {98 test();99#if TEST_STD_VER >= 26100 static_assert(test());101#endif102 103 return 0;104}105