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// 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_set<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_set<Key, std::less<Key>, KeyContainer>;37 {38 // was empty39 M m;40 KeyContainer new_keys = {7, 8};41 auto expected_keys = new_keys;42 m.replace(std::move(new_keys));43 assert(m.size() == 2);44 assert(std::ranges::equal(m, expected_keys));45 }46 M m = M({1, 2, 3});47 KeyContainer new_keys = {7, 8};48 auto expected_keys = new_keys;49 m.replace(std::move(new_keys));50 assert(m.size() == 2);51 assert(std::ranges::equal(m, expected_keys));52}53 54constexpr bool test() {55 test_one<std::vector<int>>();56#ifndef __cpp_lib_constexpr_deque57 if (!TEST_IS_CONSTANT_EVALUATED)58#endif59 test_one<std::deque<int>>();60 test_one<MinSequenceContainer<int>>();61 test_one<std::vector<int, min_allocator<int>>>();62 63 return true;64}65 66void test_exception() {67#ifndef TEST_HAS_NO_EXCEPTIONS68 using KeyContainer = ThrowOnMoveContainer<int>;69 using M = std::flat_set<int, std::ranges::less, KeyContainer>;70 71 M m;72 m.emplace(1);73 m.emplace(2);74 try {75 KeyContainer new_keys{3, 4};76 m.replace(std::move(new_keys));77 assert(false);78 } catch (int) {79 check_invariant(m);80 // In libc++, we clear the map81 LIBCPP_ASSERT(m.size() == 0);82 }83#endif84}85 86int main(int, char**) {87 test();88 test_exception();89#if TEST_STD_VER >= 2690 static_assert(test());91#endif92 93 return 0;94}95