rippled
Loading...
Searching...
No Matches
Digraph_test.cpp
1#include <test/csf/Digraph.h>
2
3#include <xrpl/beast/unit_test.h>
4
5#include <string>
6#include <vector>
7
8namespace xrpl {
9namespace test {
10
12{
13public:
14 void
15 run() override
16 {
17 using namespace csf;
18 using Graph = Digraph<char, std::string>;
19 Graph graph;
20
21 BEAST_EXPECT(!graph.connected('a', 'b'));
22 BEAST_EXPECT(!graph.edge('a', 'b'));
23 BEAST_EXPECT(!graph.disconnect('a', 'b'));
24
25 BEAST_EXPECT(graph.connect('a', 'b', "foobar"));
26 BEAST_EXPECT(graph.connected('a', 'b'));
27 BEAST_EXPECT(
28 *graph.edge('a', 'b') == "foobar"); // NOLINT(bugprone-unchecked-optional-access)
29
30 BEAST_EXPECT(!graph.connect('a', 'b', "repeat"));
31 BEAST_EXPECT(graph.disconnect('a', 'b'));
32 BEAST_EXPECT(graph.connect('a', 'b', "repeat"));
33 BEAST_EXPECT(graph.connected('a', 'b'));
34 BEAST_EXPECT(
35 *graph.edge('a', 'b') == "repeat"); // NOLINT(bugprone-unchecked-optional-access)
36
37 BEAST_EXPECT(graph.connect('a', 'c', "tree"));
38
39 {
41
42 for (auto const& edge : graph.outEdges('a'))
43 {
44 edges.emplace_back(edge.source, edge.target, edge.data);
45 }
46
48 expected.emplace_back('a', 'b', "repeat");
49 expected.emplace_back('a', 'c', "tree");
50 BEAST_EXPECT(edges == expected);
51 BEAST_EXPECT(graph.outDegree('a') == expected.size());
52 }
53
54 BEAST_EXPECT(graph.outEdges('r').size() == 0);
55 BEAST_EXPECT(graph.outDegree('r') == 0);
56 BEAST_EXPECT(graph.outDegree('c') == 0);
57
58 // only 'a' has out edges
59 BEAST_EXPECT(graph.outVertices().size() == 1);
60 std::vector<char> const expected = {'b', 'c'};
61
62 BEAST_EXPECT((graph.outVertices('a') == expected));
63 BEAST_EXPECT(graph.outVertices('b').size() == 0);
64 BEAST_EXPECT(graph.outVertices('c').size() == 0);
65 BEAST_EXPECT(graph.outVertices('r').size() == 0);
66
68 graph.saveDot(ss, [](char v) { return v; });
69 std::string const expectedDot =
70 "digraph {\n"
71 "a -> b;\n"
72 "a -> c;\n"
73 "}\n";
74 BEAST_EXPECT(ss.str() == expectedDot);
75 }
76};
77
78BEAST_DEFINE_TESTSUITE(Digraph, csf, xrpl);
79
80} // namespace test
81} // namespace xrpl
A testsuite class.
Definition suite.h:51
void run() override
Runs the suite.
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T size(T... args)
T str(T... args)