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
10
11// iota_view::<iterator>::operator{<,>,<=,>=,==,!=,<=>}
12
13#include <ranges>
14#include <compare>
15
16#include "test_macros.h"
17#include "test_iterators.h"
18#include "../types.h"
19
20constexpr bool test() {
21 {
22 // Test `int`, which has operator<=>; the iota iterator should also have operator<=>.
23 using R = std::ranges::iota_view<int>;
24 static_assert(std::three_way_comparable<std::ranges::iterator_t<R>>);
25
26 std::same_as<R> auto r = std::views::iota(42);
27 auto iter1 = r.begin();
28 auto iter2 = iter1 + 1;
29
30 assert(!(iter1 < iter1)); assert(iter1 < iter2); assert(!(iter2 < iter1));
31 assert(iter1 <= iter1); assert(iter1 <= iter2); assert(!(iter2 <= iter1));
32 assert(!(iter1 > iter1)); assert(!(iter1 > iter2)); assert(iter2 > iter1);
33 assert(iter1 >= iter1); assert(!(iter1 >= iter2)); assert(iter2 >= iter1);
34 assert(iter1 == iter1); assert(!(iter1 == iter2)); assert(iter2 == iter2);
35 assert(!(iter1 != iter1)); assert(iter1 != iter2); assert(!(iter2 != iter2));
36
37 assert((iter1 <=> iter2) == std::strong_ordering::less);
38 assert((iter1 <=> iter1) == std::strong_ordering::equal);
39 assert((iter2 <=> iter1) == std::strong_ordering::greater);
40 }
41
42 {
43 // Test a new-school iterator with operator<=>; the iota iterator should also have operator<=>.
44 using It = three_way_contiguous_iterator<int*>;
45 static_assert(std::three_way_comparable<It>);
46 using R = std::ranges::iota_view<It>;
47 static_assert(std::three_way_comparable<std::ranges::iterator_t<R>>);
48
49 int a[] = {1,2,3};
50 std::same_as<R> auto r = std::views::iota(It(a));
51 auto iter1 = r.begin();
52 auto iter2 = iter1 + 1;
53
54 assert(!(iter1 < iter1)); assert(iter1 < iter2); assert(!(iter2 < iter1));
55 assert(iter1 <= iter1); assert(iter1 <= iter2); assert(!(iter2 <= iter1));
56 assert(!(iter1 > iter1)); assert(!(iter1 > iter2)); assert(iter2 > iter1);
57 assert(iter1 >= iter1); assert(!(iter1 >= iter2)); assert(iter2 >= iter1);
58 assert(iter1 == iter1); assert(!(iter1 == iter2)); assert(iter2 == iter2);
59 assert(!(iter1 != iter1)); assert(iter1 != iter2); assert(!(iter2 != iter2));
60
61 assert((iter1 <=> iter2) == std::strong_ordering::less);
62 assert((iter1 <=> iter1) == std::strong_ordering::equal);
63 assert((iter2 <=> iter1) == std::strong_ordering::greater);
64 }
65
66 {
67 // Test an old-school iterator with no operator<=>; the iota iterator shouldn't have operator<=> either.
68 using It = random_access_iterator<int*>;
69 static_assert(!std::three_way_comparable<It>);
70 using R = std::ranges::iota_view<It>;
71 static_assert(!std::three_way_comparable<std::ranges::iterator_t<R>>);
72
73 int a[] = {1,2,3};
74 std::same_as<R> auto r = std::views::iota(It(a));
75 auto iter1 = r.begin();
76 auto iter2 = iter1 + 1;
77
78 assert(!(iter1 < iter1)); assert(iter1 < iter2); assert(!(iter2 < iter1));
79 assert(iter1 <= iter1); assert(iter1 <= iter2); assert(!(iter2 <= iter1));
80 assert(!(iter1 > iter1)); assert(!(iter1 > iter2)); assert(iter2 > iter1);
81 assert(iter1 >= iter1); assert(!(iter1 >= iter2)); assert(iter2 >= iter1);
82 assert(iter1 == iter1); assert(!(iter1 == iter2)); assert(iter2 == iter2);
83 assert(!(iter1 != iter1)); assert(iter1 != iter2); assert(!(iter2 != iter2));
84 }
85
86 return true;
87}
88
89int main(int, char**) {
90 test();
91 static_assert(test());
92
93 return 0;
94}
95

source code of libcxx/test/std/ranges/range.factories/range.iota.view/iterator/compare.pass.cpp