Skip to content

Commit 37e9ccc

Browse files
committed
concat_view tests
1 parent 1c93f88 commit 37e9ccc

10 files changed

Lines changed: 119 additions & 17 deletions

File tree

include/gl/graph.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "gl/impl/impl_tags.hpp"
1010
#include "gl/io/stream_options_manipulator.hpp"
1111
#include "gl/util/ranges.hpp"
12+
1213
#include <sys/types.h>
1314

1415
#include <set>

include/gl/util/ranges.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ class concat_view : public std::ranges::view_interface<concat_view<V1, V2>> {
125125
}
126126

127127
constexpr iterator& operator++() {
128-
(this->_it1 != this->_end1) ? ++this->_it1 : ++this->_it2;
128+
if (this->_it1 != this->_end1)
129+
++this->_it1;
130+
else
131+
++this->_it2;
129132
return *this;
130133
}
131134

include/hgl/conversion.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ template <gl::traits::c_undirected_graph G>
297297
using g_id_type = typename G::id_type;
298298

299299
G g{h.order() + h.size()};
300-
const auto align_edge_id = [shift = static_cast<g_id_type>(h.order())](const auto eid) -> g_id_type {
300+
const auto align_edge_id =
301+
[shift = static_cast<g_id_type>(h.order())](const auto eid) -> g_id_type {
301302
return eid + shift;
302303
};
303304

@@ -323,7 +324,8 @@ template <gl::traits::c_directed_graph G>
323324
using g_id_type = typename G::id_type;
324325

325326
G g{h.order() + h.size()};
326-
const auto align_edge_id = [shift = static_cast<g_id_type>(h.order())](const auto eid) -> g_id_type {
327+
const auto align_edge_id =
328+
[shift = static_cast<g_id_type>(h.order())](const auto eid) -> g_id_type {
327329
return eid + shift;
328330
};
329331

include/hgl/impl/incidence_matrix.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ class incidence_matrix<hgl::undirected_t, ImplTag> final {
183183
gl_attr_force_inline auto _incident_with(const id_type id) const noexcept {
184184
if constexpr (Element == layout_tag::major_element) { // incident with major
185185
return std::views::iota(initial_id_v<id_type>, this->_matrix_row_size)
186-
| std::views::filter([&row = this->_matrix[to_idx(id)]](id_type minor_id) { return row[to_idx(minor_id)]; });
186+
| std::views::filter([&row = this->_matrix[to_idx(id)]](id_type minor_id) {
187+
return row[to_idx(minor_id)];
188+
});
187189
}
188190
else { // incident with minor
189191
return std::views::iota(initial_id_v<id_type>, this->_matrix.size())
@@ -487,8 +489,8 @@ class incidence_matrix<hgl::bf_directed_t, ImplTag> final {
487489
else { // query minor
488490
return std::views::iota(initial_id_v<id_type>, this->_matrix.size())
489491
| std::views::filter([this, minor_idx = to_idx(id), pred](id_type major_id) {
490-
return pred(this->_matrix[to_idx(major_id)][minor_idx]);
491-
});
492+
return pred(this->_matrix[to_idx(major_id)][minor_idx]);
493+
});
492494
}
493495
}
494496

tests/include/testing/common/wrnsup.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#define SUPPRESS_PRAGMA(x) _Pragma(#x)
66

7-
#define SUPPRESS_WARNING_BEGIN(w) \
8-
SUPPRESS_PRAGMA(GCC diagnostic push) \
7+
#define SUPPRESS_WARNING_BEGIN(w) \
8+
SUPPRESS_PRAGMA(GCC diagnostic push) \
99
SUPPRESS_PRAGMA(GCC diagnostic ignored w)
1010

1111
#define SUPPRESS_WARNING_END SUPPRESS_PRAGMA(GCC diagnostic pop)

tests/source/gl/test_adjacency_matrix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ TEST_CASE_TEMPLATE_DEFINE("directed adjacency matrix tests", SutType, directed_a
225225

226226
SUBCASE("at should return a view equivalent to the matrix row of the given vertex") {
227227
for (const auto vertex_id : std::views::iota(constants::v1_id, constants::n_elements)) {
228-
const auto target_id = static_cast<gl::default_id_type>((vertex_id + 1u) % constants::n_elements);
228+
const auto target_id =
229+
static_cast<gl::default_id_type>((vertex_id + 1u) % constants::n_elements);
229230
const auto edge = add_edge(vertex_id, target_id);
230231
auto row_view = sut.at(vertex_id);
231232

tests/source/gl/test_alg_dijkstra.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ TEST_CASE_TEMPLATE_DEFINE(
6969
const auto parent_id = id == 0u ? 0u : (id - 1u) / 2u;
7070
expected_predecessors.push_back(parent_id);
7171

72-
const auto vertex_depth = static_cast<distance_type>(std::floor(std::log2(id + 1uz)));
72+
const auto vertex_depth =
73+
static_cast<distance_type>(std::floor(std::log2(id + 1uz)));
7374
expected_distances.push_back(vertex_depth);
7475
}
7576
}
@@ -184,7 +185,8 @@ TEST_CASE_TEMPLATE_DEFINE(
184185
const auto parent_id = id == 0u ? 0u : (id - 1u) / 2u;
185186
expected_predecessors.push_back(parent_id);
186187

187-
const auto vertex_depth = static_cast<distance_type>(std::floor(std::log2(id + 1uz)));
188+
const auto vertex_depth =
189+
static_cast<distance_type>(std::floor(std::log2(id + 1uz)));
188190
expected_distances.push_back(vertex_depth);
189191
}
190192
}

tests/source/gl/test_graph.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ struct test_graph {
8989
graph.vertex_ids(),
9090
[&graph, expected_n_edges = n_incident_edges_for_fully_connected_vertex(graph)](
9191
const gl::default_id_type vertex_id
92-
) { return static_cast<std::size_t>(gl::util::range_size(graph.adjacent_edges(vertex_id))) == expected_n_edges; }
92+
) {
93+
return static_cast<std::size_t>(gl::util::range_size(graph.adjacent_edges(vertex_id)
94+
))
95+
== expected_n_edges;
96+
}
9397
));
9498
}
9599

@@ -216,7 +220,10 @@ TEST_CASE_TEMPLATE_DEFINE("graph structure tests", TraitsType, graph_traits_temp
216220

217221
SUBCASE("get_vertex should throw if the given id is invalid") {
218222
sut_type sut{constants::n_elements};
219-
CHECK_THROWS_AS(static_cast<void>(sut.get_vertex(static_cast<gl::default_id_type>(sut.order()))), std::out_of_range);
223+
CHECK_THROWS_AS(
224+
static_cast<void>(sut.get_vertex(static_cast<gl::default_id_type>(sut.order()))),
225+
std::out_of_range
226+
);
220227
}
221228

222229
SUBCASE("get_vertex should return a vertex with the given id") {
@@ -911,7 +918,8 @@ TEST_CASE_TEMPLATE_DEFINE("properties getter tests", TraitsType, property_graph_
911918
sut_type sut{constants::n_elements};
912919
for (auto vertex : sut.vertices()) {
913920
vertex.properties() = std::format("vertex_{}", vertex.id());
914-
const auto target_id = static_cast<gl::default_id_type>((vertex.id() + 1uz) % constants::n_elements);
921+
const auto target_id =
922+
static_cast<gl::default_id_type>((vertex.id() + 1uz) % constants::n_elements);
915923
sut.add_edge(vertex.id(), target_id).properties() = std::format("edge_{}", vertex.id());
916924
}
917925

@@ -932,7 +940,10 @@ TEST_CASE_TEMPLATE_DEFINE("properties getter tests", TraitsType, property_graph_
932940
for (auto [id, property] : std::views::enumerate(emap)) {
933941
CHECK_EQ(property, std::format("edge_{}", id));
934942
CHECK_EQ(emap[id], std::format("edge_{}", id));
935-
CHECK_EQ(sut.get_edge_properties(static_cast<gl::default_id_type>(id)), std::format("edge_{}", id));
943+
CHECK_EQ(
944+
sut.get_edge_properties(static_cast<gl::default_id_type>(id)),
945+
std::format("edge_{}", id)
946+
);
936947
}
937948

938949
CHECK_THROWS_AS(

tests/source/gl/test_graph_topology_builders.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ template <gl::traits::c_graph GraphType>
9494
using id_type = typename GraphType::id_type;
9595
using vertex_type = typename GraphType::vertex_type;
9696
return [&graph](const vertex_type& source) {
97-
const auto prev_vertex_id = static_cast<id_type>((source.id() + graph.order() - 1uz) % graph.order());
97+
const auto prev_vertex_id =
98+
static_cast<id_type>((source.id() + graph.order() - 1uz) % graph.order());
9899
const auto prev_vertex = graph.get_vertex(prev_vertex_id);
99100

100101
return std::ranges::all_of(graph.vertices(), [&](const auto& vertex) {
@@ -111,7 +112,8 @@ template <gl::traits::c_graph GraphType>
111112
const auto next_vertex_id = static_cast<id_type>((source.id() + 1uz) % graph.order());
112113
const auto next_vertex = graph.get_vertex(next_vertex_id);
113114

114-
const auto prev_vertex_id = static_cast<id_type>((source.id() + graph.order() - 1uz) % graph.order());
115+
const auto prev_vertex_id =
116+
static_cast<id_type>((source.id() + graph.order() - 1uz) % graph.order());
115117
const auto prev_vertex = graph.get_vertex(prev_vertex_id);
116118

117119
return std::ranges::all_of(graph.vertices(), [&](const auto& vertex) {

tests/source/gl/test_util.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <gl/util/math.hpp>
44
#include <gl/util/ranges.hpp>
55

6+
#include <algorithm>
67
#include <ranges>
78
#include <vector>
89

@@ -55,6 +56,83 @@ TEST_CASE("upow_sum function test") {
5556
CHECK_EQ(gl::util::upow_sum(base, i_begin, i_end), expected_result);
5657
}
5758

59+
struct test_concat_view {
60+
std::vector<int> v1;
61+
std::vector<int> v2;
62+
std::vector<int> expected;
63+
};
64+
65+
TEST_CASE_FIXTURE(test_concat_view, "concat should sequentially combine two identical range types") {
66+
SUBCASE("Both ranges are non-empty") {
67+
v1 = {1, 2, 3};
68+
v2 = {4, 5, 6};
69+
expected = {1, 2, 3, 4, 5, 6};
70+
}
71+
SUBCASE("First range is empty") {
72+
v1 = {};
73+
v2 = {4, 5, 6};
74+
expected = {4, 5, 6};
75+
}
76+
SUBCASE("Second range is empty") {
77+
v1 = {1, 2, 3};
78+
v2 = {};
79+
expected = {1, 2, 3};
80+
}
81+
SUBCASE("Both ranges are empty") {
82+
v1 = {};
83+
v2 = {};
84+
expected = {};
85+
}
86+
87+
auto concat_vw = gl::util::concat(v1, v2);
88+
89+
CHECK_EQ(std::ranges::distance(concat_vw), expected.size());
90+
CHECK(std::ranges::equal(concat_vw, expected));
91+
92+
const auto concat_vec = concat_vw | std::ranges::to<std::vector>();
93+
CHECK_EQ(concat_vec.size(), expected.size());
94+
CHECK(std::ranges::equal(concat_vec, expected));
95+
}
96+
97+
TEST_CASE_FIXTURE(test_concat_view, "concat_view satisfies C++20 range concepts") {
98+
v1 = {1, 2};
99+
v2 = {3, 4};
100+
101+
auto concat_vw = gl::util::concat(v1, v2);
102+
103+
static_assert(std::ranges::view<decltype(concat_vw)>);
104+
static_assert(std::ranges::forward_range<decltype(concat_vw)>);
105+
106+
CHECK_EQ(std::ranges::distance(concat_vw), 4);
107+
}
108+
109+
TEST_CASE_FIXTURE(test_concat_view, "concat_view propagates const correctness") {
110+
v1 = {10, 20};
111+
v2 = {30, 40};
112+
113+
const auto& cv1 = v1;
114+
const auto& cv2 = v2;
115+
116+
auto concat_vw = gl::util::concat(cv1, cv2);
117+
118+
int sum = 0;
119+
for (const auto& val : concat_vw)
120+
sum += val;
121+
122+
CHECK_EQ(sum, 100);
123+
}
124+
125+
TEST_CASE("concat should seamlessly bridge heterogeneous view types") {
126+
auto vw1 = std::views::iota(1, 4);
127+
std::vector<int> vw2 = {4, 5, 6};
128+
129+
auto concat_vw = gl::util::concat(vw1, vw2);
130+
std::vector<int> expected = {1, 2, 3, 4, 5, 6};
131+
132+
CHECK_EQ(std::ranges::distance(concat_vw), expected.size());
133+
CHECK(std::ranges::equal(concat_vw, expected));
134+
}
135+
58136
TEST_SUITE_END(); // test_util
59137

60138
} // namespace gl_testing

0 commit comments

Comments
 (0)