Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / iterators / predef.iterators / counted.iterator / deref.pass.cpp
blobea7083f17cf8eee3877965707794446dbe47d9ee
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // constexpr decltype(auto) operator*();
12 // constexpr decltype(auto) operator*() const
13 // requires dereferenceable<const I>;
15 #include <iterator>
17 #include "test_macros.h"
18 #include "test_iterators.h"
20 struct InputOrOutputArchetype {
21 using difference_type = int;
23 int *ptr;
25 constexpr int operator*() const { return *ptr; }
26 constexpr void operator++(int) { ++ptr; }
27 constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
30 struct NonConstDeref {
31 using difference_type = int;
33 int *ptr;
35 constexpr int operator*() { return *ptr; }
36 constexpr void operator++(int) { ++ptr; }
37 constexpr NonConstDeref& operator++() { ++ptr; return *this; }
40 template<class T>
41 concept IsDereferenceable = requires(T& i) {
42 *i;
45 constexpr bool test() {
46 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
49 static_assert( IsDereferenceable<std::counted_iterator<InputOrOutputArchetype>>);
50 static_assert( IsDereferenceable<const std::counted_iterator<InputOrOutputArchetype>>);
51 static_assert( IsDereferenceable<std::counted_iterator<NonConstDeref>>);
52 static_assert(!IsDereferenceable<const std::counted_iterator<NonConstDeref>>);
56 std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
57 for (int i = 1; i < 9; ++i, ++iter)
58 assert(*iter == i);
62 std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
63 for (int i = 1; i < 9; ++i, ++iter)
64 assert(*iter == i);
68 std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
69 for (int i = 1; i < 9; ++i, ++iter)
70 assert(*iter == i);
74 std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
75 for (int i = 1; i < 9; ++i, ++iter)
76 assert(*iter == i);
80 const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
81 assert(*iter == 1);
85 const std::counted_iterator iter(forward_iterator<int*>{buffer + 1}, 7);
86 assert(*iter == 2);
90 const std::counted_iterator iter(contiguous_iterator<int*>{buffer + 2}, 6);
91 assert(*iter == 3);
95 const std::counted_iterator iter(InputOrOutputArchetype{buffer + 2}, 6);
96 assert(*iter == 3);
99 return true;
102 int main(int, char**) {
103 test();
104 static_assert(test());
106 return 0;