brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · de297f1 Raw
129 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_multiset& operator=(const flat_multiset& m);14 15#include <algorithm>16#include <cassert>17#include <deque>18#include <flat_set>19#include <functional>20#include <utility>21#include <vector>22 23#include "operator_hijacker.h"24#include "test_macros.h"25#include "../../../test_compare.h"26#include "test_allocator.h"27 28template <template <class...> class KeyContainer>29constexpr void test() {30  {31    // test_allocator is not propagated32    using C = test_less<int>;33    KeyContainer<int, test_allocator<int>> ks({1, 3, 5, 5}, test_allocator<int>(6));34    using M = std::flat_multiset<int, C, decltype(ks)>;35    auto mo = M(ks, C(5));36    auto m  = M({{3, 4, 5, 4}}, C(3), test_allocator<int>(2));37    m       = mo;38 39    assert(m.key_comp() == C(5));40    assert(std::ranges::equal(m, ks));41    auto keys = std::move(m).extract();42    assert(keys.get_allocator() == test_allocator<int>(2));43 44    // mo is unchanged45    assert(mo.key_comp() == C(5));46    assert(std::ranges::equal(mo, ks));47    auto keys2 = std::move(mo).extract();48    assert(keys2.get_allocator() == test_allocator<int>(6));49  }50  {51    // other_allocator is propagated52    using C              = test_less<int>;53    using Ks             = KeyContainer<int, other_allocator<int>>;54    auto ks              = Ks({1, 3, 5, 3}, other_allocator<int>(6));55    const int expected[] = {1, 3, 3, 5};56    using M              = std::flat_multiset<int, C, Ks>;57    auto mo              = M(Ks(ks, other_allocator<int>(6)), C(5));58    auto m               = M({3, 4, 5}, C(3), other_allocator<int>(2));59    m                    = mo;60 61    assert(m.key_comp() == C(5));62    assert(std::ranges::equal(m, expected));63    auto keys = std::move(m).extract();64    assert(keys.get_allocator() == other_allocator<int>(6));65 66    // mo is unchanged67    assert(mo.key_comp() == C(5));68    assert(std::ranges::equal(mo, expected));69    auto keys2 = std::move(mo).extract();70    assert(keys2.get_allocator() == other_allocator<int>(6));71  }72  if (!TEST_IS_CONSTANT_EVALUATED) {73    // comparator is copied and invariant is preserved74    using M = std::flat_multiset<int, std::function<bool(int, int)>>;75    M mo    = M({1, 2}, std::less<int>());76    M m     = M({1, 2}, std::greater<int>());77    assert(m.key_comp()(2, 1) == true);78    assert(m != mo);79    m = mo;80    assert(m.key_comp()(2, 1) == false);81    assert(m == mo);82  }83  {84    // self-assignment85    using M = std::flat_multiset<int>;86    M m     = {{1, 2}};87    m       = std::as_const(m);88    assert((m == M{{1, 2}}));89  }90  {91    // was empty92    using M = std::flat_multiset<int>;93    M m;94    assert(m.size() == 0);95    m              = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5};96    int expected[] = {1, 2, 2, 3, 3, 3, 4, 5, 5, 6};97    assert(std::ranges::equal(m, expected));98  }99  {100    // Validate whether the container can be copy-assigned (move-assigned, swapped)101    // with an ADL-hijacking operator&102    std::flat_multiset<operator_hijacker> so;103    std::flat_multiset<operator_hijacker> s;104    s = so;105    s = std::move(so);106    swap(s, so);107  }108}109 110constexpr bool test() {111  test<std::vector>();112 113#ifndef __cpp_lib_constexpr_deque114  if (!TEST_IS_CONSTANT_EVALUATED)115#endif116    test<std::deque>();117 118  return true;119}120 121int main(int, char**) {122  test();123#if TEST_STD_VER >= 26124  static_assert(test());125#endif126 127  return 0;128}129