COIN-OR::LEMON - Graph Library

source: lemon-1.0/test/maps_test.cc

1.0 r1.0.7
Last change on this file was 427:c59bdcc8e33e, checked in by Alpar Juttner <alpar@…>, 8 years ago

Unify sources

File size: 11.8 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2011
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
28using namespace lemon;
29using namespace lemon::concepts;
30
31struct A {};
32inline bool operator<(A, A) { return true; }
33struct B {};
34
35class C {
36  int x;
37public:
38  C(int _x) : x(_x) {}
39};
40
41class F {
42public:
43  typedef A argument_type;
44  typedef B result_type;
45
46  B operator()(const A&) const { return B(); }
47private:
48  F& operator=(const F&);
49};
50
51int func(A) { return 3; }
52
53int binc(int a, B) { return a+1; }
54
55typedef ReadMap<A, double> DoubleMap;
56typedef ReadWriteMap<A, double> DoubleWriteMap;
57typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
58
59typedef ReadMap<A, bool> BoolMap;
60typedef ReadWriteMap<A, bool> BoolWriteMap;
61typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
62
63int 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&>,
73               ReferenceMap<A,B,B&,const B&> >();
74  checkConcept<ReferenceMap<A,C,C&,const C&>,
75               ReferenceMap<A,C,C&,const C&> >();
76
77  // NullMap
78  {
79    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
80    NullMap<A,B> map1;
81    NullMap<A,B> map2 = map1;
82    map1 = nullMap<A,B>();
83  }
84
85  // ConstMap
86  {
87    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
88    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
89    ConstMap<A,B> map1;
90    ConstMap<A,B> map2 = B();
91    ConstMap<A,B> map3 = map1;
92    map1 = constMap<A>(B());
93    map1 = constMap<A,B>();
94    map1.setAll(B());
95    ConstMap<A,C> map4(C(1));
96    ConstMap<A,C> map5 = map4;
97    map4 = constMap<A>(C(2));
98    map4.setAll(C(3));
99
100    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
101    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
102
103    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
104    ConstMap<A,Const<int,10> > map6;
105    ConstMap<A,Const<int,10> > map7 = map6;
106    map6 = constMap<A,int,10>();
107    map7 = constMap<A,Const<int,10> >();
108    check(map6[A()] == 10 && map7[A()] == 10,
109          "Something is wrong with ConstMap");
110  }
111
112  // IdentityMap
113  {
114    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
115    IdentityMap<A> map1;
116    IdentityMap<A> map2 = map1;
117    map1 = identityMap<A>();
118
119    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
120    check(identityMap<double>()[1.0] == 1.0 &&
121          identityMap<double>()[3.14] == 3.14,
122          "Something is wrong with IdentityMap");
123  }
124
125  // RangeMap
126  {
127    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
128    RangeMap<B> map1;
129    RangeMap<B> map2(10);
130    RangeMap<B> map3(10,B());
131    RangeMap<B> map4 = map1;
132    RangeMap<B> map5 = rangeMap<B>();
133    RangeMap<B> map6 = rangeMap<B>(10);
134    RangeMap<B> map7 = rangeMap(10,B());
135
136    checkConcept< ReferenceMap<int, double, double&, const double&>,
137                  RangeMap<double> >();
138    std::vector<double> v(10, 0);
139    v[5] = 100;
140    RangeMap<double> map8(v);
141    RangeMap<double> map9 = rangeMap(v);
142    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
143          "Something is wrong with RangeMap");
144  }
145
146  // SparseMap
147  {
148    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
149    SparseMap<A,B> map1;
150    SparseMap<A,B> map2 = B();
151    SparseMap<A,B> map3 = sparseMap<A,B>();
152    SparseMap<A,B> map4 = sparseMap<A>(B());
153
154    checkConcept< ReferenceMap<double, int, int&, const int&>,
155                  SparseMap<double, int> >();
156    std::map<double, int> m;
157    SparseMap<double, int> map5(m);
158    SparseMap<double, int> map6(m,10);
159    SparseMap<double, int> map7 = sparseMap(m);
160    SparseMap<double, int> map8 = sparseMap(m,10);
161
162    check(map5[1.0] == 0 && map5[3.14] == 0 &&
163          map6[1.0] == 10 && map6[3.14] == 10,
164          "Something is wrong with SparseMap");
165    map5[1.0] = map6[3.14] = 100;
166    check(map5[1.0] == 100 && map5[3.14] == 0 &&
167          map6[1.0] == 10 && map6[3.14] == 100,
168          "Something is wrong with SparseMap");
169  }
170
171  // ComposeMap
172  {
173    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
174    checkConcept<ReadMap<B,double>, CompMap>();
175    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
176    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
177
178    SparseMap<double, bool> m1(false); m1[3.14] = true;
179    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
180    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
181          "Something is wrong with ComposeMap")
182  }
183
184  // CombineMap
185  {
186    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
187    checkConcept<ReadMap<A,double>, CombMap>();
188    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
189    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
190
191    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
192          "Something is wrong with CombineMap");
193  }
194
195  // FunctorToMap, MapToFunctor
196  {
197    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
198    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
199    FunctorToMap<F> map1;
200    FunctorToMap<F> map2 = FunctorToMap<F>(F());
201    B b = functorToMap(F())[A()];
202
203    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
204    MapToFunctor<ReadMap<A,B> > map =
205      MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
206
207    check(functorToMap(&func)[A()] == 3,
208          "Something is wrong with FunctorToMap");
209    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
210          "Something is wrong with MapToFunctor");
211    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
212          mapToFunctor(functorToMap(&func))[A()] == 3,
213          "Something is wrong with FunctorToMap or MapToFunctor");
214    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
215          "Something is wrong with FunctorToMap or MapToFunctor");
216  }
217
218  // ConvertMap
219  {
220    checkConcept<ReadMap<double,double>,
221      ConvertMap<ReadMap<double, int>, double> >();
222    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
223    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
224  }
225
226  // ForkMap
227  {
228    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
229
230    typedef RangeMap<double> RM;
231    typedef SparseMap<int, double> SM;
232    RM m1(10, -1);
233    SM m2(-1);
234    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
235    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
236    ForkMap<RM, SM> map1(m1,m2);
237    ForkMap<SM, RM> map2 = forkMap(m2,m1);
238    map2.set(5, 10);
239    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
240          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
241          "Something is wrong with ForkMap");
242  }
243
244  // Arithmetic maps:
245  // - AddMap, SubMap, MulMap, DivMap
246  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
247  // - NegMap, NegWriteMap, AbsMap
248  {
249    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
250    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
251    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
252    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
253
254    ConstMap<int, double> c1(1.0), c2(3.14);
255    IdentityMap<int> im;
256    ConvertMap<IdentityMap<int>, double> id(im);
257    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
258          "Something is wrong with AddMap");
259    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
260          "Something is wrong with SubMap");
261    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
262          "Something is wrong with MulMap");
263    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
264          "Something is wrong with DivMap");
265
266    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
267    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
268    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
269    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
270    checkConcept<DoubleMap, NegMap<DoubleMap> >();
271    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
272    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
273
274    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
275          "Something is wrong with ShiftMap");
276    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
277          shiftWriteMap(id, 2.0)[10] == 12.0,
278          "Something is wrong with ShiftWriteMap");
279    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
280          "Something is wrong with ScaleMap");
281    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
282          scaleWriteMap(id, 2.0)[10] == 20.0,
283          "Something is wrong with ScaleWriteMap");
284    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
285          "Something is wrong with NegMap");
286    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
287          "Something is wrong with NegWriteMap");
288    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
289          "Something is wrong with AbsMap");
290  }
291
292  // Logical maps:
293  // - TrueMap, FalseMap
294  // - AndMap, OrMap
295  // - NotMap, NotWriteMap
296  // - EqualMap, LessMap
297  {
298    checkConcept<BoolMap, TrueMap<A> >();
299    checkConcept<BoolMap, FalseMap<A> >();
300    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
301    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
302    checkConcept<BoolMap, NotMap<BoolMap> >();
303    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
304    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
305    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
306
307    TrueMap<int> tm;
308    FalseMap<int> fm;
309    RangeMap<bool> rm(2);
310    rm[0] = true; rm[1] = false;
311    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
312          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
313          "Something is wrong with AndMap");
314    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
315          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
316          "Something is wrong with OrMap");
317    check(!notMap(rm)[0] && notMap(rm)[1],
318          "Something is wrong with NotMap");
319    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
320          "Something is wrong with NotWriteMap");
321
322    ConstMap<int, double> cm(2.0);
323    IdentityMap<int> im;
324    ConvertMap<IdentityMap<int>, double> id(im);
325    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
326          "Something is wrong with LessMap");
327    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
328          "Something is wrong with EqualMap");
329  }
330
331  // LoggerBoolMap
332  {
333    typedef std::vector<int> vec;
334    vec v1;
335    vec v2(10);
336    LoggerBoolMap<std::back_insert_iterator<vec> >
337      map1(std::back_inserter(v1));
338    LoggerBoolMap<vec::iterator> map2(v2.begin());
339    map1.set(10, false);
340    map1.set(20, true);   map2.set(20, true);
341    map1.set(30, false);  map2.set(40, false);
342    map1.set(50, true);   map2.set(50, true);
343    map1.set(60, true);   map2.set(60, true);
344    check(v1.size() == 3 && v2.size() == 10 &&
345          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
346          v2[0]==20 && v2[1]==50 && v2[2]==60,
347          "Something is wrong with LoggerBoolMap");
348
349    int i = 0;
350    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
351          it != map2.end(); ++it )
352      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
353  }
354
355  return 0;
356}
Note: See TracBrowser for help on using the repository browser.