Debian Codegraph @

libcxx/test/std/containers/views/views.span/span.iterators/iterator.pass.cpp

raw
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 // <span>
12
13 // class iterator
14
15 #include <cassert>
16 #include <concepts>
17 #include <iterator>
18 #include <span>
19 #include <string>
20 #include <version> // __cpp_lib_ranges_as_const is not defined in span.
21
22 #include "test_macros.h"
23
24 template <class T>
25 constexpr void test_type() {
26 using C = std::span<T>;
27 typename C::iterator ii1{}, ii2{};
28 typename C::iterator ii4 = ii1;
29 // TODO Test against C++23 after implementing
30 // P2278R4 cbegin should always return a constant iterator
31 // The means adjusting the #ifdef to guard against C++23.
32 #ifdef __cpp_lib_ranges_as_const
33 typename C::const_iterator cii{};
34 #endif
35 assert(ii1 == ii2);
36 assert(ii1 == ii4);
37 #ifdef __cpp_lib_ranges_as_const
38 assert(ii1 == cii);
39 #endif
40
41 assert(!(ii1 != ii2));
42 #ifdef __cpp_lib_ranges_as_const
43 assert(!(ii1 != cii));
44 #endif
45
46 T v;
47 C c{&v, 1};
48 assert(c.begin() == std::begin(c));
49 assert(c.rbegin() == std::rbegin(c));
50 #ifdef __cpp_lib_ranges_as_const
51 assert(c.cbegin() == std::cbegin(c));
52 assert(c.crbegin() == std::crbegin(c));
53 #endif
54
55 assert(c.end() == std::end(c));
56 assert(c.rend() == std::rend(c));
57 #ifdef __cpp_lib_ranges_as_const
58 assert(c.cend() == std::cend(c));
59 assert(c.crend() == std::crend(c));
60 #endif
61
62 assert(std::begin(c) != std::end(c));
63 assert(std::rbegin(c) != std::rend(c));
64 #ifdef __cpp_lib_ranges_as_const
65 assert(std::cbegin(c) != std::cend(c));
66 assert(std::crbegin(c) != std::crend(c));
67 #endif
68
69 // P1614 + LWG3352
70 std::same_as<std::strong_ordering> decltype(auto) r1 = ii1 <=> ii2;
71 assert(r1 == std::strong_ordering::equal);
72
73 #ifdef __cpp_lib_ranges_as_const
74 std::same_as<std::strong_ordering> decltype(auto) r2 = cii <=> ii2;
75 assert(r2 == std::strong_ordering::equal);
76 #endif
77 }
78
79 constexpr bool test() {
80 test_type<char>();
81 test_type<int>();
82 test_type<std::string>();
83
84 return true;
85 }
86
87 int main(int, char**) {
88 test();
89 static_assert(test(), "");
90
91 return 0;
92 }