test/graph_test.cc
changeset 165 b4c336c27a03
parent 109 abddaa08b507
child 171 02f4d5d9bfd7
equal deleted inserted replaced
2:d947fc18f3ba 3:e96a06cbcd4f
    20 #include <lemon/list_graph.h>
    20 #include <lemon/list_graph.h>
    21 #include <lemon/smart_graph.h>
    21 #include <lemon/smart_graph.h>
    22 // #include <lemon/full_graph.h>
    22 // #include <lemon/full_graph.h>
    23 // #include <lemon/grid_graph.h>
    23 // #include <lemon/grid_graph.h>
    24 
    24 
    25 //#include <lemon/graph_utils.h>
    25 #include <lemon/graph_utils.h>
    26 
    26 
    27 #include "test_tools.h"
    27 #include "test_tools.h"
    28 
    28 
    29 
    29 
    30 using namespace lemon;
    30 using namespace lemon;
    80   check(uee == e, "Wrong edge number.");
    80   check(uee == e, "Wrong edge number.");
    81   //  check(countEdges(g) == e, "Wrong edge number.");
    81   //  check(countEdges(g) == e, "Wrong edge number.");
    82 }
    82 }
    83 
    83 
    84 template <typename Graph>
    84 template <typename Graph>
    85 void print_items(Graph &g) {
    85 void check_graph_counts() {
    86 
    86 
    87   typedef typename Graph::NodeIt NodeIt;
    87   TEMPLATE_GRAPH_TYPEDEFS(Graph);
    88   typedef typename Graph::EdgeIt EdgeIt;
       
    89   typedef typename Graph::ArcIt ArcIt;
       
    90 
       
    91   std::cout << "Nodes" << std::endl;
       
    92   int i=0;
       
    93   for(NodeIt it(g); it!=INVALID; ++it, ++i) {
       
    94     std::cout << "  " << i << ": " << g.id(it) << std::endl;
       
    95   }
       
    96 
       
    97   std::cout << "Edge" << std::endl;
       
    98   i=0;
       
    99   for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
       
   100     std::cout << "  " << i << ": " << g.id(it) 
       
   101 	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
       
   102 	 << ")" << std::endl;
       
   103   }
       
   104 
       
   105   std::cout << "Arc" << std::endl;
       
   106   i=0;
       
   107   for(ArcIt it(g); it!=INVALID; ++it, ++i) {
       
   108     std::cout << "  " << i << ": " << g.id(it)
       
   109 	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
       
   110 	 << ")" << std::endl;
       
   111   }
       
   112 
       
   113 }
       
   114 
       
   115 template <typename Graph>
       
   116 void check_graph() {
       
   117 
       
   118   typedef typename Graph::Node Node;
       
   119   typedef typename Graph::Edge Edge;
       
   120   typedef typename Graph::Arc Arc;
       
   121   typedef typename Graph::NodeIt NodeIt;
       
   122   typedef typename Graph::EdgeIt EdgeIt;
       
   123   typedef typename Graph::ArcIt ArcIt;
       
   124 
       
   125   Graph g;
    88   Graph g;
   126 
    89 
   127   check_item_counts(g,0,0);
    90   check_item_counts(g,0,0);
   128 
    91 
   129   Node
    92   Node
   133 
    96 
   134   Edge
    97   Edge
   135     e1 = g.addEdge(n1, n2),
    98     e1 = g.addEdge(n1, n2),
   136     e2 = g.addEdge(n2, n3);
    99     e2 = g.addEdge(n2, n3);
   137 
   100 
   138   // print_items(g);
       
   139 
       
   140   check_item_counts(g,3,2);
   101   check_item_counts(g,3,2);
   141 }
   102 }
       
   103 
       
   104 template <typename Graph>
       
   105 void check_graph_validity() {
       
   106 
       
   107   TEMPLATE_GRAPH_TYPEDEFS(Graph);
       
   108   Graph g;
       
   109 
       
   110   check_item_counts(g,0,0);
       
   111 
       
   112   Node
       
   113     n1 = g.addNode(),
       
   114     n2 = g.addNode(),
       
   115     n3 = g.addNode();
       
   116 
       
   117   Edge
       
   118     e1 = g.addEdge(n1, n2),
       
   119     e2 = g.addEdge(n2, n3);
       
   120 
       
   121   check(g.valid(n1), "Validity check");
       
   122   check(g.valid(e1), "Validity check");
       
   123   check(g.valid(g.direct(e1, true)), "Validity check");
       
   124 
       
   125   check(!g.valid(g.nodeFromId(-1)), "Validity check");
       
   126   check(!g.valid(g.edgeFromId(-1)), "Validity check");
       
   127   check(!g.valid(g.arcFromId(-1)), "Validity check");
       
   128     
       
   129 }
       
   130 
       
   131 template <typename Graph>
       
   132 void check_graph_validity_erase() {
       
   133 
       
   134   TEMPLATE_GRAPH_TYPEDEFS(Graph);
       
   135   Graph g;
       
   136 
       
   137   check_item_counts(g,0,0);
       
   138 
       
   139   Node
       
   140     n1 = g.addNode(),
       
   141     n2 = g.addNode(),
       
   142     n3 = g.addNode();
       
   143 
       
   144   Edge
       
   145     e1 = g.addEdge(n1, n2),
       
   146     e2 = g.addEdge(n2, n3);
       
   147 
       
   148   check(g.valid(n1), "Validity check");
       
   149   check(g.valid(e1), "Validity check");
       
   150   check(g.valid(g.direct(e1, true)), "Validity check");
       
   151 
       
   152   g.erase(n1);
       
   153 
       
   154   check(!g.valid(n1), "Validity check");
       
   155   check(g.valid(n2), "Validity check");
       
   156   check(g.valid(n3), "Validity check");
       
   157   check(!g.valid(e1), "Validity check");
       
   158   check(g.valid(e2), "Validity check");
       
   159 
       
   160   check(!g.valid(g.nodeFromId(-1)), "Validity check");
       
   161   check(!g.valid(g.edgeFromId(-1)), "Validity check");
       
   162   check(!g.valid(g.arcFromId(-1)), "Validity check");
       
   163     
       
   164 }
       
   165 
       
   166 
   142 
   167 
   143 // void checkGridGraph(const GridGraph& g, int w, int h) {
   168 // void checkGridGraph(const GridGraph& g, int w, int h) {
   144 //   check(g.width() == w, "Wrong width");
   169 //   check(g.width() == w, "Wrong width");
   145 //   check(g.height() == h, "Wrong height");
   170 //   check(g.height() == h, "Wrong height");
   146 
   171 
   185 // }
   210 // }
   186 
   211 
   187 int main() {
   212 int main() {
   188   check_concepts();
   213   check_concepts();
   189 
   214 
   190   check_graph<ListGraph>();
   215   check_graph_counts<ListGraph>();
   191   check_graph<SmartGraph>();
   216   check_graph_counts<SmartGraph>();
       
   217 
       
   218   check_graph_validity_erase<ListGraph>();
       
   219   check_graph_validity<SmartGraph>();
   192 
   220 
   193 //   {
   221 //   {
   194 //     FullGraph g(5);
   222 //     FullGraph g(5);
   195 //     check_item_counts(g, 5, 10);
   223 //     check_item_counts(g, 5, 10);
   196 //   }
   224 //   }