graph.vvh
Go to the documentation of this file.00001 #ifndef TEST_GRAPH_VVH
00002 #define TEST_GRAPH_VVH
00003
00010 #include <test/test.h>
00011
00012 #include <util/forall.h>
00013 #include <util/unorderedset.h>
00014
00021 #define CHECK_NEIGHBORS_GRAPH(G) \
00022 TEST_RESULT &= test::check_neighborsGraph(G, __LINE__)
00023
00030 #define CHECK_NEIGHBORS_BIGRAPH(G) \
00031 TEST_RESULT &= test::check_neighborsBiGraph(G, __LINE__)
00032
00038 #define CHECK_SYMMETRIC_GRAPH(G) \
00039 TEST_RESULT &= test::check_symmetricGraph(G, __LINE__)
00040
00046 #define CHECK_SYMMETRIC_BIGRAPH(G) \
00047 TEST_RESULT &= test::check_symmetricBiGraph(G, __LINE__)
00048
00049 namespace test
00050 {
00051
00052 template <typename VertexContent>
00053 QString toStr(const graph::Vertex<VertexContent>& v)
00054 {
00055 return QString("0x%1").arg(v.id(), 0, 16);
00056 }
00057
00058 template <typename Graph>
00059 bool check_neighborsGraph(const Graph& G, size_t line_nb)
00060 {
00061 bool result = true;
00062 typedef typename Graph::vertex_t vertex;
00063 forall const vertex& v in G:
00064 {
00065 std::unordered_multiset<vertex> ns;
00066 forall const vertex& n in G.neighbors(v):
00067 {
00068 if(v == n)
00069 {
00070 if(result) FAILED_FUNCTION(line_nb);
00071 out << endl << " vertex " << toStr(v) << " is its own neighbor" << endl;
00072 result = false;
00073 }
00074 if(!G.contains(n))
00075 {
00076 if(result) FAILED_FUNCTION(line_nb);
00077 out << endl << " vertex " << toStr(n) << " is neighbor of " << toStr(v) << " but is not in the graph" << endl;
00078 result = false;
00079 }
00080 if(ns.count(n))
00081 {
00082 if(result) FAILED_FUNCTION(line_nb);
00083 out << endl << " vertex " << toStr(n) << " is " << ns.count(n)+1 << " times the neighbor of " << toStr(v) << endl;
00084 result = false;
00085 }
00086 ns.insert(n);
00087 }
00088 }
00089 return result;
00090 }
00091
00092 template <typename BiGraph>
00093 bool check_neighborsBiGraph(const BiGraph& G, size_t line_nb)
00094 {
00095 bool result = true;
00096 typedef typename BiGraph::vertex1_t vertex1;
00097 typedef typename BiGraph::vertex2_t vertex2;
00098 forall const vertex1& v in G.vertices1():
00099 {
00100 std::unordered_multiset<vertex2> ns;
00101 forall const vertex2& n in G.neighbors(v):
00102 {
00103 if(!G.contains(n))
00104 {
00105 if(result) FAILED_FUNCTION(line_nb);
00106 out << endl << " vertex2 " << toStr(n) << " is neighbor of 0x"
00107 << QString::number(v.id(), 16) << " but is not in the graph" << endl;
00108 result = false;
00109 }
00110 if(ns.count(n))
00111 {
00112 if(result) FAILED_FUNCTION(line_nb);
00113 out << endl << " vertex2 " << toStr(n) << " is "
00114 << ns.count(n)+1 << " times the neighbor of " << toStr(v) << endl;
00115 result = false;
00116 }
00117 ns.insert(n);
00118 }
00119 }
00120 forall const vertex2& v in G.vertices2():
00121 {
00122 std::unordered_multiset<vertex1> ns;
00123 forall const vertex1& n in G.neighbors(v):
00124 {
00125 if(!G.contains(n))
00126 {
00127 if(result) FAILED_FUNCTION(line_nb);
00128 out << endl << " vertex1 " << toStr(n) << " is neighbor of 0x"
00129 << QString::number(v.id(), 16) << " but is not in the graph" << endl;
00130 result = false;
00131 }
00132 if(ns.count(n))
00133 {
00134 if(result) FAILED_FUNCTION(line_nb);
00135 out << endl << " vertex1 " << toStr(n) << " is "
00136 << ns.count(n)+1 << " times the neighbor of " << toStr(v) << endl;
00137 result = false;
00138 }
00139 ns.insert(n);
00140 }
00141 }
00142 return result;
00143 }
00144
00145 template <typename Graph>
00146 bool check_symmetricGraph(const Graph& G, size_t line_nb)
00147 {
00148 bool result = true;
00149 typedef typename Graph::vertex_t vertex;
00150 forall const vertex& v in G:
00151 forall const vertex& n in G.neighbors(v):
00152 {
00153 if(!G.edge(n,v))
00154 {
00155 if(result) FAILED_FUNCTION(line_nb);
00156 out << endl << " vertex " << toStr(v) << " is not a neighbor of 0x"
00157 << QString::number(n.id(), 16) << endl;
00158 result = false;
00159 }
00160 }
00161 return result;
00162 }
00163
00164 template <typename BiGraph>
00165 bool check_symmetricBiGraph(const BiGraph& G, size_t line_nb)
00166 {
00167 bool result = true;
00168 typedef typename BiGraph::vertex1_t vertex1;
00169 typedef typename BiGraph::vertex2_t vertex2;
00170 forall const vertex1& v in G.vertices1():
00171 forall const vertex2& n in G.neighbors(v):
00172 {
00173 if(!G.edge(n,v))
00174 {
00175 if(result) FAILED_FUNCTION(line_nb);
00176 out << endl << " vertex1 " << toStr(v) << " is not a neighbor of vertex2 0x"
00177 << QString::number(n.id(), 16) << endl;
00178 result = false;
00179 }
00180 }
00181 forall const vertex2& v in G.vertices2():
00182 forall const vertex1& n in G.neighbors(v):
00183 {
00184 if(!G.edge(n,v))
00185 {
00186 if(result) FAILED_FUNCTION(line_nb);
00187 out << endl << " vertex2 " << toStr(v) << " is not a neighbor of vertex1 0x"
00188 << QString::number(n.id(), 16) << endl;
00189 result = false;
00190 }
00191 }
00192 return result;
00193 }
00194
00195 };
00196
00197 #endif // TEST_GRAPH_VVH
00198