brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 9e88cbb Raw
95 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_set>12 13// flat_set(flat_set&&, const allocator_type&);14 15#include <algorithm>16#include <deque>17#include <flat_set>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>28constexpr void test() {29  {30    int expected[] = {1, 2, 3};31    using C        = test_less<int>;32    using A        = test_allocator<int>;33    using M        = std::flat_set<int, C, KeyContainer<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 = std::move(m).extract();40    assert(keys.get_allocator() == A(3));41    assert(std::ranges::equal(keys, expected));42 43    // The original flat_set is moved-from.44    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));45    assert(mo.empty());46    assert(mo.key_comp() == C(5));47    assert(std::move(mo).extract().get_allocator() == A(7));48  }49  {50    // moved-from object maintains invariant if one of underlying container does not clear after move51    using M = std::flat_set<int, std::less<>, CopyOnlyVector<int>>;52    M m1    = M({1, 2, 3});53    M m2(std::move(m1), std::allocator<int>{});54    assert(m2.size() == 3);55    check_invariant(m1);56    LIBCPP_ASSERT(m1.empty());57  }58}59 60constexpr bool test() {61  {62    // The constructors in this subclause shall not participate in overload63    // resolution unless uses_allocator_v<container_type, Alloc> is true.64 65    using C  = test_less<int>;66    using A1 = test_allocator<int>;67    using A2 = other_allocator<int>;68    using V1 = std::vector<int, A1>;69    using V2 = std::vector<int, A2>;70    using M1 = std::flat_set<int, C, V1>;71    using M2 = std::flat_set<int, C, V2>;72    static_assert(std::is_constructible_v<M1, M1&&, const A1&>);73    static_assert(std::is_constructible_v<M2, M2&&, const A2&>);74    static_assert(!std::is_constructible_v<M1, M1&&, const A2&>);75    static_assert(!std::is_constructible_v<M2, M2&&, const A1&>);76  }77 78  test<std::vector>();79#ifndef __cpp_lib_constexpr_deque80  if (!TEST_IS_CONSTANT_EVALUATED)81#endif82    test<std::deque>();83 84  return true;85}86 87int main(int, char**) {88  test();89#if TEST_STD_VER >= 2690  static_assert(test());91#endif92 93  return 0;94}95