97 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// void replace(container_type&& key_cont);14 15#include <algorithm>16#include <deque>17#include <concepts>18#include <flat_set>19#include <functional>20 21#include "MinSequenceContainer.h"22#include "../helpers.h"23#include "test_macros.h"24#include "min_allocator.h"25 26template <class T, class... Args>27concept CanReplace = requires(T t, Args&&... args) { t.replace(std::forward<Args>(args)...); };28 29using Set = std::flat_multiset<int, int>;30static_assert(CanReplace<Set, std::vector<int>>);31static_assert(!CanReplace<Set, const std::vector<int>&>);32 33template <class KeyContainer>34constexpr void test_one() {35 using Key = typename KeyContainer::value_type;36 using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>;37 {38 // was empty39 M m;40 KeyContainer new_keys = {7, 7, 8};41 auto expected_keys = new_keys;42 m.replace(std::move(new_keys));43 assert(m.size() == 3);44 assert(std::ranges::equal(m, expected_keys));45 }46 {47 M m = M({1, 1, 2, 2, 3});48 KeyContainer new_keys = {7, 7, 8, 8};49 auto expected_keys = new_keys;50 m.replace(std::move(new_keys));51 assert(m.size() == 4);52 assert(std::ranges::equal(m, expected_keys));53 }54}55 56constexpr bool test() {57 test_one<std::vector<int>>();58#ifndef __cpp_lib_constexpr_deque59 if (!TEST_IS_CONSTANT_EVALUATED)60#endif61 test_one<std::deque<int>>();62 test_one<MinSequenceContainer<int>>();63 test_one<std::vector<int, min_allocator<int>>>();64 65 return true;66}67 68void test_exception() {69#ifndef TEST_HAS_NO_EXCEPTIONS70 using KeyContainer = ThrowOnMoveContainer<int>;71 using M = std::flat_multiset<int, std::ranges::less, KeyContainer>;72 73 M m;74 m.emplace(1);75 m.emplace(2);76 try {77 KeyContainer new_keys{3, 4};78 m.replace(std::move(new_keys));79 assert(false);80 } catch (int) {81 check_invariant(m);82 // In libc++, we clear the set83 LIBCPP_ASSERT(m.size() == 0);84 }85#endif86}87 88int main(int, char**) {89 test();90#if TEST_STD_VER >= 2691 static_assert(test());92#endif93 test_exception();94 95 return 0;96}97