Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / iterators / predef.iterators / counted.iterator / minus.iter.pass.cpp
blobf2767a960ffbd015c366d8237d517e833e557753
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 iter_difference_t<I2> operator-(
13 // const counted_iterator& x, const counted_iterator<I2>& y);
15 #include <iterator>
17 #include "test_macros.h"
18 #include "test_iterators.h"
20 // This iterator is common_with forward_iterator but NOT comparable with it.
21 template <class It>
22 class CommonWithForwardIter
24 It it_;
26 public:
27 typedef std::input_iterator_tag iterator_category;
28 typedef typename std::iterator_traits<It>::value_type value_type;
29 typedef typename std::iterator_traits<It>::difference_type difference_type;
30 typedef It pointer;
31 typedef typename std::iterator_traits<It>::reference reference;
33 constexpr It base() const {return it_;}
35 CommonWithForwardIter() = default;
36 explicit constexpr CommonWithForwardIter(It it) : it_(it) {}
37 constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {}
39 constexpr reference operator*() const {return *it_;}
41 constexpr CommonWithForwardIter& operator++() {++it_; return *this;}
42 constexpr CommonWithForwardIter operator++(int)
43 {CommonWithForwardIter tmp(*this); ++(*this); return tmp;}
46 constexpr bool test() {
47 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
50 std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8);
51 std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4);
52 assert(iter1 - iter2 == -4);
53 assert(iter2 - iter1 == 4);
54 assert(iter1.count() == 8);
55 assert(iter2.count() == 4);
57 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
60 const std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8);
61 const std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4);
62 assert(iter1 - iter2 == -4);
63 assert(iter2 - iter1 == 4);
64 assert(iter1.count() == 8);
65 assert(iter2.count() == 4);
67 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
70 std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8);
71 std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6);
72 assert(iter1 - iter2 == -2);
73 assert(iter2 - iter1 == 2);
74 assert(iter1.count() == 8);
75 assert(iter2.count() == 6);
77 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
80 const std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8);
81 const std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6);
82 assert(iter1 - iter2 == -2);
83 assert(iter2 - iter1 == 2);
84 assert(iter1.count() == 8);
85 assert(iter2.count() == 6);
87 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
89 // The minus operator works even if Iter is not a random_access_iterator because
90 // counted_iterator is able to implement it by subtracting the counts rather than
91 // the underlying iterators.
93 std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8);
94 std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
95 assert(iter1 - iter2 == -2);
96 assert(iter2 - iter1 == 2);
97 assert(iter1.count() == 8);
98 assert(iter2.count() == 6);
100 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
103 const std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8);
104 const std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
105 assert(iter1 - iter2 == -2);
106 assert(iter2 - iter1 == 2);
107 assert(iter1.count() == 8);
108 assert(iter2.count() == 6);
110 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
113 return true;
116 int main(int, char**) {
117 test();
118 static_assert(test());
120 return 0;