COIN-OR::LEMON - Graph Library

source: lemon-main/test/maps_test.cc @ 1207:eba5aa390aca

Last change on this file since 1207:eba5aa390aca was 1131:4add05447ca0, checked in by Alpar Juttner <alpar@…>, 10 years ago

Tests and bugfixes for the STL style iterators (#325)

File size: 35.0 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-2013
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#include <deque>
20#include <set>
21
22#include <lemon/concept_check.h>
23#include <lemon/concepts/maps.h>
24#include <lemon/maps.h>
25#include <lemon/list_graph.h>
26#include <lemon/smart_graph.h>
27#include <lemon/adaptors.h>
28#include <lemon/dfs.h>
29#include <algorithm>
30
31#include "test_tools.h"
32
33using namespace lemon;
34using namespace lemon::concepts;
35
36struct A {};
37inline bool operator<(A, A) { return true; }
38struct B {};
39
40class C {
41  int _x;
42public:
43  C(int x) : _x(x) {}
44  int get() const { return _x; }
45};
46inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); }
47inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); }
48
49C createC(int x) { return C(x); }
50
51template <typename T>
52class Less {
53  T _t;
54public:
55  Less(T t): _t(t) {}
56  bool operator()(const T& t) const { return t < _t; }
57};
58
59class F {
60public:
61  typedef A argument_type;
62  typedef B result_type;
63
64  B operator()(const A&) const { return B(); }
65private:
66  F& operator=(const F&);
67};
68
69int func(A) { return 3; }
70
71int binc(int a, B) { return a+1; }
72
73template <typename T>
74class Sum {
75  T& _sum;
76public:
77  Sum(T& sum) : _sum(sum) {}
78  void operator()(const T& t) { _sum += t; }
79};
80
81typedef ReadMap<A, double> DoubleMap;
82typedef ReadWriteMap<A, double> DoubleWriteMap;
83typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
84
85typedef ReadMap<A, bool> BoolMap;
86typedef ReadWriteMap<A, bool> BoolWriteMap;
87typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
88
89int main()
90{
91  // Map concepts
92  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
93  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
94  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
95  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
96  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
97  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
98  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
99  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
100
101  // NullMap
102  {
103    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
104    NullMap<A,B> map1;
105    NullMap<A,B> map2 = map1;
106    ::lemon::ignore_unused_variable_warning(map2);
107    map1 = nullMap<A,B>();
108  }
109
110  // ConstMap
111  {
112    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
113    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
114    ConstMap<A,B> map1;
115    ConstMap<A,B> map2 = B();
116    ConstMap<A,B> map3 = map1;
117    ::lemon::ignore_unused_variable_warning(map2,map3);
118
119    map1 = constMap<A>(B());
120    map1 = constMap<A,B>();
121    map1.setAll(B());
122    ConstMap<A,C> map4(C(1));
123    ConstMap<A,C> map5 = map4;
124    ::lemon::ignore_unused_variable_warning(map5);
125
126    map4 = constMap<A>(C(2));
127    map4.setAll(C(3));
128
129    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
130    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
131
132    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
133    ConstMap<A,Const<int,10> > map6;
134    ConstMap<A,Const<int,10> > map7 = map6;
135    map6 = constMap<A,int,10>();
136    map7 = constMap<A,Const<int,10> >();
137    check(map6[A()] == 10 && map7[A()] == 10,
138          "Something is wrong with ConstMap");
139  }
140
141  // IdentityMap
142  {
143    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
144    IdentityMap<A> map1;
145    IdentityMap<A> map2 = map1;
146    ::lemon::ignore_unused_variable_warning(map2);
147
148    map1 = identityMap<A>();
149
150    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
151    check(identityMap<double>()[1.0] == 1.0 &&
152          identityMap<double>()[3.14] == 3.14,
153          "Something is wrong with IdentityMap");
154  }
155
156  // RangeMap
157  {
158    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
159    RangeMap<B> map1;
160    RangeMap<B> map2(10);
161    RangeMap<B> map3(10,B());
162    RangeMap<B> map4 = map1;
163    RangeMap<B> map5 = rangeMap<B>();
164    RangeMap<B> map6 = rangeMap<B>(10);
165    RangeMap<B> map7 = rangeMap(10,B());
166
167    checkConcept< ReferenceMap<int, double, double&, const double&>,
168                  RangeMap<double> >();
169    std::vector<double> v(10, 0);
170    v[5] = 100;
171    RangeMap<double> map8(v);
172    RangeMap<double> map9 = rangeMap(v);
173    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
174          "Something is wrong with RangeMap");
175  }
176
177  // SparseMap
178  {
179    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
180    SparseMap<A,B> map1;
181    SparseMap<A,B> map2 = B();
182    SparseMap<A,B> map3 = sparseMap<A,B>();
183    SparseMap<A,B> map4 = sparseMap<A>(B());
184
185    checkConcept< ReferenceMap<double, int, int&, const int&>,
186                  SparseMap<double, int> >();
187    std::map<double, int> m;
188    SparseMap<double, int> map5(m);
189    SparseMap<double, int> map6(m,10);
190    SparseMap<double, int> map7 = sparseMap(m);
191    SparseMap<double, int> map8 = sparseMap(m,10);
192
193    check(map5[1.0] == 0 && map5[3.14] == 0 &&
194          map6[1.0] == 10 && map6[3.14] == 10,
195          "Something is wrong with SparseMap");
196    map5[1.0] = map6[3.14] = 100;
197    check(map5[1.0] == 100 && map5[3.14] == 0 &&
198          map6[1.0] == 10 && map6[3.14] == 100,
199          "Something is wrong with SparseMap");
200  }
201
202  // ComposeMap
203  {
204    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
205    checkConcept<ReadMap<B,double>, CompMap>();
206    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
207    ::lemon::ignore_unused_variable_warning(map1);
208    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
209    ::lemon::ignore_unused_variable_warning(map2);
210
211    SparseMap<double, bool> m1(false); m1[3.14] = true;
212    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
213    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
214          "Something is wrong with ComposeMap")
215  }
216
217  // CombineMap
218  {
219    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
220    checkConcept<ReadMap<A,double>, CombMap>();
221    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
222    ::lemon::ignore_unused_variable_warning(map1);
223    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
224    ::lemon::ignore_unused_variable_warning(map2);
225
226    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
227          "Something is wrong with CombineMap");
228  }
229
230  // FunctorToMap, MapToFunctor
231  {
232    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
233    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
234    FunctorToMap<F> map1;
235    FunctorToMap<F> map2 = FunctorToMap<F>(F());
236    ::lemon::ignore_unused_variable_warning(map2);
237
238    B b = functorToMap(F())[A()];
239    ::lemon::ignore_unused_variable_warning(b);
240
241    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
242    MapToFunctor<ReadMap<A,B> > map =
243      MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
244    ::lemon::ignore_unused_variable_warning(map);
245
246    check(functorToMap(&func)[A()] == 3,
247          "Something is wrong with FunctorToMap");
248    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
249          "Something is wrong with MapToFunctor");
250    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
251          mapToFunctor(functorToMap(&func))[A()] == 3,
252          "Something is wrong with FunctorToMap or MapToFunctor");
253    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
254          "Something is wrong with FunctorToMap or MapToFunctor");
255  }
256
257  // ConvertMap
258  {
259    checkConcept<ReadMap<double,double>,
260      ConvertMap<ReadMap<double, int>, double> >();
261    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
262    ::lemon::ignore_unused_variable_warning(map1);
263    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
264    ::lemon::ignore_unused_variable_warning(map2);
265
266  }
267
268  // ForkMap
269  {
270    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
271
272    typedef RangeMap<double> RM;
273    typedef SparseMap<int, double> SM;
274    RM m1(10, -1);
275    SM m2(-1);
276    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
277    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
278    ForkMap<RM, SM> map1(m1,m2);
279    ForkMap<SM, RM> map2 = forkMap(m2,m1);
280    map2.set(5, 10);
281    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
282          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
283          "Something is wrong with ForkMap");
284  }
285
286  // Arithmetic maps:
287  // - AddMap, SubMap, MulMap, DivMap
288  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
289  // - NegMap, NegWriteMap, AbsMap
290  {
291    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
292    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
293    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
294    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
295
296    ConstMap<int, double> c1(1.0), c2(3.14);
297    IdentityMap<int> im;
298    ConvertMap<IdentityMap<int>, double> id(im);
299    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
300          "Something is wrong with AddMap");
301    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
302          "Something is wrong with SubMap");
303    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
304          "Something is wrong with MulMap");
305    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
306          "Something is wrong with DivMap");
307
308    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
309    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
310    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
311    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
312    checkConcept<DoubleMap, NegMap<DoubleMap> >();
313    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
314    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
315
316    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
317          "Something is wrong with ShiftMap");
318    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
319          shiftWriteMap(id, 2.0)[10] == 12.0,
320          "Something is wrong with ShiftWriteMap");
321    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
322          "Something is wrong with ScaleMap");
323    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
324          scaleWriteMap(id, 2.0)[10] == 20.0,
325          "Something is wrong with ScaleWriteMap");
326    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
327          "Something is wrong with NegMap");
328    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
329          "Something is wrong with NegWriteMap");
330    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
331          "Something is wrong with AbsMap");
332  }
333
334  // Logical maps:
335  // - TrueMap, FalseMap
336  // - AndMap, OrMap
337  // - NotMap, NotWriteMap
338  // - EqualMap, LessMap
339  {
340    checkConcept<BoolMap, TrueMap<A> >();
341    checkConcept<BoolMap, FalseMap<A> >();
342    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
343    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
344    checkConcept<BoolMap, NotMap<BoolMap> >();
345    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
346    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
347    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
348
349    TrueMap<int> tm;
350    FalseMap<int> fm;
351    RangeMap<bool> rm(2);
352    rm[0] = true; rm[1] = false;
353    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
354          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
355          "Something is wrong with AndMap");
356    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
357          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
358          "Something is wrong with OrMap");
359    check(!notMap(rm)[0] && notMap(rm)[1],
360          "Something is wrong with NotMap");
361    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
362          "Something is wrong with NotWriteMap");
363
364    ConstMap<int, double> cm(2.0);
365    IdentityMap<int> im;
366    ConvertMap<IdentityMap<int>, double> id(im);
367    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
368          "Something is wrong with LessMap");
369    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
370          "Something is wrong with EqualMap");
371  }
372
373  // LoggerBoolMap
374  {
375    typedef std::vector<int> vec;
376    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
377    checkConcept<WriteMap<int, bool>,
378                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
379
380    vec v1;
381    vec v2(10);
382    LoggerBoolMap<std::back_insert_iterator<vec> >
383      map1(std::back_inserter(v1));
384    LoggerBoolMap<vec::iterator> map2(v2.begin());
385    map1.set(10, false);
386    map1.set(20, true);   map2.set(20, true);
387    map1.set(30, false);  map2.set(40, false);
388    map1.set(50, true);   map2.set(50, true);
389    map1.set(60, true);   map2.set(60, true);
390    check(v1.size() == 3 && v2.size() == 10 &&
391          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
392          v2[0]==20 && v2[1]==50 && v2[2]==60,
393          "Something is wrong with LoggerBoolMap");
394
395    int i = 0;
396    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
397          it != map2.end(); ++it )
398      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
399
400    typedef ListDigraph Graph;
401    DIGRAPH_TYPEDEFS(Graph);
402    Graph gr;
403
404    Node n0 = gr.addNode();
405    Node n1 = gr.addNode();
406    Node n2 = gr.addNode();
407    Node n3 = gr.addNode();
408
409    gr.addArc(n3, n0);
410    gr.addArc(n3, n2);
411    gr.addArc(n0, n2);
412    gr.addArc(n2, n1);
413    gr.addArc(n0, n1);
414
415    {
416      std::vector<Node> v;
417      dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
418
419      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
420            "Something is wrong with LoggerBoolMap");
421    }
422    {
423      std::vector<Node> v(countNodes(gr));
424      dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
425
426      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
427            "Something is wrong with LoggerBoolMap");
428    }
429  }
430
431  // IdMap, RangeIdMap
432  {
433    typedef ListDigraph Graph;
434    DIGRAPH_TYPEDEFS(Graph);
435
436    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
437    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
438    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
439    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
440
441    Graph gr;
442    IdMap<Graph, Node> nmap(gr);
443    IdMap<Graph, Arc> amap(gr);
444    RangeIdMap<Graph, Node> nrmap(gr);
445    RangeIdMap<Graph, Arc> armap(gr);
446
447    Node n0 = gr.addNode();
448    Node n1 = gr.addNode();
449    Node n2 = gr.addNode();
450
451    Arc a0 = gr.addArc(n0, n1);
452    Arc a1 = gr.addArc(n0, n2);
453    Arc a2 = gr.addArc(n2, n1);
454    Arc a3 = gr.addArc(n2, n0);
455
456    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
457    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
458    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
459
460    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
461    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
462    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
463    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
464
465    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
466    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
467
468    check(nrmap.size() == 3 && armap.size() == 4,
469          "Wrong RangeIdMap::size()");
470
471    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
472    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
473    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
474
475    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
476    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
477    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
478    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
479
480    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
481    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
482
483    gr.erase(n1);
484
485    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
486    nrmap.swap(n2, n0);
487    if (armap[a1] == 1) armap.swap(a1, a3);
488    armap.swap(a3, a1);
489
490    check(nrmap.size() == 2 && armap.size() == 2,
491          "Wrong RangeIdMap::size()");
492
493    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
494    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
495
496    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
497    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
498
499    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
500    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
501  }
502
503  // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
504  {
505    typedef ListGraph Graph;
506    GRAPH_TYPEDEFS(Graph);
507
508    checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
509    checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
510    checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
511    checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
512    checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
513    checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
514
515    Graph gr;
516    Node n0 = gr.addNode();
517    Node n1 = gr.addNode();
518    Node n2 = gr.addNode();
519
520    gr.addEdge(n0,n1);
521    gr.addEdge(n1,n2);
522    gr.addEdge(n0,n2);
523    gr.addEdge(n2,n1);
524    gr.addEdge(n1,n2);
525    gr.addEdge(n0,n1);
526
527    for (EdgeIt e(gr); e != INVALID; ++e) {
528      check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
529      check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
530    }
531
532    check(mapCompare(gr,
533          sourceMap(orienter(gr, constMap<Edge, bool>(true))),
534          targetMap(orienter(gr, constMap<Edge, bool>(false)))),
535          "Wrong SourceMap or TargetMap");
536
537    typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
538    ConstMap<Edge, bool> true_edge_map(true);
539    Digraph dgr(gr, true_edge_map);
540    OutDegMap<Digraph> odm(dgr);
541    InDegMap<Digraph> idm(dgr);
542
543    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
544    check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
545
546    gr.addEdge(n2, n0);
547
548    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
549    check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
550  }
551
552  // CrossRefMap
553  {
554    typedef ListDigraph Graph;
555    DIGRAPH_TYPEDEFS(Graph);
556
557    checkConcept<ReadWriteMap<Node, int>,
558                 CrossRefMap<Graph, Node, int> >();
559    checkConcept<ReadWriteMap<Node, bool>,
560                 CrossRefMap<Graph, Node, bool> >();
561    checkConcept<ReadWriteMap<Node, double>,
562                 CrossRefMap<Graph, Node, double> >();
563
564    Graph gr;
565    typedef CrossRefMap<Graph, Node, char> CRMap;
566    CRMap map(gr);
567
568    Node n0 = gr.addNode();
569    Node n1 = gr.addNode();
570    Node n2 = gr.addNode();
571
572    map.set(n0, 'A');
573    map.set(n1, 'B');
574    map.set(n2, 'C');
575
576    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
577          "Wrong CrossRefMap");
578    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
579          "Wrong CrossRefMap");
580    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
581          "Wrong CrossRefMap");
582    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
583          "Wrong CrossRefMap::count()");
584
585    CRMap::ValueIt it = map.beginValue();
586    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
587          it == map.endValue(), "Wrong value iterator");
588
589    map.set(n2, 'A');
590
591    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
592          "Wrong CrossRefMap");
593    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
594    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
595    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
596          "Wrong CrossRefMap");
597    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
598          "Wrong CrossRefMap::count()");
599
600    it = map.beginValue();
601    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
602          it == map.endValue(), "Wrong value iterator");
603
604    map.set(n0, 'C');
605
606    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
607          "Wrong CrossRefMap");
608    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
609    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
610    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
611    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
612          "Wrong CrossRefMap::count()");
613
614    it = map.beginValue();
615    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
616          it == map.endValue(), "Wrong value iterator");
617  }
618
619  // CrossRefMap
620  {
621    typedef SmartDigraph Graph;
622    DIGRAPH_TYPEDEFS(Graph);
623
624    checkConcept<ReadWriteMap<Node, int>,
625                 CrossRefMap<Graph, Node, int> >();
626
627    Graph gr;
628    typedef CrossRefMap<Graph, Node, char> CRMap;
629    typedef CRMap::ValueIterator ValueIt;
630    CRMap map(gr);
631
632    Node n0 = gr.addNode();
633    Node n1 = gr.addNode();
634    Node n2 = gr.addNode();
635
636    map.set(n0, 'A');
637    map.set(n1, 'B');
638    map.set(n2, 'C');
639    map.set(n2, 'A');
640    map.set(n0, 'C');
641
642    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
643          "Wrong CrossRefMap");
644    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
645    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
646    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
647
648    ValueIt it = map.beginValue();
649    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
650          it == map.endValue(), "Wrong value iterator");
651  }
652
653  // Iterable bool map
654  {
655    typedef SmartGraph Graph;
656    typedef SmartGraph::Node Item;
657
658    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
659    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
660
661    const int num = 10;
662    Graph g;
663    Ibm map0(g, true);
664    std::vector<Item> items;
665    for (int i = 0; i < num; ++i) {
666      items.push_back(g.addNode());
667    }
668
669    Ibm map1(g, true);
670    int n = 0;
671    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
672      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
673      ++n;
674    }
675    check(n == num, "Wrong number");
676
677    n = 0;
678    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
679        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
680        ++n;
681    }
682    check(n == num, "Wrong number");
683    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
684    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
685
686    map1[items[5]] = true;
687
688    n = 0;
689    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
690        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
691        ++n;
692    }
693    check(n == num, "Wrong number");
694
695    map1[items[num / 2]] = false;
696    check(map1[items[num / 2]] == false, "Wrong map value");
697
698    n = 0;
699    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
700        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
701        ++n;
702    }
703    check(n == num - 1, "Wrong number");
704
705    n = 0;
706    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
707        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
708        ++n;
709    }
710    check(n == 1, "Wrong number");
711
712    map1[items[0]] = false;
713    check(map1[items[0]] == false, "Wrong map value");
714
715    map1[items[num - 1]] = false;
716    check(map1[items[num - 1]] == false, "Wrong map value");
717
718    n = 0;
719    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
720        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
721        ++n;
722    }
723    check(n == num - 3, "Wrong number");
724    check(map1.trueNum() == num - 3, "Wrong number");
725
726    n = 0;
727    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
728        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
729        ++n;
730    }
731    check(n == 3, "Wrong number");
732    check(map1.falseNum() == 3, "Wrong number");
733
734#ifdef LEMON_CXX11
735    {
736      int c = 0;
737      for(auto v: map1.items(false)) { c++; ::lemon::ignore_unused_variable_warning(v); }
738      check(c == map1.falseNum(), "Wrong number");
739    }
740    {
741      int c = 0;
742      for(auto v: map1.items(true)) { c++; ::lemon::ignore_unused_variable_warning(v); }
743      check(c == map1.trueNum(), "Wrong number");
744    }
745    {
746      int c = 0;
747      for(auto v: map1.falseKeys()) { c++; ::lemon::ignore_unused_variable_warning(v); }
748      check(c == map1.falseNum(), "Wrong number");
749    }
750    {
751      int c = 0;
752      for(auto v: map1.trueKeys()) { c++; ::lemon::ignore_unused_variable_warning(v); }
753      check(c == map1.trueNum(), "Wrong number");
754    }
755#endif
756
757  }
758
759  // Iterable int map
760  {
761    typedef SmartGraph Graph;
762    typedef SmartGraph::Node Item;
763    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
764
765    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
766
767    const int num = 10;
768    Graph g;
769    Iim map0(g, 0);
770    std::vector<Item> items;
771    for (int i = 0; i < num; ++i) {
772      items.push_back(g.addNode());
773    }
774
775    Iim map1(g);
776    check(map1.size() == 0, "Wrong size");
777
778    for (int i = 0; i < num; ++i) {
779      map1[items[i]] = i;
780    }
781    check(map1.size() == num, "Wrong size");
782
783    for (int i = 0; i < num; ++i) {
784      Iim::ItemIt it(map1, i);
785      check(static_cast<Item>(it) == items[i], "Wrong value");
786      ++it;
787      check(static_cast<Item>(it) == INVALID, "Wrong value");
788    }
789
790    for (int i = 0; i < num; ++i) {
791      map1[items[i]] = i % 2;
792    }
793    check(map1.size() == 2, "Wrong size");
794
795    int n = 0;
796    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
797      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
798      ++n;
799    }
800    check(n == (num + 1) / 2, "Wrong number");
801
802    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
803      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
804      ++n;
805    }
806    check(n == num, "Wrong number");
807#ifdef LEMON_CXX11
808    {
809      int c = 0;
810      for(auto v: map1.items(0)) { c++; ::lemon::ignore_unused_variable_warning(v); }
811      check(c == (num + 1) / 2, "Wrong number");
812      for(auto v: map1.items(1)) { c++; ::lemon::ignore_unused_variable_warning(v); }
813      check(c == num, "Wrong number");
814    }
815#endif
816
817  }
818
819  // Iterable value map
820  {
821    typedef SmartGraph Graph;
822    typedef SmartGraph::Node Item;
823    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
824
825    checkConcept<ReadWriteMap<Item, double>, Ivm>();
826
827    const int num = 10;
828    Graph g;
829    Ivm map0(g, 0.0);
830    std::vector<Item> items;
831    for (int i = 0; i < num; ++i) {
832      items.push_back(g.addNode());
833    }
834
835    Ivm map1(g, 0.0);
836    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
837    check(*map1.beginValue() == 0.0, "Wrong value");
838
839    for (int i = 0; i < num; ++i) {
840      map1.set(items[i], static_cast<double>(i));
841    }
842    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
843
844    for (int i = 0; i < num; ++i) {
845      Ivm::ItemIt it(map1, static_cast<double>(i));
846      check(static_cast<Item>(it) == items[i], "Wrong value");
847      ++it;
848      check(static_cast<Item>(it) == INVALID, "Wrong value");
849    }
850
851    for (Ivm::ValueIt vit = map1.beginValue();
852         vit != map1.endValue(); ++vit) {
853      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
854            "Wrong ValueIt");
855    }
856
857    for (int i = 0; i < num; ++i) {
858      map1.set(items[i], static_cast<double>(i % 2));
859    }
860    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
861
862    int n = 0;
863    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
864      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
865      ++n;
866    }
867    check(n == (num + 1) / 2, "Wrong number");
868
869    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
870      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
871      ++n;
872    }
873    check(n == num, "Wrong number");
874
875#ifdef LEMON_CXX11
876    {
877      int c = 0;
878      for(auto v: map1.items(0.0)) { c++; ::lemon::ignore_unused_variable_warning(v); }
879      check(c == (num + 1) / 2, "Wrong number");
880      for(auto v: map1.items(1.0)) { c++; ::lemon::ignore_unused_variable_warning(v); }
881      check(c == num, "Wrong number");
882    }
883#endif
884
885  }
886
887  // Graph map utilities:
888  // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
889  // mapFind(), mapFindIf(), mapCount(), mapCountIf()
890  // mapCopy(), mapCompare(), mapFill()
891  {
892    DIGRAPH_TYPEDEFS(SmartDigraph);
893
894    SmartDigraph g;
895    Node n1 = g.addNode();
896    Node n2 = g.addNode();
897    Node n3 = g.addNode();
898
899    SmartDigraph::NodeMap<int> map1(g);
900    SmartDigraph::ArcMap<char> map2(g);
901    ConstMap<Node, A> cmap1 = A();
902    ConstMap<Arc, C> cmap2 = C(0);
903
904    map1[n1] = 10;
905    map1[n2] = 5;
906    map1[n3] = 12;
907
908    // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
909    check(mapMin(g, map1) == n2, "Wrong mapMin()");
910    check(mapMax(g, map1) == n3, "Wrong mapMax()");
911    check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()");
912    check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()");
913    check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()");
914    check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()");
915
916    check(mapMin(g, map2) == INVALID, "Wrong mapMin()");
917    check(mapMax(g, map2) == INVALID, "Wrong mapMax()");
918
919    check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
920    check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()");
921
922    Arc a1 = g.addArc(n1, n2);
923    Arc a2 = g.addArc(n1, n3);
924    Arc a3 = g.addArc(n2, n3);
925    Arc a4 = g.addArc(n3, n1);
926
927    map2[a1] = 'b';
928    map2[a2] = 'a';
929    map2[a3] = 'b';
930    map2[a4] = 'c';
931
932    // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
933    check(mapMin(g, map2) == a2, "Wrong mapMin()");
934    check(mapMax(g, map2) == a4, "Wrong mapMax()");
935    check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()");
936    check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()");
937    check(mapMinValue(g, map2, std::greater<int>()) == 'c',
938          "Wrong mapMinValue()");
939    check(mapMaxValue(g, map2, std::greater<int>()) == 'a',
940          "Wrong mapMaxValue()");
941
942    check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
943    check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()");
944    check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()");
945
946    check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2,
947          "Wrong mapMin()");
948    check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4,
949          "Wrong mapMax()");
950    check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'),
951          "Wrong mapMinValue()");
952    check(mapMaxValue(g, composeMap(functorToMap(&createC), map2)) == C('c'),
953          "Wrong mapMaxValue()");
954
955    // mapFind(), mapFindIf()
956    check(mapFind(g, map1, 5) == n2, "Wrong mapFind()");
957    check(mapFind(g, map1, 6) == INVALID, "Wrong mapFind()");
958    check(mapFind(g, map2, 'a') == a2, "Wrong mapFind()");
959    check(mapFind(g, map2, 'e') == INVALID, "Wrong mapFind()");
960    check(mapFind(g, cmap2, C(0)) == ArcIt(g), "Wrong mapFind()");
961    check(mapFind(g, cmap2, C(1)) == INVALID, "Wrong mapFind()");
962
963    check(mapFindIf(g, map1, Less<int>(7)) == n2,
964          "Wrong mapFindIf()");
965    check(mapFindIf(g, map1, Less<int>(5)) == INVALID,
966          "Wrong mapFindIf()");
967    check(mapFindIf(g, map2, Less<char>('d')) == ArcIt(g),
968          "Wrong mapFindIf()");
969    check(mapFindIf(g, map2, Less<char>('a')) == INVALID,
970          "Wrong mapFindIf()");
971
972    // mapCount(), mapCountIf()
973    check(mapCount(g, map1, 5) == 1, "Wrong mapCount()");
974    check(mapCount(g, map1, 6) == 0, "Wrong mapCount()");
975    check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()");
976    check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()");
977    check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()");
978    check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()");
979    check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()");
980
981    check(mapCountIf(g, map1, Less<int>(11)) == 2,
982          "Wrong mapCountIf()");
983    check(mapCountIf(g, map1, Less<int>(13)) == 3,
984          "Wrong mapCountIf()");
985    check(mapCountIf(g, map1, Less<int>(5)) == 0,
986          "Wrong mapCountIf()");
987    check(mapCountIf(g, map2, Less<char>('d')) == 4,
988          "Wrong mapCountIf()");
989    check(mapCountIf(g, map2, Less<char>('c')) == 3,
990          "Wrong mapCountIf()");
991    check(mapCountIf(g, map2, Less<char>('a')) == 0,
992          "Wrong mapCountIf()");
993
994    // MapIt, ConstMapIt
995/*
996These tests can be used after applying bugfix #330
997    typedef SmartDigraph::NodeMap<int>::MapIt MapIt;
998    typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt;
999    check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5,
1000          "Wrong NodeMap<>::MapIt");
1001    check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12,
1002          "Wrong NodeMap<>::MapIt");
1003
1004    int sum = 0;
1005    std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum));
1006    check(sum == 27, "Wrong NodeMap<>::MapIt");
1007    std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum));
1008    check(sum == 54, "Wrong NodeMap<>::ConstMapIt");
1009*/
1010
1011    // mapCopy(), mapCompare(), mapFill()
1012    check(mapCompare(g, map1, map1), "Wrong mapCompare()");
1013    check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()");
1014    check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()");
1015    check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()");
1016    check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()");
1017
1018    SmartDigraph::NodeMap<int> map3(g, 0);
1019    SmartDigraph::ArcMap<char> map4(g, 'a');
1020
1021    check(!mapCompare(g, map1, map3), "Wrong mapCompare()");
1022    check(!mapCompare(g, map2, map4), "Wrong mapCompare()");
1023
1024    mapCopy(g, map1, map3);
1025    mapCopy(g, map2, map4);
1026
1027    check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()");
1028    check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()");
1029
1030    Undirector<SmartDigraph> ug(g);
1031    Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x');
1032    Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14);
1033
1034    check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
1035    check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
1036    check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
1037    check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
1038
1039    mapCopy(g, map2, umap1);
1040
1041    check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
1042    check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
1043    check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
1044    check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
1045
1046    mapCopy(g, map2, umap1);
1047    mapCopy(g, umap1, map2);
1048    mapCopy(ug, map2, umap1);
1049    mapCopy(ug, umap1, map2);
1050
1051    check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
1052    mapCopy(ug, umap1, umap2);
1053    check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
1054
1055    check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()");
1056    mapFill(g, map1, 2);
1057    check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()");
1058
1059    check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()");
1060    mapCopy(g, constMap<Arc>('z'), map2);
1061    check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()");
1062  }
1063
1064  return 0;
1065}
Note: See TracBrowser for help on using the repository browser.