xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.transform/iterator/compare.pass.cpp (revision b8cb1dc9ea87faa8e8e9ab7a31710a8c0bb8b084)
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 // transform_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 
test()20 constexpr bool test() {
21   {
22     // Test a new-school iterator with operator<=>; the transform iterator should also have operator<=>.
23     using It = three_way_contiguous_iterator<int*>;
24     static_assert(std::three_way_comparable<It>);
25     using R = std::ranges::transform_view<std::ranges::subrange<It>, PlusOne>;
26     static_assert(std::three_way_comparable<std::ranges::iterator_t<R>>);
27 
28     int a[] = {1,2,3};
29     std::same_as<R> auto r = std::ranges::subrange<It>(It(a), It(a+3)) | std::views::transform(PlusOne());
30     auto iter1 = r.begin();
31     auto iter2 = iter1 + 1;
32 
33     assert(!(iter1 < iter1));  assert(iter1 < iter2);     assert(!(iter2 < iter1));
34     assert(iter1 <= iter1);    assert(iter1 <= iter2);    assert(!(iter2 <= iter1));
35     assert(!(iter1 > iter1));  assert(!(iter1 > iter2));  assert(iter2 > iter1);
36     assert(iter1 >= iter1);    assert(!(iter1 >= iter2)); assert(iter2 >= iter1);
37     assert(iter1 == iter1);    assert(!(iter1 == iter2)); assert(iter2 == iter2);
38     assert(!(iter1 != iter1)); assert(iter1 != iter2);    assert(!(iter2 != iter2));
39 
40     assert((iter1 <=> iter2) == std::strong_ordering::less);
41     assert((iter1 <=> iter1) == std::strong_ordering::equal);
42     assert((iter2 <=> iter1) == std::strong_ordering::greater);
43   }
44 
45   {
46     // Test an old-school iterator with no operator<=>; the transform iterator shouldn't have operator<=> either.
47     using It = random_access_iterator<int*>;
48     static_assert(!std::three_way_comparable<It>);
49     using R = std::ranges::transform_view<std::ranges::subrange<It>, PlusOne>;
50     static_assert(!std::three_way_comparable<std::ranges::iterator_t<R>>);
51 
52     int a[] = {1,2,3};
53     std::same_as<R> auto r = std::ranges::subrange<It>(It(a), It(a+3)) | std::views::transform(PlusOne());
54     auto iter1 = r.begin();
55     auto iter2 = iter1 + 1;
56 
57     assert(!(iter1 < iter1));  assert(iter1 < iter2);     assert(!(iter2 < iter1));
58     assert(iter1 <= iter1);    assert(iter1 <= iter2);    assert(!(iter2 <= iter1));
59     assert(!(iter1 > iter1));  assert(!(iter1 > iter2));  assert(iter2 > iter1);
60     assert(iter1 >= iter1);    assert(!(iter1 >= iter2)); assert(iter2 >= iter1);
61     assert(iter1 == iter1);    assert(!(iter1 == iter2)); assert(iter2 == iter2);
62     assert(!(iter1 != iter1)); assert(iter1 != iter2);    assert(!(iter2 != iter2));
63   }
64 
65   return true;
66 }
67 
main(int,char **)68 int main(int, char**) {
69   test();
70   static_assert(test());
71 
72   return 0;
73 }
74