1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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>
26 #include "test_tools.h"
28 using namespace lemon;
29 using namespace lemon::concepts;
32 inline bool operator<(A, A) { return true; }
43 typedef A argument_type;
44 typedef B result_type;
46 B operator()(const A&) const { return B(); }
48 F& operator=(const F&);
51 int func(A) { return 3; }
53 int binc(int a, B) { return a+1; }
55 typedef ReadMap<A, double> DoubleMap;
56 typedef ReadWriteMap<A, double> DoubleWriteMap;
57 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
59 typedef ReadMap<A, bool> BoolMap;
60 typedef ReadWriteMap<A, bool> BoolWriteMap;
61 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
66 checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
67 checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
68 checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
69 checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
70 checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
71 checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
72 checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
73 checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
77 checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
79 NullMap<A,B> map2 = map1;
80 map1 = nullMap<A,B>();
85 checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
86 checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
88 ConstMap<A,B> map2 = B();
89 ConstMap<A,B> map3 = map1;
90 map1 = constMap<A>(B());
91 map1 = constMap<A,B>();
93 ConstMap<A,C> map4(C(1));
94 ConstMap<A,C> map5 = map4;
95 map4 = constMap<A>(C(2));
98 checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
99 check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
101 checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
102 ConstMap<A,Const<int,10> > map6;
103 ConstMap<A,Const<int,10> > map7 = map6;
104 map6 = constMap<A,int,10>();
105 map7 = constMap<A,Const<int,10> >();
106 check(map6[A()] == 10 && map7[A()] == 10,
107 "Something is wrong with ConstMap");
112 checkConcept<ReadMap<A,A>, IdentityMap<A> >();
114 IdentityMap<A> map2 = map1;
115 map1 = identityMap<A>();
117 checkConcept<ReadMap<double,double>, IdentityMap<double> >();
118 check(identityMap<double>()[1.0] == 1.0 &&
119 identityMap<double>()[3.14] == 3.14,
120 "Something is wrong with IdentityMap");
125 checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
127 RangeMap<B> map2(10);
128 RangeMap<B> map3(10,B());
129 RangeMap<B> map4 = map1;
130 RangeMap<B> map5 = rangeMap<B>();
131 RangeMap<B> map6 = rangeMap<B>(10);
132 RangeMap<B> map7 = rangeMap(10,B());
134 checkConcept< ReferenceMap<int, double, double&, const double&>,
135 RangeMap<double> >();
136 std::vector<double> v(10, 0);
138 RangeMap<double> map8(v);
139 RangeMap<double> map9 = rangeMap(v);
140 check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
141 "Something is wrong with RangeMap");
146 checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
148 SparseMap<A,B> map2 = B();
149 SparseMap<A,B> map3 = sparseMap<A,B>();
150 SparseMap<A,B> map4 = sparseMap<A>(B());
152 checkConcept< ReferenceMap<double, int, int&, const int&>,
153 SparseMap<double, int> >();
154 std::map<double, int> m;
155 SparseMap<double, int> map5(m);
156 SparseMap<double, int> map6(m,10);
157 SparseMap<double, int> map7 = sparseMap(m);
158 SparseMap<double, int> map8 = sparseMap(m,10);
160 check(map5[1.0] == 0 && map5[3.14] == 0 &&
161 map6[1.0] == 10 && map6[3.14] == 10,
162 "Something is wrong with SparseMap");
163 map5[1.0] = map6[3.14] = 100;
164 check(map5[1.0] == 100 && map5[3.14] == 0 &&
165 map6[1.0] == 10 && map6[3.14] == 100,
166 "Something is wrong with SparseMap");
171 typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
172 checkConcept<ReadMap<B,double>, CompMap>();
173 CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
174 CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
176 SparseMap<double, bool> m1(false); m1[3.14] = true;
177 RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
178 check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
179 "Something is wrong with ComposeMap")
184 typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
185 checkConcept<ReadMap<A,double>, CombMap>();
186 CombMap map1 = CombMap(DoubleMap(), DoubleMap());
187 CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
189 check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
190 "Something is wrong with CombineMap");
193 // FunctorToMap, MapToFunctor
195 checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
196 checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
197 FunctorToMap<F> map1;
198 FunctorToMap<F> map2 = FunctorToMap<F>(F());
199 B b = functorToMap(F())[A()];
201 checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
202 MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
204 check(functorToMap(&func)[A()] == 3,
205 "Something is wrong with FunctorToMap");
206 check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
207 "Something is wrong with MapToFunctor");
208 check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
209 mapToFunctor(functorToMap(&func))[A()] == 3,
210 "Something is wrong with FunctorToMap or MapToFunctor");
211 check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
212 "Something is wrong with FunctorToMap or MapToFunctor");
217 checkConcept<ReadMap<double,double>,
218 ConvertMap<ReadMap<double, int>, double> >();
219 ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
220 ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
225 checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
227 typedef RangeMap<double> RM;
228 typedef SparseMap<int, double> SM;
231 checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
232 checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
233 ForkMap<RM, SM> map1(m1,m2);
234 ForkMap<SM, RM> map2 = forkMap(m2,m1);
236 check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
237 m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
238 "Something is wrong with ForkMap");
242 // - AddMap, SubMap, MulMap, DivMap
243 // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
244 // - NegMap, NegWriteMap, AbsMap
246 checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
247 checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
248 checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
249 checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
251 ConstMap<int, double> c1(1.0), c2(3.14);
253 ConvertMap<IdentityMap<int>, double> id(im);
254 check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0,
255 "Something is wrong with AddMap");
256 check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
257 "Something is wrong with SubMap");
258 check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28,
259 "Something is wrong with MulMap");
260 check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57,
261 "Something is wrong with DivMap");
263 checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
264 checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
265 checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
266 checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
267 checkConcept<DoubleMap, NegMap<DoubleMap> >();
268 checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
269 checkConcept<DoubleMap, AbsMap<DoubleMap> >();
271 check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
272 "Something is wrong with ShiftMap");
273 check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
274 shiftWriteMap(id, 2.0)[10] == 12.0,
275 "Something is wrong with ShiftWriteMap");
276 check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
277 "Something is wrong with ScaleMap");
278 check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
279 scaleWriteMap(id, 2.0)[10] == 20.0,
280 "Something is wrong with ScaleWriteMap");
281 check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
282 "Something is wrong with NegMap");
283 check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
284 "Something is wrong with NegWriteMap");
285 check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
286 "Something is wrong with AbsMap");
290 // - TrueMap, FalseMap
292 // - NotMap, NotWriteMap
293 // - EqualMap, LessMap
295 checkConcept<BoolMap, TrueMap<A> >();
296 checkConcept<BoolMap, FalseMap<A> >();
297 checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
298 checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
299 checkConcept<BoolMap, NotMap<BoolMap> >();
300 checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
301 checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
302 checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
306 RangeMap<bool> rm(2);
307 rm[0] = true; rm[1] = false;
308 check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
309 !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
310 "Something is wrong with AndMap");
311 check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
312 orMap(fm,rm)[0] && !orMap(fm,rm)[1],
313 "Something is wrong with OrMap");
314 check(!notMap(rm)[0] && notMap(rm)[1],
315 "Something is wrong with NotMap");
316 check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
317 "Something is wrong with NotWriteMap");
319 ConstMap<int, double> cm(2.0);
321 ConvertMap<IdentityMap<int>, double> id(im);
322 check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
323 "Something is wrong with LessMap");
324 check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
325 "Something is wrong with EqualMap");
330 typedef std::vector<int> vec;
333 LoggerBoolMap<std::back_insert_iterator<vec> >
334 map1(std::back_inserter(v1));
335 LoggerBoolMap<vec::iterator> map2(v2.begin());
337 map1.set(20, true); map2.set(20, true);
338 map1.set(30, false); map2.set(40, false);
339 map1.set(50, true); map2.set(50, true);
340 map1.set(60, true); map2.set(60, true);
341 check(v1.size() == 3 && v2.size() == 10 &&
342 v1[0]==20 && v1[1]==50 && v1[2]==60 &&
343 v2[0]==20 && v2[1]==50 && v2[2]==60,
344 "Something is wrong with LoggerBoolMap");
347 for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
348 it != map2.end(); ++it )
349 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
354 typedef SmartGraph Graph;
355 typedef SmartGraph::Node Item;
357 typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
358 checkConcept<ReadWriteMap<Item, int>, Ibm>();
362 std::vector<Item> items;
363 for (int i = 0; i < num; ++i) {
364 items.push_back(g.addNode());
369 for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
370 check(map1[static_cast<Item>(it)], "Wrong TrueIt");
373 check(n == num, "Wrong number");
376 for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
377 check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
380 check(n == num, "Wrong number");
381 check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
382 check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
384 map1[items[5]] = true;
387 for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
388 check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
391 check(n == num, "Wrong number");
393 map1[items[num / 2]] = false;
394 check(map1[items[num / 2]] == false, "Wrong map value");
397 for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
398 check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
401 check(n == num - 1, "Wrong number");
404 for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
405 check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
408 check(n == 1, "Wrong number");
410 map1[items[0]] = false;
411 check(map1[items[0]] == false, "Wrong map value");
413 map1[items[num - 1]] = false;
414 check(map1[items[num - 1]] == false, "Wrong map value");
417 for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
418 check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
421 check(n == num - 3, "Wrong number");
422 check(map1.trueNum() == num - 3, "Wrong number");
425 for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
426 check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
429 check(n == 3, "Wrong number");
430 check(map1.falseNum() == 3, "Wrong number");
435 typedef SmartGraph Graph;
436 typedef SmartGraph::Node Item;
437 typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
439 checkConcept<ReadWriteMap<Item, int>, Iim>();
443 std::vector<Item> items;
444 for (int i = 0; i < num; ++i) {
445 items.push_back(g.addNode());
449 check(map1.size() == 0, "Wrong size");
451 for (int i = 0; i < num; ++i) {
454 check(map1.size() == num, "Wrong size");
456 for (int i = 0; i < num; ++i) {
457 Iim::ItemIt it(map1, i);
458 check(static_cast<Item>(it) == items[i], "Wrong value");
460 check(static_cast<Item>(it) == INVALID, "Wrong value");
463 for (int i = 0; i < num; ++i) {
464 map1[items[i]] = i % 2;
466 check(map1.size() == 2, "Wrong size");
469 for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
470 check(map1[static_cast<Item>(it)] == 0, "Wrong Value");
473 check(n == (num + 1) / 2, "Wrong number");
475 for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
476 check(map1[static_cast<Item>(it)] == 1, "Wrong Value");
479 check(n == num, "Wrong number");
483 // Iterable value map
485 typedef SmartGraph Graph;
486 typedef SmartGraph::Node Item;
487 typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
489 checkConcept<ReadWriteMap<Item, double>, Ivm>();
493 std::vector<Item> items;
494 for (int i = 0; i < num; ++i) {
495 items.push_back(g.addNode());
499 check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
500 check(*map1.beginValue() == 0.0, "Wrong value");
502 for (int i = 0; i < num; ++i) {
503 map1.set(items[i], static_cast<double>(i));
505 check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
507 for (int i = 0; i < num; ++i) {
508 Ivm::ItemIt it(map1, static_cast<double>(i));
509 check(static_cast<Item>(it) == items[i], "Wrong value");
511 check(static_cast<Item>(it) == INVALID, "Wrong value");
514 for (Ivm::ValueIterator vit = map1.beginValue();
515 vit != map1.endValue(); ++vit) {
516 check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
517 "Wrong ValueIterator");
520 for (int i = 0; i < num; ++i) {
521 map1.set(items[i], static_cast<double>(i % 2));
523 check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
526 for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
527 check(map1[static_cast<Item>(it)] == 0.0, "Wrong Value");
530 check(n == (num + 1) / 2, "Wrong number");
532 for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
533 check(map1[static_cast<Item>(it)] == 1.0, "Wrong Value");
536 check(n == num, "Wrong number");