COIN-OR::LEMON - Graph Library

source: lemon-main/test/maps_test.cc @ 781:6f10c6ec5a21

Last change on this file since 781:6f10c6ec5a21 was 726:3fc2a801c39e, checked in by Alpar Juttner <alpar@…>, 15 years ago

Merge #302

File size: 25.8 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#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
30#include "test_tools.h"
31
32using namespace lemon;
33using namespace lemon::concepts;
34
35struct A {};
36inline bool operator<(A, A) { return true; }
37struct B {};
38
39class C {
40  int x;
41public:
42  C(int _x) : x(_x) {}
43};
44
45class F {
46public:
47  typedef A argument_type;
48  typedef B result_type;
49
50  B operator()(const A&) const { return B(); }
51private:
52  F& operator=(const F&);
53};
54
55int func(A) { return 3; }
56
57int binc(int a, B) { return a+1; }
58
59typedef ReadMap<A, double> DoubleMap;
60typedef ReadWriteMap<A, double> DoubleWriteMap;
61typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
62
63typedef ReadMap<A, bool> BoolMap;
64typedef ReadWriteMap<A, bool> BoolWriteMap;
65typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
66
67template<typename Map1, typename Map2, typename ItemIt>
68void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
69  for (; it != INVALID; ++it)
70    check(map1[it] == map2[it], "The maps are not equal");
71}
72
73int main()
74{
75  // Map concepts
76  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
77  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
78  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
79  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
80  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
81  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
82  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
83  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
84
85  // NullMap
86  {
87    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
88    NullMap<A,B> map1;
89    NullMap<A,B> map2 = map1;
90    map1 = nullMap<A,B>();
91  }
92
93  // ConstMap
94  {
95    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
96    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
97    ConstMap<A,B> map1;
98    ConstMap<A,B> map2 = B();
99    ConstMap<A,B> map3 = map1;
100    map1 = constMap<A>(B());
101    map1 = constMap<A,B>();
102    map1.setAll(B());
103    ConstMap<A,C> map4(C(1));
104    ConstMap<A,C> map5 = map4;
105    map4 = constMap<A>(C(2));
106    map4.setAll(C(3));
107
108    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
109    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
110
111    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
112    ConstMap<A,Const<int,10> > map6;
113    ConstMap<A,Const<int,10> > map7 = map6;
114    map6 = constMap<A,int,10>();
115    map7 = constMap<A,Const<int,10> >();
116    check(map6[A()] == 10 && map7[A()] == 10,
117          "Something is wrong with ConstMap");
118  }
119
120  // IdentityMap
121  {
122    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
123    IdentityMap<A> map1;
124    IdentityMap<A> map2 = map1;
125    map1 = identityMap<A>();
126
127    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
128    check(identityMap<double>()[1.0] == 1.0 &&
129          identityMap<double>()[3.14] == 3.14,
130          "Something is wrong with IdentityMap");
131  }
132
133  // RangeMap
134  {
135    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
136    RangeMap<B> map1;
137    RangeMap<B> map2(10);
138    RangeMap<B> map3(10,B());
139    RangeMap<B> map4 = map1;
140    RangeMap<B> map5 = rangeMap<B>();
141    RangeMap<B> map6 = rangeMap<B>(10);
142    RangeMap<B> map7 = rangeMap(10,B());
143
144    checkConcept< ReferenceMap<int, double, double&, const double&>,
145                  RangeMap<double> >();
146    std::vector<double> v(10, 0);
147    v[5] = 100;
148    RangeMap<double> map8(v);
149    RangeMap<double> map9 = rangeMap(v);
150    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
151          "Something is wrong with RangeMap");
152  }
153
154  // SparseMap
155  {
156    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
157    SparseMap<A,B> map1;
158    SparseMap<A,B> map2 = B();
159    SparseMap<A,B> map3 = sparseMap<A,B>();
160    SparseMap<A,B> map4 = sparseMap<A>(B());
161
162    checkConcept< ReferenceMap<double, int, int&, const int&>,
163                  SparseMap<double, int> >();
164    std::map<double, int> m;
165    SparseMap<double, int> map5(m);
166    SparseMap<double, int> map6(m,10);
167    SparseMap<double, int> map7 = sparseMap(m);
168    SparseMap<double, int> map8 = sparseMap(m,10);
169
170    check(map5[1.0] == 0 && map5[3.14] == 0 &&
171          map6[1.0] == 10 && map6[3.14] == 10,
172          "Something is wrong with SparseMap");
173    map5[1.0] = map6[3.14] = 100;
174    check(map5[1.0] == 100 && map5[3.14] == 0 &&
175          map6[1.0] == 10 && map6[3.14] == 100,
176          "Something is wrong with SparseMap");
177  }
178
179  // ComposeMap
180  {
181    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
182    checkConcept<ReadMap<B,double>, CompMap>();
183    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
184    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
185
186    SparseMap<double, bool> m1(false); m1[3.14] = true;
187    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
188    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
189          "Something is wrong with ComposeMap")
190  }
191
192  // CombineMap
193  {
194    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
195    checkConcept<ReadMap<A,double>, CombMap>();
196    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
197    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
198
199    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
200          "Something is wrong with CombineMap");
201  }
202
203  // FunctorToMap, MapToFunctor
204  {
205    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
206    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
207    FunctorToMap<F> map1;
208    FunctorToMap<F> map2 = FunctorToMap<F>(F());
209    B b = functorToMap(F())[A()];
210
211    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
212    MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
213
214    check(functorToMap(&func)[A()] == 3,
215          "Something is wrong with FunctorToMap");
216    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
217          "Something is wrong with MapToFunctor");
218    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
219          mapToFunctor(functorToMap(&func))[A()] == 3,
220          "Something is wrong with FunctorToMap or MapToFunctor");
221    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
222          "Something is wrong with FunctorToMap or MapToFunctor");
223  }
224
225  // ConvertMap
226  {
227    checkConcept<ReadMap<double,double>,
228      ConvertMap<ReadMap<double, int>, double> >();
229    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
230    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
231  }
232
233  // ForkMap
234  {
235    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
236
237    typedef RangeMap<double> RM;
238    typedef SparseMap<int, double> SM;
239    RM m1(10, -1);
240    SM m2(-1);
241    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
242    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
243    ForkMap<RM, SM> map1(m1,m2);
244    ForkMap<SM, RM> map2 = forkMap(m2,m1);
245    map2.set(5, 10);
246    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
247          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
248          "Something is wrong with ForkMap");
249  }
250
251  // Arithmetic maps:
252  // - AddMap, SubMap, MulMap, DivMap
253  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
254  // - NegMap, NegWriteMap, AbsMap
255  {
256    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
257    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
258    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
259    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
260
261    ConstMap<int, double> c1(1.0), c2(3.14);
262    IdentityMap<int> im;
263    ConvertMap<IdentityMap<int>, double> id(im);
264    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
265          "Something is wrong with AddMap");
266    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
267          "Something is wrong with SubMap");
268    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
269          "Something is wrong with MulMap");
270    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
271          "Something is wrong with DivMap");
272
273    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
274    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
275    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
276    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
277    checkConcept<DoubleMap, NegMap<DoubleMap> >();
278    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
279    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
280
281    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
282          "Something is wrong with ShiftMap");
283    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
284          shiftWriteMap(id, 2.0)[10] == 12.0,
285          "Something is wrong with ShiftWriteMap");
286    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
287          "Something is wrong with ScaleMap");
288    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
289          scaleWriteMap(id, 2.0)[10] == 20.0,
290          "Something is wrong with ScaleWriteMap");
291    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
292          "Something is wrong with NegMap");
293    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
294          "Something is wrong with NegWriteMap");
295    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
296          "Something is wrong with AbsMap");
297  }
298
299  // Logical maps:
300  // - TrueMap, FalseMap
301  // - AndMap, OrMap
302  // - NotMap, NotWriteMap
303  // - EqualMap, LessMap
304  {
305    checkConcept<BoolMap, TrueMap<A> >();
306    checkConcept<BoolMap, FalseMap<A> >();
307    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
308    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
309    checkConcept<BoolMap, NotMap<BoolMap> >();
310    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
311    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
312    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
313
314    TrueMap<int> tm;
315    FalseMap<int> fm;
316    RangeMap<bool> rm(2);
317    rm[0] = true; rm[1] = false;
318    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
319          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
320          "Something is wrong with AndMap");
321    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
322          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
323          "Something is wrong with OrMap");
324    check(!notMap(rm)[0] && notMap(rm)[1],
325          "Something is wrong with NotMap");
326    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
327          "Something is wrong with NotWriteMap");
328
329    ConstMap<int, double> cm(2.0);
330    IdentityMap<int> im;
331    ConvertMap<IdentityMap<int>, double> id(im);
332    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
333          "Something is wrong with LessMap");
334    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
335          "Something is wrong with EqualMap");
336  }
337
338  // LoggerBoolMap
339  {
340    typedef std::vector<int> vec;
341    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
342    checkConcept<WriteMap<int, bool>,
343                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
344
345    vec v1;
346    vec v2(10);
347    LoggerBoolMap<std::back_insert_iterator<vec> >
348      map1(std::back_inserter(v1));
349    LoggerBoolMap<vec::iterator> map2(v2.begin());
350    map1.set(10, false);
351    map1.set(20, true);   map2.set(20, true);
352    map1.set(30, false);  map2.set(40, false);
353    map1.set(50, true);   map2.set(50, true);
354    map1.set(60, true);   map2.set(60, true);
355    check(v1.size() == 3 && v2.size() == 10 &&
356          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
357          v2[0]==20 && v2[1]==50 && v2[2]==60,
358          "Something is wrong with LoggerBoolMap");
359
360    int i = 0;
361    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
362          it != map2.end(); ++it )
363      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
364   
365    typedef ListDigraph Graph;
366    DIGRAPH_TYPEDEFS(Graph);
367    Graph gr;
368
369    Node n0 = gr.addNode();
370    Node n1 = gr.addNode();
371    Node n2 = gr.addNode();
372    Node n3 = gr.addNode();
373   
374    gr.addArc(n3, n0);
375    gr.addArc(n3, n2);
376    gr.addArc(n0, n2);
377    gr.addArc(n2, n1);
378    gr.addArc(n0, n1);
379   
380    {
381      std::vector<Node> v;
382      dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
383
384      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
385            "Something is wrong with LoggerBoolMap");
386    }
387    {
388      std::vector<Node> v(countNodes(gr));
389      dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
390     
391      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
392            "Something is wrong with LoggerBoolMap");
393    }
394  }
395 
396  // IdMap, RangeIdMap
397  {
398    typedef ListDigraph Graph;
399    DIGRAPH_TYPEDEFS(Graph);
400
401    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
402    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
403    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
404    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
405   
406    Graph gr;
407    IdMap<Graph, Node> nmap(gr);
408    IdMap<Graph, Arc> amap(gr);
409    RangeIdMap<Graph, Node> nrmap(gr);
410    RangeIdMap<Graph, Arc> armap(gr);
411   
412    Node n0 = gr.addNode();
413    Node n1 = gr.addNode();
414    Node n2 = gr.addNode();
415   
416    Arc a0 = gr.addArc(n0, n1);
417    Arc a1 = gr.addArc(n0, n2);
418    Arc a2 = gr.addArc(n2, n1);
419    Arc a3 = gr.addArc(n2, n0);
420   
421    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
422    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
423    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
424
425    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
426    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
427    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
428    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
429
430    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
431    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
432   
433    check(nrmap.size() == 3 && armap.size() == 4,
434          "Wrong RangeIdMap::size()");
435
436    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
437    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
438    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
439   
440    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
441    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
442    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
443    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
444
445    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
446    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
447   
448    gr.erase(n1);
449   
450    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
451    nrmap.swap(n2, n0);
452    if (armap[a1] == 1) armap.swap(a1, a3);
453    armap.swap(a3, a1);
454   
455    check(nrmap.size() == 2 && armap.size() == 2,
456          "Wrong RangeIdMap::size()");
457
458    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
459    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
460   
461    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
462    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
463
464    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
465    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
466  }
467 
468  // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
469  {
470    typedef ListGraph Graph;
471    GRAPH_TYPEDEFS(Graph);
472   
473    checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
474    checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
475    checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
476    checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
477    checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
478    checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
479
480    Graph gr;
481    Node n0 = gr.addNode();
482    Node n1 = gr.addNode();
483    Node n2 = gr.addNode();
484   
485    gr.addEdge(n0,n1);
486    gr.addEdge(n1,n2);
487    gr.addEdge(n0,n2);
488    gr.addEdge(n2,n1);
489    gr.addEdge(n1,n2);
490    gr.addEdge(n0,n1);
491   
492    for (EdgeIt e(gr); e != INVALID; ++e) {
493      check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
494      check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
495    }
496   
497    compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
498               targetMap(orienter(gr, constMap<Edge, bool>(false))),
499               EdgeIt(gr));
500
501    typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
502    Digraph dgr(gr, constMap<Edge, bool>(true));
503    OutDegMap<Digraph> odm(dgr);
504    InDegMap<Digraph> idm(dgr);
505   
506    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
507    check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
508   
509    gr.addEdge(n2, n0);
510
511    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
512    check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
513  }
514 
515  // CrossRefMap
516  {
517    typedef ListDigraph Graph;
518    DIGRAPH_TYPEDEFS(Graph);
519
520    checkConcept<ReadWriteMap<Node, int>,
521                 CrossRefMap<Graph, Node, int> >();
522    checkConcept<ReadWriteMap<Node, bool>,
523                 CrossRefMap<Graph, Node, bool> >();
524    checkConcept<ReadWriteMap<Node, double>,
525                 CrossRefMap<Graph, Node, double> >();
526   
527    Graph gr;
528    typedef CrossRefMap<Graph, Node, char> CRMap;
529    CRMap map(gr);
530   
531    Node n0 = gr.addNode();
532    Node n1 = gr.addNode();
533    Node n2 = gr.addNode();
534   
535    map.set(n0, 'A');
536    map.set(n1, 'B');
537    map.set(n2, 'C');
538   
539    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
540          "Wrong CrossRefMap");
541    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
542          "Wrong CrossRefMap");
543    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
544          "Wrong CrossRefMap");
545    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
546          "Wrong CrossRefMap::count()");
547   
548    CRMap::ValueIt it = map.beginValue();
549    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
550          it == map.endValue(), "Wrong value iterator");
551   
552    map.set(n2, 'A');
553
554    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
555          "Wrong CrossRefMap");
556    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
557    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
558    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
559          "Wrong CrossRefMap");
560    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
561          "Wrong CrossRefMap::count()");
562
563    it = map.beginValue();
564    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
565          it == map.endValue(), "Wrong value iterator");
566
567    map.set(n0, 'C');
568
569    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
570          "Wrong CrossRefMap");
571    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
572    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
573    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
574    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
575          "Wrong CrossRefMap::count()");
576
577    it = map.beginValue();
578    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
579          it == map.endValue(), "Wrong value iterator");
580  }
581
582  // CrossRefMap
583  {
584    typedef SmartDigraph Graph;
585    DIGRAPH_TYPEDEFS(Graph);
586
587    checkConcept<ReadWriteMap<Node, int>,
588                 CrossRefMap<Graph, Node, int> >();
589   
590    Graph gr;
591    typedef CrossRefMap<Graph, Node, char> CRMap;
592    typedef CRMap::ValueIterator ValueIt;
593    CRMap map(gr);
594   
595    Node n0 = gr.addNode();
596    Node n1 = gr.addNode();
597    Node n2 = gr.addNode();
598   
599    map.set(n0, 'A');
600    map.set(n1, 'B');
601    map.set(n2, 'C');
602    map.set(n2, 'A');
603    map.set(n0, 'C');
604
605    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
606          "Wrong CrossRefMap");
607    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
608    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
609    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
610
611    ValueIt it = map.beginValue();
612    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
613          it == map.endValue(), "Wrong value iterator");
614  }
615 
616  // Iterable bool map
617  {
618    typedef SmartGraph Graph;
619    typedef SmartGraph::Node Item;
620
621    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
622    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
623
624    const int num = 10;
625    Graph g;
626    std::vector<Item> items;
627    for (int i = 0; i < num; ++i) {
628      items.push_back(g.addNode());
629    }
630
631    Ibm map1(g, true);
632    int n = 0;
633    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
634      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
635      ++n;
636    }
637    check(n == num, "Wrong number");
638
639    n = 0;
640    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
641        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
642        ++n;
643    }
644    check(n == num, "Wrong number");
645    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
646    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
647
648    map1[items[5]] = true;
649
650    n = 0;
651    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
652        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
653        ++n;
654    }
655    check(n == num, "Wrong number");
656
657    map1[items[num / 2]] = false;
658    check(map1[items[num / 2]] == false, "Wrong map value");
659
660    n = 0;
661    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
662        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
663        ++n;
664    }
665    check(n == num - 1, "Wrong number");
666
667    n = 0;
668    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
669        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
670        ++n;
671    }
672    check(n == 1, "Wrong number");
673
674    map1[items[0]] = false;
675    check(map1[items[0]] == false, "Wrong map value");
676
677    map1[items[num - 1]] = false;
678    check(map1[items[num - 1]] == false, "Wrong map value");
679
680    n = 0;
681    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
682        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
683        ++n;
684    }
685    check(n == num - 3, "Wrong number");
686    check(map1.trueNum() == num - 3, "Wrong number");
687
688    n = 0;
689    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
690        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
691        ++n;
692    }
693    check(n == 3, "Wrong number");
694    check(map1.falseNum() == 3, "Wrong number");
695  }
696
697  // Iterable int map
698  {
699    typedef SmartGraph Graph;
700    typedef SmartGraph::Node Item;
701    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
702
703    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
704
705    const int num = 10;
706    Graph g;
707    std::vector<Item> items;
708    for (int i = 0; i < num; ++i) {
709      items.push_back(g.addNode());
710    }
711
712    Iim map1(g);
713    check(map1.size() == 0, "Wrong size");
714
715    for (int i = 0; i < num; ++i) {
716      map1[items[i]] = i;
717    }
718    check(map1.size() == num, "Wrong size");
719
720    for (int i = 0; i < num; ++i) {
721      Iim::ItemIt it(map1, i);
722      check(static_cast<Item>(it) == items[i], "Wrong value");
723      ++it;
724      check(static_cast<Item>(it) == INVALID, "Wrong value");
725    }
726
727    for (int i = 0; i < num; ++i) {
728      map1[items[i]] = i % 2;
729    }
730    check(map1.size() == 2, "Wrong size");
731
732    int n = 0;
733    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
734      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
735      ++n;
736    }
737    check(n == (num + 1) / 2, "Wrong number");
738
739    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
740      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
741      ++n;
742    }
743    check(n == num, "Wrong number");
744
745  }
746
747  // Iterable value map
748  {
749    typedef SmartGraph Graph;
750    typedef SmartGraph::Node Item;
751    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
752
753    checkConcept<ReadWriteMap<Item, double>, Ivm>();
754
755    const int num = 10;
756    Graph g;
757    std::vector<Item> items;
758    for (int i = 0; i < num; ++i) {
759      items.push_back(g.addNode());
760    }
761
762    Ivm map1(g, 0.0);
763    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
764    check(*map1.beginValue() == 0.0, "Wrong value");
765
766    for (int i = 0; i < num; ++i) {
767      map1.set(items[i], static_cast<double>(i));
768    }
769    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
770
771    for (int i = 0; i < num; ++i) {
772      Ivm::ItemIt it(map1, static_cast<double>(i));
773      check(static_cast<Item>(it) == items[i], "Wrong value");
774      ++it;
775      check(static_cast<Item>(it) == INVALID, "Wrong value");
776    }
777
778    for (Ivm::ValueIt vit = map1.beginValue();
779         vit != map1.endValue(); ++vit) {
780      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
781            "Wrong ValueIt");
782    }
783
784    for (int i = 0; i < num; ++i) {
785      map1.set(items[i], static_cast<double>(i % 2));
786    }
787    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
788
789    int n = 0;
790    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
791      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
792      ++n;
793    }
794    check(n == (num + 1) / 2, "Wrong number");
795
796    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
797      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
798      ++n;
799    }
800    check(n == num, "Wrong number");
801
802  }
803  return 0;
804}
Note: See TracBrowser for help on using the repository browser.