Skip to content

Commit f6d45e3

Browse files
committed
edge_descriptor alignment
1 parent aaf296a commit f6d45e3

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

include/gl/edge_descriptor.hpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,27 @@ class edge_descriptor final {
6464

6565
~edge_descriptor() = default;
6666

67-
// TODO: add tests
68-
[[nodiscard]] bool operator==(const edge_descriptor& other) const noexcept {
69-
return this->_id == other._id; // compare vertices ?
67+
[[nodiscard]] bool operator==(const edge_descriptor& other) const noexcept
68+
requires(type_traits::is_directed_v<type>)
69+
{
70+
return this->_id == other._id and (this->_vertices == other._vertices);
71+
}
72+
73+
[[nodiscard]] bool operator==(const edge_descriptor& other) const noexcept
74+
requires(type_traits::is_undirected_v<type>)
75+
{
76+
return this->_id == other._id
77+
and (this->_vertices == other._vertices
78+
or (this->_vertices == other.incident_vertices_r()));
79+
}
80+
81+
[[nodiscard]] gl_attr_force_inline operator bool() const noexcept {
82+
return this->is_valid();
7083
}
7184

72-
// TODO: add tests
7385
[[nodiscard]] bool is_valid() const noexcept {
74-
return this->_id != constants::invalid_id;
86+
return this->_id != constants::invalid_id and this->_vertices.first != constants::invalid_id
87+
and this->_vertices.second != constants::invalid_id;
7588
}
7689

7790
[[nodiscard]] constexpr bool is_directed() const noexcept {
@@ -93,6 +106,10 @@ class edge_descriptor final {
93106
return this->_vertices;
94107
}
95108

109+
[[nodiscard]] gl_attr_force_inline types::homogeneous_pair<const types::id_type> incident_vertices_r() const noexcept {
110+
return std::make_pair(this->_vertices.second, this->_vertices.first);
111+
}
112+
96113
// TODO: rename to source
97114
[[nodiscard]] gl_attr_force_inline const types::id_type first() const noexcept {
98115
return this->_vertices.first;

tests/source/gl/test_edge_descriptor.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ TEST_CASE_FIXTURE(
4242
CHECK_FALSE(directed_edge.is_undirected());
4343
}
4444

45+
TEST_CASE_FIXTURE(
46+
test_edge_descriptor, "edges should be equal only if their IDs and vertices are the same"
47+
) {
48+
// directed edges
49+
gl::directed_edge<> dedge1{id1, v1, v2};
50+
gl::directed_edge<> dedge2{id1, v1, v2};
51+
gl::directed_edge<> dedge3{id2, v1, v2};
52+
gl::directed_edge<> dedge4{id1, v2, v1};
53+
54+
CHECK_EQ(dedge1, dedge2);
55+
CHECK_NE(dedge1, dedge3);
56+
CHECK_NE(dedge1, dedge4);
57+
58+
// undirected edges
59+
gl::undirected_edge<> uedge1{id1, v1, v2};
60+
gl::undirected_edge<> uedge2{id1, v1, v2};
61+
gl::undirected_edge<> uedge3{id2, v1, v2};
62+
gl::undirected_edge<> uedge4{id1, v2, v1};
63+
64+
CHECK_EQ(uedge1, uedge2);
65+
CHECK_NE(uedge1, uedge3);
66+
CHECK_EQ(uedge1, uedge4);
67+
}
68+
4569
TEST_CASE_TEMPLATE_DEFINE(
4670
"properties should be properly initialized", EdgeType, properties_edge_directional_tag_template
4771
) {
@@ -60,9 +84,16 @@ TEST_CASE_TEMPLATE_DEFINE(
6084
"directional_tag-independent tests", EdgeType, edge_directional_tag_template
6185
) {
6286
test_edge_descriptor fixture{};
63-
6487
EdgeType sut{fixture.id1, fixture.v1, fixture.v2};
6588

89+
SUBCASE("an edge should be valid if it has a valid ID and vertices") {
90+
CHECK(sut.is_valid());
91+
CHECK_FALSE(EdgeType{constants::invalid_id, constants::invalid_id, constants::invalid_id});
92+
CHECK_FALSE(EdgeType{constants::invalid_id, fixture.v1, fixture.v2});
93+
CHECK_FALSE(EdgeType{fixture.id1, constants::invalid_id, fixture.v2});
94+
CHECK_FALSE(EdgeType{fixture.id1, fixture.v1, constants::invalid_id});
95+
}
96+
6697
SUBCASE("id() should return the ID of the edge") {
6798
CHECK_EQ(sut.id(), fixture.id1);
6899
}
@@ -74,6 +105,13 @@ TEST_CASE_TEMPLATE_DEFINE(
74105
CHECK_EQ(vertices.second, fixture.v2);
75106
}
76107

108+
SUBCASE("incident_vertices_r should return the pair of vertex IDS the edge was initialized "
109+
"with but with switched order") {
110+
const auto& vertices = sut.incident_vertices_r();
111+
CHECK_EQ(vertices.first, fixture.v2);
112+
CHECK_EQ(vertices.second, fixture.v1);
113+
}
114+
77115
SUBCASE("first should return the first vertex descriptor the edge was initialized with") {
78116
CHECK_EQ(sut.first(), fixture.v1);
79117
}

0 commit comments

Comments
 (0)