$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tests/util/graph.cc
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #include <mln/util/graph.hh>
27 #include <iostream>
28 
29 int main ()
30 {
31  using namespace mln;
32 
33  util::graph g;
34 
35  g.add_vertices(6);
36  g.add_edge (0, 1);
37  g.add_edge (0, 2);
38  g.add_edge (3, 4);
39  g.add_edge (4, 5);
40  g.add_edge (5, 4);
41  g.add_edge (1, 0);
42  g.add_edge (5, 3);
43  g.add_edge (2, 1);
44 
45 
46  // Vertex iter and edge iter
47  {
48  unsigned i = 0;
49  mln_vertex_fwd_iter_(util::graph) v(g);
50  for_all(v)
51  mln_assertion(i++ == v.id());
52  mln_assertion(i != 0);
53 
54  i = 0;
55  mln_edge_fwd_iter_(util::graph) e(g);
56  for_all(e)
57  mln_assertion(i++ == e.id());
58  mln_assertion(i != 0);
59  }
60  {
61  unsigned i = g.v_nmax() - 1;
62  mln_vertex_bkd_iter_(util::graph) v(g);
63  for_all(v)
64  mln_assertion(i-- == v.id());
65  mln_assertion(i != g.v_nmax() - 1);
66 
67  i = g.e_nmax() - 1;
68  mln_edge_bkd_iter_(util::graph) e(g);
69  for_all(e)
70  mln_assertion(i-- == e.id());
71  mln_assertion(i != g.e_nmax() - 1);
72  }
73 
74  // vertex iter + Edge nbh iter
75  {
76  mln_vertex_fwd_iter_(util::graph) v(g);
77  mln_vertex_nbh_edge_fwd_iter_(util::graph) n(v);
78  for_all(v)
79  {
80  unsigned i = 0;
81  for_all(n)
82  mln_assertion(i++ == n.index());
83  mln_assertion(i != 0);
84  }
85  }
86  {
87  mln_vertex_bkd_iter_(util::graph) v(g);
88  mln_vertex_nbh_edge_bkd_iter_(util::graph) e(v);
89  for_all(v)
90  {
91  unsigned i = v.nmax_nbh_edges();
92  for_all(e)
93  mln_assertion(--i == e.index());
94  mln_assertion((v.nmax_nbh_edges() == 0 && i == 0) || i != v.nmax_nbh_edges());
95  }
96  }
97 
98  {
99  mln_edge_fwd_iter_(util::graph) e(g);
100  mln_edge_nbh_edge_fwd_iter_(util::graph) n(e);
101  for_all(e)
102  {
103  unsigned i = 0;
104  for_all(n)
105  ++i;
106  // we check i == e.nmax_nbh_edges() - 2 since e is it's own neighboor and the
107  // iterator skip it.
108  mln_assertion((i == 0 && e.nmax_nbh_edges() < 2) || i == e.nmax_nbh_edges() - 2);
109  }
110  }
111  {
112  mln_edge_bkd_iter_(util::graph) e(g);
113  mln_edge_nbh_edge_bkd_iter_(util::graph) n(e);
114  for_all(e)
115  {
116  //std::cout << "== e.id() = " << e.id() << std::endl;
117  unsigned i = e.nmax_nbh_edges();
118  for_all(n)
119  --i;
120  // we check i == e.nmax_nbh_edges() - 2 since e is it's own neighboor and the
121  // iterator skip it.
122  mln_assertion((i == e.nmax_nbh_edges() && e.nmax_nbh_edges() < 2) || i == 2);
123 
124  }
125  }
126 
127  {
128  mln_vertex_fwd_iter_(util::graph) v(g);
129  mln_vertex_nbh_vertex_fwd_iter_(util::graph) n(v);
130  for_all(v)
131  {
132  unsigned i = 0;
133  for_all(n)
134  ++i;
135  mln_assertion(i == v.nmax_nbh_vertices());
136  }
137  }
138  {
139  mln_vertex_bkd_iter_(util::graph) v(g);
140  mln_vertex_nbh_vertex_bkd_iter_(util::graph) n(v);
141  for_all(v)
142  {
143  unsigned i = v.nmax_nbh_vertices();
144  for_all(n)
145  --i;
146  mln_assertion(i == 0);
147  }
148  }
149 }