src/test/graph_test.h
author marci
Tue, 21 Sep 2004 11:54:20 +0000
changeset 892 004636791dd7
parent 856 e9d73b8e3ab6
child 906 17f31d280385
permissions -rw-r--r--
graph_wrappers now pass the tests.
     1 // -*- c++ -*-
     2 #ifndef HUGO_TEST_GRAPH_TEST_H
     3 #define HUGO_TEST_GRAPH_TEST_H
     4 
     5 
     6 #include "test_tools.h"
     7 
     8 //! \ingroup misc
     9 //! \file
    10 //! \brief Some utility to  test graph classes.
    11 namespace hugo {
    12 
    13   struct DummyType {
    14     int value;
    15     DummyType() {}
    16     DummyType(int p) : value(p) {}
    17     DummyType& operator=(int p) { value = p; return *this;}
    18   };
    19 
    20 
    21   template<class Graph> void checkCompileStaticGraph(Graph &G) 
    22     {
    23       typedef typename Graph::Node Node;
    24       typedef typename Graph::NodeIt NodeIt;
    25       typedef typename Graph::Edge Edge;
    26       typedef typename Graph::EdgeIt EdgeIt;
    27       typedef typename Graph::InEdgeIt InEdgeIt;
    28       typedef typename Graph::OutEdgeIt OutEdgeIt;
    29   
    30       {
    31 	Node i; Node j(i); Node k(INVALID);
    32 	i=j;
    33 	bool b; b=true;
    34 	b=(i==INVALID); b=(i!=INVALID);
    35 	b=(i==j); b=(i!=j); b=(i<j);
    36       }
    37       {
    38 	NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
    39 	i=j;
    40 	j=G.first(i);
    41 	j=++i;
    42 	bool b; b=true;
    43 	b=(i==INVALID); b=(i!=INVALID);
    44 	Node n(i);
    45 	n=i;
    46 	b=(i==j); b=(i!=j); b=(i<j);
    47 	//Node ->NodeIt conversion
    48 	NodeIt ni(G,n);
    49       }
    50       {
    51 	Edge i; Edge j(i); Edge k(INVALID);
    52 	i=j;
    53 	bool b; b=true;
    54 	b=(i==INVALID); b=(i!=INVALID);
    55 	b=(i==j); b=(i!=j); b=(i<j);
    56       }
    57       {
    58 	EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
    59 	i=j;
    60 	j=G.first(i);
    61 	j=++i;
    62 	bool b; b=true;
    63 	b=(i==INVALID); b=(i!=INVALID);
    64 	Edge e(i);
    65 	e=i;
    66 	b=(i==j); b=(i!=j); b=(i<j);
    67 	//Edge ->EdgeIt conversion
    68 	EdgeIt ei(G,e);
    69       }
    70       {
    71 	Node n;
    72 	InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n);
    73 	i=j;
    74 	j=G.first(i,n);
    75 	j=++i;
    76 	bool b; b=true;
    77 	b=(i==INVALID); b=(i!=INVALID);
    78 	Edge e(i);
    79 	e=i;
    80 	b=(i==j); b=(i!=j); b=(i<j);
    81 	//Edge ->InEdgeIt conversion
    82 	InEdgeIt ei(G,e);
    83       }
    84       {
    85 	Node n;
    86 	OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
    87 	i=j;
    88 	j=G.first(i,n);
    89 	j=++i;
    90 	bool b; b=true;
    91 	b=(i==INVALID); b=(i!=INVALID);
    92 	Edge e(i);
    93 	e=i;
    94 	b=(i==j); b=(i!=j); b=(i<j);
    95 	//Edge ->OutEdgeIt conversion
    96 	OutEdgeIt ei(G,e);
    97       }
    98       {
    99 	Node n,m;
   100 	n=m=INVALID;
   101 	Edge e;
   102 	e=INVALID;
   103 	n=G.tail(e);
   104 	n=G.head(e);
   105       }
   106       // id tests
   107       { Node n; int i=G.id(n); i=i; }
   108       { Edge e; int i=G.id(e); i=i; }
   109       //NodeMap tests
   110       {
   111 	Node k;
   112 	typename Graph::template NodeMap<int> m(G);
   113 	//Const map
   114 	typename Graph::template NodeMap<int> const &cm = m;
   115 	//Inicialize with default value
   116 	typename Graph::template NodeMap<int> mdef(G,12);
   117 	//Copy
   118 	typename Graph::template NodeMap<int> mm(cm);
   119 	//Copy from another type
   120 	typename Graph::template NodeMap<double> dm(cm);
   121 	//Copy to more complex type
   122 	typename Graph::template NodeMap<DummyType> em(cm);
   123 	int v;
   124 	v=m[k]; m[k]=v; m.set(k,v);
   125 	v=cm[k];
   126     
   127 	m=cm;  
   128 	dm=cm; //Copy from another type  
   129 	em=cm; //Copy to more complex type
   130 	{
   131 	  //Check the typedef's
   132 	  typename Graph::template NodeMap<int>::ValueType val;
   133 	  val=1;
   134 	  typename Graph::template NodeMap<int>::KeyType key;
   135 	  key = typename Graph::NodeIt(G);
   136 	}
   137       }  
   138       { //bool NodeMap
   139 	Node k;
   140 	typename Graph::template NodeMap<bool> m(G);
   141 	typename Graph::template NodeMap<bool> const &cm = m;  //Const map
   142 	//Inicialize with default value
   143 	typename Graph::template NodeMap<bool> mdef(G,12);
   144 	typename Graph::template NodeMap<bool> mm(cm);   //Copy
   145 	typename Graph::template NodeMap<int> dm(cm); //Copy from another type
   146 	bool v;
   147 	v=m[k]; m[k]=v; m.set(k,v);
   148 	v=cm[k];
   149     
   150 	m=cm;  
   151 	dm=cm; //Copy from another type
   152 	m=dm; //Copy to another type
   153 
   154 	{
   155 	  //Check the typedef's
   156 	  typename Graph::template NodeMap<bool>::ValueType val;
   157 	  val=true;
   158 	  typename Graph::template NodeMap<bool>::KeyType key;
   159 	  key= typename Graph::NodeIt(G);
   160 	}
   161       }
   162       //EdgeMap tests
   163       {
   164 	Edge k;
   165 	typename Graph::template EdgeMap<int> m(G);
   166 	typename Graph::template EdgeMap<int> const &cm = m;  //Const map
   167 	//Inicialize with default value
   168 	typename Graph::template EdgeMap<int> mdef(G,12);
   169 	typename Graph::template EdgeMap<int> mm(cm);   //Copy
   170 	typename Graph::template EdgeMap<double> dm(cm); //Copy from another type
   171 	int v;
   172 	v=m[k]; m[k]=v; m.set(k,v);
   173 	v=cm[k];
   174     
   175 	m=cm;  
   176 	dm=cm; //Copy from another type
   177 	{
   178 	  //Check the typedef's
   179 	  typename Graph::template EdgeMap<int>::ValueType val;
   180 	  val=1;
   181 	  typename Graph::template EdgeMap<int>::KeyType key;
   182 	  key= typename Graph::EdgeIt(G);
   183 	}
   184       }  
   185       { //bool EdgeMap
   186 	Edge k;
   187 	typename Graph::template EdgeMap<bool> m(G);
   188 	typename Graph::template EdgeMap<bool> const &cm = m;  //Const map
   189 	//Inicialize with default value
   190 	typename Graph::template EdgeMap<bool> mdef(G,12);
   191 	typename Graph::template EdgeMap<bool> mm(cm);   //Copy
   192 	typename Graph::template EdgeMap<int> dm(cm); //Copy from another type
   193 	bool v;
   194 	v=m[k]; m[k]=v; m.set(k,v);
   195 	v=cm[k];
   196     
   197 	m=cm;  
   198 	dm=cm; //Copy from another type
   199 	m=dm; //Copy to another type
   200 	{
   201 	  //Check the typedef's
   202 	  typename Graph::template EdgeMap<bool>::ValueType val;
   203 	  val=true;
   204 	  typename Graph::template EdgeMap<bool>::KeyType key;
   205 	  key= typename Graph::EdgeIt(G);
   206 	}
   207       }
   208     }
   209 
   210   template<class Graph> void checkCompileGraph(Graph &G) 
   211     {
   212       checkCompileStaticGraph(G);
   213 
   214       typedef typename Graph::Node Node;
   215       typedef typename Graph::NodeIt NodeIt;
   216       typedef typename Graph::Edge Edge;
   217       typedef typename Graph::EdgeIt EdgeIt;
   218       typedef typename Graph::InEdgeIt InEdgeIt;
   219       typedef typename Graph::OutEdgeIt OutEdgeIt;
   220   
   221       Node n,m;
   222       n=G.addNode();
   223       m=G.addNode();
   224       Edge e;
   225       e=G.addEdge(n,m); 
   226   
   227       //  G.clear();
   228     }
   229 
   230   template<class Graph> void checkCompileGraphEraseEdge(Graph &G) 
   231     {
   232       typename Graph::Edge e;
   233       G.erase(e);
   234     }
   235 
   236   template<class Graph> void checkCompileGraphEraseNode(Graph &G) 
   237     {
   238       typename Graph::Node n;
   239       G.erase(n);
   240     }
   241 
   242   template<class Graph> void checkCompileErasableGraph(Graph &G) 
   243     {
   244       checkCompileGraph(G);
   245       checkCompileGraphEraseNode(G);
   246       checkCompileGraphEraseEdge(G);
   247     }
   248 
   249   template<class Graph> void checkCompileGraphFindEdge(Graph &G) 
   250     {
   251       typedef typename Graph::NodeIt Node;
   252       typedef typename Graph::NodeIt NodeIt;
   253 
   254       G.findEdge(NodeIt(G),++NodeIt(G),G.findEdge(NodeIt(G),++NodeIt(G)));
   255       G.findEdge(Node(),Node(),G.findEdge(Node(),Node()));  
   256     }
   257 
   258   template<class Graph> void checkGraphNodeList(Graph &G, int nn)
   259     {
   260       typename Graph::NodeIt n(G);
   261       for(int i=0;i<nn;i++) {
   262 	check(n!=INVALID,"Wrong Node list linking.");
   263 	++n;
   264       }
   265       check(n==INVALID,"Wrong Node list linking.");
   266     }
   267 
   268   template<class Graph> void checkGraphEdgeList(Graph &G, int nn)
   269     {
   270       typedef typename Graph::EdgeIt EdgeIt;
   271 
   272       EdgeIt e(G);
   273       for(int i=0;i<nn;i++) {
   274 	check(e!=INVALID,"Wrong Edge list linking.");
   275 	++e;
   276       }
   277       check(e==INVALID,"Wrong Edge list linking.");
   278     }
   279 
   280   template<class Graph> void checkGraphOutEdgeList(Graph &G,
   281 						   typename Graph::Node n,
   282 						   int nn)
   283     {
   284       typename Graph::OutEdgeIt e(G,n);
   285       for(int i=0;i<nn;i++) {
   286 	check(e!=INVALID,"Wrong OutEdge list linking.");
   287 	++e;
   288       }
   289       check(e==INVALID,"Wrong OutEdge list linking.");
   290     }
   291 
   292   template<class Graph> void checkGraphInEdgeList(Graph &G,
   293 						  typename Graph::Node n,
   294 						  int nn)
   295     {
   296       typename Graph::InEdgeIt e(G,n);
   297       for(int i=0;i<nn;i++) {
   298 	check(e!=INVALID,"Wrong InEdge list linking.");
   299 	++e;
   300       }
   301       check(e==INVALID,"Wrong InEdge list linking.");
   302     }
   303 
   304   ///\file
   305   ///\todo Check head(), tail() as well;
   306 
   307   
   308 } //namespace hugo
   309 
   310 
   311 #endif