|
| 1 | +#include "testing/hgl/constants.hpp" |
| 2 | + |
| 3 | +#include <doctest.h> |
| 4 | +#include <hgl/impl/hyperedge_list.hpp> |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +namespace hgl_testing { |
| 9 | + |
| 10 | +TEST_SUITE_BEGIN("test_hyperedge_list"); |
| 11 | + |
| 12 | +struct test_hyperedge_list { |
| 13 | + template <typename HyperedgeList> |
| 14 | + typename HyperedgeList::hypergraph_storage_type& storage(HyperedgeList& sut) const noexcept { |
| 15 | + return sut._storage; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +struct test_undirected_hyperedge_list : public test_hyperedge_list { |
| 20 | + using sut_type = hgl::impl::undirected_hyperedge_list; |
| 21 | +}; |
| 22 | + |
| 23 | +TEST_CASE_FIXTURE(test_undirected_hyperedge_list, "should initialize empty storage by default") { |
| 24 | + sut_type sut{}; |
| 25 | + CHECK(storage(sut).empty()); |
| 26 | +} |
| 27 | + |
| 28 | +TEST_CASE_FIXTURE( |
| 29 | + test_undirected_hyperedge_list, |
| 30 | + "initialization with size parameters should properly initialize storage" |
| 31 | +) { |
| 32 | + sut_type sut(constants::n_vertices, constants::n_hyperedges); |
| 33 | + CHECK_EQ(storage(sut).size(), constants::n_hyperedges); |
| 34 | + CHECK(std::ranges::all_of(storage(sut), [](const auto& hyperedge_storage) { |
| 35 | + return hyperedge_storage.empty(); |
| 36 | + })); |
| 37 | +} |
| 38 | + |
| 39 | +TEST_CASE_FIXTURE( |
| 40 | + test_undirected_hyperedge_list, "add_hyperedges should properly extend the hypergraph storage" |
| 41 | +) { |
| 42 | + sut_type sut{}; |
| 43 | + sut.add_hyperedges(constants::n_hyperedges); |
| 44 | + CHECK_EQ(storage(sut).size(), constants::n_hyperedges); |
| 45 | + CHECK(std::ranges::all_of(storage(sut), [](const auto& hyperedge_storage) { |
| 46 | + return hyperedge_storage.empty(); |
| 47 | + })); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_CASE_FIXTURE( |
| 51 | + test_undirected_hyperedge_list, |
| 52 | + "remove_hyperedge should properly erase the proper hyperedge storage from the hypergraph" |
| 53 | +) { |
| 54 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 55 | + REQUIRE_EQ(storage(sut).size(), constants::n_hyperedges); |
| 56 | + |
| 57 | + sut.remove_hyperedge(constants::id1); |
| 58 | + CHECK_EQ(storage(sut).size(), constants::n_hyperedges - 1uz); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_CASE_FIXTURE(test_undirected_hyperedge_list, "hyperedge_size should return 0 by default") { |
| 62 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 63 | + CHECK(std::ranges::all_of(constants::hyperedge_ids_view, [&sut](const auto hyperedge_id) { |
| 64 | + return sut.hyperedge_size(hyperedge_id) == 0uz; |
| 65 | + })); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_CASE_FIXTURE( |
| 69 | + test_undirected_hyperedge_list, "hyperedge_vertices should remove an empty view by default" |
| 70 | +) { |
| 71 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 72 | + CHECK(std::ranges::all_of(constants::hyperedge_ids_view, [&sut](const auto hyperedge_id) { |
| 73 | + return std::ranges::empty(sut.hyperedge_vertices(hyperedge_id)); |
| 74 | + })); |
| 75 | +} |
| 76 | + |
| 77 | +TEST_CASE_FIXTURE( |
| 78 | + test_undirected_hyperedge_list, |
| 79 | + "bind(hyperedge, vertex) should add the vertex to the given hyperedge's storage only if the " |
| 80 | + "they are not bound" |
| 81 | +) { |
| 82 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 83 | + REQUIRE(std::ranges::empty(sut.hyperedge_vertices(constants::id1))); |
| 84 | + |
| 85 | + sut.bind(constants::id1, constants::id1); |
| 86 | + const auto vertices1 = sut.hyperedge_vertices(constants::id1) | std::ranges::to<std::vector>(); |
| 87 | + CHECK_EQ(sut.hyperedge_size(constants::id1), 1uz); |
| 88 | + CHECK_EQ(std::ranges::size(vertices1), 1uz); |
| 89 | + CHECK(std::ranges::contains(vertices1, constants::id1)); |
| 90 | + CHECK(std::ranges::equal(vertices1, storage(sut)[constants::id1])); |
| 91 | + |
| 92 | + sut.bind(constants::id1, constants::id1); |
| 93 | + const auto vertices2 = sut.hyperedge_vertices(constants::id1) | std::ranges::to<std::vector>(); |
| 94 | + CHECK_EQ(std::ranges::size(vertices2), 1uz); |
| 95 | + CHECK(std::ranges::equal(vertices2, vertices1)); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_CASE_FIXTURE( |
| 99 | + test_undirected_hyperedge_list, |
| 100 | + "unbind(hyperedge, vertex) should remove the vertex from the given hyperedge's storage only if " |
| 101 | + "the they are bound" |
| 102 | +) { |
| 103 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 104 | + REQUIRE(std::ranges::empty(sut.hyperedge_vertices(constants::id1))); |
| 105 | + |
| 106 | + sut.bind(constants::id1, constants::id1); |
| 107 | + const auto vertices1 = sut.hyperedge_vertices(constants::id1) | std::ranges::to<std::vector>(); |
| 108 | + REQUIRE_EQ(sut.hyperedge_size(constants::id1), 1uz); |
| 109 | + REQUIRE(std::ranges::contains(vertices1, constants::id1)); |
| 110 | + |
| 111 | + sut.unbind(constants::id1, constants::id2); |
| 112 | + const auto vertices2 = sut.hyperedge_vertices(constants::id1) | std::ranges::to<std::vector>(); |
| 113 | + REQUIRE_EQ(std::ranges::size(vertices2), 1uz); |
| 114 | + REQUIRE(std::ranges::equal(vertices2, vertices1)); |
| 115 | + |
| 116 | + sut.unbind(constants::id1, constants::id1); |
| 117 | + CHECK(std::ranges::empty(sut.hyperedge_vertices(constants::id1))); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_CASE_FIXTURE( |
| 121 | + test_undirected_hyperedge_list, |
| 122 | + "are_bound(hyperedge, vertex) should return true only when the given vertex is present in the " |
| 123 | + "hyperedge's storage" |
| 124 | +) { |
| 125 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 126 | + REQUIRE(std::ranges::empty(sut.hyperedge_vertices(constants::id1))); |
| 127 | + |
| 128 | + sut.bind(constants::id1, constants::id1); |
| 129 | + const auto vertices1 = sut.hyperedge_vertices(constants::id1) | std::ranges::to<std::vector>(); |
| 130 | + REQUIRE_EQ(sut.hyperedge_size(constants::id1), 1uz); |
| 131 | + REQUIRE(std::ranges::contains(vertices1, constants::id1)); |
| 132 | + |
| 133 | + CHECK(sut.are_bound(constants::id1, constants::id1)); |
| 134 | + CHECK_FALSE(sut.are_bound(constants::id1, constants::id2)); |
| 135 | + CHECK_FALSE(sut.are_bound(constants::id2, constants::id1)); |
| 136 | +} |
| 137 | + |
| 138 | +TEST_CASE_FIXTURE( |
| 139 | + test_undirected_hyperedge_list, |
| 140 | + "remove_vertex should unbind the given vertex from all hyperedges" |
| 141 | +) { |
| 142 | + sut_type sut{constants::n_vertices, constants::n_hyperedges}; |
| 143 | + for (const auto hyperedge_id : constants::hyperedge_ids_view) |
| 144 | + sut.bind(hyperedge_id, constants::id1); |
| 145 | + REQUIRE(std::ranges::all_of(constants::hyperedge_ids_view, [&sut](const auto hyperedge_id) { |
| 146 | + return sut.are_bound(hyperedge_id, constants::id1); |
| 147 | + })); |
| 148 | + |
| 149 | + sut.remove_vertex(constants::id1); |
| 150 | + CHECK(std::ranges::all_of(constants::hyperedge_ids_view, [&sut](const auto hyperedge_id) { |
| 151 | + return std::ranges::empty(sut.hyperedge_vertices(hyperedge_id)); |
| 152 | + })); |
| 153 | +} |
| 154 | + |
| 155 | +TEST_SUITE_END(); // test_hyperedge_list |
| 156 | + |
| 157 | +} // namespace hgl_testing |
0 commit comments