Line data Source code
1 : /*
2 : * Graph_test.cpp
3 : *
4 : * Created on: 2 September 2024
5 : * Author: John Freeman
6 : *
7 : * These unit tests are basically to test out various
8 : * boost::adjacency_list tools to ensure we can generate the *.dot
9 : * files we need to plot configurations
10 : *
11 : * Much of this is heavily based on
12 : * https://www.boost.org/doc/libs/1_77_0/libs/graph/doc/bundles.html
13 : *
14 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
15 : * Licensing/copyright details are in the COPYING file that you should have
16 : * received with this code.
17 : */
18 :
19 : #define BOOST_TEST_MODULE Graph_test // NOLINT
20 :
21 : #include "boost/graph/adjacency_list.hpp"
22 : #include "boost/graph/graph_traits.hpp"
23 : #include "boost/graph/graphviz.hpp"
24 : #include "boost/graph/labeled_graph.hpp"
25 : #include "boost/test/unit_test.hpp"
26 :
27 : #include <iostream>
28 : #include <sstream>
29 : #include <string>
30 : #include <vector>
31 :
32 : using namespace boost;
33 :
34 : BOOST_AUTO_TEST_SUITE(Graph_test)
35 :
36 : namespace graphtest {
37 :
38 : struct City
39 : {
40 : std::string name;
41 : int population;
42 : std::vector<int> zipcodes;
43 : };
44 :
45 : struct Highway
46 : {
47 : std::string name;
48 : double miles;
49 : int speed_limit;
50 : };
51 :
52 : } // namespace graphtest
53 :
54 2 : BOOST_AUTO_TEST_CASE(Construct)
55 : {
56 1 : adjacency_list<vecS, vecS, bidirectionalS, graphtest::City, graphtest::Highway> interstate;
57 :
58 1 : auto chicago = add_vertex({ "Chicago", 2800000, { 60647, 60622 } }, interstate);
59 1 : auto nyc = add_vertex({ "New York", 8300000, { 10024, 10011 } }, interstate);
60 :
61 1 : auto vtxs = vertices(interstate);
62 1 : BOOST_REQUIRE(std::distance(vtxs.first, vtxs.second) == 2);
63 :
64 1 : add_edge(chicago, nyc, { "I-80", 800, 65 }, interstate);
65 :
66 1 : auto ejes = edges(interstate);
67 1 : BOOST_REQUIRE(std::distance(ejes.first, ejes.second) == 1);
68 :
69 : // Visually look at the DOT output, and then do a typical automated unit test on it
70 :
71 1 : boost::write_graphviz(std::cout,
72 : interstate,
73 : boost::make_label_writer(boost::get(&graphtest::City::name, interstate)),
74 : boost::make_label_writer(boost::get(&graphtest::Highway::name, interstate)));
75 :
76 1 : std::stringstream dotfilestream;
77 1 : boost::write_graphviz(dotfilestream,
78 : interstate,
79 : boost::make_label_writer(boost::get(&graphtest::City::name, interstate)),
80 : boost::make_label_writer(boost::get(&graphtest::Highway::name, interstate)));
81 :
82 4 : for (auto& token : { "New York", "Chicago", "I-80" }) {
83 3 : BOOST_REQUIRE(dotfilestream.str().find(token) != std::string::npos);
84 : }
85 6 : }
86 :
87 : BOOST_AUTO_TEST_SUITE_END()
|