src/test/graph_test.h
changeset 938 70e2886211d5
parent 937 d4e911acef3d
child 946 c94ef40a22ce
equal deleted inserted replaced
7:676fe51066f2 8:c0f064ecdebd
    21 
    21 
    22 //! \ingroup misc
    22 //! \ingroup misc
    23 //! \file
    23 //! \file
    24 //! \brief Some utility to  test graph classes.
    24 //! \brief Some utility to  test graph classes.
    25 namespace lemon {
    25 namespace lemon {
    26 
       
    27   struct DummyType {
       
    28     int value;
       
    29     DummyType() {}
       
    30     DummyType(int p) : value(p) {}
       
    31     DummyType& operator=(int p) { value = p; return *this;}
       
    32   };
       
    33 
       
    34 
       
    35   template<class Graph> void checkCompileStaticGraph(Graph &G) 
       
    36     {
       
    37       typedef typename Graph::Node Node;
       
    38       typedef typename Graph::NodeIt NodeIt;
       
    39       typedef typename Graph::Edge Edge;
       
    40       typedef typename Graph::EdgeIt EdgeIt;
       
    41       typedef typename Graph::InEdgeIt InEdgeIt;
       
    42       typedef typename Graph::OutEdgeIt OutEdgeIt;
       
    43   
       
    44       {
       
    45 	Node i; Node j(i); Node k(INVALID);
       
    46 	i=j;
       
    47 	bool b; b=true;
       
    48 	b=(i==INVALID); b=(i!=INVALID);
       
    49 	b=(i==j); b=(i!=j); b=(i<j);
       
    50       }
       
    51       {
       
    52 	NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
       
    53 	i=j;
       
    54 	j=G.first(i);
       
    55 	j=++i;
       
    56 	bool b; b=true;
       
    57 	b=(i==INVALID); b=(i!=INVALID);
       
    58 	Node n(i);
       
    59 	n=i;
       
    60 	b=(i==j); b=(i!=j); b=(i<j);
       
    61 	//Node ->NodeIt conversion
       
    62 	NodeIt ni(G,n);
       
    63       }
       
    64       {
       
    65 	Edge i; Edge j(i); Edge k(INVALID);
       
    66 	i=j;
       
    67 	bool b; b=true;
       
    68 	b=(i==INVALID); b=(i!=INVALID);
       
    69 	b=(i==j); b=(i!=j); b=(i<j);
       
    70       }
       
    71       {
       
    72 	EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
       
    73 	i=j;
       
    74 	j=G.first(i);
       
    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 ->EdgeIt conversion
       
    82 	EdgeIt ei(G,e);
       
    83       }
       
    84       {
       
    85 	Node n;
       
    86 	InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt 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 ->InEdgeIt conversion
       
    96 	InEdgeIt ei(G,e);
       
    97       }
       
    98       {
       
    99 	Node n;
       
   100 	OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
       
   101 	i=j;
       
   102 	j=G.first(i,n);
       
   103 	j=++i;
       
   104 	bool b; b=true;
       
   105 	b=(i==INVALID); b=(i!=INVALID);
       
   106 	Edge e(i);
       
   107 	e=i;
       
   108 	b=(i==j); b=(i!=j); b=(i<j);
       
   109 	//Edge ->OutEdgeIt conversion
       
   110 	OutEdgeIt ei(G,e);
       
   111       }
       
   112       {
       
   113 	Node n,m;
       
   114 	n=m=INVALID;
       
   115 	Edge e;
       
   116 	e=INVALID;
       
   117 	n=G.tail(e);
       
   118 	n=G.head(e);
       
   119       }
       
   120       // id tests
       
   121       { Node n; int i=G.id(n); i=i; }
       
   122       { Edge e; int i=G.id(e); i=i; }
       
   123       //NodeMap tests
       
   124       {
       
   125 	Node k;
       
   126 	typename Graph::template NodeMap<int> m(G);
       
   127 	//Const map
       
   128 	typename Graph::template NodeMap<int> const &cm = m;
       
   129 	//Inicialize with default value
       
   130 	typename Graph::template NodeMap<int> mdef(G,12);
       
   131 	//Copy
       
   132 	typename Graph::template NodeMap<int> mm(cm);
       
   133 	//Copy from another type
       
   134 	typename Graph::template NodeMap<double> dm(cm);
       
   135 	//Copy to more complex type
       
   136 	typename Graph::template NodeMap<DummyType> em(cm);
       
   137 	int v;
       
   138 	v=m[k]; m[k]=v; m.set(k,v);
       
   139 	v=cm[k];
       
   140     
       
   141 	m=cm;  
       
   142 	dm=cm; //Copy from another type  
       
   143 	em=cm; //Copy to more complex type
       
   144 	{
       
   145 	  //Check the typedef's
       
   146 	  typename Graph::template NodeMap<int>::ValueType val;
       
   147 	  val=1;
       
   148 	  typename Graph::template NodeMap<int>::KeyType key;
       
   149 	  key = typename Graph::NodeIt(G);
       
   150 	}
       
   151       }  
       
   152       { //bool NodeMap
       
   153 	Node k;
       
   154 	typename Graph::template NodeMap<bool> m(G);
       
   155 	typename Graph::template NodeMap<bool> const &cm = m;  //Const map
       
   156 	//Inicialize with default value
       
   157 	typename Graph::template NodeMap<bool> mdef(G,12);
       
   158 	typename Graph::template NodeMap<bool> mm(cm);   //Copy
       
   159 	typename Graph::template NodeMap<int> dm(cm); //Copy from another type
       
   160 	bool v;
       
   161 	v=m[k]; m[k]=v; m.set(k,v);
       
   162 	v=cm[k];
       
   163     
       
   164 	m=cm;  
       
   165 	dm=cm; //Copy from another type
       
   166 	m=dm; //Copy to another type
       
   167 
       
   168 	{
       
   169 	  //Check the typedef's
       
   170 	  typename Graph::template NodeMap<bool>::ValueType val;
       
   171 	  val=true;
       
   172 	  typename Graph::template NodeMap<bool>::KeyType key;
       
   173 	  key= typename Graph::NodeIt(G);
       
   174 	}
       
   175       }
       
   176       //EdgeMap tests
       
   177       {
       
   178 	Edge k;
       
   179 	typename Graph::template EdgeMap<int> m(G);
       
   180 	typename Graph::template EdgeMap<int> const &cm = m;  //Const map
       
   181 	//Inicialize with default value
       
   182 	typename Graph::template EdgeMap<int> mdef(G,12);
       
   183 	typename Graph::template EdgeMap<int> mm(cm);   //Copy
       
   184 	typename Graph::template EdgeMap<double> dm(cm); //Copy from another type
       
   185 	int v;
       
   186 	v=m[k]; m[k]=v; m.set(k,v);
       
   187 	v=cm[k];
       
   188     
       
   189 	m=cm;  
       
   190 	dm=cm; //Copy from another type
       
   191 	{
       
   192 	  //Check the typedef's
       
   193 	  typename Graph::template EdgeMap<int>::ValueType val;
       
   194 	  val=1;
       
   195 	  typename Graph::template EdgeMap<int>::KeyType key;
       
   196 	  key= typename Graph::EdgeIt(G);
       
   197 	}
       
   198       }  
       
   199       { //bool EdgeMap
       
   200 	Edge k;
       
   201 	typename Graph::template EdgeMap<bool> m(G);
       
   202 	typename Graph::template EdgeMap<bool> const &cm = m;  //Const map
       
   203 	//Inicialize with default value
       
   204 	typename Graph::template EdgeMap<bool> mdef(G,12);
       
   205 	typename Graph::template EdgeMap<bool> mm(cm);   //Copy
       
   206 	typename Graph::template EdgeMap<int> dm(cm); //Copy from another type
       
   207 	bool v;
       
   208 	v=m[k]; m[k]=v; m.set(k,v);
       
   209 	v=cm[k];
       
   210     
       
   211 	m=cm;  
       
   212 	dm=cm; //Copy from another type
       
   213 	m=dm; //Copy to another type
       
   214 	{
       
   215 	  //Check the typedef's
       
   216 	  typename Graph::template EdgeMap<bool>::ValueType val;
       
   217 	  val=true;
       
   218 	  typename Graph::template EdgeMap<bool>::KeyType key;
       
   219 	  key= typename Graph::EdgeIt(G);
       
   220 	}
       
   221       }
       
   222     }
       
   223 
       
   224   template<class Graph> void checkCompileGraph(Graph &G) 
       
   225     {
       
   226       checkCompileStaticGraph(G);
       
   227 
       
   228       typedef typename Graph::Node Node;
       
   229       typedef typename Graph::NodeIt NodeIt;
       
   230       typedef typename Graph::Edge Edge;
       
   231       typedef typename Graph::EdgeIt EdgeIt;
       
   232       typedef typename Graph::InEdgeIt InEdgeIt;
       
   233       typedef typename Graph::OutEdgeIt OutEdgeIt;
       
   234   
       
   235       Node n,m;
       
   236       n=G.addNode();
       
   237       m=G.addNode();
       
   238       Edge e;
       
   239       e=G.addEdge(n,m); 
       
   240   
       
   241       //  G.clear();
       
   242     }
       
   243 
       
   244   template<class Graph> void checkCompileGraphEraseEdge(Graph &G) 
       
   245     {
       
   246       typename Graph::Edge e;
       
   247       G.erase(e);
       
   248     }
       
   249 
       
   250   template<class Graph> void checkCompileGraphEraseNode(Graph &G) 
       
   251     {
       
   252       typename Graph::Node n;
       
   253       G.erase(n);
       
   254     }
       
   255 
       
   256   template<class Graph> void checkCompileErasableGraph(Graph &G) 
       
   257     {
       
   258       checkCompileGraph(G);
       
   259       checkCompileGraphEraseNode(G);
       
   260       checkCompileGraphEraseEdge(G);
       
   261     }
       
   262 
       
   263   template<class Graph> void checkCompileGraphFindEdge(Graph &G) 
       
   264     {
       
   265       typedef typename Graph::NodeIt Node;
       
   266       typedef typename Graph::NodeIt NodeIt;
       
   267 
       
   268       G.findEdge(NodeIt(G),++NodeIt(G),G.findEdge(NodeIt(G),++NodeIt(G)));
       
   269       G.findEdge(Node(),Node(),G.findEdge(Node(),Node()));  
       
   270     }
       
   271 
    26 
   272   template<class Graph> void checkGraphNodeList(Graph &G, int nn)
    27   template<class Graph> void checkGraphNodeList(Graph &G, int nn)
   273     {
    28     {
   274       typename Graph::NodeIt n(G);
    29       typename Graph::NodeIt n(G);
   275       for(int i=0;i<nn;i++) {
    30       for(int i=0;i<nn;i++) {