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