3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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, "Something is wrong with ConstMap");
111 checkConcept<ReadMap<A,A>, IdentityMap<A> >();
113 IdentityMap<A> map2 = map1;
114 map1 = identityMap<A>();
116 checkConcept<ReadMap<double,double>, IdentityMap<double> >();
117 check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
118 "Something is wrong with IdentityMap");
123 checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
125 RangeMap<B> map2(10);
126 RangeMap<B> map3(10,B());
127 RangeMap<B> map4 = map1;
128 RangeMap<B> map5 = rangeMap<B>();
129 RangeMap<B> map6 = rangeMap<B>(10);
130 RangeMap<B> map7 = rangeMap(10,B());
132 checkConcept< ReferenceMap<int, double, double&, const double&>,
133 RangeMap<double> >();
134 std::vector<double> v(10, 0);
136 RangeMap<double> map8(v);
137 RangeMap<double> map9 = rangeMap(v);
138 check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
139 "Something is wrong with RangeMap");
144 checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
146 SparseMap<A,B> map2 = B();
147 SparseMap<A,B> map3 = sparseMap<A,B>();
148 SparseMap<A,B> map4 = sparseMap<A>(B());
150 checkConcept< ReferenceMap<double, int, int&, const int&>,
151 SparseMap<double, int> >();
152 std::map<double, int> m;
153 SparseMap<double, int> map5(m);
154 SparseMap<double, int> map6(m,10);
155 SparseMap<double, int> map7 = sparseMap(m);
156 SparseMap<double, int> map8 = sparseMap(m,10);
158 check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
159 "Something is wrong with SparseMap");
160 map5[1.0] = map6[3.14] = 100;
161 check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
162 "Something is wrong with SparseMap");
167 typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
168 checkConcept<ReadMap<B,double>, CompMap>();
169 CompMap map1(DoubleMap(),ReadMap<B,A>());
170 CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
172 SparseMap<double, bool> m1(false); m1[3.14] = true;
173 RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
174 check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
179 typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
180 checkConcept<ReadMap<A,double>, CombMap>();
181 CombMap map1(DoubleMap(), DoubleMap());
182 CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
184 check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
185 "Something is wrong with CombineMap");
188 // FunctorToMap, MapToFunctor
190 checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
191 checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
192 FunctorToMap<F> map1;
193 FunctorToMap<F> map2(F());
194 B b = functorToMap(F())[A()];
196 checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
197 MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
199 check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
200 check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
201 check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
202 "Something is wrong with FunctorToMap or MapToFunctor");
203 check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
204 "Something is wrong with FunctorToMap or MapToFunctor");
209 checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
210 ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
211 ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
216 checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
218 typedef RangeMap<double> RM;
219 typedef SparseMap<int, double> SM;
222 checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
223 checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
224 ForkMap<RM, SM> map1(m1,m2);
225 ForkMap<SM, RM> map2 = forkMap(m2,m1);
227 check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
228 "Something is wrong with ForkMap");
232 // - AddMap, SubMap, MulMap, DivMap
233 // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
234 // - NegMap, NegWriteMap, AbsMap
236 checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
237 checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
238 checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
239 checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
241 ConstMap<int, double> c1(1.0), c2(3.14);
243 ConvertMap<IdentityMap<int>, double> id(im);
244 check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
245 check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0, "Something is wrong with SubMap");
246 check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28, "Something is wrong with MulMap");
247 check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57, "Something is wrong with DivMap");
249 checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
250 checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
251 checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
252 checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
253 checkConcept<DoubleMap, NegMap<DoubleMap> >();
254 checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
255 checkConcept<DoubleMap, AbsMap<DoubleMap> >();
257 check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
258 "Something is wrong with ShiftMap");
259 check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
260 "Something is wrong with ShiftWriteMap");
261 check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
262 "Something is wrong with ScaleMap");
263 check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
264 "Something is wrong with ScaleWriteMap");
265 check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
266 "Something is wrong with NegMap");
267 check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
268 "Something is wrong with NegWriteMap");
269 check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
270 "Something is wrong with AbsMap");
274 // - TrueMap, FalseMap
276 // - NotMap, NotWriteMap
277 // - EqualMap, LessMap
279 checkConcept<BoolMap, TrueMap<A> >();
280 checkConcept<BoolMap, FalseMap<A> >();
281 checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
282 checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
283 checkConcept<BoolMap, NotMap<BoolMap> >();
284 checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
285 checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
286 checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
290 RangeMap<bool> rm(2);
291 rm[0] = true; rm[1] = false;
292 check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
293 "Something is wrong with AndMap");
294 check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
295 "Something is wrong with OrMap");
296 check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
297 check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
299 ConstMap<int, double> cm(2.0);
301 ConvertMap<IdentityMap<int>, double> id(im);
302 check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
303 "Something is wrong with LessMap");
304 check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
305 "Something is wrong with EqualMap");
310 typedef std::vector<int> vec;
313 StoreBoolMap<std::back_insert_iterator<vec> > map1(std::back_inserter(v1));
314 StoreBoolMap<vec::iterator> map2(v2.begin());
316 map1.set(20, true); map2.set(20, true);
317 map1.set(30, false); map2.set(40, false);
318 map1.set(50, true); map2.set(50, true);
319 map1.set(60, true); map2.set(60, true);
320 check(v1.size() == 3 && v2.size() == 10 &&
321 v1[0]==20 && v1[1]==50 && v1[2]==60 && v2[0]==20 && v2[1]==50 && v2[2]==60,
322 "Something is wrong with StoreBoolMap");
325 for ( StoreBoolMap<vec::iterator>::Iterator it = map2.begin();
326 it != map2.end(); ++it )
327 check(v1[i++] == *it, "Something is wrong with StoreBoolMap");