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_map>
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_map>
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, class ValueContainer>
33void test() {
34 using Key = typename KeyContainer::value_type;
35 using Value = typename ValueContainer::value_type;
36 using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;
37
38 M m = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}};
39 const M& cm = m;
40 ASSERT_SAME_TYPE(decltype(m.begin()), typename M::iterator);
41 ASSERT_SAME_TYPE(decltype(m.cbegin()), typename M::const_iterator);
42 ASSERT_SAME_TYPE(decltype(cm.begin()), typename M::const_iterator);
43 ASSERT_SAME_TYPE(decltype(m.end()), typename M::iterator);
44 ASSERT_SAME_TYPE(decltype(m.cend()), typename M::const_iterator);
45 ASSERT_SAME_TYPE(decltype(cm.end()), typename M::const_iterator);
46 static_assert(noexcept(m.begin()));
47 static_assert(noexcept(cm.begin()));
48 static_assert(noexcept(m.cbegin()));
49 static_assert(noexcept(m.end()));
50 static_assert(noexcept(cm.end()));
51 static_assert(noexcept(m.cend()));
52 assert(m.size() == 4);
53 assert(std::distance(m.begin(), m.end()) == 4);
54 assert(std::distance(cm.begin(), cm.end()) == 4);
55 assert(std::distance(m.cbegin(), m.cend()) == 4);
56 typename M::iterator i; // default-construct
57 i = m.begin(); // move-assignment
58 typename M::const_iterator k = i; // converting constructor
59 assert(i == k); // comparison
60 for (int j = 1; j <= 4; ++j, ++i) { // pre-increment
61 assert(i->first == j); // operator->
62 assert(i->second == 'a' + j - 1);
63 }
64 assert(i == m.end());
65 for (int j = 4; j >= 1; --j) {
66 --i; // pre-decrement
67 assert((*i).first == j);
68 assert((*i).second == 'a' + j - 1);
69 }
70 assert(i == m.begin());
71}
72
73int main(int, char**) {
74 test<std::vector<int>, std::vector<char>>();
75 test<std::deque<int>, std::vector<char>>();
76 test<MinSequenceContainer<int>, MinSequenceContainer<char>>();
77 test<std::vector<int, min_allocator<int>>, std::vector<char, min_allocator<char>>>();
78
79 {
80 // N3644 testing
81 using C = std::flat_map<int, char>;
82 C::iterator ii1{}, ii2{};
83 C::iterator ii4 = ii1;
84 C::const_iterator cii{};
85 assert(ii1 == ii2);
86 assert(ii1 == ii4);
87 assert(!(ii1 != ii2));
88
89 assert((ii1 == cii));
90 assert((cii == ii1));
91 assert(!(ii1 != cii));
92 assert(!(cii != ii1));
93 }
94
95 return 0;
96}
97

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