xrpld
Loading...
Searching...
No Matches
Digraph.cpp
1#include <csf/Digraph.h>
2
3#include <gtest/gtest.h>
4
5#include <sstream>
6#include <string>
7#include <tuple>
8#include <vector>
9
10namespace xrpl::test {
11
12TEST(DigraphTest, digraph)
13{
14 using namespace csf;
15 using Graph = Digraph<char, std::string>;
16 Graph graph;
17
18 EXPECT_TRUE(!graph.connected('a', 'b'));
19 EXPECT_TRUE(!graph.edge('a', 'b'));
20 EXPECT_TRUE(!graph.disconnect('a', 'b'));
21
22 EXPECT_TRUE(graph.connect('a', 'b', "foobar"));
23 EXPECT_TRUE(graph.connected('a', 'b'));
24 EXPECT_TRUE(*graph.edge('a', 'b') == "foobar"); // NOLINT(bugprone-unchecked-optional-access)
25
26 EXPECT_TRUE(!graph.connect('a', 'b', "repeat"));
27 EXPECT_TRUE(graph.disconnect('a', 'b'));
28 EXPECT_TRUE(graph.connect('a', 'b', "repeat"));
29 EXPECT_TRUE(graph.connected('a', 'b'));
30 EXPECT_TRUE(*graph.edge('a', 'b') == "repeat"); // NOLINT(bugprone-unchecked-optional-access)
31
32 EXPECT_TRUE(graph.connect('a', 'c', "tree"));
33
34 {
36
37 for (auto const& edge : graph.outEdges('a'))
38 {
39 edges.emplace_back(edge.source, edge.target, edge.data);
40 }
41
43 expected.emplace_back('a', 'b', "repeat");
44 expected.emplace_back('a', 'c', "tree");
45 EXPECT_TRUE(edges == expected);
46 EXPECT_TRUE(graph.outDegree('a') == expected.size());
47 }
48
49 EXPECT_TRUE(graph.outEdges('r').size() == 0);
50 EXPECT_TRUE(graph.outDegree('r') == 0);
51 EXPECT_TRUE(graph.outDegree('c') == 0);
52
53 // only 'a' has out edges
54 EXPECT_TRUE(graph.outVertices().size() == 1);
55 std::vector<char> const expected = {'b', 'c'};
56
57 EXPECT_TRUE((graph.outVertices('a') == expected));
58 EXPECT_TRUE(graph.outVertices('b').size() == 0);
59 EXPECT_TRUE(graph.outVertices('c').size() == 0);
60 EXPECT_TRUE(graph.outVertices('r').size() == 0);
61
63 graph.saveDot(ss, [](char v) { return v; });
64 std::string const expectedDot =
65 "digraph {\n"
66 "a -> b;\n"
67 "a -> c;\n"
68 "}\n";
69 EXPECT_TRUE(ss.str() == expectedDot);
70}
71
72} // namespace xrpl::test
T emplace_back(T... args)
TEST(UnitsTest, types)
Definition Units.cpp:16
T size(T... args)
T str(T... args)