COIN-OR::LEMON - Graph Library

source: lemon/test/maps_test.cc @ 1097:74e2dac774c8

1.1 r1.1.4
Last change on this file since 1097:74e2dac774c8 was 1081:f1398882a928, checked in by Alpar Juttner <alpar@…>, 9 years ago

Unify sources

File size: 12.8 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[25]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[25]4 *
[1081]5 * Copyright (C) 2003-2011
[25]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>
[731]25#include <lemon/list_graph.h>
[25]26
27#include "test_tools.h"
28
29using namespace lemon;
30using namespace lemon::concepts;
31
32struct A {};
33inline bool operator<(A, A) { return true; }
34struct B {};
35
[94]36class C {
37  int x;
38public:
39  C(int _x) : x(_x) {}
40};
41
[25]42class F {
43public:
44  typedef A argument_type;
45  typedef B result_type;
46
[80]47  B operator()(const A&) const { return B(); }
48private:
49  F& operator=(const F&);
[25]50};
51
[80]52int func(A) { return 3; }
[25]53
[80]54int binc(int a, B) { return a+1; }
[25]55
[80]56typedef ReadMap<A, double> DoubleMap;
57typedef ReadWriteMap<A, double> DoubleWriteMap;
58typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
[25]59
[80]60typedef ReadMap<A, bool> BoolMap;
[25]61typedef ReadWriteMap<A, bool> BoolWriteMap;
[80]62typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
[25]63
64int main()
[80]65{
66  // Map concepts
[25]67  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
[94]68  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
[25]69  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
[94]70  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
[25]71  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
[94]72  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
[1081]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&> >();
[25]77
[80]78  // NullMap
79  {
80    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
81    NullMap<A,B> map1;
82    NullMap<A,B> map2 = map1;
83    map1 = nullMap<A,B>();
84  }
85
86  // ConstMap
87  {
88    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
[123]89    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
[80]90    ConstMap<A,B> map1;
[136]91    ConstMap<A,B> map2 = B();
[80]92    ConstMap<A,B> map3 = map1;
93    map1 = constMap<A>(B());
[123]94    map1 = constMap<A,B>();
[80]95    map1.setAll(B());
[123]96    ConstMap<A,C> map4(C(1));
97    ConstMap<A,C> map5 = map4;
98    map4 = constMap<A>(C(2));
99    map4.setAll(C(3));
[82]100
[80]101    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
102    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
103
104    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
[123]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> >();
[210]109    check(map6[A()] == 10 && map7[A()] == 10,
110          "Something is wrong with ConstMap");
[80]111  }
112
113  // IdentityMap
114  {
115    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
116    IdentityMap<A> map1;
117    IdentityMap<A> map2 = map1;
118    map1 = identityMap<A>();
[82]119
[80]120    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
[210]121    check(identityMap<double>()[1.0] == 1.0 &&
122          identityMap<double>()[3.14] == 3.14,
[80]123          "Something is wrong with IdentityMap");
124  }
125
126  // RangeMap
127  {
128    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
129    RangeMap<B> map1;
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());
136
137    checkConcept< ReferenceMap<int, double, double&, const double&>,
138                  RangeMap<double> >();
139    std::vector<double> v(10, 0);
140    v[5] = 100;
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");
145  }
146
147  // SparseMap
148  {
149    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
150    SparseMap<A,B> map1;
[136]151    SparseMap<A,B> map2 = B();
[80]152    SparseMap<A,B> map3 = sparseMap<A,B>();
153    SparseMap<A,B> map4 = sparseMap<A>(B());
154
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);
162
[210]163    check(map5[1.0] == 0 && map5[3.14] == 0 &&
164          map6[1.0] == 10 && map6[3.14] == 10,
[80]165          "Something is wrong with SparseMap");
166    map5[1.0] = map6[3.14] = 100;
[210]167    check(map5[1.0] == 100 && map5[3.14] == 0 &&
168          map6[1.0] == 10 && map6[3.14] == 100,
[80]169          "Something is wrong with SparseMap");
170  }
171
172  // ComposeMap
173  {
174    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
175    checkConcept<ReadMap<B,double>, CompMap>();
[554]176    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
[80]177    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
[82]178
[80]179    SparseMap<double, bool> m1(false); m1[3.14] = true;
180    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
[210]181    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
182          "Something is wrong with ComposeMap")
[80]183  }
184
185  // CombineMap
186  {
187    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
188    checkConcept<ReadMap<A,double>, CombMap>();
[554]189    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
[80]190    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
191
192    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
193          "Something is wrong with CombineMap");
194  }
195
196  // FunctorToMap, MapToFunctor
197  {
198    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
199    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
200    FunctorToMap<F> map1;
[554]201    FunctorToMap<F> map2 = FunctorToMap<F>(F());
[80]202    B b = functorToMap(F())[A()];
203
204    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
[1081]205    MapToFunctor<ReadMap<A,B> > map =
206      MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
[80]207
[210]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,
[80]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");
217  }
218
219  // ConvertMap
220  {
[210]221    checkConcept<ReadMap<double,double>,
222      ConvertMap<ReadMap<double, int>, double> >();
[80]223    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
224    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
225  }
226
227  // ForkMap
228  {
229    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
[82]230
[80]231    typedef RangeMap<double> RM;
232    typedef SparseMap<int, double> SM;
233    RM m1(10, -1);
234    SM m2(-1);
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);
239    map2.set(5, 10);
[210]240    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
241          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
[80]242          "Something is wrong with ForkMap");
243  }
[82]244
[80]245  // Arithmetic maps:
246  // - AddMap, SubMap, MulMap, DivMap
247  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
248  // - NegMap, NegWriteMap, AbsMap
249  {
250    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
251    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
252    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
253    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
[82]254
[80]255    ConstMap<int, double> c1(1.0), c2(3.14);
256    IdentityMap<int> im;
257    ConvertMap<IdentityMap<int>, double> id(im);
[210]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");
[82]266
[80]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> >();
[25]274
[80]275    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
276          "Something is wrong with ShiftMap");
[210]277    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
278          shiftWriteMap(id, 2.0)[10] == 12.0,
[80]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");
[210]282    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
283          scaleWriteMap(id, 2.0)[10] == 20.0,
[80]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");
291  }
[82]292
293  // Logical maps:
294  // - TrueMap, FalseMap
295  // - AndMap, OrMap
296  // - NotMap, NotWriteMap
297  // - EqualMap, LessMap
[80]298  {
[82]299    checkConcept<BoolMap, TrueMap<A> >();
300    checkConcept<BoolMap, FalseMap<A> >();
301    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
302    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
[80]303    checkConcept<BoolMap, NotMap<BoolMap> >();
304    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
[82]305    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
306    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
307
308    TrueMap<int> tm;
309    FalseMap<int> fm;
[80]310    RangeMap<bool> rm(2);
311    rm[0] = true; rm[1] = false;
[210]312    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
313          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
[82]314          "Something is wrong with AndMap");
[210]315    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
316          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
[82]317          "Something is wrong with OrMap");
[210]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");
[82]322
323    ConstMap<int, double> cm(2.0);
324    IdentityMap<int> im;
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");
[80]330  }
[209]331
[167]332  // LoggerBoolMap
[159]333  {
334    typedef std::vector<int> vec;
335    vec v1;
336    vec v2(10);
[210]337    LoggerBoolMap<std::back_insert_iterator<vec> >
338      map1(std::back_inserter(v1));
[167]339    LoggerBoolMap<vec::iterator> map2(v2.begin());
[159]340    map1.set(10, false);
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 &&
[210]346          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
347          v2[0]==20 && v2[1]==50 && v2[2]==60,
[167]348          "Something is wrong with LoggerBoolMap");
[209]349
[159]350    int i = 0;
[167]351    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
[159]352          it != map2.end(); ++it )
[167]353      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
[159]354  }
[1081]355
[731]356  // CrossRefMap
357  {
358    typedef ListDigraph Graph;
359    DIGRAPH_TYPEDEFS(Graph);
360
361    checkConcept<ReadWriteMap<Node, int>,
362                 CrossRefMap<Graph, Node, int> >();
[1081]363
[731]364    Graph gr;
365    typedef CrossRefMap<Graph, Node, char> CRMap;
366    typedef CRMap::ValueIterator ValueIt;
367    CRMap map(gr);
[1081]368
[731]369    Node n0 = gr.addNode();
370    Node n1 = gr.addNode();
371    Node n2 = gr.addNode();
[1081]372
[731]373    map.set(n0, 'A');
374    map.set(n1, 'B');
375    map.set(n2, 'C');
376    map.set(n2, 'A');
377    map.set(n0, 'C');
378
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");
384
385    ValueIt it = map.beginValue();
386    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
387          it == map.endValue(), "Wrong value iterator");
388  }
[25]389
390  return 0;
391}
Note: See TracBrowser for help on using the repository browser.