COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/ugraph_test.cc @ 2111:ea1fa1bc3f6d

Last change on this file since 2111:ea1fa1bc3f6d was 2111:ea1fa1bc3f6d, checked in by Balazs Dezso, 18 years ago

Removing concepts for extendable and erasable graphs
Renaming StaticGraph? to Graph

File size: 4.7 KB
RevLine 
[1956]1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
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.
12 *
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
15 * purpose.
16 *
17 */
[962]18
[1795]19#include <lemon/bits/graph_extender.h>
[1909]20#include <lemon/concept/ugraph.h>
[962]21#include <lemon/list_graph.h>
22#include <lemon/smart_graph.h>
23#include <lemon/full_graph.h>
[1979]24#include <lemon/grid_ugraph.h>
[962]25
[1053]26#include <lemon/graph_utils.h>
27
[962]28#include "test_tools.h"
29
30
31using namespace lemon;
32using namespace lemon::concept;
33
[1053]34void check_concepts() {
[1909]35  checkConcept<UGraph, ListUGraph>();
[1034]36
[1909]37  checkConcept<UGraph, SmartUGraph>();
[1034]38
[1909]39  checkConcept<UGraph, FullUGraph>();
[1568]40
[1909]41  checkConcept<UGraph, UGraph>();
[1680]42
[1979]43  checkConcept<UGraph, GridUGraph>();
[1053]44}
45
[1054]46template <typename Graph>
[1053]47void check_item_counts(Graph &g, int n, int e) {
[1680]48  int nn = 0;
49  for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
50    ++nn;
51  }
52
53  check(nn == n, "Wrong node number.");
54  check(countNodes(g) == n, "Wrong node number.");
55
56  int ee = 0;
57  for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
58    ++ee;
59  }
60
61  check(ee == 2*e, "Wrong edge number.");
62  check(countEdges(g) == 2*e, "Wrong edge number.");
63
64  int uee = 0;
[1909]65  for (typename Graph::UEdgeIt it(g); it != INVALID; ++it) {
[1680]66    ++uee;
67  }
68
[1909]69  check(uee == e, "Wrong uedge number.");
70  check(countUEdges(g) == e, "Wrong uedge number.");
[1053]71}
72
[1054]73template <typename Graph>
[1053]74void print_items(Graph &g) {
[1054]75
76  typedef typename Graph::NodeIt NodeIt;
[1909]77  typedef typename Graph::UEdgeIt UEdgeIt;
[1054]78  typedef typename Graph::EdgeIt EdgeIt;
79
[1367]80  std::cout << "Nodes" << std::endl;
[1053]81  int i=0;
82  for(NodeIt it(g); it!=INVALID; ++it, ++i) {
[1367]83    std::cout << "  " << i << ": " << g.id(it) << std::endl;
[1053]84  }
85
[1909]86  std::cout << "UEdge" << std::endl;
[1053]87  i=0;
88  for(UEdgeIt it(g); it!=INVALID; ++it, ++i) {
[1367]89    std::cout << "  " << i << ": " << g.id(it)
[1053]90         << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
[1367]91         << ")" << std::endl;
[1053]92  }
93
[1367]94  std::cout << "Edge" << std::endl;
[1053]95  i=0;
96  for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
[1367]97    std::cout << "  " << i << ": " << g.id(it)
[1053]98         << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
[1367]99         << ")" << std::endl;
[1053]100  }
101
102}
103
[1054]104template <typename Graph>
105void check_graph() {
[1053]106
[1054]107  typedef typename Graph::Node Node;
[1909]108  typedef typename Graph::UEdge UEdge;
[1054]109  typedef typename Graph::Edge Edge;
110  typedef typename Graph::NodeIt NodeIt;
[1909]111  typedef typename Graph::UEdgeIt UEdgeIt;
[1054]112  typedef typename Graph::EdgeIt EdgeIt;
[1053]113
114  Graph g;
115
116  check_item_counts(g,0,0);
117
118  Node
119    n1 = g.addNode(),
120    n2 = g.addNode(),
121    n3 = g.addNode();
122
123  UEdge
124    e1 = g.addEdge(n1, n2),
125    e2 = g.addEdge(n2, n3);
126
127  // print_items(g);
128
129  check_item_counts(g,3,2);
[1680]130}
[1030]131
[1979]132void checkGridUGraph(const GridUGraph& g, int w, int h) {
[1680]133  check(g.width() == w, "Wrong width");
134  check(g.height() == h, "Wrong height");
[1054]135
[1680]136  for (int i = 0; i < w; ++i) {
137    for (int j = 0; j < h; ++j) {
138      check(g.col(g(i, j)) == i, "Wrong col");
139      check(g.row(g(i, j)) == j, "Wrong row");
140    }
141  }
142 
143  for (int i = 0; i < w; ++i) {
144    for (int j = 0; j < h - 1; ++j) {
145      check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
146      check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
147    }
148    check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
149  }
150
151  for (int i = 0; i < w; ++i) {
152    for (int j = 1; j < h; ++j) {
153      check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
154      check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
155    }
156    check(g.up(g(i, 0)) == INVALID, "Wrong up");
157  }
158
159  for (int j = 0; j < h; ++j) {
160    for (int i = 0; i < w - 1; ++i) {
161      check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
162      check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");     
163    }
164    check(g.right(g(w - 1, j)) == INVALID, "Wrong right");   
165  }
166
167  for (int j = 0; j < h; ++j) {
168    for (int i = 1; i < w; ++i) {
169      check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
170      check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");     
171    }
172    check(g.left(g(0, j)) == INVALID, "Wrong left");   
173  }
[1054]174}
175
176int main() {
177  check_concepts();
178
[1909]179  check_graph<ListUGraph>();
180  check_graph<SmartUGraph>();
[1054]181
[1568]182  {
[1909]183    FullUGraph g(5);
[1568]184    check_item_counts(g, 5, 10);
185  }
186
[1680]187  {
[1979]188    GridUGraph g(5, 6);
[1680]189    check_item_counts(g, 30, 49);
[1979]190    checkGridUGraph(g, 5, 6);
[1680]191  }
192
193  std::cout << __FILE__ ": All tests passed.\n";
194
[962]195  return 0;
196}
Note: See TracBrowser for help on using the repository browser.