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 // When the debug mode is enabled, we don't unwrap iterators in std::copy
10 // so we don't get this optimization.
11 // UNSUPPORTED: libcpp-has-debug-mode
12 
13 // check that std::__unwrap_iter() returns the correct type
14 
15 #include <algorithm>
16 #include <cassert>
17 #include <string>
18 #include <type_traits>
19 
20 #include "test_iterators.h"
21 #include "test_macros.h"
22 
23 template <class Iter>
24 using UnwrapT = decltype(std::__unwrap_iter(std::declval<Iter>()));
25 
26 template <class Iter>
27 using rev_iter = std::reverse_iterator<Iter>;
28 
29 template <class Iter>
30 using rev_rev_iter = rev_iter<rev_iter<Iter> >;
31 
32 static_assert(std::is_same<UnwrapT<int*>, int*>::value, "");
33 static_assert(std::is_same<UnwrapT<std::__wrap_iter<int*> >, int*>::value, "");
34 static_assert(std::is_same<UnwrapT<rev_iter<int*> >, std::reverse_iterator<int*> >::value, "");
35 static_assert(std::is_same<UnwrapT<rev_rev_iter<int*> >, int*>::value, "");
36 static_assert(std::is_same<UnwrapT<rev_rev_iter<std::__wrap_iter<int*> > >, int*>::value, "");
37 static_assert(std::is_same<UnwrapT<rev_rev_iter<rev_iter<std::__wrap_iter<int*> > > >, rev_iter<std::__wrap_iter<int*> > >::value, "");
38 
39 static_assert(std::is_same<UnwrapT<random_access_iterator<int*> >, random_access_iterator<int*> >::value, "");
40 static_assert(std::is_same<UnwrapT<rev_iter<random_access_iterator<int*> > >, rev_iter<random_access_iterator<int*> > >::value, "");
41 static_assert(std::is_same<UnwrapT<rev_rev_iter<random_access_iterator<int*> > >, random_access_iterator<int*> >::value, "");
42 static_assert(std::is_same<UnwrapT<rev_rev_iter<rev_iter<random_access_iterator<int*> > > >, rev_iter<random_access_iterator<int*> > >::value, "");
43 
test()44 TEST_CONSTEXPR_CXX20 bool test() {
45   std::string str = "Banane";
46   using Iter = std::string::iterator;
47 
48   assert(std::__unwrap_iter(str.begin()) == str.data());
49   assert(std::__unwrap_iter(str.end()) == str.data() + str.size());
50   assert(std::__unwrap_iter(rev_rev_iter<Iter>(rev_iter<Iter>(str.begin()))) == str.data());
51   assert(std::__unwrap_iter(rev_rev_iter<Iter>(rev_iter<Iter>(str.end()))) == str.data() + str.size());
52 
53   return true;
54 }
55 
main(int,char **)56 int main(int, char**) {
57   test();
58 #if TEST_STD_VER > 17
59   static_assert(test());
60 #endif
61 
62   return 0;
63 }
64