Skip to content

Commit

Permalink
adjacency_matrix: fix in_edges() is off by 1 in some cases (close #1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Jun 13, 2023
1 parent 7b7d82e commit cb974bf
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/entt/graph/adjacency_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class adjacency_matrix {
[[nodiscard]] iterable_adaptor<out_edge_iterator> out_edges(const vertex_type vertex) const noexcept {
const auto it = matrix.cbegin();
const auto from = vertex * vert;
const auto to = vertex * vert + vert;
const auto to = from + vert;
return {{it, vert, from, to, 1u}, {it, vert, to, to, 1u}};
}

Expand All @@ -270,7 +270,7 @@ class adjacency_matrix {
[[nodiscard]] iterable_adaptor<in_edge_iterator> in_edges(const vertex_type vertex) const noexcept {
const auto it = matrix.cbegin();
const auto from = vertex;
const auto to = vert * (vert - 1u) + vertex;
const auto to = vert * vert + from;
return {{it, vert, from, to, vert}, {it, vert, to, to, vert}};
}

Expand Down
128 changes: 128 additions & 0 deletions test/entt/graph/adjacency_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ TEST(AdjacencyMatrix, EdgesDirected) {
ASSERT_EQ(++it, iterable.end());
}

TEST(AdjacencyMatrix, EdgesBackwardOnlyDirected) {
entt::adjacency_matrix<entt::directed_tag> adjacency_matrix{2u};
auto iterable = adjacency_matrix.edges();

ASSERT_EQ(iterable.begin(), iterable.end());

adjacency_matrix.insert(1u, 0u);
iterable = adjacency_matrix.edges();

ASSERT_NE(iterable.begin(), iterable.end());

auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{1u}, std::size_t{0u}));
ASSERT_EQ(it, iterable.end());
}

TEST(AdjacencyMatrix, EdgesUndirected) {
entt::adjacency_matrix<entt::undirected_tag> adjacency_matrix{3u};
auto iterable = adjacency_matrix.edges();
Expand All @@ -317,6 +334,24 @@ TEST(AdjacencyMatrix, EdgesUndirected) {
ASSERT_EQ(++it, iterable.end());
}

TEST(AdjacencyMatrix, EdgesBackwardOnlyUndirected) {
entt::adjacency_matrix<entt::undirected_tag> adjacency_matrix{2u};
auto iterable = adjacency_matrix.edges();

ASSERT_EQ(iterable.begin(), iterable.end());

adjacency_matrix.insert(1u, 0u);
iterable = adjacency_matrix.edges();

ASSERT_NE(iterable.begin(), iterable.end());

auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{0u}, std::size_t{1u}));
ASSERT_EQ(*it++, std::make_pair(std::size_t{1u}, std::size_t{0u}));
ASSERT_EQ(it, iterable.end());
}

TEST(AdjacencyMatrix, OutEdgesDirected) {
entt::adjacency_matrix<entt::directed_tag> adjacency_matrix{3u};
auto iterable = adjacency_matrix.out_edges(0u);
Expand All @@ -340,6 +375,28 @@ TEST(AdjacencyMatrix, OutEdgesDirected) {
ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, OutEdgesBackwardOnlyDirected) {
entt::adjacency_matrix<entt::directed_tag> adjacency_matrix{2u};
auto iterable = adjacency_matrix.out_edges(1u);

ASSERT_EQ(iterable.begin(), iterable.end());

adjacency_matrix.insert(1u, 0u);
iterable = adjacency_matrix.out_edges(1u);

ASSERT_NE(iterable.begin(), iterable.end());

auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{1u}, std::size_t{0u}));
ASSERT_EQ(it, iterable.end());

iterable = adjacency_matrix.out_edges(0u);
it = iterable.cbegin();

ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, OutEdgesUndirected) {
entt::adjacency_matrix<entt::undirected_tag> adjacency_matrix{3u};
auto iterable = adjacency_matrix.out_edges(0u);
Expand All @@ -365,6 +422,30 @@ TEST(AdjacencyMatrix, OutEdgesUndirected) {
ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, OutEdgesBackwardOnlyUndirected) {
entt::adjacency_matrix<entt::undirected_tag> adjacency_matrix{2u};
auto iterable = adjacency_matrix.out_edges(1u);

ASSERT_EQ(iterable.begin(), iterable.end());

adjacency_matrix.insert(1u, 0u);
iterable = adjacency_matrix.out_edges(1u);

ASSERT_NE(iterable.begin(), iterable.end());

auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{1u}, std::size_t{0u}));
ASSERT_EQ(it, iterable.end());

iterable = adjacency_matrix.out_edges(0u);
it = iterable.cbegin();

ASSERT_NE(it, iterable.cend());
ASSERT_EQ(*it++, std::make_pair(std::size_t{0u}, std::size_t{1u}));
ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, InEdgesDirected) {
entt::adjacency_matrix<entt::directed_tag> adjacency_matrix{3u};
auto iterable = adjacency_matrix.in_edges(1u);
Expand All @@ -388,6 +469,28 @@ TEST(AdjacencyMatrix, InEdgesDirected) {
ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, InEdgesBackwardOnlyDirected) {
entt::adjacency_matrix<entt::directed_tag> adjacency_matrix{2u};
auto iterable = adjacency_matrix.in_edges(0u);

ASSERT_EQ(iterable.begin(), iterable.end());

adjacency_matrix.insert(1u, 0u);
iterable = adjacency_matrix.in_edges(0u);

ASSERT_NE(iterable.begin(), iterable.end());

auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{1u}, std::size_t{0u}));
ASSERT_EQ(it, iterable.end());

iterable = adjacency_matrix.in_edges(1u);
it = iterable.cbegin();

ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, InEdgesUndirected) {
entt::adjacency_matrix<entt::undirected_tag> adjacency_matrix{3u};
auto iterable = adjacency_matrix.in_edges(1u);
Expand All @@ -403,6 +506,7 @@ TEST(AdjacencyMatrix, InEdgesUndirected) {
auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{0u}, std::size_t{1u}));
ASSERT_EQ(*it++, std::make_pair(std::size_t{2u}, std::size_t{1u}));
ASSERT_EQ(it, iterable.end());

iterable = adjacency_matrix.in_edges(0u);
Expand All @@ -413,6 +517,30 @@ TEST(AdjacencyMatrix, InEdgesUndirected) {
ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, InEdgesBackwardOnlyUndirected) {
entt::adjacency_matrix<entt::undirected_tag> adjacency_matrix{2u};
auto iterable = adjacency_matrix.in_edges(0u);

ASSERT_EQ(iterable.begin(), iterable.end());

adjacency_matrix.insert(1u, 0u);
iterable = adjacency_matrix.in_edges(0u);

ASSERT_NE(iterable.begin(), iterable.end());

auto it = iterable.begin();

ASSERT_EQ(*it++, std::make_pair(std::size_t{1u}, std::size_t{0u}));
ASSERT_EQ(it, iterable.end());

iterable = adjacency_matrix.in_edges(1u);
it = iterable.cbegin();

ASSERT_NE(it, iterable.cend());
ASSERT_EQ(*it++, std::make_pair(std::size_t{0u}, std::size_t{1u}));
ASSERT_EQ(it, iterable.cend());
}

TEST(AdjacencyMatrix, ThrowingAllocator) {
using allocator = test::throwing_allocator<std::size_t>;
using exception = typename allocator::exception_type;
Expand Down

0 comments on commit cb974bf

Please sign in to comment.