COIN-OR::LEMON - Graph Library

source: lemon-main/test/maps_test.cc @ 723:acdd0bd75a55

Last change on this file since 723:acdd0bd75a55 was 723:acdd0bd75a55, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Extend maps_test.cc (#302)

File size: 24.9 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    typedef CRMap::ValueIterator ValueIt;
530    CRMap map(gr);
531   
532    Node n0 = gr.addNode();
533    Node n1 = gr.addNode();
534    Node n2 = gr.addNode();
535   
536    map.set(n0, 'A');
537    map.set(n1, 'B');
538    map.set(n2, 'C');
539   
540    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
541          "Wrong CrossRefMap");
542    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
543          "Wrong CrossRefMap");
544    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
545          "Wrong CrossRefMap");
546    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
547          "Wrong CrossRefMap::count()");
548   
549    ValueIt it = map.beginValue();
550    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
551          it == map.endValue(), "Wrong value iterator");
552   
553    map.set(n2, 'A');
554
555    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
556          "Wrong CrossRefMap");
557    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
558    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
559    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
560          "Wrong CrossRefMap");
561    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
562          "Wrong CrossRefMap::count()");
563
564    it = map.beginValue();
565    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
566          it == map.endValue(), "Wrong value iterator");
567
568    map.set(n0, 'C');
569
570    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
571          "Wrong CrossRefMap");
572    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
573    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
574    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
575    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
576          "Wrong CrossRefMap::count()");
577
578    it = map.beginValue();
579    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
580          it == map.endValue(), "Wrong value iterator");
581  }
582
583  // Iterable bool map
584  {
585    typedef SmartGraph Graph;
586    typedef SmartGraph::Node Item;
587
588    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
589    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
590
591    const int num = 10;
592    Graph g;
593    std::vector<Item> items;
594    for (int i = 0; i < num; ++i) {
595      items.push_back(g.addNode());
596    }
597
598    Ibm map1(g, true);
599    int n = 0;
600    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
601      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
602      ++n;
603    }
604    check(n == num, "Wrong number");
605
606    n = 0;
607    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
608        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
609        ++n;
610    }
611    check(n == num, "Wrong number");
612    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
613    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
614
615    map1[items[5]] = true;
616
617    n = 0;
618    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
619        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
620        ++n;
621    }
622    check(n == num, "Wrong number");
623
624    map1[items[num / 2]] = false;
625    check(map1[items[num / 2]] == false, "Wrong map value");
626
627    n = 0;
628    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
629        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
630        ++n;
631    }
632    check(n == num - 1, "Wrong number");
633
634    n = 0;
635    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
636        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
637        ++n;
638    }
639    check(n == 1, "Wrong number");
640
641    map1[items[0]] = false;
642    check(map1[items[0]] == false, "Wrong map value");
643
644    map1[items[num - 1]] = false;
645    check(map1[items[num - 1]] == false, "Wrong map value");
646
647    n = 0;
648    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
649        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
650        ++n;
651    }
652    check(n == num - 3, "Wrong number");
653    check(map1.trueNum() == num - 3, "Wrong number");
654
655    n = 0;
656    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
657        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
658        ++n;
659    }
660    check(n == 3, "Wrong number");
661    check(map1.falseNum() == 3, "Wrong number");
662  }
663
664  // Iterable int map
665  {
666    typedef SmartGraph Graph;
667    typedef SmartGraph::Node Item;
668    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
669
670    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
671
672    const int num = 10;
673    Graph g;
674    std::vector<Item> items;
675    for (int i = 0; i < num; ++i) {
676      items.push_back(g.addNode());
677    }
678
679    Iim map1(g);
680    check(map1.size() == 0, "Wrong size");
681
682    for (int i = 0; i < num; ++i) {
683      map1[items[i]] = i;
684    }
685    check(map1.size() == num, "Wrong size");
686
687    for (int i = 0; i < num; ++i) {
688      Iim::ItemIt it(map1, i);
689      check(static_cast<Item>(it) == items[i], "Wrong value");
690      ++it;
691      check(static_cast<Item>(it) == INVALID, "Wrong value");
692    }
693
694    for (int i = 0; i < num; ++i) {
695      map1[items[i]] = i % 2;
696    }
697    check(map1.size() == 2, "Wrong size");
698
699    int n = 0;
700    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
701      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
702      ++n;
703    }
704    check(n == (num + 1) / 2, "Wrong number");
705
706    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
707      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
708      ++n;
709    }
710    check(n == num, "Wrong number");
711
712  }
713
714  // Iterable value map
715  {
716    typedef SmartGraph Graph;
717    typedef SmartGraph::Node Item;
718    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
719
720    checkConcept<ReadWriteMap<Item, double>, Ivm>();
721
722    const int num = 10;
723    Graph g;
724    std::vector<Item> items;
725    for (int i = 0; i < num; ++i) {
726      items.push_back(g.addNode());
727    }
728
729    Ivm map1(g, 0.0);
730    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
731    check(*map1.beginValue() == 0.0, "Wrong value");
732
733    for (int i = 0; i < num; ++i) {
734      map1.set(items[i], static_cast<double>(i));
735    }
736    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
737
738    for (int i = 0; i < num; ++i) {
739      Ivm::ItemIt it(map1, static_cast<double>(i));
740      check(static_cast<Item>(it) == items[i], "Wrong value");
741      ++it;
742      check(static_cast<Item>(it) == INVALID, "Wrong value");
743    }
744
745    for (Ivm::ValueIterator vit = map1.beginValue();
746         vit != map1.endValue(); ++vit) {
747      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
748            "Wrong ValueIterator");
749    }
750
751    for (int i = 0; i < num; ++i) {
752      map1.set(items[i], static_cast<double>(i % 2));
753    }
754    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
755
756    int n = 0;
757    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
758      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
759      ++n;
760    }
761    check(n == (num + 1) / 2, "Wrong number");
762
763    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
764      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
765      ++n;
766    }
767    check(n == num, "Wrong number");
768
769  }
770  return 0;
771}
Note: See TracBrowser for help on using the repository browser.