1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
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>
31 #include "test_tools.h"
33 using namespace lemon;
34 using namespace lemon::concepts;
37 inline bool operator<(A, A) { return true; }
44 int get() const { return _x; }
46 inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); }
47 inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); }
49 C createC(int x) { return C(x); }
56 bool operator()(const T& t) const { return t < _t; }
61 typedef A argument_type;
62 typedef B result_type;
67 B operator()(const A&) const { return B(); }
69 F& operator=(const F&);
72 int func(A) { return 3; }
74 int binc(int a, B) { return a+1; }
80 Sum(T& sum) : _sum(sum) {}
81 void operator()(const T& t) { _sum += t; }
84 typedef ReadMap<A, double> DoubleMap;
85 typedef ReadWriteMap<A, double> DoubleWriteMap;
86 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
88 typedef ReadMap<A, bool> BoolMap;
89 typedef ReadWriteMap<A, bool> BoolWriteMap;
90 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
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&> >();
106 checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
108 NullMap<A,B> map2 = map1;
109 ::lemon::ignore_unused_variable_warning(map2);
110 map1 = nullMap<A,B>();
115 checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
116 checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
118 ConstMap<A,B> map2 = B();
119 ConstMap<A,B> map3 = map1;
120 ::lemon::ignore_unused_variable_warning(map2,map3);
122 map1 = constMap<A>(B());
123 map1 = constMap<A,B>();
125 ConstMap<A,C> map4(C(1));
126 ConstMap<A,C> map5 = map4;
127 ::lemon::ignore_unused_variable_warning(map5);
129 map4 = constMap<A>(C(2));
132 checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
133 check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
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");
146 checkConcept<ReadMap<A,A>, IdentityMap<A> >();
148 IdentityMap<A> map2 = map1;
149 ::lemon::ignore_unused_variable_warning(map2);
151 map1 = identityMap<A>();
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");
161 checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
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());
170 checkConcept< ReferenceMap<int, double, double&, const double&>,
171 RangeMap<double> >();
172 std::vector<double> v(10, 0);
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");
182 checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
184 SparseMap<A,B> map2 = B();
185 SparseMap<A,B> map3 = sparseMap<A,B>();
186 SparseMap<A,B> map4 = sparseMap<A>(B());
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);
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");
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);
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")
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);
229 check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
230 "Something is wrong with CombineMap");
233 // FunctorToMap, MapToFunctor
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);
241 B b = functorToMap(F())[A()];
242 ::lemon::ignore_unused_variable_warning(b);
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);
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");
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);
273 checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
275 typedef RangeMap<double> RM;
276 typedef SparseMap<int, double> SM;
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);
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");
290 // - AddMap, SubMap, MulMap, DivMap
291 // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
292 // - NegMap, NegWriteMap, AbsMap
294 checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
295 checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
296 checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
297 checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
299 ConstMap<int, double> c1(1.0), c2(3.14);
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");
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> >();
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");
338 // - TrueMap, FalseMap
340 // - NotMap, NotWriteMap
341 // - EqualMap, LessMap
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> >();
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");
367 ConstMap<int, double> cm(2.0);
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");
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> > >();
385 LoggerBoolMap<std::back_insert_iterator<vec> >
386 map1(std::back_inserter(v1));
387 LoggerBoolMap<vec::iterator> map2(v2.begin());
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");
399 for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
400 it != map2.end(); ++it )
401 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
403 typedef ListDigraph Graph;
404 DIGRAPH_TYPEDEFS(Graph);
407 Node n0 = gr.addNode();
408 Node n1 = gr.addNode();
409 Node n2 = gr.addNode();
410 Node n3 = gr.addNode();
420 dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
422 check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
423 "Something is wrong with LoggerBoolMap");
426 std::vector<Node> v(countNodes(gr));
427 dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
429 check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
430 "Something is wrong with LoggerBoolMap");
436 typedef ListDigraph Graph;
437 DIGRAPH_TYPEDEFS(Graph);
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> >();
445 IdMap<Graph, Node> nmap(gr);
446 IdMap<Graph, Arc> amap(gr);
447 RangeIdMap<Graph, Node> nrmap(gr);
448 RangeIdMap<Graph, Arc> armap(gr);
450 Node n0 = gr.addNode();
451 Node n1 = gr.addNode();
452 Node n2 = gr.addNode();
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);
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");
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");
468 check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
469 check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
471 check(nrmap.size() == 3 && armap.size() == 4,
472 "Wrong RangeIdMap::size()");
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");
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");
483 check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
484 check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
488 if (nrmap[n0] == 1) nrmap.swap(n0, n2);
490 if (armap[a1] == 1) armap.swap(a1, a3);
493 check(nrmap.size() == 2 && armap.size() == 2,
494 "Wrong RangeIdMap::size()");
496 check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
497 check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
499 check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
500 check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
502 check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
503 check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
506 // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
508 typedef ListGraph Graph;
509 GRAPH_TYPEDEFS(Graph);
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> >();
519 Node n0 = gr.addNode();
520 Node n1 = gr.addNode();
521 Node n2 = gr.addNode();
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");
536 sourceMap(orienter(gr, constMap<Edge, bool>(true))),
537 targetMap(orienter(gr, constMap<Edge, bool>(false)))),
538 "Wrong SourceMap or TargetMap");
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);
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");
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");
557 typedef ListDigraph Graph;
558 DIGRAPH_TYPEDEFS(Graph);
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> >();
568 typedef CrossRefMap<Graph, Node, char> CRMap;
571 Node n0 = gr.addNode();
572 Node n1 = gr.addNode();
573 Node n2 = gr.addNode();
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()");
588 CRMap::ValueIt it = map.beginValue();
589 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
590 it == map.endValue(), "Wrong value iterator");
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()");
603 it = map.beginValue();
604 check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
605 it == map.endValue(), "Wrong value iterator");
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()");
617 it = map.beginValue();
618 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
619 it == map.endValue(), "Wrong value iterator");
624 typedef SmartDigraph Graph;
625 DIGRAPH_TYPEDEFS(Graph);
627 checkConcept<ReadWriteMap<Node, int>,
628 CrossRefMap<Graph, Node, int> >();
631 typedef CrossRefMap<Graph, Node, char> CRMap;
632 typedef CRMap::ValueIterator ValueIt;
635 Node n0 = gr.addNode();
636 Node n1 = gr.addNode();
637 Node n2 = gr.addNode();
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");
651 ValueIt it = map.beginValue();
652 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
653 it == map.endValue(), "Wrong value iterator");
658 typedef SmartGraph Graph;
659 typedef SmartGraph::Node Item;
661 typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
662 checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
667 std::vector<Item> items;
668 for (int i = 0; i < num; ++i) {
669 items.push_back(g.addNode());
674 for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
675 check(map1[static_cast<Item>(it)], "Wrong TrueIt");
678 check(n == num, "Wrong number");
681 for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
682 check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
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");
689 map1[items[5]] = true;
692 for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
693 check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
696 check(n == num, "Wrong number");
698 map1[items[num / 2]] = false;
699 check(map1[items[num / 2]] == false, "Wrong map value");
702 for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
703 check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
706 check(n == num - 1, "Wrong number");
709 for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
710 check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
713 check(n == 1, "Wrong number");
715 map1[items[0]] = false;
716 check(map1[items[0]] == false, "Wrong map value");
718 map1[items[num - 1]] = false;
719 check(map1[items[num - 1]] == false, "Wrong map value");
722 for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
723 check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
726 check(n == num - 3, "Wrong number");
727 check(map1.trueNum() == num - 3, "Wrong number");
730 for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
731 check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
734 check(n == 3, "Wrong number");
735 check(map1.falseNum() == 3, "Wrong number");
740 for(auto v: map1.items(false)) { c++; ::lemon::ignore_unused_variable_warning(v); }
741 check(c == map1.falseNum(), "Wrong number");
745 for(auto v: map1.items(true)) { c++; ::lemon::ignore_unused_variable_warning(v); }
746 check(c == map1.trueNum(), "Wrong number");
750 for(auto v: map1.falseKeys()) { c++; ::lemon::ignore_unused_variable_warning(v); }
751 check(c == map1.falseNum(), "Wrong number");
755 for(auto v: map1.trueKeys()) { c++; ::lemon::ignore_unused_variable_warning(v); }
756 check(c == map1.trueNum(), "Wrong number");
764 typedef SmartGraph Graph;
765 typedef SmartGraph::Node Item;
766 typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
768 checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
773 std::vector<Item> items;
774 for (int i = 0; i < num; ++i) {
775 items.push_back(g.addNode());
779 check(map1.size() == 0, "Wrong size");
781 for (int i = 0; i < num; ++i) {
784 check(map1.size() == num, "Wrong size");
786 for (int i = 0; i < num; ++i) {
787 Iim::ItemIt it(map1, i);
788 check(static_cast<Item>(it) == items[i], "Wrong value");
790 check(static_cast<Item>(it) == INVALID, "Wrong value");
793 for (int i = 0; i < num; ++i) {
794 map1[items[i]] = i % 2;
796 check(map1.size() == 2, "Wrong size");
799 for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
800 check(map1[static_cast<Item>(it)] == 0, "Wrong value");
803 check(n == (num + 1) / 2, "Wrong number");
805 for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
806 check(map1[static_cast<Item>(it)] == 1, "Wrong value");
809 check(n == num, "Wrong number");
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");
822 // Iterable value map
824 typedef SmartGraph Graph;
825 typedef SmartGraph::Node Item;
826 typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
828 checkConcept<ReadWriteMap<Item, double>, Ivm>();
833 std::vector<Item> items;
834 for (int i = 0; i < num; ++i) {
835 items.push_back(g.addNode());
839 check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
840 check(*map1.beginValue() == 0.0, "Wrong value");
842 for (int i = 0; i < num; ++i) {
843 map1.set(items[i], static_cast<double>(i));
845 check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
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");
851 check(static_cast<Item>(it) == INVALID, "Wrong value");
854 for (Ivm::ValueIt vit = map1.beginValue();
855 vit != map1.endValue(); ++vit) {
856 check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
860 for (int i = 0; i < num; ++i) {
861 map1.set(items[i], static_cast<double>(i % 2));
863 check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
866 for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
867 check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
870 check(n == (num + 1) / 2, "Wrong number");
872 for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
873 check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
876 check(n == num, "Wrong number");
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");
890 // Graph map utilities:
891 // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
892 // mapFind(), mapFindIf(), mapCount(), mapCountIf()
893 // mapCopy(), mapCompare(), mapFill()
895 DIGRAPH_TYPEDEFS(SmartDigraph);
898 Node n1 = g.addNode();
899 Node n2 = g.addNode();
900 Node n3 = g.addNode();
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);
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()");
919 check(mapMin(g, map2) == INVALID, "Wrong mapMin()");
920 check(mapMax(g, map2) == INVALID, "Wrong mapMax()");
922 check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
923 check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()");
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);
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()");
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()");
949 check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2,
951 check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4,
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()");
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()");
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()");
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()");
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()");
999 These 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");
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");
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()");
1021 SmartDigraph::NodeMap<int> map3(g, 0);
1022 SmartDigraph::ArcMap<char> map4(g, 'a');
1024 check(!mapCompare(g, map1, map3), "Wrong mapCompare()");
1025 check(!mapCompare(g, map2, map4), "Wrong mapCompare()");
1027 mapCopy(g, map1, map3);
1028 mapCopy(g, map2, map4);
1030 check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()");
1031 check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()");
1033 Undirector<SmartDigraph> ug(g);
1034 Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x');
1035 Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14);
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()");
1042 mapCopy(g, map2, umap1);
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()");
1049 mapCopy(g, map2, umap1);
1050 mapCopy(g, umap1, map2);
1051 mapCopy(ug, map2, umap1);
1052 mapCopy(ug, umap1, map2);
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()");
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()");
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()");