test_indexedgraph.cxx
Go to the documentation of this file.
2 #include "WireCellUtil/Testing.h"
3 
4 #include <boost/graph/graphviz.hpp>
5 
6 using namespace WireCell;
7 
8 char foo(size_t ind)
9 {
10  return "if"[ind];
11 }
12 
13 typedef std::shared_ptr<int> iptr_t;
14 typedef std::shared_ptr<float> fptr_t;
15 typedef std::variant<iptr_t, fptr_t> if_t;
16 
17 struct if_node_t {
19  if_node_t() : ptr() { }
20  if_node_t(const if_t& ift) : ptr(ift) { }
21  if_node_t(const iptr_t& i) : ptr(i) { }
22  if_node_t(const fptr_t& f) : ptr(f) { }
23 
24  bool operator==(const if_node_t &other) const {
25  return ptr == other.ptr;
26  }
27 };
28 
29 
30 namespace std {
31  template<>
32  struct hash<if_node_t> {
33  std::size_t operator()(const if_node_t& n) const {
34  if (std::holds_alternative<iptr_t>(n.ptr)) {
35  return (size_t)(std::get<iptr_t>(n.ptr).get());
36  }
37  if (std::holds_alternative<fptr_t>(n.ptr)) {
38  return (size_t)(std::get<fptr_t>(n.ptr).get());
39  }
40  return 0;
41  }
42  };
43 }
44 
45 int main()
46 {
47 
48 
49  std::unordered_map<if_t, int> p2i;
50 
51  typedef IndexedGraph<if_node_t> indexed_graph_t;
52  indexed_graph_t g;
53  if_t one = std::make_shared<int>(42);
54  if_t two = std::make_shared<float>(6.9);
55  if_t tre = std::make_shared<float>(33);
56 
57  p2i[one] = 1;
58  p2i[two] = 2;
59  p2i[tre] = 3;
60 
61  // test variant
62  Assert(1 == tre.index());
63  Assert(*std::get<1>(two) < *std::get<1>(tre));
64  Assert(one < two); // caution: compares against indices first!
65  Assert(*std::get<0>(one) == 42);
66  Assert(nullptr == std::get_if<1>(&one));
67  Assert('i' == foo(0));
68  Assert('f' == foo(1));
69 
70  if_t oneprime = one;
71  Assert(one == oneprime);
72 
73  auto v1 = g.vertex(one);
74  auto v2 = g.vertex(one);
75  Assert(v1 == v2);
76  g.edge(one, two);
77  g.edge(one, two);
78  g.edge(two, one);
79  g.edge(one, tre);
80  auto verts = boost::vertices(g.graph());
81  Assert(verts.second-verts.first == 3);
82 
83  Assert(g.has(oneprime));
84 
85  indexed_graph_t g2(g.graph());
86 
87  for (auto n : g2.neighbors(two)) {
88  Assert(g2.has(n));
89  }
90 
91 
92 
93  std::vector<std::string> names{"one","two","tre"};
94  std::unordered_map<indexed_graph_t::vdesc_t, std::string> ids;
95  for (auto u : boost::make_iterator_range(vertices(g2.graph()))) {
96  ids[u] = names[ids.size()];
97  }
98  boost::default_writer w;
99  boost::write_graphviz(std::cout,
100  g2.graph(),
101  w,w,w,
102  boost::make_assoc_property_map(ids));
103 
104  return 0;
105 
106 }
if_node_t(const fptr_t &f)
std::variant< iptr_t, fptr_t > if_t
char foo(size_t ind)
STL namespace.
static const double g
Definition: Units.h:145
if_node_t(const if_t &ift)
bool operator==(const if_node_t &other) const
#define Assert
Definition: Testing.h:7
std::shared_ptr< int > iptr_t
std::shared_ptr< float > fptr_t
Definition: Main.h:22
int main()
std::size_t operator()(const if_node_t &n) const
if_node_t(const iptr_t &i)
std::size_t n
Definition: format.h:3399