brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · de4cef1 Raw
112 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&&);14 15#include <algorithm>16#include <deque>17#include <flat_map>18#include <functional>19#include <type_traits>20#include <utility>21#include <vector>22 23#include "../helpers.h"24#include "test_macros.h"25#include "../../../test_compare.h"26#include "test_allocator.h"27#include "min_allocator.h"28 29template <template <class...> class KeyContainer, template <class...> class ValueContainer>30constexpr void test() {31  {32    using C = test_less<int>;33    using A = test_allocator<int>;34    using M = std::flat_map<int, int, C, KeyContainer<int, A>, ValueContainer<int, A>>;35    M mo    = M({{1, 1}, {2, 2}, {3, 1}}, C(5), A(7));36    M m     = std::move(mo);37    assert((m == M{{1, 1}, {2, 2}, {3, 1}}));38    assert(m.key_comp() == C(5));39    assert(m.keys().get_allocator() == A(7));40    assert(m.values().get_allocator() == A(7));41 42    assert(mo.empty());43    assert(mo.key_comp() == C(5));44    assert(mo.keys().get_allocator().get_id() == test_alloc_base::moved_value);45    assert(mo.values().get_allocator().get_id() == test_alloc_base::moved_value);46  }47  {48    using C = test_less<int>;49    using A = min_allocator<int>;50    using M = std::flat_map<int, int, C, KeyContainer<int, A>, ValueContainer<int, A>>;51    M mo    = M({{1, 1}, {2, 2}, {3, 1}}, C(5), A());52    M m     = std::move(mo);53    assert((m == M{{1, 1}, {2, 2}, {3, 1}}));54    assert(m.key_comp() == C(5));55    assert(m.keys().get_allocator() == A());56    assert(m.values().get_allocator() == A());57 58    assert(mo.empty());59    assert(mo.key_comp() == C(5));60    assert(m.keys().get_allocator() == A());61    assert(m.values().get_allocator() == A());62  }63  if (!TEST_IS_CONSTANT_EVALUATED) {64    // A moved-from flat_map maintains its class invariant in the presence of moved-from comparators.65    using M = std::flat_map<int, int, std::function<bool(int, int)>, KeyContainer<int>, ValueContainer<int>>;66    M mo    = M({{1, 1}, {2, 2}, {3, 1}}, std::less<int>());67    M m     = std::move(mo);68    assert(m.size() == 3);69    assert(std::is_sorted(m.begin(), m.end(), m.value_comp()));70    assert(m.key_comp()(1, 2) == true);71 72    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));73    LIBCPP_ASSERT(m.key_comp()(1, 2) == true);74    LIBCPP_ASSERT(mo.empty());75    mo.insert({{1, 1}, {2, 2}, {3, 1}}); // insert has no preconditions76    assert(m == mo);77  }78  {79    // moved-from object maintains invariant if one of underlying container does not clear after move80    using M = std::flat_map<int, int, std::less<>, KeyContainer<int>, CopyOnlyVector<int>>;81    M m1    = M({1, 2, 3}, {1, 2, 3});82    M m2    = std::move(m1);83    assert(m2.size() == 3);84    check_invariant(m1);85    LIBCPP_ASSERT(m1.empty());86    LIBCPP_ASSERT(m1.keys().size() == 0);87    LIBCPP_ASSERT(m1.values().size() == 0);88  }89}90 91constexpr bool test() {92  test<std::vector, std::vector>();93 94#ifndef __cpp_lib_constexpr_deque95  if (!TEST_IS_CONSTANT_EVALUATED)96#endif97  {98    test<std::deque, std::deque>();99  }100 101  return true;102}103 104int main(int, char**) {105  test();106#if TEST_STD_VER >= 26107  static_assert(test());108#endif109 110  return 0;111}112