c++: add fixed test [PR94100]
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / element_access / constexpr.cc
blob358ded47ad94741ac585dbe69d1d830ce5175f2b
1 // { dg-do compile { target c++20 } }
3 #include <vector>
4 #include <testsuite_hooks.h>
6 constexpr bool
7 test_iterators()
9 std::vector<int> v;
10 VERIFY( v.begin() == v.end() );
11 v.reserve(1);
12 VERIFY( v.begin() == v.end() );
13 v.resize(2);
14 VERIFY( v.begin() != v.end() );
15 VERIFY( v.cbegin() == v.begin() );
16 VERIFY( v.crbegin() == v.rbegin() );
17 VERIFY( v.cend() == v.end() );
18 VERIFY( v.crend() == v.rend() );
20 auto it = v.begin();
21 VERIFY( it[0] == 0 );
22 VERIFY( &*it == &v.front() );
23 VERIFY( &it[1] == &v[1] );
24 VERIFY( it++ == v.begin() );
25 VERIFY( ++it == v.end() );
26 VERIFY( (it - 2) == v.begin() );
27 VERIFY( (it - v.begin()) == 2 );
28 it -= 2;
29 it += 1;
30 VERIFY( (it + 1) == v.end() );
31 VERIFY( (1 + it) == v.end() );
32 it = it + 1;
33 auto it2 = v.begin();
34 std::swap(it, it2);
35 VERIFY( it == v.begin() );
36 VERIFY( it2 == v.end() );
38 auto rit = v.rbegin();
39 VERIFY( rit[0] == 0 );
40 VERIFY( &*rit == &v.back() );
41 VERIFY( &rit[1] == &v[0] );
42 VERIFY( rit++ == v.rbegin() );
43 VERIFY( ++rit == v.rend() );
44 VERIFY( (rit - 2) == v.rbegin() );
45 VERIFY( (rit - v.rbegin()) == 2 );
46 rit -= 2;
47 rit += 1;
48 VERIFY( (rit + 1) == v.rend() );
49 VERIFY( (1 + rit) == v.rend() );
50 rit = rit + 1;
51 auto rit2 = v.rbegin();
52 std::swap(rit, rit2);
53 VERIFY( rit == v.rbegin() );
54 VERIFY( rit2 == v.rend() );
56 return true;
59 static_assert(test_iterators());
61 constexpr bool
62 test_access()
64 std::vector<int> v{1, 2, 3};
65 VERIFY( v.at(1) == 2 );
66 VERIFY( v[2] == 3 );
67 VERIFY( &v[2] == &v.at(2) );
68 VERIFY( &v.front() == &v[0] );
69 VERIFY( &v.back() == &v[2] );
71 const auto& vc = v;
72 VERIFY( vc.at(1) == 2 );
73 VERIFY( &vc.at(1) == &v.at(1) );
74 VERIFY( &vc.at(1) == &vc[1] );
75 VERIFY( &vc.front() == &vc[0] );
76 VERIFY( &vc.back() == &vc[2] );
78 return true;
81 static_assert(test_access());
83 template<typename T = int>
84 constexpr std::false_type
85 access_empty() { return {}; }
87 template<typename T = int>
88 requires (std::bool_constant<&std::vector<T>().at(0) != nullptr>::value)
89 constexpr std::true_type
90 access_empty() { return {}; }
92 template<typename T = int>
93 requires (std::bool_constant<&std::vector<T>()[0] != nullptr>::value)
94 constexpr std::true_type
95 access_empty() { return {}; }
97 template<typename T = int>
98 requires (std::bool_constant<&std::vector<T>().front() != nullptr>::value)
99 constexpr std::true_type
100 access_empty() { return {}; }
102 template<typename T = int>
103 requires (std::bool_constant<&std::vector<T>().back() != nullptr>::value)
104 constexpr std::true_type
105 access_empty() { return {}; }
107 static_assert( ! access_empty() );
109 template<typename T = int>
110 constexpr std::false_type
111 access_past_the_end() { return {}; }
113 template<typename T = int>
114 requires (std::bool_constant<&std::vector<T>(3).at(3) != nullptr>::value)
115 constexpr std::true_type
116 access_past_the_end() { return {}; }
118 template<typename T = int>
119 requires (std::bool_constant<&std::vector<T>(3)[3] != nullptr>::value)
120 constexpr std::true_type
121 access_past_the_end() { return {}; }
123 static_assert( ! access_past_the_end() );