test/maps_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 18 Mar 2010 13:18:58 +0100
changeset 958 d6052a9c4e8d
parent 773 3fc2a801c39e
child 956 141f9c0db4a3
permissions -rw-r--r--
Backed out changeset a6eb9698c321 (#360, #51)
     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-2009
     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 #include <lemon/smart_graph.h>
    27 #include <lemon/adaptors.h>
    28 #include <lemon/dfs.h>
    29 #include <algorithm>
    30 
    31 #include "test_tools.h"
    32 
    33 using namespace lemon;
    34 using namespace lemon::concepts;
    35 
    36 struct A {};
    37 inline bool operator<(A, A) { return true; }
    38 struct B {};
    39 
    40 class C {
    41   int _x;
    42 public:
    43   C(int x) : _x(x) {}
    44   int get() const { return _x; }
    45 };
    46 inline bool operator<(C c1, C c2) { return c1.get() < c2.get(); }
    47 inline bool operator==(C c1, C c2) { return c1.get() == c2.get(); }
    48 
    49 C createC(int x) { return C(x); }
    50 
    51 template <typename T>
    52 class Less {
    53   T _t;
    54 public:
    55   Less(T t): _t(t) {}
    56   bool operator()(const T& t) const { return t < _t; }
    57 };
    58 
    59 class F {
    60 public:
    61   typedef A argument_type;
    62   typedef B result_type;
    63 
    64   B operator()(const A&) const { return B(); }
    65 private:
    66   F& operator=(const F&);
    67 };
    68 
    69 int func(A) { return 3; }
    70 
    71 int binc(int a, B) { return a+1; }
    72 
    73 template <typename T>
    74 class Sum {
    75   T& _sum;
    76 public:
    77   Sum(T& sum) : _sum(sum) {}
    78   void operator()(const T& t) { _sum += t; }
    79 };
    80 
    81 typedef ReadMap<A, double> DoubleMap;
    82 typedef ReadWriteMap<A, double> DoubleWriteMap;
    83 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
    84 
    85 typedef ReadMap<A, bool> BoolMap;
    86 typedef ReadWriteMap<A, bool> BoolWriteMap;
    87 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
    88 
    89 int main()
    90 {
    91   // Map concepts
    92   checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
    93   checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
    94   checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
    95   checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
    96   checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
    97   checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
    98   checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
    99   checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
   100 
   101   // NullMap
   102   {
   103     checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
   104     NullMap<A,B> map1;
   105     NullMap<A,B> map2 = map1;
   106     map1 = nullMap<A,B>();
   107   }
   108 
   109   // ConstMap
   110   {
   111     checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
   112     checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
   113     ConstMap<A,B> map1;
   114     ConstMap<A,B> map2 = B();
   115     ConstMap<A,B> map3 = map1;
   116     map1 = constMap<A>(B());
   117     map1 = constMap<A,B>();
   118     map1.setAll(B());
   119     ConstMap<A,C> map4(C(1));
   120     ConstMap<A,C> map5 = map4;
   121     map4 = constMap<A>(C(2));
   122     map4.setAll(C(3));
   123 
   124     checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
   125     check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
   126 
   127     checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
   128     ConstMap<A,Const<int,10> > map6;
   129     ConstMap<A,Const<int,10> > map7 = map6;
   130     map6 = constMap<A,int,10>();
   131     map7 = constMap<A,Const<int,10> >();
   132     check(map6[A()] == 10 && map7[A()] == 10,
   133           "Something is wrong with ConstMap");
   134   }
   135 
   136   // IdentityMap
   137   {
   138     checkConcept<ReadMap<A,A>, IdentityMap<A> >();
   139     IdentityMap<A> map1;
   140     IdentityMap<A> map2 = map1;
   141     map1 = identityMap<A>();
   142 
   143     checkConcept<ReadMap<double,double>, IdentityMap<double> >();
   144     check(identityMap<double>()[1.0] == 1.0 &&
   145           identityMap<double>()[3.14] == 3.14,
   146           "Something is wrong with IdentityMap");
   147   }
   148 
   149   // RangeMap
   150   {
   151     checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
   152     RangeMap<B> map1;
   153     RangeMap<B> map2(10);
   154     RangeMap<B> map3(10,B());
   155     RangeMap<B> map4 = map1;
   156     RangeMap<B> map5 = rangeMap<B>();
   157     RangeMap<B> map6 = rangeMap<B>(10);
   158     RangeMap<B> map7 = rangeMap(10,B());
   159 
   160     checkConcept< ReferenceMap<int, double, double&, const double&>,
   161                   RangeMap<double> >();
   162     std::vector<double> v(10, 0);
   163     v[5] = 100;
   164     RangeMap<double> map8(v);
   165     RangeMap<double> map9 = rangeMap(v);
   166     check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
   167           "Something is wrong with RangeMap");
   168   }
   169 
   170   // SparseMap
   171   {
   172     checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
   173     SparseMap<A,B> map1;
   174     SparseMap<A,B> map2 = B();
   175     SparseMap<A,B> map3 = sparseMap<A,B>();
   176     SparseMap<A,B> map4 = sparseMap<A>(B());
   177 
   178     checkConcept< ReferenceMap<double, int, int&, const int&>,
   179                   SparseMap<double, int> >();
   180     std::map<double, int> m;
   181     SparseMap<double, int> map5(m);
   182     SparseMap<double, int> map6(m,10);
   183     SparseMap<double, int> map7 = sparseMap(m);
   184     SparseMap<double, int> map8 = sparseMap(m,10);
   185 
   186     check(map5[1.0] == 0 && map5[3.14] == 0 &&
   187           map6[1.0] == 10 && map6[3.14] == 10,
   188           "Something is wrong with SparseMap");
   189     map5[1.0] = map6[3.14] = 100;
   190     check(map5[1.0] == 100 && map5[3.14] == 0 &&
   191           map6[1.0] == 10 && map6[3.14] == 100,
   192           "Something is wrong with SparseMap");
   193   }
   194 
   195   // ComposeMap
   196   {
   197     typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
   198     checkConcept<ReadMap<B,double>, CompMap>();
   199     CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
   200     CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
   201 
   202     SparseMap<double, bool> m1(false); m1[3.14] = true;
   203     RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
   204     check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
   205           "Something is wrong with ComposeMap")
   206   }
   207 
   208   // CombineMap
   209   {
   210     typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
   211     checkConcept<ReadMap<A,double>, CombMap>();
   212     CombMap map1 = CombMap(DoubleMap(), DoubleMap());
   213     CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
   214 
   215     check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
   216           "Something is wrong with CombineMap");
   217   }
   218 
   219   // FunctorToMap, MapToFunctor
   220   {
   221     checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
   222     checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
   223     FunctorToMap<F> map1;
   224     FunctorToMap<F> map2 = FunctorToMap<F>(F());
   225     B b = functorToMap(F())[A()];
   226 
   227     checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
   228     MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
   229 
   230     check(functorToMap(&func)[A()] == 3,
   231           "Something is wrong with FunctorToMap");
   232     check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
   233           "Something is wrong with MapToFunctor");
   234     check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
   235           mapToFunctor(functorToMap(&func))[A()] == 3,
   236           "Something is wrong with FunctorToMap or MapToFunctor");
   237     check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
   238           "Something is wrong with FunctorToMap or MapToFunctor");
   239   }
   240 
   241   // ConvertMap
   242   {
   243     checkConcept<ReadMap<double,double>,
   244       ConvertMap<ReadMap<double, int>, double> >();
   245     ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
   246     ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
   247   }
   248 
   249   // ForkMap
   250   {
   251     checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
   252 
   253     typedef RangeMap<double> RM;
   254     typedef SparseMap<int, double> SM;
   255     RM m1(10, -1);
   256     SM m2(-1);
   257     checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
   258     checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
   259     ForkMap<RM, SM> map1(m1,m2);
   260     ForkMap<SM, RM> map2 = forkMap(m2,m1);
   261     map2.set(5, 10);
   262     check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
   263           m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
   264           "Something is wrong with ForkMap");
   265   }
   266 
   267   // Arithmetic maps:
   268   // - AddMap, SubMap, MulMap, DivMap
   269   // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
   270   // - NegMap, NegWriteMap, AbsMap
   271   {
   272     checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
   273     checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
   274     checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
   275     checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
   276 
   277     ConstMap<int, double> c1(1.0), c2(3.14);
   278     IdentityMap<int> im;
   279     ConvertMap<IdentityMap<int>, double> id(im);
   280     check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
   281           "Something is wrong with AddMap");
   282     check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
   283           "Something is wrong with SubMap");
   284     check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
   285           "Something is wrong with MulMap");
   286     check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
   287           "Something is wrong with DivMap");
   288 
   289     checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
   290     checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
   291     checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
   292     checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
   293     checkConcept<DoubleMap, NegMap<DoubleMap> >();
   294     checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
   295     checkConcept<DoubleMap, AbsMap<DoubleMap> >();
   296 
   297     check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
   298           "Something is wrong with ShiftMap");
   299     check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
   300           shiftWriteMap(id, 2.0)[10] == 12.0,
   301           "Something is wrong with ShiftWriteMap");
   302     check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
   303           "Something is wrong with ScaleMap");
   304     check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
   305           scaleWriteMap(id, 2.0)[10] == 20.0,
   306           "Something is wrong with ScaleWriteMap");
   307     check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
   308           "Something is wrong with NegMap");
   309     check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
   310           "Something is wrong with NegWriteMap");
   311     check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
   312           "Something is wrong with AbsMap");
   313   }
   314 
   315   // Logical maps:
   316   // - TrueMap, FalseMap
   317   // - AndMap, OrMap
   318   // - NotMap, NotWriteMap
   319   // - EqualMap, LessMap
   320   {
   321     checkConcept<BoolMap, TrueMap<A> >();
   322     checkConcept<BoolMap, FalseMap<A> >();
   323     checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
   324     checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
   325     checkConcept<BoolMap, NotMap<BoolMap> >();
   326     checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
   327     checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
   328     checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
   329 
   330     TrueMap<int> tm;
   331     FalseMap<int> fm;
   332     RangeMap<bool> rm(2);
   333     rm[0] = true; rm[1] = false;
   334     check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
   335           !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
   336           "Something is wrong with AndMap");
   337     check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
   338           orMap(fm,rm)[0] && !orMap(fm,rm)[1],
   339           "Something is wrong with OrMap");
   340     check(!notMap(rm)[0] && notMap(rm)[1],
   341           "Something is wrong with NotMap");
   342     check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
   343           "Something is wrong with NotWriteMap");
   344 
   345     ConstMap<int, double> cm(2.0);
   346     IdentityMap<int> im;
   347     ConvertMap<IdentityMap<int>, double> id(im);
   348     check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
   349           "Something is wrong with LessMap");
   350     check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
   351           "Something is wrong with EqualMap");
   352   }
   353 
   354   // LoggerBoolMap
   355   {
   356     typedef std::vector<int> vec;
   357     checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
   358     checkConcept<WriteMap<int, bool>,
   359                  LoggerBoolMap<std::back_insert_iterator<vec> > >();
   360 
   361     vec v1;
   362     vec v2(10);
   363     LoggerBoolMap<std::back_insert_iterator<vec> >
   364       map1(std::back_inserter(v1));
   365     LoggerBoolMap<vec::iterator> map2(v2.begin());
   366     map1.set(10, false);
   367     map1.set(20, true);   map2.set(20, true);
   368     map1.set(30, false);  map2.set(40, false);
   369     map1.set(50, true);   map2.set(50, true);
   370     map1.set(60, true);   map2.set(60, true);
   371     check(v1.size() == 3 && v2.size() == 10 &&
   372           v1[0]==20 && v1[1]==50 && v1[2]==60 &&
   373           v2[0]==20 && v2[1]==50 && v2[2]==60,
   374           "Something is wrong with LoggerBoolMap");
   375 
   376     int i = 0;
   377     for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
   378           it != map2.end(); ++it )
   379       check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
   380     
   381     typedef ListDigraph Graph;
   382     DIGRAPH_TYPEDEFS(Graph);
   383     Graph gr;
   384 
   385     Node n0 = gr.addNode();
   386     Node n1 = gr.addNode();
   387     Node n2 = gr.addNode();
   388     Node n3 = gr.addNode();
   389     
   390     gr.addArc(n3, n0);
   391     gr.addArc(n3, n2);
   392     gr.addArc(n0, n2);
   393     gr.addArc(n2, n1);
   394     gr.addArc(n0, n1);
   395     
   396     {
   397       std::vector<Node> v;
   398       dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
   399 
   400       check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
   401             "Something is wrong with LoggerBoolMap");
   402     }
   403     {
   404       std::vector<Node> v(countNodes(gr));
   405       dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
   406       
   407       check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
   408             "Something is wrong with LoggerBoolMap");
   409     }
   410   }
   411   
   412   // IdMap, RangeIdMap
   413   {
   414     typedef ListDigraph Graph;
   415     DIGRAPH_TYPEDEFS(Graph);
   416 
   417     checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
   418     checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
   419     checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
   420     checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
   421     
   422     Graph gr;
   423     IdMap<Graph, Node> nmap(gr);
   424     IdMap<Graph, Arc> amap(gr);
   425     RangeIdMap<Graph, Node> nrmap(gr);
   426     RangeIdMap<Graph, Arc> armap(gr);
   427     
   428     Node n0 = gr.addNode();
   429     Node n1 = gr.addNode();
   430     Node n2 = gr.addNode();
   431     
   432     Arc a0 = gr.addArc(n0, n1);
   433     Arc a1 = gr.addArc(n0, n2);
   434     Arc a2 = gr.addArc(n2, n1);
   435     Arc a3 = gr.addArc(n2, n0);
   436     
   437     check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
   438     check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
   439     check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
   440 
   441     check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
   442     check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
   443     check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
   444     check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
   445 
   446     check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
   447     check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
   448     
   449     check(nrmap.size() == 3 && armap.size() == 4,
   450           "Wrong RangeIdMap::size()");
   451 
   452     check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
   453     check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
   454     check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
   455     
   456     check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
   457     check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
   458     check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
   459     check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
   460 
   461     check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
   462     check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
   463     
   464     gr.erase(n1);
   465     
   466     if (nrmap[n0] == 1) nrmap.swap(n0, n2);
   467     nrmap.swap(n2, n0);
   468     if (armap[a1] == 1) armap.swap(a1, a3);
   469     armap.swap(a3, a1);
   470     
   471     check(nrmap.size() == 2 && armap.size() == 2,
   472           "Wrong RangeIdMap::size()");
   473 
   474     check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
   475     check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
   476     
   477     check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
   478     check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
   479 
   480     check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
   481     check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
   482   }
   483   
   484   // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
   485   {
   486     typedef ListGraph Graph;
   487     GRAPH_TYPEDEFS(Graph);
   488     
   489     checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
   490     checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
   491     checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
   492     checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
   493     checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
   494     checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
   495 
   496     Graph gr;
   497     Node n0 = gr.addNode();
   498     Node n1 = gr.addNode();
   499     Node n2 = gr.addNode();
   500     
   501     gr.addEdge(n0,n1);
   502     gr.addEdge(n1,n2);
   503     gr.addEdge(n0,n2);
   504     gr.addEdge(n2,n1);
   505     gr.addEdge(n1,n2);
   506     gr.addEdge(n0,n1);
   507     
   508     for (EdgeIt e(gr); e != INVALID; ++e) {
   509       check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
   510       check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
   511     }
   512     
   513     check(mapCompare(gr,
   514           sourceMap(orienter(gr, constMap<Edge, bool>(true))),
   515           targetMap(orienter(gr, constMap<Edge, bool>(false)))),
   516           "Wrong SourceMap or TargetMap");
   517 
   518     typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
   519     Digraph dgr(gr, constMap<Edge, bool>(true));
   520     OutDegMap<Digraph> odm(dgr);
   521     InDegMap<Digraph> idm(dgr);
   522     
   523     check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
   524     check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
   525    
   526     gr.addEdge(n2, n0);
   527 
   528     check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
   529     check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
   530   }
   531   
   532   // CrossRefMap
   533   {
   534     typedef ListDigraph Graph;
   535     DIGRAPH_TYPEDEFS(Graph);
   536 
   537     checkConcept<ReadWriteMap<Node, int>,
   538                  CrossRefMap<Graph, Node, int> >();
   539     checkConcept<ReadWriteMap<Node, bool>,
   540                  CrossRefMap<Graph, Node, bool> >();
   541     checkConcept<ReadWriteMap<Node, double>,
   542                  CrossRefMap<Graph, Node, double> >();
   543     
   544     Graph gr;
   545     typedef CrossRefMap<Graph, Node, char> CRMap;
   546     CRMap map(gr);
   547     
   548     Node n0 = gr.addNode();
   549     Node n1 = gr.addNode();
   550     Node n2 = gr.addNode();
   551     
   552     map.set(n0, 'A');
   553     map.set(n1, 'B');
   554     map.set(n2, 'C');
   555     
   556     check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
   557           "Wrong CrossRefMap");
   558     check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
   559           "Wrong CrossRefMap");
   560     check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
   561           "Wrong CrossRefMap");
   562     check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
   563           "Wrong CrossRefMap::count()");
   564     
   565     CRMap::ValueIt it = map.beginValue();
   566     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   567           it == map.endValue(), "Wrong value iterator");
   568     
   569     map.set(n2, 'A');
   570 
   571     check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
   572           "Wrong CrossRefMap");
   573     check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
   574     check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
   575     check(map('C') == INVALID && map.inverse()['C'] == INVALID,
   576           "Wrong CrossRefMap");
   577     check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
   578           "Wrong CrossRefMap::count()");
   579 
   580     it = map.beginValue();
   581     check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
   582           it == map.endValue(), "Wrong value iterator");
   583 
   584     map.set(n0, 'C');
   585 
   586     check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
   587           "Wrong CrossRefMap");
   588     check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
   589     check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
   590     check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
   591     check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
   592           "Wrong CrossRefMap::count()");
   593 
   594     it = map.beginValue();
   595     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   596           it == map.endValue(), "Wrong value iterator");
   597   }
   598 
   599   // CrossRefMap
   600   {
   601     typedef SmartDigraph Graph;
   602     DIGRAPH_TYPEDEFS(Graph);
   603 
   604     checkConcept<ReadWriteMap<Node, int>,
   605                  CrossRefMap<Graph, Node, int> >();
   606     
   607     Graph gr;
   608     typedef CrossRefMap<Graph, Node, char> CRMap;
   609     typedef CRMap::ValueIterator ValueIt;
   610     CRMap map(gr);
   611     
   612     Node n0 = gr.addNode();
   613     Node n1 = gr.addNode();
   614     Node n2 = gr.addNode();
   615     
   616     map.set(n0, 'A');
   617     map.set(n1, 'B');
   618     map.set(n2, 'C');
   619     map.set(n2, 'A');
   620     map.set(n0, 'C');
   621 
   622     check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
   623           "Wrong CrossRefMap");
   624     check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
   625     check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
   626     check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
   627 
   628     ValueIt it = map.beginValue();
   629     check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
   630           it == map.endValue(), "Wrong value iterator");
   631   }
   632   
   633   // Iterable bool map
   634   {
   635     typedef SmartGraph Graph;
   636     typedef SmartGraph::Node Item;
   637 
   638     typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
   639     checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
   640 
   641     const int num = 10;
   642     Graph g;
   643     std::vector<Item> items;
   644     for (int i = 0; i < num; ++i) {
   645       items.push_back(g.addNode());
   646     }
   647 
   648     Ibm map1(g, true);
   649     int n = 0;
   650     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
   651       check(map1[static_cast<Item>(it)], "Wrong TrueIt");
   652       ++n;
   653     }
   654     check(n == num, "Wrong number");
   655 
   656     n = 0;
   657     for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
   658         check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
   659         ++n;
   660     }
   661     check(n == num, "Wrong number");
   662     check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
   663     check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
   664 
   665     map1[items[5]] = true;
   666 
   667     n = 0;
   668     for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
   669         check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
   670         ++n;
   671     }
   672     check(n == num, "Wrong number");
   673 
   674     map1[items[num / 2]] = false;
   675     check(map1[items[num / 2]] == false, "Wrong map value");
   676 
   677     n = 0;
   678     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
   679         check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
   680         ++n;
   681     }
   682     check(n == num - 1, "Wrong number");
   683 
   684     n = 0;
   685     for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
   686         check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
   687         ++n;
   688     }
   689     check(n == 1, "Wrong number");
   690 
   691     map1[items[0]] = false;
   692     check(map1[items[0]] == false, "Wrong map value");
   693 
   694     map1[items[num - 1]] = false;
   695     check(map1[items[num - 1]] == false, "Wrong map value");
   696 
   697     n = 0;
   698     for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
   699         check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
   700         ++n;
   701     }
   702     check(n == num - 3, "Wrong number");
   703     check(map1.trueNum() == num - 3, "Wrong number");
   704 
   705     n = 0;
   706     for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
   707         check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
   708         ++n;
   709     }
   710     check(n == 3, "Wrong number");
   711     check(map1.falseNum() == 3, "Wrong number");
   712   }
   713 
   714   // Iterable int map
   715   {
   716     typedef SmartGraph Graph;
   717     typedef SmartGraph::Node Item;
   718     typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
   719 
   720     checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
   721 
   722     const int num = 10;
   723     Graph g;
   724     std::vector<Item> items;
   725     for (int i = 0; i < num; ++i) {
   726       items.push_back(g.addNode());
   727     }
   728 
   729     Iim map1(g);
   730     check(map1.size() == 0, "Wrong size");
   731 
   732     for (int i = 0; i < num; ++i) {
   733       map1[items[i]] = i;
   734     }
   735     check(map1.size() == num, "Wrong size");
   736 
   737     for (int i = 0; i < num; ++i) {
   738       Iim::ItemIt it(map1, i);
   739       check(static_cast<Item>(it) == items[i], "Wrong value");
   740       ++it;
   741       check(static_cast<Item>(it) == INVALID, "Wrong value");
   742     }
   743 
   744     for (int i = 0; i < num; ++i) {
   745       map1[items[i]] = i % 2;
   746     }
   747     check(map1.size() == 2, "Wrong size");
   748 
   749     int n = 0;
   750     for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
   751       check(map1[static_cast<Item>(it)] == 0, "Wrong value");
   752       ++n;
   753     }
   754     check(n == (num + 1) / 2, "Wrong number");
   755 
   756     for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
   757       check(map1[static_cast<Item>(it)] == 1, "Wrong value");
   758       ++n;
   759     }
   760     check(n == num, "Wrong number");
   761 
   762   }
   763 
   764   // Iterable value map
   765   {
   766     typedef SmartGraph Graph;
   767     typedef SmartGraph::Node Item;
   768     typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
   769 
   770     checkConcept<ReadWriteMap<Item, double>, Ivm>();
   771 
   772     const int num = 10;
   773     Graph g;
   774     std::vector<Item> items;
   775     for (int i = 0; i < num; ++i) {
   776       items.push_back(g.addNode());
   777     }
   778 
   779     Ivm map1(g, 0.0);
   780     check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
   781     check(*map1.beginValue() == 0.0, "Wrong value");
   782 
   783     for (int i = 0; i < num; ++i) {
   784       map1.set(items[i], static_cast<double>(i));
   785     }
   786     check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
   787 
   788     for (int i = 0; i < num; ++i) {
   789       Ivm::ItemIt it(map1, static_cast<double>(i));
   790       check(static_cast<Item>(it) == items[i], "Wrong value");
   791       ++it;
   792       check(static_cast<Item>(it) == INVALID, "Wrong value");
   793     }
   794 
   795     for (Ivm::ValueIt vit = map1.beginValue();
   796          vit != map1.endValue(); ++vit) {
   797       check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
   798             "Wrong ValueIt");
   799     }
   800 
   801     for (int i = 0; i < num; ++i) {
   802       map1.set(items[i], static_cast<double>(i % 2));
   803     }
   804     check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
   805 
   806     int n = 0;
   807     for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
   808       check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
   809       ++n;
   810     }
   811     check(n == (num + 1) / 2, "Wrong number");
   812 
   813     for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
   814       check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
   815       ++n;
   816     }
   817     check(n == num, "Wrong number");
   818 
   819   }
   820   
   821   // Graph map utilities:
   822   // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
   823   // mapFind(), mapFindIf(), mapCount(), mapCountIf()
   824   // mapCopy(), mapCompare(), mapFill()
   825   {
   826     DIGRAPH_TYPEDEFS(SmartDigraph);
   827 
   828     SmartDigraph g;
   829     Node n1 = g.addNode();
   830     Node n2 = g.addNode();
   831     Node n3 = g.addNode();
   832     
   833     SmartDigraph::NodeMap<int> map1(g);
   834     SmartDigraph::ArcMap<char> map2(g);
   835     ConstMap<Node, A> cmap1 = A();
   836     ConstMap<Arc, C> cmap2 = C(0);
   837     
   838     map1[n1] = 10;
   839     map1[n2] = 5;
   840     map1[n3] = 12;
   841     
   842     // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
   843     check(mapMin(g, map1) == n2, "Wrong mapMin()");
   844     check(mapMax(g, map1) == n3, "Wrong mapMax()");
   845     check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()");
   846     check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()");
   847     check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()");
   848     check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()");
   849 
   850     check(mapMin(g, map2) == INVALID, "Wrong mapMin()");
   851     check(mapMax(g, map2) == INVALID, "Wrong mapMax()");
   852 
   853     check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
   854     check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()");
   855 
   856     Arc a1 = g.addArc(n1, n2);
   857     Arc a2 = g.addArc(n1, n3);
   858     Arc a3 = g.addArc(n2, n3);
   859     Arc a4 = g.addArc(n3, n1);
   860     
   861     map2[a1] = 'b';
   862     map2[a2] = 'a';
   863     map2[a3] = 'b';
   864     map2[a4] = 'c';
   865 
   866     // mapMin(), mapMax(), mapMinValue(), mapMaxValue()
   867     check(mapMin(g, map2) == a2, "Wrong mapMin()");
   868     check(mapMax(g, map2) == a4, "Wrong mapMax()");
   869     check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()");
   870     check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()");
   871     check(mapMinValue(g, map2, std::greater<int>()) == 'c',
   872           "Wrong mapMinValue()");
   873     check(mapMaxValue(g, map2, std::greater<int>()) == 'a',
   874           "Wrong mapMaxValue()");
   875 
   876     check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()");
   877     check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()");
   878     check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()");
   879 
   880     check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2,
   881           "Wrong mapMin()");
   882     check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4,
   883           "Wrong mapMax()");
   884     check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'),
   885           "Wrong mapMinValue()");
   886     check(mapMaxValue(g, composeMap(functorToMap(&createC), map2)) == C('c'),
   887           "Wrong mapMaxValue()");
   888 
   889     // mapFind(), mapFindIf()
   890     check(mapFind(g, map1, 5) == n2, "Wrong mapFind()");
   891     check(mapFind(g, map1, 6) == INVALID, "Wrong mapFind()");
   892     check(mapFind(g, map2, 'a') == a2, "Wrong mapFind()");
   893     check(mapFind(g, map2, 'e') == INVALID, "Wrong mapFind()");
   894     check(mapFind(g, cmap2, C(0)) == ArcIt(g), "Wrong mapFind()");
   895     check(mapFind(g, cmap2, C(1)) == INVALID, "Wrong mapFind()");
   896 
   897     check(mapFindIf(g, map1, Less<int>(7)) == n2,
   898           "Wrong mapFindIf()");
   899     check(mapFindIf(g, map1, Less<int>(5)) == INVALID,
   900           "Wrong mapFindIf()");
   901     check(mapFindIf(g, map2, Less<char>('d')) == ArcIt(g),
   902           "Wrong mapFindIf()");
   903     check(mapFindIf(g, map2, Less<char>('a')) == INVALID,
   904           "Wrong mapFindIf()");
   905 
   906     // mapCount(), mapCountIf()
   907     check(mapCount(g, map1, 5) == 1, "Wrong mapCount()");
   908     check(mapCount(g, map1, 6) == 0, "Wrong mapCount()");
   909     check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()");
   910     check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()");
   911     check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()");
   912     check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()");
   913     check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()");
   914 
   915     check(mapCountIf(g, map1, Less<int>(11)) == 2,
   916           "Wrong mapCountIf()");
   917     check(mapCountIf(g, map1, Less<int>(13)) == 3,
   918           "Wrong mapCountIf()");
   919     check(mapCountIf(g, map1, Less<int>(5)) == 0,
   920           "Wrong mapCountIf()");
   921     check(mapCountIf(g, map2, Less<char>('d')) == 4,
   922           "Wrong mapCountIf()");
   923     check(mapCountIf(g, map2, Less<char>('c')) == 3,
   924           "Wrong mapCountIf()");
   925     check(mapCountIf(g, map2, Less<char>('a')) == 0,
   926           "Wrong mapCountIf()");
   927      
   928     // MapIt, ConstMapIt
   929 /*
   930 These tests can be used after applying bugfix #330
   931     typedef SmartDigraph::NodeMap<int>::MapIt MapIt;
   932     typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt;
   933     check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5,
   934           "Wrong NodeMap<>::MapIt");
   935     check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12,
   936           "Wrong NodeMap<>::MapIt");
   937     
   938     int sum = 0;
   939     std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum));
   940     check(sum == 27, "Wrong NodeMap<>::MapIt");
   941     std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum));
   942     check(sum == 54, "Wrong NodeMap<>::ConstMapIt");
   943 */
   944 
   945     // mapCopy(), mapCompare(), mapFill()
   946     check(mapCompare(g, map1, map1), "Wrong mapCompare()");
   947     check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()");
   948     check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()");
   949     check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()");
   950     check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()");
   951 
   952     SmartDigraph::NodeMap<int> map3(g, 0);
   953     SmartDigraph::ArcMap<char> map4(g, 'a');
   954     
   955     check(!mapCompare(g, map1, map3), "Wrong mapCompare()");
   956     check(!mapCompare(g, map2, map4), "Wrong mapCompare()");    
   957     
   958     mapCopy(g, map1, map3);
   959     mapCopy(g, map2, map4);
   960 
   961     check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()");
   962     check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()");    
   963     
   964     Undirector<SmartDigraph> ug(g);
   965     Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x');
   966     Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14);
   967     
   968     check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
   969     check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
   970     check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
   971     check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
   972     
   973     mapCopy(g, map2, umap1);
   974 
   975     check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()");
   976     check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()");
   977     check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()");
   978     check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()");
   979     
   980     mapCopy(g, map2, umap1);
   981     mapCopy(g, umap1, map2);
   982     mapCopy(ug, map2, umap1);
   983     mapCopy(ug, umap1, map2);
   984     
   985     check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
   986     mapCopy(ug, umap1, umap2);
   987     check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()");
   988     
   989     check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()");
   990     mapFill(g, map1, 2);
   991     check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()");
   992 
   993     check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()");
   994     mapCopy(g, constMap<Arc>('z'), map2);
   995     check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()");
   996   }
   997   
   998   return 0;
   999 }