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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
11// <flat_set>
12
13// class flat_set
14
15// void clear() noexcept;
16
17#include <cassert>
18#include <deque>
19#include <flat_set>
20#include <functional>
21#include <vector>
22
23#include "MinSequenceContainer.h"
24#include "../helpers.h"
25#include "test_macros.h"
26#include "min_allocator.h"
27
28// test noexcept
29
30template <class T>
31concept NoExceptClear = requires(T t) {
32 { t.clear() } noexcept;
33};
34
35static_assert(NoExceptClear<std::flat_set<int>>);
36#ifndef TEST_HAS_NO_EXCEPTIONS
37static_assert(NoExceptClear<std::flat_set<int, std::less<int>, ThrowOnMoveContainer<int>>>);
38#endif
39
40template <class KeyContainer>
41constexpr void test_one() {
42 using Key = typename KeyContainer::value_type;
43 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;
44 {
45 M m = {1, 2, 3, 4, 5};
46 assert(m.size() == 5);
47 ASSERT_NOEXCEPT(m.clear());
48 ASSERT_SAME_TYPE(decltype(m.clear()), void);
49 m.clear();
50 assert(m.size() == 0);
51 }
52 {
53 // was empty
54 M m;
55 assert(m.size() == 0);
56 m.clear();
57 assert(m.size() == 0);
58 }
59}
60
61constexpr bool test() {
62 test_one<std::vector<int>>();
63#ifndef __cpp_lib_constexpr_deque
64 if (!TEST_IS_CONSTANT_EVALUATED)
65#endif
66 test_one<std::deque<int>>();
67 test_one<MinSequenceContainer<int>>();
68 test_one<std::vector<int, min_allocator<int>>>();
69 test_one<std::vector<int, min_allocator<int>>>();
70
71 return true;
72}
73
74int main(int, char**) {
75 test();
76#if TEST_STD_VER >= 26
77 static_assert(test());
78#endif
79
80 return 0;
81}
82

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/clear.pass.cpp