Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / iterators / predef.iterators / counted.iterator / compare.pass.cpp
blobeb0c5d8864a7c408b8e051b832cc39bfbd89e689
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // template<common_with<I> I2>
12 // friend constexpr bool operator==(
13 // const counted_iterator& x, const counted_iterator<I2>& y);
14 // friend constexpr bool operator==(
15 // const counted_iterator& x, default_sentinel_t);
17 #include <iterator>
19 #include "test_macros.h"
20 #include "test_iterators.h"
22 // This iterator is common_with forward_iterator but NOT comparable with it.
23 template <class It>
24 class CommonWithForwardIter
26 It it_;
28 public:
29 typedef std::input_iterator_tag iterator_category;
30 typedef typename std::iterator_traits<It>::value_type value_type;
31 typedef typename std::iterator_traits<It>::difference_type difference_type;
32 typedef It pointer;
33 typedef typename std::iterator_traits<It>::reference reference;
35 constexpr It base() const {return it_;}
37 CommonWithForwardIter() = default;
38 explicit constexpr CommonWithForwardIter(It it) : it_(it) {}
39 constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {}
41 constexpr reference operator*() const {return *it_;}
43 constexpr CommonWithForwardIter& operator++() {++it_; return *this;}
44 constexpr CommonWithForwardIter operator++(int)
45 {CommonWithForwardIter tmp(*this); ++(*this); return tmp;}
48 struct InputOrOutputArchetype {
49 using difference_type = int;
51 int *ptr;
53 constexpr int operator*() { return *ptr; }
54 constexpr void operator++(int) { ++ptr; }
55 constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
58 constexpr bool test() {
59 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
63 std::counted_iterator iter1(forward_iterator<int*>(buffer), 8);
64 std::counted_iterator iter2(CommonWithForwardIter<int*>(buffer), 8);
66 assert(iter1 == iter2);
67 assert(iter2 == iter1);
68 ++iter1;
69 assert(iter1 != iter2);
70 assert(iter2 != iter1);
76 std::counted_iterator iter(cpp20_input_iterator<int*>(buffer), 8);
78 assert(iter != std::default_sentinel);
79 assert(std::default_sentinel == std::ranges::next(std::move(iter), 8));
82 std::counted_iterator iter(forward_iterator<int*>(buffer), 8);
84 assert(iter != std::default_sentinel);
85 assert(std::default_sentinel == std::ranges::next(iter, 8));
88 std::counted_iterator iter(random_access_iterator<int*>(buffer), 8);
90 assert(iter != std::default_sentinel);
91 assert(std::default_sentinel == std::ranges::next(iter, 8));
94 std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
96 assert(iter != std::default_sentinel);
97 assert(std::default_sentinel == std::ranges::next(iter, 8));
102 return true;
105 int main(int, char**) {
106 test();
107 static_assert(test());
109 return 0;