COIN-OR::LEMON - Graph Library

source: lemon-main/test/maps_test.cc

Last change on this file was 1210:da87dbdf3daf, checked in by Alpar Juttner <alpar@…>, 7 months ago

Resolve deprecation warnings of gcc 9 (#633)

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