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// iterator begin() noexcept;
14// const_iterator begin() const noexcept
15// iterator end() noexcept;
16// const_iterator end() const noexcept;
17//
18// const_iterator cbegin() const noexcept;
19// const_iterator cend() const noexcept;
20
21#include <cassert>
22#include <cstddef>
23#include <deque>
24#include <flat_set>
25#include <functional>
26#include <string>
27
28#include "MinSequenceContainer.h"
29#include "test_macros.h"
30#include "min_allocator.h"
31
32template <class KeyContainer>
33void test_one() {
34 using Key = typename KeyContainer::value_type;
35 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;
36
37 M m = {1, 2, 3, 4};
38 const M& cm = m;
39 ASSERT_SAME_TYPE(decltype(m.begin()), typename M::iterator);
40 ASSERT_SAME_TYPE(decltype(m.cbegin()), typename M::const_iterator);
41 ASSERT_SAME_TYPE(decltype(cm.begin()), typename M::const_iterator);
42 ASSERT_SAME_TYPE(decltype(m.end()), typename M::iterator);
43 ASSERT_SAME_TYPE(decltype(m.cend()), typename M::const_iterator);
44 ASSERT_SAME_TYPE(decltype(cm.end()), typename M::const_iterator);
45 static_assert(noexcept(m.begin()));
46 static_assert(noexcept(cm.begin()));
47 static_assert(noexcept(m.cbegin()));
48 static_assert(noexcept(m.end()));
49 static_assert(noexcept(cm.end()));
50 static_assert(noexcept(m.cend()));
51 assert(m.size() == 4);
52 assert(std::distance(m.begin(), m.end()) == 4);
53 assert(std::distance(cm.begin(), cm.end()) == 4);
54 assert(std::distance(m.cbegin(), m.cend()) == 4);
55 typename M::iterator i; // default-construct
56 i = m.begin(); // move-assignment
57 typename M::const_iterator k = i; // converting constructor
58 assert(i == k); // comparison
59 for (int j = 1; j <= 4; ++j, ++i) { // pre-increment
60 assert(*i == j); // operator*
61 }
62 assert(i == m.end());
63 for (int j = 4; j >= 1; --j) {
64 --i; // pre-decrement
65 assert((*i) == j);
66 }
67 assert(i == m.begin());
68}
69
70void test() {
71 test_one<std::vector<int>>();
72 test_one<std::deque<int>>();
73 test_one<MinSequenceContainer<int>>();
74 test_one<std::vector<int, min_allocator<int>>>();
75
76 {
77 // N3644 testing
78 using C = std::flat_set<int>;
79 C::iterator ii1{}, ii2{};
80 C::iterator ii4 = ii1;
81 C::const_iterator cii{};
82 assert(ii1 == ii2);
83 assert(ii1 == ii4);
84 assert(!(ii1 != ii2));
85
86 assert((ii1 == cii));
87 assert((cii == ii1));
88 assert(!(ii1 != cii));
89 assert(!(cii != ii1));
90 }
91}
92
93int main(int, char**) {
94 test();
95
96 return 0;
97}
98

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.iterators/iterator.pass.cpp