xref: /llvm-project/libcxx/test/std/ranges/range.factories/range.iota.view/ctor.value.pass.cpp (revision 0a4aa8a122aa097499c498b639a75b5e9a73e9f0)
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 // constexpr explicit iota_view(W value);
12 
13 #include <cassert>
14 #include <ranges>
15 #include <type_traits>
16 
17 #include "test_macros.h"
18 #include "types.h"
19 
20 struct SomeIntComparable {
21   using difference_type = int;
22 
23   SomeInt value_;
SomeIntComparableSomeIntComparable24   constexpr SomeIntComparable() : value_(SomeInt(10)) {}
25 
operator ==(SomeIntComparable lhs,SomeIntComparable rhs)26   friend constexpr bool operator==(SomeIntComparable lhs, SomeIntComparable rhs) {
27     return lhs.value_ == rhs.value_;
28   }
operator ==(SomeIntComparable lhs,SomeInt rhs)29   friend constexpr bool operator==(SomeIntComparable lhs, SomeInt rhs) {
30     return lhs.value_ == rhs;
31   }
operator ==(SomeInt lhs,SomeIntComparable rhs)32   friend constexpr bool operator==(SomeInt lhs, SomeIntComparable rhs) {
33     return lhs == rhs.value_;
34   }
35 
operator -(SomeIntComparable lhs,SomeIntComparable rhs)36   friend constexpr difference_type operator-(SomeIntComparable lhs, SomeIntComparable rhs) {
37     return lhs.value_ - rhs.value_;
38   }
39 
operator ++SomeIntComparable40   constexpr SomeIntComparable& operator++() { ++value_; return *this; }
operator ++SomeIntComparable41   constexpr SomeIntComparable  operator++(int) { auto tmp = *this; ++value_; return tmp; }
operator --SomeIntComparable42   constexpr SomeIntComparable  operator--() { --value_; return *this; }
43 };
44 
test()45 constexpr bool test() {
46   {
47     std::ranges::iota_view<SomeInt> io(SomeInt(42));
48     assert((*io.begin()).value_ == 42);
49     // Check that end returns std::unreachable_sentinel.
50     assert(io.end() != io.begin());
51     static_assert(std::same_as<decltype(io.end()), std::unreachable_sentinel_t>);
52   }
53 
54   {
55     std::ranges::iota_view<SomeInt, SomeIntComparable> io(SomeInt(0));
56     assert(std::ranges::next(io.begin(), 10) == io.end());
57   }
58   {
59     static_assert(!std::is_convertible_v<std::ranges::iota_view<SomeInt>, SomeInt>);
60     static_assert( std::is_constructible_v<std::ranges::iota_view<SomeInt>, SomeInt>);
61   }
62 
63   return true;
64 }
65 
main(int,char **)66 int main(int, char**) {
67   test();
68   static_assert(test());
69 
70   return 0;
71 }
72