Debian Codegraph @

common/unittest/partial_range.cpp

raw
1 #include "partial_range.h"
2 #include <type_traits>
3 #include <vector>
4
5 #define BOOST_TEST_DYN_LINK
6 #define BOOST_TEST_MODULE Rebirth partial_range
7 #include <boost/test/unit_test.hpp>
8
9 #define DXX_TEST_IGNORE_RETURN(EXPR) ({ auto &&r = EXPR; static_cast<void>(r); })
10 #define OPTIMIZER_HIDE_VARIABLE(V) asm("" : "=rm" (V) : "0" (V) : "memory")
11
12 BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector<int>);
13
14 BOOST_AUTO_TEST_CASE(exception_past_end)
15 {
16 std::vector<int> vec;
17 using partial_range_type = decltype(partial_range(vec, 1u));
18 /* Test that trying to create a range which exceeds the end of the
19 * container raises an exception.
20 */
21 BOOST_CHECK_THROW(
22 DXX_TEST_IGNORE_RETURN(partial_range(vec, 1u)),
23 partial_range_type::partial_range_error);
24 /* Even if the range is empty, if it is beyond the end, then it is
25 * an error.
26 */
27 BOOST_CHECK_THROW(
28 DXX_TEST_IGNORE_RETURN(partial_range(vec, 1u, 1u)),
29 partial_range_type::partial_range_error);
30 }
31
32 BOOST_AUTO_TEST_CASE(empty_range_end_0)
33 {
34 std::vector<int> vec;
35 bool empty = true;
36 auto &&r = partial_range(vec, 0u);
37 for (auto &&v : r)
38 {
39 (void)v;
40 empty = false;
41 }
42 BOOST_TEST(empty);
43 BOOST_TEST(r.empty());
44 }
45
46 BOOST_AUTO_TEST_CASE(empty_range_begin_1_end_1)
47 {
48 std::vector<int> vec{1};
49 bool empty = true;
50 auto &&r = partial_range(vec, 1u, 1u);
51 for (auto &&v : r)
52 {
53 (void)v;
54 empty = false;
55 }
56 BOOST_TEST(empty);
57 BOOST_TEST(r.empty());
58 }
59
60 BOOST_AUTO_TEST_CASE(empty_range_begin_2_past_end_1)
61 {
62 std::vector<int> vec{1, 2, 3};
63 bool empty = true;
64 auto &&r = partial_range(vec, 2u, 1u);
65 for (auto &&v : r)
66 {
67 (void)v;
68 empty = false;
69 }
70 BOOST_TEST(empty);
71 BOOST_TEST(r.empty());
72 }
73
74 BOOST_AUTO_TEST_CASE(range_slice_end_2)
75 {
76 std::vector<int> vec{1, 2, 3};
77 std::vector<int> out;
78 for (auto &&v : partial_range(vec, 2u))
79 out.emplace_back(v);
80 std::vector<int> expected{1, 2};
81 BOOST_TEST(out == expected);
82 }
83
84 BOOST_AUTO_TEST_CASE(range_slice_begin_1_end_2)
85 {
86 std::vector<int> vec{1, 2, 3};
87 std::vector<int> out;
88 for (auto &&v : partial_range(vec, 1u, 2u))
89 out.emplace_back(v);
90 std::vector<int> expected{2};
91 BOOST_TEST(out == expected);
92 }
93
94 BOOST_AUTO_TEST_CASE(range_slice_reversed_begin_1_end_3)
95 {
96 std::vector<int> vec{1, 2, 3, 4};
97 std::vector<int> out;
98 for (auto &&v : partial_range(vec, 1u, 3u).reversed())
99 out.emplace_back(v);
100 std::vector<int> expected{3, 2};
101 BOOST_TEST(out == expected);
102 }
103
104 /* Type system tests can be done at compile-time. Applying them as
105 * static_assert can produce a better error message than letting it fail
106 * at runtime.
107 */
108 template <typename Expected, typename partial_range_type>
109 concept assert_index_type = std::same_as<Expected, typename partial_range_type::index_type>;
110
111 static_assert(assert_index_type<void, decltype(partial_range(std::declval<std::vector<int>&>(), 0u, 1u))>);
112 template <typename T>
113 struct custom_index_type_only : std::array<int, 1>
114 {
115 using index_type = T;
116 };
117
118 template <typename T>
119 struct custom_index_type : std::array<int, 1>
120 {
121 using index_type = T;
122 void operator[](typename std::remove_reference<T>::type);
123 };
124 enum class e1 : unsigned char;
125
126 /* The type is `void` because resolving index_type fails since `int *`
127 * is not a valid argument type to operator[].
128 */
129 static_assert(assert_index_type<void, decltype(partial_range(std::declval<custom_index_type_only<int *>&>(), 0u, 1u))>);
130 static_assert(assert_index_type<std::size_t, decltype(partial_range(std::declval<custom_index_type<std::size_t>&>(), 0u, 1u))>);
131 static_assert(assert_index_type<e1, decltype(partial_range(std::declval<custom_index_type<e1>&>(), 0u, 1u))>);