COIN-OR::LEMON - Graph Library

source: lemon-main/test/maps_test.cc @ 1083:3e711ee55d31

Last change on this file since 1083:3e711ee55d31 was 1083:3e711ee55d31, checked in by Alpar Juttner <alpar@…>, 11 years ago

Add explicit namespace to ignore_unused_variable_warning() usages (#294)

File size: 13.5 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 *
[440]5 * Copyright (C) 2003-2009
[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>
[684]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> >();
[25]73  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
[94]74  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
[25]75
[80]76  // NullMap
77  {
78    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
79    NullMap<A,B> map1;
80    NullMap<A,B> map2 = map1;
[1083]81    ::lemon::ignore_unused_variable_warning(map2);
[80]82    map1 = nullMap<A,B>();
83  }
84
85  // ConstMap
86  {
87    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
[123]88    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
[80]89    ConstMap<A,B> map1;
[136]90    ConstMap<A,B> map2 = B();
[80]91    ConstMap<A,B> map3 = map1;
[1083]92    ::lemon::ignore_unused_variable_warning(map2,map3);
[997]93
[80]94    map1 = constMap<A>(B());
[123]95    map1 = constMap<A,B>();
[80]96    map1.setAll(B());
[123]97    ConstMap<A,C> map4(C(1));
98    ConstMap<A,C> map5 = map4;
[1083]99    ::lemon::ignore_unused_variable_warning(map5);
[997]100
[123]101    map4 = constMap<A>(C(2));
102    map4.setAll(C(3));
[82]103
[80]104    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
105    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
106
107    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
[123]108    ConstMap<A,Const<int,10> > map6;
109    ConstMap<A,Const<int,10> > map7 = map6;
110    map6 = constMap<A,int,10>();
111    map7 = constMap<A,Const<int,10> >();
[210]112    check(map6[A()] == 10 && map7[A()] == 10,
113          "Something is wrong with ConstMap");
[80]114  }
115
116  // IdentityMap
117  {
118    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
119    IdentityMap<A> map1;
120    IdentityMap<A> map2 = map1;
[1083]121    ::lemon::ignore_unused_variable_warning(map2);
[997]122
[80]123    map1 = identityMap<A>();
[82]124
[80]125    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
[210]126    check(identityMap<double>()[1.0] == 1.0 &&
127          identityMap<double>()[3.14] == 3.14,
[80]128          "Something is wrong with IdentityMap");
129  }
130
131  // RangeMap
132  {
133    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
134    RangeMap<B> map1;
135    RangeMap<B> map2(10);
136    RangeMap<B> map3(10,B());
137    RangeMap<B> map4 = map1;
138    RangeMap<B> map5 = rangeMap<B>();
139    RangeMap<B> map6 = rangeMap<B>(10);
140    RangeMap<B> map7 = rangeMap(10,B());
141
142    checkConcept< ReferenceMap<int, double, double&, const double&>,
143                  RangeMap<double> >();
144    std::vector<double> v(10, 0);
145    v[5] = 100;
146    RangeMap<double> map8(v);
147    RangeMap<double> map9 = rangeMap(v);
148    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
149          "Something is wrong with RangeMap");
150  }
151
152  // SparseMap
153  {
154    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
155    SparseMap<A,B> map1;
[136]156    SparseMap<A,B> map2 = B();
[80]157    SparseMap<A,B> map3 = sparseMap<A,B>();
158    SparseMap<A,B> map4 = sparseMap<A>(B());
159
160    checkConcept< ReferenceMap<double, int, int&, const int&>,
161                  SparseMap<double, int> >();
162    std::map<double, int> m;
163    SparseMap<double, int> map5(m);
164    SparseMap<double, int> map6(m,10);
165    SparseMap<double, int> map7 = sparseMap(m);
166    SparseMap<double, int> map8 = sparseMap(m,10);
167
[210]168    check(map5[1.0] == 0 && map5[3.14] == 0 &&
169          map6[1.0] == 10 && map6[3.14] == 10,
[80]170          "Something is wrong with SparseMap");
171    map5[1.0] = map6[3.14] = 100;
[210]172    check(map5[1.0] == 100 && map5[3.14] == 0 &&
173          map6[1.0] == 10 && map6[3.14] == 100,
[80]174          "Something is wrong with SparseMap");
175  }
176
177  // ComposeMap
178  {
179    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
180    checkConcept<ReadMap<B,double>, CompMap>();
[477]181    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
[1083]182    ::lemon::ignore_unused_variable_warning(map1);
[80]183    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
[1083]184    ::lemon::ignore_unused_variable_warning(map2);
[82]185
[80]186    SparseMap<double, bool> m1(false); m1[3.14] = true;
187    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
[210]188    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
189          "Something is wrong with ComposeMap")
[80]190  }
191
192  // CombineMap
193  {
194    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
195    checkConcept<ReadMap<A,double>, CombMap>();
[477]196    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
[1083]197    ::lemon::ignore_unused_variable_warning(map1);
[80]198    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
[1083]199    ::lemon::ignore_unused_variable_warning(map2);
[80]200
201    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
202          "Something is wrong with CombineMap");
203  }
204
205  // FunctorToMap, MapToFunctor
206  {
207    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
208    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
209    FunctorToMap<F> map1;
[477]210    FunctorToMap<F> map2 = FunctorToMap<F>(F());
[1083]211    ::lemon::ignore_unused_variable_warning(map2);
[997]212
[80]213    B b = functorToMap(F())[A()];
[1083]214    ::lemon::ignore_unused_variable_warning(b);
[80]215
216    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
[997]217    MapToFunctor<ReadMap<A,B> > map =
218      MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
[1083]219    ::lemon::ignore_unused_variable_warning(map);
[80]220
[210]221    check(functorToMap(&func)[A()] == 3,
222          "Something is wrong with FunctorToMap");
223    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
224          "Something is wrong with MapToFunctor");
225    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
226          mapToFunctor(functorToMap(&func))[A()] == 3,
[80]227          "Something is wrong with FunctorToMap or MapToFunctor");
228    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
229          "Something is wrong with FunctorToMap or MapToFunctor");
230  }
231
232  // ConvertMap
233  {
[210]234    checkConcept<ReadMap<double,double>,
235      ConvertMap<ReadMap<double, int>, double> >();
[80]236    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
[1083]237    ::lemon::ignore_unused_variable_warning(map1);
[80]238    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
[1083]239    ::lemon::ignore_unused_variable_warning(map2);
[997]240
[80]241  }
242
243  // ForkMap
244  {
245    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
[82]246
[80]247    typedef RangeMap<double> RM;
248    typedef SparseMap<int, double> SM;
249    RM m1(10, -1);
250    SM m2(-1);
251    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
252    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
253    ForkMap<RM, SM> map1(m1,m2);
254    ForkMap<SM, RM> map2 = forkMap(m2,m1);
255    map2.set(5, 10);
[210]256    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
257          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
[80]258          "Something is wrong with ForkMap");
259  }
[82]260
[80]261  // Arithmetic maps:
262  // - AddMap, SubMap, MulMap, DivMap
263  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
264  // - NegMap, NegWriteMap, AbsMap
265  {
266    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
267    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
268    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
269    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
[82]270
[80]271    ConstMap<int, double> c1(1.0), c2(3.14);
272    IdentityMap<int> im;
273    ConvertMap<IdentityMap<int>, double> id(im);
[210]274    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
275          "Something is wrong with AddMap");
276    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
277          "Something is wrong with SubMap");
278    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
279          "Something is wrong with MulMap");
280    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
281          "Something is wrong with DivMap");
[82]282
[80]283    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
284    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
285    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
286    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
287    checkConcept<DoubleMap, NegMap<DoubleMap> >();
288    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
289    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
[25]290
[80]291    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
292          "Something is wrong with ShiftMap");
[210]293    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
294          shiftWriteMap(id, 2.0)[10] == 12.0,
[80]295          "Something is wrong with ShiftWriteMap");
296    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
297          "Something is wrong with ScaleMap");
[210]298    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
299          scaleWriteMap(id, 2.0)[10] == 20.0,
[80]300          "Something is wrong with ScaleWriteMap");
301    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
302          "Something is wrong with NegMap");
303    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
304          "Something is wrong with NegWriteMap");
305    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
306          "Something is wrong with AbsMap");
307  }
[82]308
309  // Logical maps:
310  // - TrueMap, FalseMap
311  // - AndMap, OrMap
312  // - NotMap, NotWriteMap
313  // - EqualMap, LessMap
[80]314  {
[82]315    checkConcept<BoolMap, TrueMap<A> >();
316    checkConcept<BoolMap, FalseMap<A> >();
317    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
318    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
[80]319    checkConcept<BoolMap, NotMap<BoolMap> >();
320    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
[82]321    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
322    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
323
324    TrueMap<int> tm;
325    FalseMap<int> fm;
[80]326    RangeMap<bool> rm(2);
327    rm[0] = true; rm[1] = false;
[210]328    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
329          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
[82]330          "Something is wrong with AndMap");
[210]331    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
332          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
[82]333          "Something is wrong with OrMap");
[210]334    check(!notMap(rm)[0] && notMap(rm)[1],
335          "Something is wrong with NotMap");
336    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
337          "Something is wrong with NotWriteMap");
[82]338
339    ConstMap<int, double> cm(2.0);
340    IdentityMap<int> im;
341    ConvertMap<IdentityMap<int>, double> id(im);
342    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
343          "Something is wrong with LessMap");
344    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
345          "Something is wrong with EqualMap");
[80]346  }
[209]347
[167]348  // LoggerBoolMap
[159]349  {
350    typedef std::vector<int> vec;
351    vec v1;
352    vec v2(10);
[210]353    LoggerBoolMap<std::back_insert_iterator<vec> >
354      map1(std::back_inserter(v1));
[167]355    LoggerBoolMap<vec::iterator> map2(v2.begin());
[159]356    map1.set(10, false);
357    map1.set(20, true);   map2.set(20, true);
358    map1.set(30, false);  map2.set(40, false);
359    map1.set(50, true);   map2.set(50, true);
360    map1.set(60, true);   map2.set(60, true);
361    check(v1.size() == 3 && v2.size() == 10 &&
[210]362          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
363          v2[0]==20 && v2[1]==50 && v2[2]==60,
[167]364          "Something is wrong with LoggerBoolMap");
[209]365
[159]366    int i = 0;
[167]367    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
[159]368          it != map2.end(); ++it )
[167]369      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
[159]370  }
[684]371 
372  // CrossRefMap
373  {
374    typedef ListDigraph Graph;
375    DIGRAPH_TYPEDEFS(Graph);
376
377    checkConcept<ReadWriteMap<Node, int>,
378                 CrossRefMap<Graph, Node, int> >();
379   
380    Graph gr;
381    typedef CrossRefMap<Graph, Node, char> CRMap;
382    typedef CRMap::ValueIterator ValueIt;
383    CRMap map(gr);
384   
385    Node n0 = gr.addNode();
386    Node n1 = gr.addNode();
387    Node n2 = gr.addNode();
388   
389    map.set(n0, 'A');
390    map.set(n1, 'B');
391    map.set(n2, 'C');
392    map.set(n2, 'A');
393    map.set(n0, 'C');
394
395    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
396          "Wrong CrossRefMap");
397    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
398    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
399    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
400
401    ValueIt it = map.beginValue();
402    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
403          it == map.endValue(), "Wrong value iterator");
404  }
[25]405
406  return 0;
407}
Note: See TracBrowser for help on using the repository browser.