1/*
2 * graph_unit_test.hpp
3 *
4 * Created on: Nov 22, 2014
5 * Author: i-bird
6 */
7
8#ifndef GRAPH_UNIT_TEST_HPP_
9#define GRAPH_UNIT_TEST_HPP_
10
11#include "config.h"
12#include "map_graph.hpp"
13#include "Point_test.hpp"
14
15BOOST_AUTO_TEST_SUITE( graph_test )
16
17BOOST_AUTO_TEST_CASE( graph_use)
18{
19 std::cout << "Graph unit test start" << "\n";
20
21 //! [Define vertex and edge of the graph]
22 typedef Point_test<float> V;
23 typedef Point_test<float> E;
24 //! [Define vertex and edge of the graph]
25
26 // Point
27 Point_test<float> p;
28 p.setx(1.0);
29 p.sety(2.0);
30 p.setz(3.0);
31 p.sets(4.0);
32
33 p.get<V::v>()[0] = 1.0;
34 p.get<V::v>()[1] = 2.0;
35 p.get<V::v>()[2] = 7.0;
36
37 p.get<V::t>()[0][0] = 10.0;
38 p.get<V::t>()[0][1] = 13.0;
39 p.get<V::t>()[0][2] = 8.0;
40 p.get<V::t>()[1][0] = 19.0;
41 p.get<V::t>()[1][1] = 23.0;
42 p.get<V::t>()[1][2] = 5.0;
43 p.get<V::t>()[2][0] = 4.0;
44 p.get<V::t>()[2][1] = 3.0;
45 p.get<V::t>()[2][2] = 11.0;
46
47 //! [Create a Cartesian graph]
48
49 //! define a test graph where the vertex and edge store several scalar vectorial and tensorial quantities
50 Graph_CSR<V,E> g;
51
52 //! construct a 2D dimensional Cartesian graph
53
54 // first create the vertex
55
56 for (size_t i = 0 ; i < GS_SIZE ; i++)
57 {
58 for (size_t j = 0 ; j < GS_SIZE ; j++)
59 {
60 // Add vertex
61
62 g.addVertex(p);
63 }
64 }
65
66 // than create the edge
67
68 // Create a grid, // NOTE this does not produce any memory grid, it retain only the information
69 // and give a set of usefull function
70
71
72/* std::vector<size_t> gs;
73 gs.push_back(GS_SIZE);
74 gs.push_back(GS_SIZE);*/
75
76 size_t gs[2] = {GS_SIZE,GS_SIZE};
77
78 grid_sm<2,void> g2(gs);
79
80 // Create the edge 4 for each vertex
81
82 for (size_t i = 0 ; i < GS_SIZE ; i++)
83 {
84 for (size_t j = 0 ; j < GS_SIZE ; j++)
85 {
86 // Add 4 edge
87
88 g.addEdge<CheckExistence>(g2.Lin(i,j),g2.Lin(i+1,j),p);
89 g.addEdge<CheckExistence>(g2.Lin(i,j),g2.Lin(i,j+1),p);
90 g.addEdge<CheckExistence>(g2.Lin(i,j),g2.Lin(i-1,j),p);
91 g.addEdge<CheckExistence>(g2.Lin(i,j),g2.Lin(i,j-1),p);
92 }
93 }
94
95 //! [Create a Cartesian graph]
96
97 // Test if duplicate work
98
99 Graph_CSR<V,E> g_dup = g.duplicate();
100
101 // check if the two graph matches
102
103 for (size_t i = 0 ; i < g.getNVertex() ; i++)
104 {
105 BOOST_REQUIRE_EQUAL(g.vertex(i).template get<V::x>(),g_dup.vertex(i).template get<V::x>());
106 BOOST_REQUIRE_EQUAL(g.vertex(i).template get<V::y>(),g_dup.vertex(i).template get<V::y>());
107 BOOST_REQUIRE_EQUAL(g.vertex(i).template get<V::z>(),g_dup.vertex(i).template get<V::z>());
108 BOOST_REQUIRE_EQUAL(g.vertex(i).template get<V::s>(),g_dup.vertex(i).template get<V::s>());
109
110 for (int j = 0 ; j < 3 ; j++)
111 {
112 BOOST_REQUIRE_EQUAL(g.vertex(i).template get<V::v>()[j],g_dup.vertex(i).template get<V::v>()[j]);
113 }
114
115 for (int j = 0 ; j < 3 ; j++)
116 {
117 for (int k = 0 ; k < 3 ; k++)
118 {
119 BOOST_REQUIRE_EQUAL(g.vertex(i).template get<V::t>()[j][k],g_dup.vertex(i).template get<V::t>()[j][k]);
120 }
121 }
122 }
123
124 // Test the no edge case
125
126 //! [Create a tree graph with no edge properties]
127 Graph_CSR<V> g_no_edge;
128
129 // Create a tree
130
131 // root
132 g_no_edge.addVertex(p);
133
134 // 2 leaf
135
136 g_no_edge.addVertex(p);
137 g_no_edge.addEdge(0,1);
138 g_no_edge.addVertex(p);
139 g_no_edge.addEdge(0,2);
140
141 // 4 leaf
142
143 g_no_edge.addVertex(p);
144 g_no_edge.addEdge(1,3);
145 g_no_edge.addVertex(p);
146 g_no_edge.addEdge(1,4);
147 g_no_edge.addVertex(p);
148 g_no_edge.addEdge(2,5);
149 g_no_edge.addVertex(p);
150 g_no_edge.addEdge(2,6);
151
152 // 8 leaf
153
154 g_no_edge.addVertex(p);
155 g_no_edge.addEdge(3,7);
156 g_no_edge.addVertex(p);
157 g_no_edge.addEdge(3,8);
158 g_no_edge.addVertex(p);
159 g_no_edge.addEdge(4,9);
160 g_no_edge.addVertex(p);
161 g_no_edge.addEdge(4,10);
162 g_no_edge.addVertex(p);
163 g_no_edge.addEdge(5,11);
164 g_no_edge.addVertex(p);
165 g_no_edge.addEdge(5,12);
166 g_no_edge.addVertex(p);
167 g_no_edge.addEdge(6,13);
168 g_no_edge.addVertex(p);
169 g_no_edge.addEdge(6,14);
170
171 //! [Create a tree graph with no edge properties]
172
173 // Check that each vertex has 2 child
174
175 for (size_t i = 0 ; i < g_no_edge.getNVertex() ; i++)
176 {
177 if (g_no_edge.getNChilds(i) == 0)
178 continue;
179
180 size_t s1 = g_no_edge.getChild(i,0);
181 size_t s2 = g_no_edge.getChild(i,1);
182
183 BOOST_REQUIRE_EQUAL(s1+1,s2);
184
185 size_t a = log2(i + 1);
186 size_t start = (a == 0)?1:(2 << (a-1));
187 start -= 1;
188 size_t start_p = 2 << a;
189 start_p -= 1;
190
191 BOOST_REQUIRE_EQUAL(s1,start_p + (i - start)*2);
192 }
193
194 std::cout << "Graph unit test end" << "\n";
195}
196
197BOOST_AUTO_TEST_SUITE_END()
198
199
200#endif /* GRAPH_UNIT_TEST_HPP_ */
201