3  * This file is a part of LEMON, a generic C++ optimization library
 
     5  * Copyright (C) 2003-2007
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    19 #include <lemon/bits/graph_extender.h>
 
    20 #include <lemon/concepts/ugraph.h>
 
    21 #include <lemon/list_graph.h>
 
    22 #include <lemon/smart_graph.h>
 
    23 #include <lemon/full_graph.h>
 
    24 #include <lemon/grid_ugraph.h>
 
    26 #include <lemon/graph_utils.h>
 
    28 #include "test_tools.h"
 
    31 using namespace lemon;
 
    32 using namespace lemon::concepts;
 
    34 void check_concepts() {
 
    36   { // checking graph components
 
    37     checkConcept<BaseUGraphComponent, BaseUGraphComponent >();
 
    39     checkConcept<IDableUGraphComponent<>, 
 
    40       IDableUGraphComponent<> >();
 
    42     checkConcept<IterableUGraphComponent<>, 
 
    43       IterableUGraphComponent<> >();
 
    45     checkConcept<MappableUGraphComponent<>, 
 
    46       MappableUGraphComponent<> >();
 
    50     checkConcept<UGraph, ListUGraph>();
 
    52     checkConcept<UGraph, SmartUGraph>();
 
    54     checkConcept<UGraph, FullUGraph>();
 
    56     checkConcept<UGraph, UGraph>();
 
    58     checkConcept<UGraph, GridUGraph>();
 
    62 template <typename Graph>
 
    63 void check_item_counts(Graph &g, int n, int e) {
 
    65   for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
 
    69   check(nn == n, "Wrong node number.");
 
    70   check(countNodes(g) == n, "Wrong node number.");
 
    73   for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
 
    77   check(ee == 2*e, "Wrong edge number.");
 
    78   check(countEdges(g) == 2*e, "Wrong edge number.");
 
    81   for (typename Graph::UEdgeIt it(g); it != INVALID; ++it) {
 
    85   check(uee == e, "Wrong uedge number.");
 
    86   check(countUEdges(g) == e, "Wrong uedge number.");
 
    89 template <typename Graph>
 
    90 void print_items(Graph &g) {
 
    92   typedef typename Graph::NodeIt NodeIt;
 
    93   typedef typename Graph::UEdgeIt UEdgeIt;
 
    94   typedef typename Graph::EdgeIt EdgeIt;
 
    96   std::cout << "Nodes" << std::endl;
 
    98   for(NodeIt it(g); it!=INVALID; ++it, ++i) {
 
    99     std::cout << "  " << i << ": " << g.id(it) << std::endl;
 
   102   std::cout << "UEdge" << std::endl;
 
   104   for(UEdgeIt it(g); it!=INVALID; ++it, ++i) {
 
   105     std::cout << "  " << i << ": " << g.id(it) 
 
   106 	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
 
   110   std::cout << "Edge" << std::endl;
 
   112   for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
 
   113     std::cout << "  " << i << ": " << g.id(it)
 
   114 	 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it)) 
 
   120 template <typename Graph>
 
   123   typedef typename Graph::Node Node;
 
   124   typedef typename Graph::UEdge UEdge;
 
   125   typedef typename Graph::Edge Edge;
 
   126   typedef typename Graph::NodeIt NodeIt;
 
   127   typedef typename Graph::UEdgeIt UEdgeIt;
 
   128   typedef typename Graph::EdgeIt EdgeIt;
 
   132   check_item_counts(g,0,0);
 
   140     e1 = g.addEdge(n1, n2),
 
   141     e2 = g.addEdge(n2, n3);
 
   145   check_item_counts(g,3,2);
 
   148 void checkGridUGraph(const GridUGraph& g, int w, int h) {
 
   149   check(g.width() == w, "Wrong width");
 
   150   check(g.height() == h, "Wrong height");
 
   152   for (int i = 0; i < w; ++i) {
 
   153     for (int j = 0; j < h; ++j) {
 
   154       check(g.col(g(i, j)) == i, "Wrong col");
 
   155       check(g.row(g(i, j)) == j, "Wrong row");
 
   159   for (int i = 0; i < w; ++i) {
 
   160     for (int j = 0; j < h - 1; ++j) {
 
   161       check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
 
   162       check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
 
   164     check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
 
   167   for (int i = 0; i < w; ++i) {
 
   168     for (int j = 1; j < h; ++j) {
 
   169       check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
 
   170       check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
 
   172     check(g.up(g(i, 0)) == INVALID, "Wrong up");
 
   175   for (int j = 0; j < h; ++j) {
 
   176     for (int i = 0; i < w - 1; ++i) {
 
   177       check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
 
   178       check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");      
 
   180     check(g.right(g(w - 1, j)) == INVALID, "Wrong right");    
 
   183   for (int j = 0; j < h; ++j) {
 
   184     for (int i = 1; i < w; ++i) {
 
   185       check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
 
   186       check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");      
 
   188     check(g.left(g(0, j)) == INVALID, "Wrong left");    
 
   195   check_graph<ListUGraph>();
 
   196   check_graph<SmartUGraph>();
 
   200     check_item_counts(g, 5, 10);
 
   205     check_item_counts(g, 30, 49);
 
   206     checkGridUGraph(g, 5, 6);
 
   209   std::cout << __FILE__ ": All tests passed.\n";