Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
test_visit_order.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <graphle.hpp>
5
6#include <cstddef>
7#include <vector>
8#include <span>
9#include <ranges>
10#include <typeinfo>
11#include <format>
12
13
14namespace graphle::test {
15 using vertex_list = std::vector<std::size_t>;
16 using vertex_order = std::vector<vertex_list>;
17
18
27 template <graphle::meta::list_of_types DataStructures, typename Algorithm>
28 inline void test_visit_order(const graphle::test::ve_list_graph& src, std::size_t root, const vertex_order& vertex_ordering, Algorithm&& algorithm) {
29 DataStructures::foreach([&] <typename DS> {
30 // Intentional copy: we will remove visited elements.
31 vertex_order vertex_order_copy = vertex_ordering;
32 // Use span so we can easily pop from the front.
33 std::span ordering = vertex_order_copy;
34
35 auto structure = DS::from_ve_list(src);
36 auto graph = structure.view_as_graph();
37
38 using vertex = graphle::vertex_of<decltype(graph)>;
39 using edge = graphle::edge_of<decltype(graph)>;
40
41
44 .discover_vertex = [&] (auto vertex, auto& graph) {
45 auto it = std::ranges::find(ordering.front(), vertex->vertex_id);
46 ASSERT_FALSE(it == ordering.front().end());
47
48 ordering.front().erase(it);
49 if (ordering.front().empty()) ordering = ordering.subspan(1);
50 },
51 .finish_search = [&] (auto& graph) {
52 ASSERT_TRUE(ordering.empty());
53 }
54 };
55
56
57 SUBTEST_SCOPE(typeid(DS).name()) {
58 auto root_vertex = [&] {
59 if constexpr (vertex_list_graph<decltype(graph)>) {
60 return util::find_vertex(graph, [&] (auto v) { return v->vertex_id == root; });
61 } else if constexpr (requires { structure.root; }) {
62 if (structure.root->vertex_id == root) return structure.root.get();
63 }
64
65 FAIL("Cannot find root vertex for the given graph type.");
66 } ();
67
68
69 std::invoke(
70 algorithm,
71 graph,
72 root_vertex,
73 visitor
74 );
75 }
76 });
77 }
78}
79
80
typename std::remove_cvref_t< G >::edge_type edge_of
Definition: graph.hpp:93
typename std::remove_cvref_t< G >::vertex_type vertex_of
Definition: graph.hpp:91
constexpr deduce_as_t< T > deduce_as
Definition: value.hpp:32
std::vector< std::size_t > vertex_list
std::vector< vertex_list > vertex_order
void test_visit_order(const graphle::test::ve_list_graph &src, std::size_t root, const vertex_order &vertex_ordering, Algorithm &&algorithm)
constexpr vertex_of< G > find_vertex(G &&graph, F &&pred)
meta::deduce_as_t< Graph > deduce_graph_type
Definition: visitor.hpp:154
#define SUBTEST_SCOPE(Name)
#define FAIL(...)
#define ASSERT_TRUE(...)
#define ASSERT_FALSE(...)