| 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 | |
| 32 | template <class KeyContainer> |
| 33 | void test_one() { |
| 34 | using Key = typename KeyContainer::value_type; |
| 35 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
| 36 | |
| 37 | M m = {1, 2, 3, 4, 1, 4, 2, 3, 1}; |
| 38 | int expected[] = {1, 1, 1, 2, 2, 3, 3, 4, 4}; |
| 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() == 9); |
| 53 | assert(std::distance(m.begin(), m.end()) == 9); |
| 54 | assert(std::distance(cm.begin(), cm.end()) == 9); |
| 55 | assert(std::distance(m.cbegin(), m.cend()) == 9); |
| 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 = 0; j < 9; ++j, ++i) { // pre-increment |
| 61 | assert(*i == expected[j]); // operator* |
| 62 | } |
| 63 | assert(i == m.end()); |
| 64 | for (int j = 8; j >= 0; --j) { |
| 65 | --i; // pre-decrement |
| 66 | assert((*i) == expected[j]); |
| 67 | } |
| 68 | assert(i == m.begin()); |
| 69 | } |
| 70 | |
| 71 | void test() { |
| 72 | test_one<std::vector<int>>(); |
| 73 | test_one<std::deque<int>>(); |
| 74 | test_one<MinSequenceContainer<int>>(); |
| 75 | test_one<std::vector<int, min_allocator<int>>>(); |
| 76 | |
| 77 | { |
| 78 | // N3644 testing |
| 79 | using C = std::flat_multiset<int>; |
| 80 | C::iterator ii1{}, ii2{}; |
| 81 | C::iterator ii4 = ii1; |
| 82 | C::const_iterator cii{}; |
| 83 | assert(ii1 == ii2); |
| 84 | assert(ii1 == ii4); |
| 85 | assert(!(ii1 != ii2)); |
| 86 | |
| 87 | assert((ii1 == cii)); |
| 88 | assert((cii == ii1)); |
| 89 | assert(!(ii1 != cii)); |
| 90 | assert(!(cii != ii1)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int main(int, char**) { |
| 95 | test(); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |