COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/ugraph_test.cc @ 2086:3fc072264f77

Last change on this file since 2086:3fc072264f77 was 1979:c2992fd74dad, checked in by Balazs Dezso, 18 years ago

Mergeing extendermerge branch
Changes:

the extender system
resize for static size graph
UGraphExtender => UndirectGraphExtender?

UGraphExtenders with changed meaning

Some UGraphExtender /SubUGraphExtenders, DirectUGraphExtender/
GridGraph? => GridUGraph
radix sort to ansi compatible

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