1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2011
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>
27 #include "test_tools.h"
29 using namespace lemon;
30 using namespace lemon::concepts;
33 inline bool operator<(A, A) { return true; }
44 typedef A argument_type;
45 typedef B result_type;
47 B operator()(const A&) const { return B(); }
49 F& operator=(const F&);
52 int func(A) { return 3; }
54 int binc(int a, B) { return a+1; }
56 typedef ReadMap<A, double> DoubleMap;
57 typedef ReadWriteMap<A, double> DoubleWriteMap;
58 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
60 typedef ReadMap<A, bool> BoolMap;
61 typedef ReadWriteMap<A, bool> BoolWriteMap;
62 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
67 checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
68 checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
69 checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
70 checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
71 checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
72 checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
73 checkConcept<ReferenceMap<A,B,B&,const B&>,
74 ReferenceMap<A,B,B&,const B&> >();
75 checkConcept<ReferenceMap<A,C,C&,const C&>,
76 ReferenceMap<A,C,C&,const C&> >();
80 checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
82 NullMap<A,B> map2 = map1;
83 map1 = nullMap<A,B>();
88 checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
89 checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
91 ConstMap<A,B> map2 = B();
92 ConstMap<A,B> map3 = map1;
93 map1 = constMap<A>(B());
94 map1 = constMap<A,B>();
96 ConstMap<A,C> map4(C(1));
97 ConstMap<A,C> map5 = map4;
98 map4 = constMap<A>(C(2));
101 checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
102 check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
104 checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
105 ConstMap<A,Const<int,10> > map6;
106 ConstMap<A,Const<int,10> > map7 = map6;
107 map6 = constMap<A,int,10>();
108 map7 = constMap<A,Const<int,10> >();
109 check(map6[A()] == 10 && map7[A()] == 10,
110 "Something is wrong with ConstMap");
115 checkConcept<ReadMap<A,A>, IdentityMap<A> >();
117 IdentityMap<A> map2 = map1;
118 map1 = identityMap<A>();
120 checkConcept<ReadMap<double,double>, IdentityMap<double> >();
121 check(identityMap<double>()[1.0] == 1.0 &&
122 identityMap<double>()[3.14] == 3.14,
123 "Something is wrong with IdentityMap");
128 checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
130 RangeMap<B> map2(10);
131 RangeMap<B> map3(10,B());
132 RangeMap<B> map4 = map1;
133 RangeMap<B> map5 = rangeMap<B>();
134 RangeMap<B> map6 = rangeMap<B>(10);
135 RangeMap<B> map7 = rangeMap(10,B());
137 checkConcept< ReferenceMap<int, double, double&, const double&>,
138 RangeMap<double> >();
139 std::vector<double> v(10, 0);
141 RangeMap<double> map8(v);
142 RangeMap<double> map9 = rangeMap(v);
143 check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
144 "Something is wrong with RangeMap");
149 checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
151 SparseMap<A,B> map2 = B();
152 SparseMap<A,B> map3 = sparseMap<A,B>();
153 SparseMap<A,B> map4 = sparseMap<A>(B());
155 checkConcept< ReferenceMap<double, int, int&, const int&>,
156 SparseMap<double, int> >();
157 std::map<double, int> m;
158 SparseMap<double, int> map5(m);
159 SparseMap<double, int> map6(m,10);
160 SparseMap<double, int> map7 = sparseMap(m);
161 SparseMap<double, int> map8 = sparseMap(m,10);
163 check(map5[1.0] == 0 && map5[3.14] == 0 &&
164 map6[1.0] == 10 && map6[3.14] == 10,
165 "Something is wrong with SparseMap");
166 map5[1.0] = map6[3.14] = 100;
167 check(map5[1.0] == 100 && map5[3.14] == 0 &&
168 map6[1.0] == 10 && map6[3.14] == 100,
169 "Something is wrong with SparseMap");
174 typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
175 checkConcept<ReadMap<B,double>, CompMap>();
176 CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
177 CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
179 SparseMap<double, bool> m1(false); m1[3.14] = true;
180 RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
181 check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
182 "Something is wrong with ComposeMap")
187 typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
188 checkConcept<ReadMap<A,double>, CombMap>();
189 CombMap map1 = CombMap(DoubleMap(), DoubleMap());
190 CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
192 check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
193 "Something is wrong with CombineMap");
196 // FunctorToMap, MapToFunctor
198 checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
199 checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
200 FunctorToMap<F> map1;
201 FunctorToMap<F> map2 = FunctorToMap<F>(F());
202 B b = functorToMap(F())[A()];
204 checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
205 MapToFunctor<ReadMap<A,B> > map =
206 MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
208 check(functorToMap(&func)[A()] == 3,
209 "Something is wrong with FunctorToMap");
210 check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
211 "Something is wrong with MapToFunctor");
212 check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
213 mapToFunctor(functorToMap(&func))[A()] == 3,
214 "Something is wrong with FunctorToMap or MapToFunctor");
215 check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
216 "Something is wrong with FunctorToMap or MapToFunctor");
221 checkConcept<ReadMap<double,double>,
222 ConvertMap<ReadMap<double, int>, double> >();
223 ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
224 ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
229 checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
231 typedef RangeMap<double> RM;
232 typedef SparseMap<int, double> SM;
235 checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
236 checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
237 ForkMap<RM, SM> map1(m1,m2);
238 ForkMap<SM, RM> map2 = forkMap(m2,m1);
240 check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
241 m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
242 "Something is wrong with ForkMap");
246 // - AddMap, SubMap, MulMap, DivMap
247 // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
248 // - NegMap, NegWriteMap, AbsMap
250 checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
251 checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
252 checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
253 checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
255 ConstMap<int, double> c1(1.0), c2(3.14);
257 ConvertMap<IdentityMap<int>, double> id(im);
258 check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0,
259 "Something is wrong with AddMap");
260 check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
261 "Something is wrong with SubMap");
262 check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28,
263 "Something is wrong with MulMap");
264 check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57,
265 "Something is wrong with DivMap");
267 checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
268 checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
269 checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
270 checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
271 checkConcept<DoubleMap, NegMap<DoubleMap> >();
272 checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
273 checkConcept<DoubleMap, AbsMap<DoubleMap> >();
275 check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
276 "Something is wrong with ShiftMap");
277 check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
278 shiftWriteMap(id, 2.0)[10] == 12.0,
279 "Something is wrong with ShiftWriteMap");
280 check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
281 "Something is wrong with ScaleMap");
282 check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
283 scaleWriteMap(id, 2.0)[10] == 20.0,
284 "Something is wrong with ScaleWriteMap");
285 check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
286 "Something is wrong with NegMap");
287 check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
288 "Something is wrong with NegWriteMap");
289 check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
290 "Something is wrong with AbsMap");
294 // - TrueMap, FalseMap
296 // - NotMap, NotWriteMap
297 // - EqualMap, LessMap
299 checkConcept<BoolMap, TrueMap<A> >();
300 checkConcept<BoolMap, FalseMap<A> >();
301 checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
302 checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
303 checkConcept<BoolMap, NotMap<BoolMap> >();
304 checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
305 checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
306 checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
310 RangeMap<bool> rm(2);
311 rm[0] = true; rm[1] = false;
312 check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
313 !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
314 "Something is wrong with AndMap");
315 check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
316 orMap(fm,rm)[0] && !orMap(fm,rm)[1],
317 "Something is wrong with OrMap");
318 check(!notMap(rm)[0] && notMap(rm)[1],
319 "Something is wrong with NotMap");
320 check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
321 "Something is wrong with NotWriteMap");
323 ConstMap<int, double> cm(2.0);
325 ConvertMap<IdentityMap<int>, double> id(im);
326 check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
327 "Something is wrong with LessMap");
328 check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
329 "Something is wrong with EqualMap");
334 typedef std::vector<int> vec;
337 LoggerBoolMap<std::back_insert_iterator<vec> >
338 map1(std::back_inserter(v1));
339 LoggerBoolMap<vec::iterator> map2(v2.begin());
341 map1.set(20, true); map2.set(20, true);
342 map1.set(30, false); map2.set(40, false);
343 map1.set(50, true); map2.set(50, true);
344 map1.set(60, true); map2.set(60, true);
345 check(v1.size() == 3 && v2.size() == 10 &&
346 v1[0]==20 && v1[1]==50 && v1[2]==60 &&
347 v2[0]==20 && v2[1]==50 && v2[2]==60,
348 "Something is wrong with LoggerBoolMap");
351 for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
352 it != map2.end(); ++it )
353 check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
358 typedef ListDigraph Graph;
359 DIGRAPH_TYPEDEFS(Graph);
361 checkConcept<ReadWriteMap<Node, int>,
362 CrossRefMap<Graph, Node, int> >();
365 typedef CrossRefMap<Graph, Node, char> CRMap;
366 typedef CRMap::ValueIterator ValueIt;
369 Node n0 = gr.addNode();
370 Node n1 = gr.addNode();
371 Node n2 = gr.addNode();
379 check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
380 "Wrong CrossRefMap");
381 check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
382 check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
383 check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
385 ValueIt it = map.beginValue();
386 check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
387 it == map.endValue(), "Wrong value iterator");