COIN-OR::LEMON - Graph Library

source: lemon/test/graph_test.h @ 1193:c8fa41fcc4a7

Last change on this file since 1193:c8fa41fcc4a7 was 1193:c8fa41fcc4a7, checked in by Balazs Dezso <deba@…>, 9 years ago

Type safe red and blue node set (#69)

File size: 13.3 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2009
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 */
18
19#ifndef LEMON_TEST_GRAPH_TEST_H
20#define LEMON_TEST_GRAPH_TEST_H
21
22#include <set>
23
24#include <lemon/core.h>
25#include <lemon/maps.h>
26
27#include "test_tools.h"
28
29namespace lemon {
30
31  template<class Graph>
32  void checkGraphNodeList(const Graph &G, int cnt)
33  {
34    typename Graph::NodeIt n(G);
35    for(int i=0;i<cnt;i++) {
36      check(n!=INVALID,"Wrong Node list linking.");
37      ++n;
38    }
39    check(n==INVALID,"Wrong Node list linking.");
40    check(countNodes(G)==cnt,"Wrong Node number.");
41  }
42
43  template<class Graph>
44  void checkGraphRedNodeList(const Graph &G, int cnt)
45  {
46    typename Graph::RedIt n(G);
47    for(int i=0;i<cnt;i++) {
48      check(n!=INVALID,"Wrong red Node list linking.");
49      check(G.red(n),"Wrong node set check.");
50      check(!G.blue(n),"Wrong node set check.");
51      typename Graph::Node nn = n;
52      check(G.asRedNodeUnsafe(nn) == n,"Wrong node conversion.");
53      check(G.asRedNode(nn) == n,"Wrong node conversion.");
54      check(G.asBlueNode(nn) == INVALID,"Wrong node conversion.");
55      std::pair<typename Graph::RedNode, typename Graph::BlueNode> rbn =
56        G.asRedBlueNode(nn);
57      check(rbn.first == n,"Wrong node conversion.");
58      check(rbn.second == INVALID,"Wrong node conversion.");
59      ++n;
60    }
61    check(n==INVALID,"Wrong red Node list linking.");
62    check(countRedNodes(G)==cnt,"Wrong red Node number.");
63  }
64
65  template<class Graph>
66  void checkGraphBlueNodeList(const Graph &G, int cnt)
67  {
68    typename Graph::BlueIt n(G);
69    for(int i=0;i<cnt;i++) {
70      check(n!=INVALID,"Wrong blue Node list linking.");
71      check(G.blue(n),"Wrong node set check.");
72      check(!G.red(n),"Wrong node set check.");
73      typename Graph::Node nn = n;
74      check(G.asBlueNodeUnsafe(nn) == n,"Wrong node conversion.");
75      check(G.asBlueNode(nn) == n,"Wrong node conversion.");
76      check(G.asRedNode(nn) == INVALID,"Wrong node conversion.");
77      std::pair<typename Graph::RedNode, typename Graph::BlueNode> rbn =
78        G.asRedBlueNode(nn);
79      check(rbn.first == INVALID,"Wrong node conversion.");
80      check(rbn.second == n,"Wrong node conversion.");
81      ++n;
82    }
83    check(n==INVALID,"Wrong blue Node list linking.");
84    check(countBlueNodes(G)==cnt,"Wrong blue Node number.");
85  }
86
87  template<class Graph>
88  void checkGraphArcList(const Graph &G, int cnt)
89  {
90    typename Graph::ArcIt e(G);
91    for(int i=0;i<cnt;i++) {
92      check(e!=INVALID,"Wrong Arc list linking.");
93      check(G.oppositeNode(G.source(e), e) == G.target(e),
94            "Wrong opposite node");
95      check(G.oppositeNode(G.target(e), e) == G.source(e),
96            "Wrong opposite node");
97      ++e;
98    }
99    check(e==INVALID,"Wrong Arc list linking.");
100    check(countArcs(G)==cnt,"Wrong Arc number.");
101  }
102
103  template<class Graph>
104  void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt)
105  {
106    typename Graph::OutArcIt e(G,n);
107    for(int i=0;i<cnt;i++) {
108      check(e!=INVALID,"Wrong OutArc list linking.");
109      check(n==G.source(e),"Wrong OutArc list linking.");
110      check(n==G.baseNode(e),"Wrong OutArc list linking.");
111      check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking.");
112      ++e;
113    }
114    check(e==INVALID,"Wrong OutArc list linking.");
115    check(countOutArcs(G,n)==cnt,"Wrong OutArc number.");
116  }
117
118  template<class Graph>
119  void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt)
120  {
121    typename Graph::InArcIt e(G,n);
122    for(int i=0;i<cnt;i++) {
123      check(e!=INVALID,"Wrong InArc list linking.");
124      check(n==G.target(e),"Wrong InArc list linking.");
125      check(n==G.baseNode(e),"Wrong OutArc list linking.");
126      check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking.");
127      ++e;
128    }
129    check(e==INVALID,"Wrong InArc list linking.");
130    check(countInArcs(G,n)==cnt,"Wrong InArc number.");
131  }
132
133  template<class Graph>
134  void checkGraphEdgeList(const Graph &G, int cnt)
135  {
136    typename Graph::EdgeIt e(G);
137    for(int i=0;i<cnt;i++) {
138      check(e!=INVALID,"Wrong Edge list linking.");
139      check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node");
140      check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node");
141      ++e;
142    }
143    check(e==INVALID,"Wrong Edge list linking.");
144    check(countEdges(G)==cnt,"Wrong Edge number.");
145  }
146
147  template<class Graph>
148  void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt)
149  {
150    typename Graph::IncEdgeIt e(G,n);
151    for(int i=0;i<cnt;i++) {
152      check(e!=INVALID,"Wrong IncEdge list linking.");
153      check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking.");
154      check(n==G.baseNode(e),"Wrong OutArc list linking.");
155      check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e),
156            "Wrong OutArc list linking.");
157      ++e;
158    }
159    check(e==INVALID,"Wrong IncEdge list linking.");
160    check(countIncEdges(G,n)==cnt,"Wrong IncEdge number.");
161  }
162
163  template <class Graph>
164  void checkGraphIncEdgeArcLists(const Graph &G, typename Graph::Node n,
165                                 int cnt)
166  {
167    checkGraphIncEdgeList(G, n, cnt);
168    checkGraphOutArcList(G, n, cnt);
169    checkGraphInArcList(G, n, cnt);
170  }
171
172  template <class Graph>
173  void checkGraphConArcList(const Graph &G, int cnt) {
174    int i = 0;
175    for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
176      for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
177        for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
178          check(G.source(a) == u, "Wrong iterator.");
179          check(G.target(a) == v, "Wrong iterator.");
180          ++i;
181        }
182      }
183    }
184    check(cnt == i, "Wrong iterator.");
185  }
186
187  template <class Graph>
188  void checkGraphConEdgeList(const Graph &G, int cnt) {
189    int i = 0;
190    for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
191      for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
192        for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
193          check((G.u(e) == u && G.v(e) == v) ||
194                (G.u(e) == v && G.v(e) == u), "Wrong iterator.");
195          i += u == v ? 2 : 1;
196        }
197      }
198    }
199    check(2 * cnt == i, "Wrong iterator.");
200  }
201
202  template <typename Graph>
203  void checkArcDirections(const Graph& G) {
204    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
205      check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction");
206      check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction");
207      check(G.direct(a, G.direction(a)) == a, "Wrong direction");
208    }
209  }
210
211  template <typename Graph>
212  void checkNodeIds(const Graph& G) {
213    typedef typename Graph::Node Node;
214    std::set<int> values;
215    for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
216      check(G.nodeFromId(G.id(n)) == n, "Wrong id");
217      check(values.find(G.id(n)) == values.end(), "Wrong id");
218      check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
219      values.insert(G.id(n));
220    }
221    check(G.maxId(Node()) <= G.maxNodeId(), "Wrong maximum id");
222  }
223
224  template <typename Graph>
225  void checkRedNodeIds(const Graph& G) {
226    typedef typename Graph::RedNode RedNode;
227    std::set<int> values;
228    for (typename Graph::RedIt n(G); n != INVALID; ++n) {
229      check(G.red(n), "Wrong partition");
230      check(values.find(G.id(n)) == values.end(), "Wrong id");
231      check(G.id(n) <= G.maxRedId(), "Wrong maximum id");
232      values.insert(G.id(n));
233    }
234    check(G.maxId(RedNode()) == G.maxRedId(), "Wrong maximum id");
235  }
236
237  template <typename Graph>
238  void checkBlueNodeIds(const Graph& G) {
239    typedef typename Graph::BlueNode BlueNode;
240    std::set<int> values;
241    for (typename Graph::BlueIt n(G); n != INVALID; ++n) {
242      check(G.blue(n), "Wrong partition");
243      check(values.find(G.id(n)) == values.end(), "Wrong id");
244      check(G.id(n) <= G.maxBlueId(), "Wrong maximum id");
245      values.insert(G.id(n));
246    }
247    check(G.maxId(BlueNode()) == G.maxBlueId(), "Wrong maximum id");
248  }
249
250  template <typename Graph>
251  void checkArcIds(const Graph& G) {
252    typedef typename Graph::Arc Arc;
253    std::set<int> values;
254    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
255      check(G.arcFromId(G.id(a)) == a, "Wrong id");
256      check(values.find(G.id(a)) == values.end(), "Wrong id");
257      check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
258      values.insert(G.id(a));
259    }
260    check(G.maxId(Arc()) <= G.maxArcId(), "Wrong maximum id");
261  }
262
263  template <typename Graph>
264  void checkEdgeIds(const Graph& G) {
265    typedef typename Graph::Edge Edge;
266    std::set<int> values;
267    for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
268      check(G.edgeFromId(G.id(e)) == e, "Wrong id");
269      check(values.find(G.id(e)) == values.end(), "Wrong id");
270      check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
271      values.insert(G.id(e));
272    }
273    check(G.maxId(Edge()) <= G.maxEdgeId(), "Wrong maximum id");
274  }
275
276  template <typename Graph>
277  void checkGraphNodeMap(const Graph& G) {
278    typedef typename Graph::Node Node;
279    typedef typename Graph::NodeIt NodeIt;
280
281    typedef typename Graph::template NodeMap<int> IntNodeMap;
282    IntNodeMap map(G, 42);
283    for (NodeIt it(G); it != INVALID; ++it) {
284      check(map[it] == 42, "Wrong map constructor.");
285    }
286    int s = 0;
287    for (NodeIt it(G); it != INVALID; ++it) {
288      map[it] = 0;
289      check(map[it] == 0, "Wrong operator[].");
290      map.set(it, s);
291      check(map[it] == s, "Wrong set.");
292      ++s;
293    }
294    s = s * (s - 1) / 2;
295    for (NodeIt it(G); it != INVALID; ++it) {
296      s -= map[it];
297    }
298    check(s == 0, "Wrong sum.");
299
300    // map = constMap<Node>(12);
301    // for (NodeIt it(G); it != INVALID; ++it) {
302    //   check(map[it] == 12, "Wrong operator[].");
303    // }
304  }
305
306  template <typename Graph>
307  void checkGraphRedMap(const Graph& G) {
308    typedef typename Graph::Node Node;
309    typedef typename Graph::RedIt RedIt;
310
311    typedef typename Graph::template RedMap<int> IntRedMap;
312    IntRedMap map(G, 42);
313    for (RedIt it(G); it != INVALID; ++it) {
314      check(map[it] == 42, "Wrong map constructor.");
315    }
316    int s = 0;
317    for (RedIt it(G); it != INVALID; ++it) {
318      map[it] = 0;
319      check(map[it] == 0, "Wrong operator[].");
320      map.set(it, s);
321      check(map[it] == s, "Wrong set.");
322      ++s;
323    }
324    s = s * (s - 1) / 2;
325    for (RedIt it(G); it != INVALID; ++it) {
326      s -= map[it];
327    }
328    check(s == 0, "Wrong sum.");
329
330    // map = constMap<Node>(12);
331    // for (NodeIt it(G); it != INVALID; ++it) {
332    //   check(map[it] == 12, "Wrong operator[].");
333    // }
334  }
335
336  template <typename Graph>
337  void checkGraphBlueMap(const Graph& G) {
338    typedef typename Graph::Node Node;
339    typedef typename Graph::BlueIt BlueIt;
340
341    typedef typename Graph::template BlueMap<int> IntBlueMap;
342    IntBlueMap map(G, 42);
343    for (BlueIt it(G); it != INVALID; ++it) {
344      check(map[it] == 42, "Wrong map constructor.");
345    }
346    int s = 0;
347    for (BlueIt it(G); it != INVALID; ++it) {
348      map[it] = 0;
349      check(map[it] == 0, "Wrong operator[].");
350      map.set(it, s);
351      check(map[it] == s, "Wrong set.");
352      ++s;
353    }
354    s = s * (s - 1) / 2;
355    for (BlueIt it(G); it != INVALID; ++it) {
356      s -= map[it];
357    }
358    check(s == 0, "Wrong sum.");
359
360    // map = constMap<Node>(12);
361    // for (NodeIt it(G); it != INVALID; ++it) {
362    //   check(map[it] == 12, "Wrong operator[].");
363    // }
364  }
365
366  template <typename Graph>
367  void checkGraphArcMap(const Graph& G) {
368    typedef typename Graph::Arc Arc;
369    typedef typename Graph::ArcIt ArcIt;
370
371    typedef typename Graph::template ArcMap<int> IntArcMap;
372    IntArcMap map(G, 42);
373    for (ArcIt it(G); it != INVALID; ++it) {
374      check(map[it] == 42, "Wrong map constructor.");
375    }
376    int s = 0;
377    for (ArcIt it(G); it != INVALID; ++it) {
378      map[it] = 0;
379      check(map[it] == 0, "Wrong operator[].");
380      map.set(it, s);
381      check(map[it] == s, "Wrong set.");
382      ++s;
383    }
384    s = s * (s - 1) / 2;
385    for (ArcIt it(G); it != INVALID; ++it) {
386      s -= map[it];
387    }
388    check(s == 0, "Wrong sum.");
389
390    // map = constMap<Arc>(12);
391    // for (ArcIt it(G); it != INVALID; ++it) {
392    //   check(map[it] == 12, "Wrong operator[].");
393    // }
394  }
395
396  template <typename Graph>
397  void checkGraphEdgeMap(const Graph& G) {
398    typedef typename Graph::Edge Edge;
399    typedef typename Graph::EdgeIt EdgeIt;
400
401    typedef typename Graph::template EdgeMap<int> IntEdgeMap;
402    IntEdgeMap map(G, 42);
403    for (EdgeIt it(G); it != INVALID; ++it) {
404      check(map[it] == 42, "Wrong map constructor.");
405    }
406    int s = 0;
407    for (EdgeIt it(G); it != INVALID; ++it) {
408      map[it] = 0;
409      check(map[it] == 0, "Wrong operator[].");
410      map.set(it, s);
411      check(map[it] == s, "Wrong set.");
412      ++s;
413    }
414    s = s * (s - 1) / 2;
415    for (EdgeIt it(G); it != INVALID; ++it) {
416      s -= map[it];
417    }
418    check(s == 0, "Wrong sum.");
419
420    // map = constMap<Edge>(12);
421    // for (EdgeIt it(G); it != INVALID; ++it) {
422    //   check(map[it] == 12, "Wrong operator[].");
423    // }
424  }
425
426
427} //namespace lemon
428
429#endif
Note: See TracBrowser for help on using the repository browser.