1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2008 |
---|
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 | |
---|
26 | #include "test_tools.h" |
---|
27 | |
---|
28 | using namespace lemon; |
---|
29 | using namespace lemon::concepts; |
---|
30 | |
---|
31 | struct A {}; |
---|
32 | inline bool operator<(A, A) { return true; } |
---|
33 | struct B {}; |
---|
34 | |
---|
35 | class C { |
---|
36 | int x; |
---|
37 | public: |
---|
38 | C(int _x) : x(_x) {} |
---|
39 | }; |
---|
40 | |
---|
41 | class F { |
---|
42 | public: |
---|
43 | typedef A argument_type; |
---|
44 | typedef B result_type; |
---|
45 | |
---|
46 | B operator()(const A&) const { return B(); } |
---|
47 | private: |
---|
48 | F& operator=(const F&); |
---|
49 | }; |
---|
50 | |
---|
51 | int func(A) { return 3; } |
---|
52 | |
---|
53 | int binc(int a, B) { return a+1; } |
---|
54 | |
---|
55 | typedef ReadMap<A, double> DoubleMap; |
---|
56 | typedef ReadWriteMap<A, double> DoubleWriteMap; |
---|
57 | typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; |
---|
58 | |
---|
59 | typedef ReadMap<A, bool> BoolMap; |
---|
60 | typedef ReadWriteMap<A, bool> BoolWriteMap; |
---|
61 | typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; |
---|
62 | |
---|
63 | int main() |
---|
64 | { |
---|
65 | // Map concepts |
---|
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&> >(); |
---|
74 | |
---|
75 | // NullMap |
---|
76 | { |
---|
77 | checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >(); |
---|
78 | NullMap<A,B> map1; |
---|
79 | NullMap<A,B> map2 = map1; |
---|
80 | map1 = nullMap<A,B>(); |
---|
81 | } |
---|
82 | |
---|
83 | // ConstMap |
---|
84 | { |
---|
85 | checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >(); |
---|
86 | checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >(); |
---|
87 | ConstMap<A,B> map1; |
---|
88 | ConstMap<A,B> map2 = B(); |
---|
89 | ConstMap<A,B> map3 = map1; |
---|
90 | map1 = constMap<A>(B()); |
---|
91 | map1 = constMap<A,B>(); |
---|
92 | map1.setAll(B()); |
---|
93 | ConstMap<A,C> map4(C(1)); |
---|
94 | ConstMap<A,C> map5 = map4; |
---|
95 | map4 = constMap<A>(C(2)); |
---|
96 | map4.setAll(C(3)); |
---|
97 | |
---|
98 | checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >(); |
---|
99 | check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap"); |
---|
100 | |
---|
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"); |
---|
107 | } |
---|
108 | |
---|
109 | // IdentityMap |
---|
110 | { |
---|
111 | checkConcept<ReadMap<A,A>, IdentityMap<A> >(); |
---|
112 | IdentityMap<A> map1; |
---|
113 | IdentityMap<A> map2 = map1; |
---|
114 | map1 = identityMap<A>(); |
---|
115 | |
---|
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"); |
---|
119 | } |
---|
120 | |
---|
121 | // RangeMap |
---|
122 | { |
---|
123 | checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >(); |
---|
124 | RangeMap<B> map1; |
---|
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()); |
---|
131 | |
---|
132 | checkConcept< ReferenceMap<int, double, double&, const double&>, |
---|
133 | RangeMap<double> >(); |
---|
134 | std::vector<double> v(10, 0); |
---|
135 | v[5] = 100; |
---|
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"); |
---|
140 | } |
---|
141 | |
---|
142 | // SparseMap |
---|
143 | { |
---|
144 | checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >(); |
---|
145 | SparseMap<A,B> map1; |
---|
146 | SparseMap<A,B> map2 = B(); |
---|
147 | SparseMap<A,B> map3 = sparseMap<A,B>(); |
---|
148 | SparseMap<A,B> map4 = sparseMap<A>(B()); |
---|
149 | |
---|
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); |
---|
157 | |
---|
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"); |
---|
163 | } |
---|
164 | |
---|
165 | // ComposeMap |
---|
166 | { |
---|
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>()); |
---|
171 | |
---|
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") |
---|
175 | } |
---|
176 | |
---|
177 | // CombineMap |
---|
178 | { |
---|
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>()); |
---|
183 | |
---|
184 | check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
---|
185 | "Something is wrong with CombineMap"); |
---|
186 | } |
---|
187 | |
---|
188 | // FunctorToMap, MapToFunctor |
---|
189 | { |
---|
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()]; |
---|
195 | |
---|
196 | checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >(); |
---|
197 | MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>()); |
---|
198 | |
---|
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"); |
---|
205 | } |
---|
206 | |
---|
207 | // ConvertMap |
---|
208 | { |
---|
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)); |
---|
212 | } |
---|
213 | |
---|
214 | // ForkMap |
---|
215 | { |
---|
216 | checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >(); |
---|
217 | |
---|
218 | typedef RangeMap<double> RM; |
---|
219 | typedef SparseMap<int, double> SM; |
---|
220 | RM m1(10, -1); |
---|
221 | SM m2(-1); |
---|
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); |
---|
226 | map2.set(5, 10); |
---|
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"); |
---|
229 | } |
---|
230 | |
---|
231 | // Arithmetic maps: |
---|
232 | // - AddMap, SubMap, MulMap, DivMap |
---|
233 | // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap |
---|
234 | // - NegMap, NegWriteMap, AbsMap |
---|
235 | { |
---|
236 | checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >(); |
---|
237 | checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >(); |
---|
238 | checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >(); |
---|
239 | checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >(); |
---|
240 | |
---|
241 | ConstMap<int, double> c1(1.0), c2(3.14); |
---|
242 | IdentityMap<int> im; |
---|
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"); |
---|
248 | |
---|
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> >(); |
---|
256 | |
---|
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"); |
---|
271 | } |
---|
272 | |
---|
273 | // Logical maps: |
---|
274 | // - TrueMap, FalseMap |
---|
275 | // - AndMap, OrMap |
---|
276 | // - NotMap, NotWriteMap |
---|
277 | // - EqualMap, LessMap |
---|
278 | { |
---|
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> >(); |
---|
287 | |
---|
288 | TrueMap<int> tm; |
---|
289 | FalseMap<int> fm; |
---|
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"); |
---|
298 | |
---|
299 | ConstMap<int, double> cm(2.0); |
---|
300 | IdentityMap<int> im; |
---|
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"); |
---|
306 | } |
---|
307 | |
---|
308 | // LoggerBoolMap |
---|
309 | { |
---|
310 | typedef std::vector<int> vec; |
---|
311 | vec v1; |
---|
312 | vec v2(10); |
---|
313 | LoggerBoolMap<std::back_insert_iterator<vec> > map1(std::back_inserter(v1)); |
---|
314 | LoggerBoolMap<vec::iterator> map2(v2.begin()); |
---|
315 | map1.set(10, false); |
---|
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 LoggerBoolMap"); |
---|
323 | |
---|
324 | int i = 0; |
---|
325 | for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin(); |
---|
326 | it != map2.end(); ++it ) |
---|
327 | check(v1[i++] == *it, "Something is wrong with LoggerBoolMap"); |
---|
328 | } |
---|
329 | |
---|
330 | return 0; |
---|
331 | } |
---|