test/maps_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 09 Nov 2011 11:26:01 +0100
branch1.1
changeset 1096 bcb6e4402a6e
parent 731 7b1a6e963018
child 1158 8d2e55fac752
permissions -rw-r--r--
Update NEWS file
     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 #include <lemon/list_graph.h>
    26 
    27 #include "test_tools.h"
    28 
    29 using namespace lemon;
    30 using namespace lemon::concepts;
    31 
    32 struct A {};
    33 inline bool operator<(A, A) { return true; }
    34 struct B {};
    35 
    36 class C {
    37   int x;
    38 public:
    39   C(int _x) : x(_x) {}
    40 };
    41 
    42 class F {
    43 public:
    44   typedef A argument_type;
    45   typedef B result_type;
    46 
    47   B operator()(const A&) const { return B(); }
    48 private:
    49   F& operator=(const F&);
    50 };
    51 
    52 int func(A) { return 3; }
    53 
    54 int binc(int a, B) { return a+1; }
    55 
    56 typedef ReadMap<A, double> DoubleMap;
    57 typedef ReadWriteMap<A, double> DoubleWriteMap;
    58 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
    59 
    60 typedef ReadMap<A, bool> BoolMap;
    61 typedef ReadWriteMap<A, bool> BoolWriteMap;
    62 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
    63 
    64 int main()
    65 {
    66   // Map concepts
    67   checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
    68   checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
    69   checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
    70   checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
    71   checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
    72   checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
    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&> >();
    77 
    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> >();
    89     checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
    90     ConstMap<A,B> map1;
    91     ConstMap<A,B> map2 = B();
    92     ConstMap<A,B> map3 = map1;
    93     map1 = constMap<A>(B());
    94     map1 = constMap<A,B>();
    95     map1.setAll(B());
    96     ConstMap<A,C> map4(C(1));
    97     ConstMap<A,C> map5 = map4;
    98     map4 = constMap<A>(C(2));
    99     map4.setAll(C(3));
   100 
   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> > >();
   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> >();
   109     check(map6[A()] == 10 && map7[A()] == 10,
   110           "Something is wrong with ConstMap");
   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>();
   119 
   120     checkConcept<ReadMap<double,double>, IdentityMap<double> >();
   121     check(identityMap<double>()[1.0] == 1.0 &&
   122           identityMap<double>()[3.14] == 3.14,
   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;
   151     SparseMap<A,B> map2 = B();
   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 
   163     check(map5[1.0] == 0 && map5[3.14] == 0 &&
   164           map6[1.0] == 10 && map6[3.14] == 10,
   165           "Something is wrong with SparseMap");
   166     map5[1.0] = map6[3.14] = 100;
   167     check(map5[1.0] == 100 && map5[3.14] == 0 &&
   168           map6[1.0] == 10 && map6[3.14] == 100,
   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>();
   176     CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
   177     CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
   178 
   179     SparseMap<double, bool> m1(false); m1[3.14] = true;
   180     RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
   181     check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
   182           "Something is wrong with ComposeMap")
   183   }
   184 
   185   // CombineMap
   186   {
   187     typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
   188     checkConcept<ReadMap<A,double>, CombMap>();
   189     CombMap map1 = CombMap(DoubleMap(), DoubleMap());
   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;
   201     FunctorToMap<F> map2 = FunctorToMap<F>(F());
   202     B b = functorToMap(F())[A()];
   203 
   204     checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
   205     MapToFunctor<ReadMap<A,B> > map =
   206       MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
   207 
   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,
   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   {
   221     checkConcept<ReadMap<double,double>,
   222       ConvertMap<ReadMap<double, int>, double> >();
   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> >();
   230 
   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);
   240     check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
   241           m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
   242           "Something is wrong with ForkMap");
   243   }
   244 
   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> >();
   254 
   255     ConstMap<int, double> c1(1.0), c2(3.14);
   256     IdentityMap<int> im;
   257     ConvertMap<IdentityMap<int>, double> id(im);
   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");
   266 
   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> >();
   274 
   275     check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
   276           "Something is wrong with ShiftMap");
   277     check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
   278           shiftWriteMap(id, 2.0)[10] == 12.0,
   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");
   282     check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
   283           scaleWriteMap(id, 2.0)[10] == 20.0,
   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   }
   292 
   293   // Logical maps:
   294   // - TrueMap, FalseMap
   295   // - AndMap, OrMap
   296   // - NotMap, NotWriteMap
   297   // - EqualMap, LessMap
   298   {
   299     checkConcept<BoolMap, TrueMap<A> >();
   300     checkConcept<BoolMap, FalseMap<A> >();
   301     checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
   302     checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
   303     checkConcept<BoolMap, NotMap<BoolMap> >();
   304     checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
   305     checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
   306     checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
   307 
   308     TrueMap<int> tm;
   309     FalseMap<int> fm;
   310     RangeMap<bool> rm(2);
   311     rm[0] = true; rm[1] = false;
   312     check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
   313           !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
   314           "Something is wrong with AndMap");
   315     check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
   316           orMap(fm,rm)[0] && !orMap(fm,rm)[1],
   317           "Something is wrong with OrMap");
   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");
   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");
   330   }
   331 
   332   // LoggerBoolMap
   333   {
   334     typedef std::vector<int> vec;
   335     vec v1;
   336     vec v2(10);
   337     LoggerBoolMap<std::back_insert_iterator<vec> >
   338       map1(std::back_inserter(v1));
   339     LoggerBoolMap<vec::iterator> map2(v2.begin());
   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 &&
   346           v1[0]==20 && v1[1]==50 && v1[2]==60 &&
   347           v2[0]==20 && v2[1]==50 && v2[2]==60,
   348           "Something is wrong with LoggerBoolMap");
   349 
   350     int i = 0;
   351     for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
   352           it != map2.end(); ++it )
   353       check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
   354   }
   355 
   356   // CrossRefMap
   357   {
   358     typedef ListDigraph Graph;
   359     DIGRAPH_TYPEDEFS(Graph);
   360 
   361     checkConcept<ReadWriteMap<Node, int>,
   362                  CrossRefMap<Graph, Node, int> >();
   363 
   364     Graph gr;
   365     typedef CrossRefMap<Graph, Node, char> CRMap;
   366     typedef CRMap::ValueIterator ValueIt;
   367     CRMap map(gr);
   368 
   369     Node n0 = gr.addNode();
   370     Node n1 = gr.addNode();
   371     Node n2 = gr.addNode();
   372 
   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   }
   389 
   390   return 0;
   391 }