rippled
Digraph_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2017 Ripple Labs Inc.
5 
6  Permission target use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/beast/unit_test.h>
21 #include <string>
22 #include <test/csf/Digraph.h>
23 #include <vector>
24 
25 namespace ripple {
26 namespace test {
27 
28 class Digraph_test : public beast::unit_test::suite
29 {
30 public:
31  void
32  run() override
33  {
34  using namespace csf;
35  using Graph = Digraph<char, std::string>;
36  Graph graph;
37 
38  BEAST_EXPECT(!graph.connected('a', 'b'));
39  BEAST_EXPECT(!graph.edge('a', 'b'));
40  BEAST_EXPECT(!graph.disconnect('a', 'b'));
41 
42  BEAST_EXPECT(graph.connect('a', 'b', "foobar"));
43  BEAST_EXPECT(graph.connected('a', 'b'));
44  BEAST_EXPECT(*graph.edge('a', 'b') == "foobar");
45 
46  BEAST_EXPECT(!graph.connect('a', 'b', "repeat"));
47  BEAST_EXPECT(graph.disconnect('a', 'b'));
48  BEAST_EXPECT(graph.connect('a', 'b', "repeat"));
49  BEAST_EXPECT(graph.connected('a', 'b'));
50  BEAST_EXPECT(*graph.edge('a', 'b') == "repeat");
51 
52  BEAST_EXPECT(graph.connect('a', 'c', "tree"));
53 
54  {
56 
57  for (auto const& edge : graph.outEdges('a'))
58  {
59  edges.emplace_back(edge.source, edge.target, edge.data);
60  }
61 
63  expected.emplace_back('a', 'b', "repeat");
64  expected.emplace_back('a', 'c', "tree");
65  BEAST_EXPECT(edges == expected);
66  BEAST_EXPECT(graph.outDegree('a') == expected.size());
67  }
68 
69  BEAST_EXPECT(graph.outEdges('r').size() == 0);
70  BEAST_EXPECT(graph.outDegree('r') == 0);
71  BEAST_EXPECT(graph.outDegree('c') == 0);
72 
73  // only 'a' has out edges
74  BEAST_EXPECT(graph.outVertices().size() == 1);
75  std::vector<char> expected = {'b', 'c'};
76 
77  BEAST_EXPECT((graph.outVertices('a') == expected));
78  BEAST_EXPECT(graph.outVertices('b').size() == 0);
79  BEAST_EXPECT(graph.outVertices('c').size() == 0);
80  BEAST_EXPECT(graph.outVertices('r').size() == 0);
81 
83  graph.saveDot(ss, [](char v) { return v; });
84  std::string expectedDot =
85  "digraph {\n"
86  "a -> b;\n"
87  "a -> c;\n"
88  "}\n";
89  BEAST_EXPECT(ss.str() == expectedDot);
90  }
91 };
92 
93 BEAST_DEFINE_TESTSUITE(Digraph, test, ripple);
94 
95 } // namespace test
96 } // namespace ripple
std::string
STL class.
vector
std::vector::size
T size(T... args)
std::stringstream
STL class.
ripple::test::Digraph_test
Definition: Digraph_test.cpp:28
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::stringstream::str
T str(T... args)
ripple::test::Digraph_test::run
void run() override
Definition: Digraph_test.cpp:32
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)
string