|
10 | 10 | #include <tests/test_types.hpp>
|
11 | 11 |
|
12 | 12 | #include <concepts>
|
| 13 | +#include <cstdlib> |
13 | 14 | #include <type_traits>
|
14 | 15 | #include <set>
|
15 | 16 | #include <vector>
|
16 | 17 |
|
17 |
| -namespace beman::optional26::test { |
| 18 | +#define CONSTEXPR_EXPECT_EQ(val1, val2) \ |
| 19 | + if (::std::is_constant_evaluated()) { \ |
| 20 | + if (!((val1) == (val2))) { \ |
| 21 | + ::std::abort(); \ |
| 22 | + } \ |
| 23 | + } else \ |
| 24 | + EXPECT_EQ(val1, val2) |
| 25 | + |
| 26 | +namespace beman::optional26::tests { |
18 | 27 |
|
19 | 28 | // Dummy containers helper.
|
20 | 29 | struct container {};
|
21 | 30 |
|
22 |
| -} // namespace beman::optional26::test |
| 31 | +} // namespace beman::optional26::tests |
23 | 32 |
|
24 |
| -using namespace beman::optional26::test; |
| 33 | +using namespace beman::optional26::tests; |
25 | 34 |
|
26 | 35 | TEST(IteratorTest, IteratorConcepts) {
|
27 | 36 | const auto test = [](auto&& it) {
|
@@ -53,88 +62,104 @@ TEST(IteratorTest, IteratorConcepts) {
|
53 | 62 | }
|
54 | 63 |
|
55 | 64 | TEST(IteratorTest, DereferenceOperator) {
|
56 |
| - std::vector<int> v{10, 20, 30, 40, 50}; |
57 |
| - auto it = beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}; |
| 65 | + auto lambda = [&] { |
| 66 | + std::vector<int> v{10, 20, 30, 40, 50}; |
| 67 | + auto it = beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}; |
58 | 68 |
|
59 |
| - EXPECT_EQ(*it, 10); |
60 |
| - *it = 100; |
61 |
| - EXPECT_EQ(*it, 100); |
| 69 | + CONSTEXPR_EXPECT_EQ(*it, 10); |
| 70 | + *it = 100; |
| 71 | + CONSTEXPR_EXPECT_EQ(*it, 100); |
62 | 72 |
|
63 |
| - it += 2; |
64 |
| - EXPECT_EQ(*it, 30); |
| 73 | + it += 2; |
| 74 | + CONSTEXPR_EXPECT_EQ(*it, 30); |
65 | 75 |
|
66 |
| - *it = 300; |
67 |
| - EXPECT_EQ(*it, 300); |
| 76 | + *it = 300; |
| 77 | + CONSTEXPR_EXPECT_EQ(*it, 300); |
| 78 | + }; |
| 79 | + static_assert((lambda(), true)); |
| 80 | + lambda(); |
68 | 81 | }
|
69 | 82 |
|
70 | 83 | TEST(IteratorTest, ForwardIterator) {
|
71 |
| - std::vector<int> v{10, 20, 30, 40, 50}; |
72 |
| - const std::vector<int> cv{10, 20, 30, 40, 50}; |
| 84 | + auto lambda = [&] { |
| 85 | + std::vector<int> v{10, 20, 30, 40, 50}; |
| 86 | + const std::vector<int> cv{10, 20, 30, 40, 50}; |
73 | 87 |
|
74 |
| - const auto test = [](auto&& it) { |
75 |
| - EXPECT_EQ(*it, 10); |
| 88 | + const auto test = [](auto&& it) { |
| 89 | + CONSTEXPR_EXPECT_EQ(*it, 10); |
76 | 90 |
|
77 |
| - ++it; // prefixed increment |
78 |
| - EXPECT_EQ(*it, 20); |
| 91 | + ++it; // prefixed increment |
| 92 | + CONSTEXPR_EXPECT_EQ(*it, 20); |
79 | 93 |
|
80 |
| - it++; // postfixed increment |
81 |
| - EXPECT_EQ(*it, 30); |
| 94 | + it++; // postfixed increment |
| 95 | + CONSTEXPR_EXPECT_EQ(*it, 30); |
82 | 96 |
|
83 |
| - it++; |
84 |
| - EXPECT_EQ(*it, 40); |
| 97 | + it++; |
| 98 | + CONSTEXPR_EXPECT_EQ(*it, 40); |
85 | 99 |
|
86 |
| - ++it; |
87 |
| - EXPECT_EQ(*it, 50); |
88 |
| - }; |
| 100 | + ++it; |
| 101 | + CONSTEXPR_EXPECT_EQ(*it, 50); |
| 102 | + }; |
89 | 103 |
|
90 |
| - test(beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}); |
91 |
| - test(beman::optional26::detail::contiguous_iterator<const int, decltype(v)>{cv.data()}); |
| 104 | + test(beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}); |
| 105 | + test(beman::optional26::detail::contiguous_iterator<const int, decltype(v)>{cv.data()}); |
| 106 | + }; |
| 107 | + static_assert((lambda(), true)); |
| 108 | + lambda(); |
92 | 109 | }
|
93 | 110 |
|
94 | 111 | TEST(IteratorTest, BidirectionalIterator) {
|
95 |
| - std::vector<int> v{10, 20, 30, 40, 50}; |
96 |
| - const std::vector<int> cv{10, 20, 30, 40, 50}; |
97 |
| - const auto test = [](auto&& it) { |
98 |
| - it++; |
99 |
| - it++; |
100 |
| - EXPECT_EQ(*it, 30); |
101 |
| - |
102 |
| - --it; // prefixed decrement |
103 |
| - EXPECT_EQ(*it, 20); |
104 |
| - |
105 |
| - it--; // postfixed decrement |
106 |
| - EXPECT_EQ(*it, 10); |
| 112 | + auto lambda = [&] { |
| 113 | + std::vector<int> v{10, 20, 30, 40, 50}; |
| 114 | + const std::vector<int> cv{10, 20, 30, 40, 50}; |
| 115 | + const auto test = [](auto&& it) { |
| 116 | + it++; |
| 117 | + it++; |
| 118 | + CONSTEXPR_EXPECT_EQ(*it, 30); |
| 119 | + |
| 120 | + --it; // prefixed decrement |
| 121 | + CONSTEXPR_EXPECT_EQ(*it, 20); |
| 122 | + |
| 123 | + it--; // postfixed decrement |
| 124 | + CONSTEXPR_EXPECT_EQ(*it, 10); |
| 125 | + }; |
| 126 | + |
| 127 | + test(beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}); |
| 128 | + test(beman::optional26::detail::contiguous_iterator<const int, decltype(v)>{cv.data()}); |
107 | 129 | };
|
108 |
| - |
109 |
| - test(beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}); |
110 |
| - test(beman::optional26::detail::contiguous_iterator<const int, decltype(v)>{cv.data()}); |
| 130 | + static_assert((lambda(), true)); |
| 131 | + lambda(); |
111 | 132 | }
|
112 | 133 |
|
113 | 134 | TEST(IteratorTest, RandomAccessIterator) {
|
114 |
| - std::vector<int> v{10, 20, 30, 40, 50}; |
115 |
| - const std::vector<int> cv{10, 20, 30, 40, 50}; |
116 |
| - const auto test = [](auto&& it) { |
117 |
| - EXPECT_EQ(it[0], 10); |
118 |
| - EXPECT_EQ(it[1], 20); |
119 |
| - EXPECT_EQ(it[2], 30); |
120 |
| - EXPECT_EQ(it[3], 40); |
121 |
| - EXPECT_EQ(it[4], 50); |
122 |
| - |
123 |
| - it += 2; |
124 |
| - EXPECT_EQ(*it, 30); |
125 |
| - |
126 |
| - it -= 1; |
127 |
| - EXPECT_EQ(*it, 20); |
128 |
| - |
129 |
| - it = it + 2; |
130 |
| - EXPECT_EQ(*it, 40); |
131 |
| - |
132 |
| - it = it - 1; |
133 |
| - EXPECT_EQ(*it, 30); |
| 135 | + auto lambda = [&] { |
| 136 | + std::vector<int> v{10, 20, 30, 40, 50}; |
| 137 | + const std::vector<int> cv{10, 20, 30, 40, 50}; |
| 138 | + const auto test = [](auto&& it) { |
| 139 | + CONSTEXPR_EXPECT_EQ(it[0], 10); |
| 140 | + CONSTEXPR_EXPECT_EQ(it[1], 20); |
| 141 | + CONSTEXPR_EXPECT_EQ(it[2], 30); |
| 142 | + CONSTEXPR_EXPECT_EQ(it[3], 40); |
| 143 | + CONSTEXPR_EXPECT_EQ(it[4], 50); |
| 144 | + |
| 145 | + it += 2; |
| 146 | + CONSTEXPR_EXPECT_EQ(*it, 30); |
| 147 | + |
| 148 | + it -= 1; |
| 149 | + CONSTEXPR_EXPECT_EQ(*it, 20); |
| 150 | + |
| 151 | + it = it + 2; |
| 152 | + CONSTEXPR_EXPECT_EQ(*it, 40); |
| 153 | + |
| 154 | + it = it - 1; |
| 155 | + CONSTEXPR_EXPECT_EQ(*it, 30); |
| 156 | + }; |
| 157 | + |
| 158 | + test(beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}); |
| 159 | + test(beman::optional26::detail::contiguous_iterator<const int, decltype(v)>{cv.data()}); |
134 | 160 | };
|
135 |
| - |
136 |
| - test(beman::optional26::detail::contiguous_iterator<int, decltype(v)>{v.data()}); |
137 |
| - test(beman::optional26::detail::contiguous_iterator<const int, decltype(v)>{cv.data()}); |
| 161 | + static_assert((lambda(), true)); |
| 162 | + lambda(); |
138 | 163 | }
|
139 | 164 |
|
140 | 165 | TEST(IteratorTest, ContainerType) {
|
|
0 commit comments