xref: /llvm-project/libcxx/test/std/ranges/range.factories/range.iota.view/iterator/star.pass.cpp (revision e4e0288f5bb191128f4c1b66baa3fd6f8e3bd26a)
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 W operator*() const noexcept(is_nothrow_copy_constructible_v<W>);
12 
13 #include "test_macros.h"
14 
15 TEST_CLANG_DIAGNOSTIC_IGNORED("-Wsign-compare")
16 TEST_GCC_DIAGNOSTIC_IGNORED("-Wsign-compare")
17 TEST_MSVC_DIAGNOSTIC_IGNORED(4018) // various "signed/unsigned mismatch"
18 
19 #include <ranges>
20 #include <cassert>
21 
22 #include "../types.h"
23 
24 struct NotNoexceptCopy {
25   using difference_type = int;
26 
27   int value_;
NotNoexceptCopyNotNoexceptCopy28   constexpr explicit NotNoexceptCopy(int value = 0) : value_(value) {}
NotNoexceptCopyNotNoexceptCopy29   constexpr NotNoexceptCopy(const NotNoexceptCopy& other) noexcept(false) : value_(other.value_) {}
30 
31   bool operator==(const NotNoexceptCopy&) const = default;
32 
operator +=(NotNoexceptCopy & lhs,const NotNoexceptCopy & rhs)33   friend constexpr NotNoexceptCopy& operator+=(NotNoexceptCopy &lhs, const NotNoexceptCopy& rhs) {
34     lhs.value_ += rhs.value_; return lhs;
35   }
operator -=(NotNoexceptCopy & lhs,const NotNoexceptCopy & rhs)36   friend constexpr NotNoexceptCopy& operator-=(NotNoexceptCopy &lhs, const NotNoexceptCopy& rhs) {
37     lhs.value_ -= rhs.value_; return lhs;
38   }
39 
operator +(NotNoexceptCopy lhs,NotNoexceptCopy rhs)40   friend constexpr NotNoexceptCopy operator+(NotNoexceptCopy lhs, NotNoexceptCopy rhs) {
41     return NotNoexceptCopy{lhs.value_ + rhs.value_};
42   }
operator -(NotNoexceptCopy lhs,NotNoexceptCopy rhs)43   friend constexpr int operator-(NotNoexceptCopy lhs, NotNoexceptCopy rhs) {
44     return lhs.value_ - rhs.value_;
45   }
46 
operator ++NotNoexceptCopy47   constexpr NotNoexceptCopy& operator++()     { ++value_; return *this; }
operator ++NotNoexceptCopy48   constexpr void              operator++(int) { ++value_;               }
49 };
50 
51 template<class T>
testType()52 constexpr void testType() {
53   {
54     std::ranges::iota_view<T> io(T(0));
55     auto iter = io.begin();
56     for (int i = 0; i < 100; ++i, ++iter)
57       assert(*iter == T(i));
58 
59     static_assert(noexcept(*iter) == !std::same_as<T, NotNoexceptCopy>);
60   }
61   {
62     std::ranges::iota_view<T> io(T(10));
63     auto iter = io.begin();
64     for (int i = 10; i < 100; ++i, ++iter)
65       assert(*iter == T(i));
66   }
67   {
68     const std::ranges::iota_view<T> io(T(0));
69     auto iter = io.begin();
70     for (int i = 0; i < 100; ++i, ++iter)
71       assert(*iter == T(i));
72   }
73   {
74     const std::ranges::iota_view<T> io(T(10));
75     auto iter = io.begin();
76     for (int i = 10; i < 100; ++i, ++iter)
77       assert(*iter == T(i));
78   }
79 }
80 
test()81 constexpr bool test() {
82   testType<SomeInt>();
83   testType<NotNoexceptCopy>();
84   testType<signed long>();
85   testType<unsigned long>();
86   testType<int>();
87   testType<unsigned>();
88   testType<short>();
89   testType<unsigned short>();
90 
91   // Tests a mix of signed unsigned types.
92   {
93     const std::ranges::iota_view<int, unsigned> io(0, 10);
94     auto iter = io.begin();
95     for (int i = 0; i < 10; ++i, ++iter)
96       assert(*iter == i);
97   }
98 
99   return true;
100 }
101 
main(int,char **)102 int main(int, char**) {
103   test();
104   static_assert(test());
105 
106   return 0;
107 }
108