test/undir_graph_test.cc
changeset 1696 4e03a355d2ea
parent 1568 f694f75de683
child 1795 ed3c253b9c29
equal deleted inserted replaced
1:b45fab9e09b8 2:f9cee707bab9
     3 #include <lemon/bits/undir_graph_extender.h>
     3 #include <lemon/bits/undir_graph_extender.h>
     4 #include <lemon/concept/undir_graph.h>
     4 #include <lemon/concept/undir_graph.h>
     5 #include <lemon/list_graph.h>
     5 #include <lemon/list_graph.h>
     6 #include <lemon/smart_graph.h>
     6 #include <lemon/smart_graph.h>
     7 #include <lemon/full_graph.h>
     7 #include <lemon/full_graph.h>
       
     8 #include <lemon/grid_graph.h>
     8 
     9 
     9 #include <lemon/graph_utils.h>
    10 #include <lemon/graph_utils.h>
    10 
    11 
    11 #include "test_tools.h"
    12 #include "test_tools.h"
    12 
    13 
    41   checkConcept<ExtendableUndirGraph, UndirSmartGraph>();
    42   checkConcept<ExtendableUndirGraph, UndirSmartGraph>();
    42 
    43 
    43   checkConcept<UndirGraph, UndirFullGraph>();
    44   checkConcept<UndirGraph, UndirFullGraph>();
    44 
    45 
    45   checkConcept<UndirGraph, UndirGraph>();
    46   checkConcept<UndirGraph, UndirGraph>();
       
    47 
       
    48   checkConcept<UndirGraph, GridGraph>();
    46 }
    49 }
    47 
    50 
    48 template <typename Graph>
    51 template <typename Graph>
    49 void check_item_counts(Graph &g, int n, int e) {
    52 void check_item_counts(Graph &g, int n, int e) {
    50   check(countNodes(g)==n, "Wrong node number.");
    53   int nn = 0;
    51   check(countEdges(g)==2*e, "Wrong edge number.");
    54   for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
       
    55     ++nn;
       
    56   }
       
    57 
       
    58   check(nn == n, "Wrong node number.");
       
    59   check(countNodes(g) == n, "Wrong node number.");
       
    60 
       
    61   int ee = 0;
       
    62   for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
       
    63     ++ee;
       
    64   }
       
    65 
       
    66   check(ee == 2*e, "Wrong edge number.");
       
    67   check(countEdges(g) == 2*e, "Wrong edge number.");
       
    68 
       
    69   int uee = 0;
       
    70   for (typename Graph::UndirEdgeIt it(g); it != INVALID; ++it) {
       
    71     ++uee;
       
    72   }
       
    73 
       
    74   check(uee == e, "Wrong undir edge number.");
       
    75   check(countUndirEdges(g) == e, "Wrong undir edge number.");
    52 }
    76 }
    53 
    77 
    54 template <typename Graph>
    78 template <typename Graph>
    55 void print_items(Graph &g) {
    79 void print_items(Graph &g) {
    56 
    80 
   106     e2 = g.addEdge(n2, n3);
   130     e2 = g.addEdge(n2, n3);
   107 
   131 
   108   // print_items(g);
   132   // print_items(g);
   109 
   133 
   110   check_item_counts(g,3,2);
   134   check_item_counts(g,3,2);
   111 
   135 }
   112 
   136 
       
   137 void checkGridGraph(const GridGraph& g, int w, int h) {
       
   138   check(g.width() == w, "Wrong width");
       
   139   check(g.height() == h, "Wrong height");
       
   140 
       
   141   for (int i = 0; i < w; ++i) {
       
   142     for (int j = 0; j < h; ++j) {
       
   143       check(g.col(g(i, j)) == i, "Wrong col");
       
   144       check(g.row(g(i, j)) == j, "Wrong row");
       
   145     }
       
   146   }
       
   147   
       
   148   for (int i = 0; i < w; ++i) {
       
   149     for (int j = 0; j < h - 1; ++j) {
       
   150       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
       
   151       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
       
   152     }
       
   153     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
       
   154   }
       
   155 
       
   156   for (int i = 0; i < w; ++i) {
       
   157     for (int j = 1; j < h; ++j) {
       
   158       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
       
   159       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
       
   160     }
       
   161     check(g.up(g(i, 0)) == INVALID, "Wrong up");
       
   162   }
       
   163 
       
   164   for (int j = 0; j < h; ++j) {
       
   165     for (int i = 0; i < w - 1; ++i) {
       
   166       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
       
   167       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
       
   168     }
       
   169     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
       
   170   }
       
   171 
       
   172   for (int j = 0; j < h; ++j) {
       
   173     for (int i = 1; i < w; ++i) {
       
   174       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
       
   175       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
       
   176     }
       
   177     check(g.left(g(0, j)) == INVALID, "Wrong left");    
       
   178   }
   113 }
   179 }
   114 
   180 
   115 int main() {
   181 int main() {
   116   check_concepts();
   182   check_concepts();
   117 
   183 
   121   {
   187   {
   122     UndirFullGraph g(5);
   188     UndirFullGraph g(5);
   123     check_item_counts(g, 5, 10);
   189     check_item_counts(g, 5, 10);
   124   }
   190   }
   125 
   191 
       
   192   {
       
   193     GridGraph g(5, 6);
       
   194     check_item_counts(g, 30, 49);
       
   195     checkGridGraph(g, 5, 6);
       
   196   }
       
   197 
       
   198   std::cout << __FILE__ ": All tests passed.\n";
       
   199 
   126   return 0;
   200   return 0;
   127 }
   201 }