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