117 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& operator=(const flat_set& m);14 15#include <algorithm>16#include <cassert>17#include <deque>18#include <flat_set>19#include <functional>20#include <vector>21 22#include "operator_hijacker.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 // test_allocator is not propagated31 using C = test_less<int>;32 KeyContainer<int, test_allocator<int>> ks({1, 3, 5}, test_allocator<int>(6));33 using M = std::flat_set<int, C, decltype(ks)>;34 auto mo = M(ks, C(5));35 auto m = M({{3, 4, 5}}, C(3), test_allocator<int>(2));36 m = mo;37 38 assert(m.key_comp() == C(5));39 assert(std::ranges::equal(m, ks));40 auto keys = std::move(m).extract();41 assert(keys.get_allocator() == test_allocator<int>(2));42 43 // mo is unchanged44 assert(mo.key_comp() == C(5));45 assert(std::ranges::equal(mo, ks));46 auto keys2 = std::move(mo).extract();47 assert(keys2.get_allocator() == test_allocator<int>(6));48 }49 {50 // other_allocator is propagated51 using C = test_less<int>;52 using Ks = KeyContainer<int, other_allocator<int>>;53 auto ks = Ks({1, 3, 5}, other_allocator<int>(6));54 using M = std::flat_set<int, C, Ks>;55 auto mo = M(Ks(ks, other_allocator<int>(6)), C(5));56 auto m = M({3, 4, 5}, C(3), other_allocator<int>(2));57 m = mo;58 59 assert(m.key_comp() == C(5));60 assert(std::ranges::equal(m, ks));61 auto keys = std::move(m).extract();62 assert(keys.get_allocator() == other_allocator<int>(6));63 64 // mo is unchanged65 assert(mo.key_comp() == C(5));66 assert(std::ranges::equal(mo, ks));67 auto keys2 = std::move(mo).extract();68 assert(keys2.get_allocator() == other_allocator<int>(6));69 }70 if (!TEST_IS_CONSTANT_EVALUATED) {71 // comparator is copied and invariant is preserved72 using M = std::flat_set<int, std::function<bool(int, int)>, KeyContainer<int>>;73 M mo = M({1, 2}, std::less<int>());74 M m = M({1, 2}, std::greater<int>());75 assert(m.key_comp()(2, 1) == true);76 assert(m != mo);77 m = mo;78 assert(m.key_comp()(2, 1) == false);79 assert(m == mo);80 }81 {82 // self-assignment83 using M = std::flat_set<int, std::less<int>, KeyContainer<int>>;84 M m = {{1, 2}};85 m = static_cast<const M&>(m);86 assert((m == M{{1, 2}}));87 }88 {89 // Validate whether the container can be copy-assigned (move-assigned, swapped)90 // with an ADL-hijacking operator&91 std::flat_set<operator_hijacker> so;92 std::flat_set<operator_hijacker> s;93 s = so;94 s = std::move(so);95 swap(s, so);96 }97}98 99constexpr bool test() {100 test<std::vector>();101#ifndef __cpp_lib_constexpr_deque102 if (!TEST_IS_CONSTANT_EVALUATED)103#endif104 test<std::deque>();105 106 return true;107}108 109int main(int, char**) {110 test();111#if TEST_STD_VER >= 26112 static_assert(test());113#endif114 115 return 0;116}117