COIN-OR::LEMON - Graph Library

source: lemon-main/test/maps_test.cc @ 722:b52189c479fb

Last change on this file since 722:b52189c479fb was 721:99124ea4f048, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Merge

File size: 22.3 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19#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
28#include "test_tools.h"
29
30using namespace lemon;
31using namespace lemon::concepts;
32
33struct A {};
34inline bool operator<(A, A) { return true; }
35struct B {};
36
37class C {
38  int x;
39public:
40  C(int _x) : x(_x) {}
41};
42
43class F {
44public:
45  typedef A argument_type;
46  typedef B result_type;
47
48  B operator()(const A&) const { return B(); }
49private:
50  F& operator=(const F&);
51};
52
53int func(A) { return 3; }
54
55int binc(int a, B) { return a+1; }
56
57typedef ReadMap<A, double> DoubleMap;
58typedef ReadWriteMap<A, double> DoubleWriteMap;
59typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
60
61typedef ReadMap<A, bool> BoolMap;
62typedef ReadWriteMap<A, bool> BoolWriteMap;
63typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
64
65int main()
66{
67  // Map concepts
68  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
69  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
70  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
71  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
72  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
73  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
74  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
75  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
76
77  // NullMap
78  {
79    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
80    NullMap<A,B> map1;
81    NullMap<A,B> map2 = map1;
82    map1 = nullMap<A,B>();
83  }
84
85  // ConstMap
86  {
87    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
88    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
89    ConstMap<A,B> map1;
90    ConstMap<A,B> map2 = B();
91    ConstMap<A,B> map3 = map1;
92    map1 = constMap<A>(B());
93    map1 = constMap<A,B>();
94    map1.setAll(B());
95    ConstMap<A,C> map4(C(1));
96    ConstMap<A,C> map5 = map4;
97    map4 = constMap<A>(C(2));
98    map4.setAll(C(3));
99
100    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
101    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
102
103    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
104    ConstMap<A,Const<int,10> > map6;
105    ConstMap<A,Const<int,10> > map7 = map6;
106    map6 = constMap<A,int,10>();
107    map7 = constMap<A,Const<int,10> >();
108    check(map6[A()] == 10 && map7[A()] == 10,
109          "Something is wrong with ConstMap");
110  }
111
112  // IdentityMap
113  {
114    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
115    IdentityMap<A> map1;
116    IdentityMap<A> map2 = map1;
117    map1 = identityMap<A>();
118
119    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
120    check(identityMap<double>()[1.0] == 1.0 &&
121          identityMap<double>()[3.14] == 3.14,
122          "Something is wrong with IdentityMap");
123  }
124
125  // RangeMap
126  {
127    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
128    RangeMap<B> map1;
129    RangeMap<B> map2(10);
130    RangeMap<B> map3(10,B());
131    RangeMap<B> map4 = map1;
132    RangeMap<B> map5 = rangeMap<B>();
133    RangeMap<B> map6 = rangeMap<B>(10);
134    RangeMap<B> map7 = rangeMap(10,B());
135
136    checkConcept< ReferenceMap<int, double, double&, const double&>,
137                  RangeMap<double> >();
138    std::vector<double> v(10, 0);
139    v[5] = 100;
140    RangeMap<double> map8(v);
141    RangeMap<double> map9 = rangeMap(v);
142    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
143          "Something is wrong with RangeMap");
144  }
145
146  // SparseMap
147  {
148    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
149    SparseMap<A,B> map1;
150    SparseMap<A,B> map2 = B();
151    SparseMap<A,B> map3 = sparseMap<A,B>();
152    SparseMap<A,B> map4 = sparseMap<A>(B());
153
154    checkConcept< ReferenceMap<double, int, int&, const int&>,
155                  SparseMap<double, int> >();
156    std::map<double, int> m;
157    SparseMap<double, int> map5(m);
158    SparseMap<double, int> map6(m,10);
159    SparseMap<double, int> map7 = sparseMap(m);
160    SparseMap<double, int> map8 = sparseMap(m,10);
161
162    check(map5[1.0] == 0 && map5[3.14] == 0 &&
163          map6[1.0] == 10 && map6[3.14] == 10,
164          "Something is wrong with SparseMap");
165    map5[1.0] = map6[3.14] = 100;
166    check(map5[1.0] == 100 && map5[3.14] == 0 &&
167          map6[1.0] == 10 && map6[3.14] == 100,
168          "Something is wrong with SparseMap");
169  }
170
171  // ComposeMap
172  {
173    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
174    checkConcept<ReadMap<B,double>, CompMap>();
175    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
176    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
177
178    SparseMap<double, bool> m1(false); m1[3.14] = true;
179    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
180    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
181          "Something is wrong with ComposeMap")
182  }
183
184  // CombineMap
185  {
186    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
187    checkConcept<ReadMap<A,double>, CombMap>();
188    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
189    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
190
191    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
192          "Something is wrong with CombineMap");
193  }
194
195  // FunctorToMap, MapToFunctor
196  {
197    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
198    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
199    FunctorToMap<F> map1;
200    FunctorToMap<F> map2 = FunctorToMap<F>(F());
201    B b = functorToMap(F())[A()];
202
203    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
204    MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
205
206    check(functorToMap(&func)[A()] == 3,
207          "Something is wrong with FunctorToMap");
208    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
209          "Something is wrong with MapToFunctor");
210    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
211          mapToFunctor(functorToMap(&func))[A()] == 3,
212          "Something is wrong with FunctorToMap or MapToFunctor");
213    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
214          "Something is wrong with FunctorToMap or MapToFunctor");
215  }
216
217  // ConvertMap
218  {
219    checkConcept<ReadMap<double,double>,
220      ConvertMap<ReadMap<double, int>, double> >();
221    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
222    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
223  }
224
225  // ForkMap
226  {
227    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
228
229    typedef RangeMap<double> RM;
230    typedef SparseMap<int, double> SM;
231    RM m1(10, -1);
232    SM m2(-1);
233    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
234    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
235    ForkMap<RM, SM> map1(m1,m2);
236    ForkMap<SM, RM> map2 = forkMap(m2,m1);
237    map2.set(5, 10);
238    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
239          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
240          "Something is wrong with ForkMap");
241  }
242
243  // Arithmetic maps:
244  // - AddMap, SubMap, MulMap, DivMap
245  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
246  // - NegMap, NegWriteMap, AbsMap
247  {
248    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
249    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
250    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
251    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
252
253    ConstMap<int, double> c1(1.0), c2(3.14);
254    IdentityMap<int> im;
255    ConvertMap<IdentityMap<int>, double> id(im);
256    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
257          "Something is wrong with AddMap");
258    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
259          "Something is wrong with SubMap");
260    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
261          "Something is wrong with MulMap");
262    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
263          "Something is wrong with DivMap");
264
265    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
266    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
267    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
268    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
269    checkConcept<DoubleMap, NegMap<DoubleMap> >();
270    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
271    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
272
273    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
274          "Something is wrong with ShiftMap");
275    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
276          shiftWriteMap(id, 2.0)[10] == 12.0,
277          "Something is wrong with ShiftWriteMap");
278    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
279          "Something is wrong with ScaleMap");
280    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
281          scaleWriteMap(id, 2.0)[10] == 20.0,
282          "Something is wrong with ScaleWriteMap");
283    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
284          "Something is wrong with NegMap");
285    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
286          "Something is wrong with NegWriteMap");
287    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
288          "Something is wrong with AbsMap");
289  }
290
291  // Logical maps:
292  // - TrueMap, FalseMap
293  // - AndMap, OrMap
294  // - NotMap, NotWriteMap
295  // - EqualMap, LessMap
296  {
297    checkConcept<BoolMap, TrueMap<A> >();
298    checkConcept<BoolMap, FalseMap<A> >();
299    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
300    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
301    checkConcept<BoolMap, NotMap<BoolMap> >();
302    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
303    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
304    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
305
306    TrueMap<int> tm;
307    FalseMap<int> fm;
308    RangeMap<bool> rm(2);
309    rm[0] = true; rm[1] = false;
310    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
311          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
312          "Something is wrong with AndMap");
313    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
314          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
315          "Something is wrong with OrMap");
316    check(!notMap(rm)[0] && notMap(rm)[1],
317          "Something is wrong with NotMap");
318    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
319          "Something is wrong with NotWriteMap");
320
321    ConstMap<int, double> cm(2.0);
322    IdentityMap<int> im;
323    ConvertMap<IdentityMap<int>, double> id(im);
324    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
325          "Something is wrong with LessMap");
326    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
327          "Something is wrong with EqualMap");
328  }
329
330  // LoggerBoolMap
331  {
332    typedef std::vector<int> vec;
333    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
334    checkConcept<WriteMap<int, bool>,
335                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
336
337    vec v1;
338    vec v2(10);
339    LoggerBoolMap<std::back_insert_iterator<vec> >
340      map1(std::back_inserter(v1));
341    LoggerBoolMap<vec::iterator> map2(v2.begin());
342    map1.set(10, false);
343    map1.set(20, true);   map2.set(20, true);
344    map1.set(30, false);  map2.set(40, false);
345    map1.set(50, true);   map2.set(50, true);
346    map1.set(60, true);   map2.set(60, true);
347    check(v1.size() == 3 && v2.size() == 10 &&
348          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
349          v2[0]==20 && v2[1]==50 && v2[2]==60,
350          "Something is wrong with LoggerBoolMap");
351
352    int i = 0;
353    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
354          it != map2.end(); ++it )
355      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
356  }
357 
358  // IdMap, RangeIdMap
359  {
360    typedef ListDigraph Graph;
361    DIGRAPH_TYPEDEFS(Graph);
362
363    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
364    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
365    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
366    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
367   
368    Graph gr;
369    IdMap<Graph, Node> nmap(gr);
370    IdMap<Graph, Arc> amap(gr);
371    RangeIdMap<Graph, Node> nrmap(gr);
372    RangeIdMap<Graph, Arc> armap(gr);
373   
374    Node n0 = gr.addNode();
375    Node n1 = gr.addNode();
376    Node n2 = gr.addNode();
377   
378    Arc a0 = gr.addArc(n0, n1);
379    Arc a1 = gr.addArc(n0, n2);
380    Arc a2 = gr.addArc(n2, n1);
381    Arc a3 = gr.addArc(n2, n0);
382   
383    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
384    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
385    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
386
387    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
388    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
389    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
390    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
391
392    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
393    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
394   
395    check(nrmap.size() == 3 && armap.size() == 4,
396          "Wrong RangeIdMap::size()");
397
398    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
399    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
400    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
401   
402    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
403    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
404    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
405    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
406
407    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
408    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
409   
410    gr.erase(n1);
411   
412    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
413    nrmap.swap(n2, n0);
414    if (armap[a1] == 1) armap.swap(a1, a3);
415    armap.swap(a3, a1);
416   
417    check(nrmap.size() == 2 && armap.size() == 2,
418          "Wrong RangeIdMap::size()");
419
420    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
421    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
422   
423    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
424    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
425
426    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
427    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
428  }
429 
430  // CrossRefMap
431  {
432    typedef ListDigraph Graph;
433    DIGRAPH_TYPEDEFS(Graph);
434
435    checkConcept<ReadWriteMap<Node, int>,
436                 CrossRefMap<Graph, Node, int> >();
437    checkConcept<ReadWriteMap<Node, bool>,
438                 CrossRefMap<Graph, Node, bool> >();
439    checkConcept<ReadWriteMap<Node, double>,
440                 CrossRefMap<Graph, Node, double> >();
441   
442    Graph gr;
443    typedef CrossRefMap<Graph, Node, char> CRMap;
444    typedef CRMap::ValueIterator ValueIt;
445    CRMap map(gr);
446   
447    Node n0 = gr.addNode();
448    Node n1 = gr.addNode();
449    Node n2 = gr.addNode();
450   
451    map.set(n0, 'A');
452    map.set(n1, 'B');
453    map.set(n2, 'C');
454   
455    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
456          "Wrong CrossRefMap");
457    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
458          "Wrong CrossRefMap");
459    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
460          "Wrong CrossRefMap");
461    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
462          "Wrong CrossRefMap::count()");
463   
464    ValueIt it = map.beginValue();
465    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
466          it == map.endValue(), "Wrong value iterator");
467   
468    map.set(n2, 'A');
469
470    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
471          "Wrong CrossRefMap");
472    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
473    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
474    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
475          "Wrong CrossRefMap");
476    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
477          "Wrong CrossRefMap::count()");
478
479    it = map.beginValue();
480    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
481          it == map.endValue(), "Wrong value iterator");
482
483    map.set(n0, 'C');
484
485    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
486          "Wrong CrossRefMap");
487    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
488    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
489    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
490    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
491          "Wrong CrossRefMap::count()");
492
493    it = map.beginValue();
494    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
495          it == map.endValue(), "Wrong value iterator");
496  }
497
498  // Iterable bool map
499  {
500    typedef SmartGraph Graph;
501    typedef SmartGraph::Node Item;
502
503    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
504    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
505
506    const int num = 10;
507    Graph g;
508    std::vector<Item> items;
509    for (int i = 0; i < num; ++i) {
510      items.push_back(g.addNode());
511    }
512
513    Ibm map1(g, true);
514    int n = 0;
515    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
516      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
517      ++n;
518    }
519    check(n == num, "Wrong number");
520
521    n = 0;
522    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
523        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
524        ++n;
525    }
526    check(n == num, "Wrong number");
527    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
528    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
529
530    map1[items[5]] = true;
531
532    n = 0;
533    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
534        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
535        ++n;
536    }
537    check(n == num, "Wrong number");
538
539    map1[items[num / 2]] = false;
540    check(map1[items[num / 2]] == false, "Wrong map value");
541
542    n = 0;
543    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
544        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
545        ++n;
546    }
547    check(n == num - 1, "Wrong number");
548
549    n = 0;
550    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
551        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
552        ++n;
553    }
554    check(n == 1, "Wrong number");
555
556    map1[items[0]] = false;
557    check(map1[items[0]] == false, "Wrong map value");
558
559    map1[items[num - 1]] = false;
560    check(map1[items[num - 1]] == false, "Wrong map value");
561
562    n = 0;
563    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
564        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
565        ++n;
566    }
567    check(n == num - 3, "Wrong number");
568    check(map1.trueNum() == num - 3, "Wrong number");
569
570    n = 0;
571    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
572        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
573        ++n;
574    }
575    check(n == 3, "Wrong number");
576    check(map1.falseNum() == 3, "Wrong number");
577  }
578
579  // Iterable int map
580  {
581    typedef SmartGraph Graph;
582    typedef SmartGraph::Node Item;
583    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
584
585    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
586
587    const int num = 10;
588    Graph g;
589    std::vector<Item> items;
590    for (int i = 0; i < num; ++i) {
591      items.push_back(g.addNode());
592    }
593
594    Iim map1(g);
595    check(map1.size() == 0, "Wrong size");
596
597    for (int i = 0; i < num; ++i) {
598      map1[items[i]] = i;
599    }
600    check(map1.size() == num, "Wrong size");
601
602    for (int i = 0; i < num; ++i) {
603      Iim::ItemIt it(map1, i);
604      check(static_cast<Item>(it) == items[i], "Wrong value");
605      ++it;
606      check(static_cast<Item>(it) == INVALID, "Wrong value");
607    }
608
609    for (int i = 0; i < num; ++i) {
610      map1[items[i]] = i % 2;
611    }
612    check(map1.size() == 2, "Wrong size");
613
614    int n = 0;
615    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
616      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
617      ++n;
618    }
619    check(n == (num + 1) / 2, "Wrong number");
620
621    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
622      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
623      ++n;
624    }
625    check(n == num, "Wrong number");
626
627  }
628
629  // Iterable value map
630  {
631    typedef SmartGraph Graph;
632    typedef SmartGraph::Node Item;
633    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
634
635    checkConcept<ReadWriteMap<Item, double>, Ivm>();
636
637    const int num = 10;
638    Graph g;
639    std::vector<Item> items;
640    for (int i = 0; i < num; ++i) {
641      items.push_back(g.addNode());
642    }
643
644    Ivm map1(g, 0.0);
645    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
646    check(*map1.beginValue() == 0.0, "Wrong value");
647
648    for (int i = 0; i < num; ++i) {
649      map1.set(items[i], static_cast<double>(i));
650    }
651    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
652
653    for (int i = 0; i < num; ++i) {
654      Ivm::ItemIt it(map1, static_cast<double>(i));
655      check(static_cast<Item>(it) == items[i], "Wrong value");
656      ++it;
657      check(static_cast<Item>(it) == INVALID, "Wrong value");
658    }
659
660    for (Ivm::ValueIterator vit = map1.beginValue();
661         vit != map1.endValue(); ++vit) {
662      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
663            "Wrong ValueIterator");
664    }
665
666    for (int i = 0; i < num; ++i) {
667      map1.set(items[i], static_cast<double>(i % 2));
668    }
669    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
670
671    int n = 0;
672    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
673      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
674      ++n;
675    }
676    check(n == (num + 1) / 2, "Wrong number");
677
678    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
679      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
680      ++n;
681    }
682    check(n == num, "Wrong number");
683
684  }
685  return 0;
686}
Note: See TracBrowser for help on using the repository browser.