Debian Codegraph @

common/unittest/enumerate.cpp

raw
1 #include "d_enumerate.h"
2 #include "d_zip.h"
3 #include <list>
4
5 #define BOOST_TEST_DYN_LINK
6 #define BOOST_TEST_MODULE Rebirth enumerate
7 #include <boost/test/unit_test.hpp>
8
9 template <typename range_index_type, std::input_or_output_iterator range_iterator_type, std::sentinel_for<range_iterator_type> sentinel_type>
10 std::ostream &boost_test_print_type(std::ostream &ostr, const enumerated_iterator<range_index_type, range_iterator_type, sentinel_type> &right)
11 {
12 auto &&[idx, val] = *right;
13 return (ostr << "enumerated_iterator(" << idx << ", " << val << ")");
14 }
15
16 /* Test that an enumerated range is empty when the component range is empty.
17 */
18 BOOST_AUTO_TEST_CASE(enumerate_empty)
19 {
20 bool empty = true;
21 std::array<int, 0> a;
22 for (auto &&[idx, v] : enumerate(a))
23 {
24 (void)idx;
25 (void)v;
26 empty = false;
27 }
28 BOOST_TEST(empty);
29 }
30
31 /* Test that an enumerated range is the length of the underlying sequence.
32 */
33 BOOST_AUTO_TEST_CASE(enumerate_length)
34 {
35 unsigned count{0};
36 std::array<int, 2> a;
37 for (auto &&[idx, v] : enumerate(a))
38 {
39 (void)idx;
40 (void)v;
41 ++ count;
42 }
43 BOOST_TEST(count == a.size());
44 }
45
46 /* Test that an enumerated value references the underlying storage.
47 */
48 BOOST_AUTO_TEST_CASE(enumerate_passthrough_modifications)
49 {
50 std::array<int, 1> a{{1}};
51 for (auto &&[idx, v] : enumerate(a))
52 {
53 (void)idx;
54 ++ v;
55 }
56 BOOST_TEST(a[0] == 2);
57 }
58
59 /* Test that an enumerated index tracks properly.
60 */
61 BOOST_AUTO_TEST_CASE(enumerate_starting_value)
62 {
63 const std::array<int, 4> a{{1, 2, 3, 4}};
64 for (auto &&[idx, v] : enumerate(a))
65 BOOST_TEST(idx + 1 == v);
66 for (auto &&[idx, v] : enumerate(a, 1))
67 BOOST_TEST(idx == v);
68 }
69
70 BOOST_AUTO_TEST_CASE(enumerate_zip)
71 {
72 const std::array<int, 4> a{{1, 2, 3, 4}};
73 const std::array<int, 4> b{{5, 6, 7, 8}};
74 for (auto &&[idx, va, vb] : enumerate(zip(a, b), 1))
75 {
76 BOOST_CHECK_EQUAL(idx, va);
77 BOOST_CHECK_EQUAL(idx + 4, vb);
78 }
79 }
80
81 BOOST_AUTO_TEST_CASE(enumerate_bidirectional)
82 {
83 const std::list<int> a{1, 2};
84 const auto &&en = enumerate(a);
85 auto i = en.begin();
86 static_assert(std::bidirectional_iterator<decltype(i)>);
87 static_assert(!std::random_access_iterator<decltype(i)>);
88 {
89 auto &&[idx, val] = *i;
90 BOOST_CHECK_EQUAL(idx, 0);
91 BOOST_CHECK_EQUAL(val, 1);
92 }
93 {
94 const auto j{i};
95 BOOST_CHECK_EQUAL(j, i);
96 ++i;
97 BOOST_CHECK_EQUAL(j, en.begin());
98 BOOST_CHECK_NE(j, i);
99 }
100 {
101 auto &&[idx, val] = *i;
102 BOOST_CHECK_EQUAL(idx, 1);
103 BOOST_CHECK_EQUAL(val, 2);
104 }
105 --i;
106 {
107 auto &&[idx, val] = *i;
108 BOOST_CHECK_EQUAL(idx, 0);
109 BOOST_CHECK_EQUAL(val, 1);
110 }
111 }
112
113 BOOST_AUTO_TEST_CASE(enumerate_random_access)
114 {
115 const std::vector<int> a{4, 5, 6};
116 const auto &&en = enumerate(a);
117 auto i = en.begin();
118 static_assert(std::bidirectional_iterator<decltype(i)>);
119 static_assert(std::random_access_iterator<decltype(i)>);
120 {
121 auto &&[idx, val] = *i;
122 BOOST_CHECK_EQUAL(idx, 0);
123 BOOST_CHECK_EQUAL(val, 4);
124 }
125 {
126 auto &&[idx, val] = i[2];
127 BOOST_CHECK_EQUAL(idx, 2);
128 BOOST_CHECK_EQUAL(val, 6);
129 }
130 {
131 const auto j = i + 2;
132 BOOST_CHECK_NE(i, j);
133 i += 2;
134 BOOST_CHECK_EQUAL(i, j);
135 }
136 {
137 auto &&[idx, val] = *i;
138 BOOST_CHECK_EQUAL(idx, 2);
139 BOOST_CHECK_EQUAL(val, 6);
140 }
141 {
142 const auto j = i - 2;
143 BOOST_CHECK_NE(i, j);
144 i -= 2;
145 BOOST_CHECK_EQUAL(i, j);
146 }
147 {
148 auto &&[idx, val] = *i;
149 BOOST_CHECK_EQUAL(idx, 0);
150 BOOST_CHECK_EQUAL(val, 4);
151 }
152 }
153
154 /* Type system tests can be done at compile-time. Applying them as
155 * static_assert can produce a better error message than letting it fail
156 * at runtime.
157 */
158 template <typename Expected, typename enumerate_type>
159 concept assert_index_type = std::same_as<Expected, typename enumerate_type::index_type>;
160
161 static_assert(assert_index_type<std::size_t, decltype(enumerate(std::declval<std::array<int, 1> &>()))>);
162
163 template <typename T>
164 struct custom_index_type : std::array<int, 1>
165 {
166 using index_type = T;
167 void operator[](typename std::remove_reference<T>::type);
168 };
169 enum class e1 : unsigned char;
170
171 static_assert(assert_index_type<e1, decltype(enumerate(std::declval<custom_index_type<e1>&>()))>);