COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/maps_test.cc

    r39 r94  
    3232inline bool operator<(A, A) { return true; }
    3333struct B {};
     34
     35class C {
     36  int x;
     37public:
     38  C(int _x) : x(_x) {}
     39};
    3440
    3541class F {
     
    3844  typedef B result_type;
    3945
    40   B operator()(const A &) const {return B();}
     46  B operator()(const A&) const { return B(); }
     47private:
     48  F& operator=(const F&);
    4149};
    4250
    43 int func(A) {return 3;}
    44 
    45 int binc(int, B) {return 4;}
    46 
    47 typedef ReadMap<A,double> DoubleMap;
    48 typedef ReadWriteMap<A, double> WriteDoubleMap;
    49 
    50 typedef ReadMap<A,bool> BoolMap;
     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;
    5160typedef ReadWriteMap<A, bool> BoolWriteMap;
     61typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
    5262
    5363int main()
    54 { // checking graph components
    55  
     64{
     65  // Map concepts
    5666  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
     67  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
    5768  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
     69  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
    5870  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
     71  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
    5972  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
    60 
    61   checkConcept<ReadMap<A,double>, AddMap<DoubleMap,DoubleMap> >();
    62   checkConcept<ReadMap<A,double>, SubMap<DoubleMap,DoubleMap> >();
    63   checkConcept<ReadMap<A,double>, MulMap<DoubleMap,DoubleMap> >();
    64   checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
    65   checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
    66   checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
    67   checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
    68   checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
    69   checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
    70   checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >();
    71   checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >();
    72   checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >();
    73   checkConcept<ReadWriteMap<A,double>,
    74     ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >();
    75  
    76   checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
    77 
    78   checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
    79 
    80   checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
    81   checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
    82 
    83   checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
    84   checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
    85   checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
    86   checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
    87   checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
    88   checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
    89 
    90   int a;
    91  
    92   a=mapFunctor(constMap<A,int>(2))(A());
    93   check(a==2,"Something is wrong with mapFunctor");
    94 
    95   B b;
    96   b=functorMap(F())[A()];
    97 
    98   a=functorMap(&func)[A()];
    99   check(a==3,"Something is wrong with functorMap");
    100 
    101   a=combineMap(constMap<B, int, 1>(), identityMap<B>(), &binc)[B()];
    102   check(a==4,"Something is wrong with combineMap");
    103  
    104 
    105   std::cout << __FILE__ ": All tests passed.\n";
    106  
     73  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
     74
     75  // NullMap
     76  {
     77    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
     78    NullMap<A,B> map1;
     79    NullMap<A,B> map2 = map1;
     80    map1 = nullMap<A,B>();
     81  }
     82
     83  // ConstMap
     84  {
     85    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
     86    ConstMap<A,B> map1;
     87    ConstMap<A,B> map2(B());
     88    ConstMap<A,B> map3 = map1;
     89    map1 = constMap<A>(B());
     90    map1.setAll(B());
     91
     92    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
     93    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
     94
     95    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
     96    ConstMap<A,Const<int,10> > map4;
     97    ConstMap<A,Const<int,10> > map5 = map4;
     98    map4 = map5;
     99    check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
     100  }
     101
     102  // IdentityMap
     103  {
     104    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
     105    IdentityMap<A> map1;
     106    IdentityMap<A> map2 = map1;
     107    map1 = identityMap<A>();
     108
     109    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
     110    check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
     111          "Something is wrong with IdentityMap");
     112  }
     113
     114  // RangeMap
     115  {
     116    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
     117    RangeMap<B> map1;
     118    RangeMap<B> map2(10);
     119    RangeMap<B> map3(10,B());
     120    RangeMap<B> map4 = map1;
     121    RangeMap<B> map5 = rangeMap<B>();
     122    RangeMap<B> map6 = rangeMap<B>(10);
     123    RangeMap<B> map7 = rangeMap(10,B());
     124
     125    checkConcept< ReferenceMap<int, double, double&, const double&>,
     126                  RangeMap<double> >();
     127    std::vector<double> v(10, 0);
     128    v[5] = 100;
     129    RangeMap<double> map8(v);
     130    RangeMap<double> map9 = rangeMap(v);
     131    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
     132          "Something is wrong with RangeMap");
     133  }
     134
     135  // SparseMap
     136  {
     137    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
     138    SparseMap<A,B> map1;
     139    SparseMap<A,B> map2(B());
     140    SparseMap<A,B> map3 = sparseMap<A,B>();
     141    SparseMap<A,B> map4 = sparseMap<A>(B());
     142
     143    checkConcept< ReferenceMap<double, int, int&, const int&>,
     144                  SparseMap<double, int> >();
     145    std::map<double, int> m;
     146    SparseMap<double, int> map5(m);
     147    SparseMap<double, int> map6(m,10);
     148    SparseMap<double, int> map7 = sparseMap(m);
     149    SparseMap<double, int> map8 = sparseMap(m,10);
     150
     151    check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
     152          "Something is wrong with SparseMap");
     153    map5[1.0] = map6[3.14] = 100;
     154    check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
     155          "Something is wrong with SparseMap");
     156  }
     157
     158  // ComposeMap
     159  {
     160    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
     161    checkConcept<ReadMap<B,double>, CompMap>();
     162    CompMap map1(DoubleMap(),ReadMap<B,A>());
     163    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
     164
     165    SparseMap<double, bool> m1(false); m1[3.14] = true;
     166    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
     167    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
     168  }
     169
     170  // CombineMap
     171  {
     172    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
     173    checkConcept<ReadMap<A,double>, CombMap>();
     174    CombMap map1(DoubleMap(), DoubleMap());
     175    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
     176
     177    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
     178          "Something is wrong with CombineMap");
     179  }
     180
     181  // FunctorToMap, MapToFunctor
     182  {
     183    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
     184    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
     185    FunctorToMap<F> map1;
     186    FunctorToMap<F> map2(F());
     187    B b = functorToMap(F())[A()];
     188
     189    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
     190    MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
     191
     192    check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
     193    check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
     194    check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
     195          "Something is wrong with FunctorToMap or MapToFunctor");
     196    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
     197          "Something is wrong with FunctorToMap or MapToFunctor");
     198  }
     199
     200  // ConvertMap
     201  {
     202    checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
     203    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
     204    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
     205  }
     206
     207  // ForkMap
     208  {
     209    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
     210
     211    typedef RangeMap<double> RM;
     212    typedef SparseMap<int, double> SM;
     213    RM m1(10, -1);
     214    SM m2(-1);
     215    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
     216    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
     217    ForkMap<RM, SM> map1(m1,m2);
     218    ForkMap<SM, RM> map2 = forkMap(m2,m1);
     219    map2.set(5, 10);
     220    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
     221          "Something is wrong with ForkMap");
     222  }
     223
     224  // Arithmetic maps:
     225  // - AddMap, SubMap, MulMap, DivMap
     226  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
     227  // - NegMap, NegWriteMap, AbsMap
     228  {
     229    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
     230    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
     231    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
     232    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
     233
     234    ConstMap<int, double> c1(1.0), c2(3.14);
     235    IdentityMap<int> im;
     236    ConvertMap<IdentityMap<int>, double> id(im);
     237    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
     238    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
     239    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
     240    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
     241
     242    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
     243    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
     244    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
     245    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
     246    checkConcept<DoubleMap, NegMap<DoubleMap> >();
     247    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
     248    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
     249
     250    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
     251          "Something is wrong with ShiftMap");
     252    check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
     253          "Something is wrong with ShiftWriteMap");
     254    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
     255          "Something is wrong with ScaleMap");
     256    check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
     257          "Something is wrong with ScaleWriteMap");
     258    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
     259          "Something is wrong with NegMap");
     260    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
     261          "Something is wrong with NegWriteMap");
     262    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
     263          "Something is wrong with AbsMap");
     264  }
     265
     266  // Logical maps:
     267  // - TrueMap, FalseMap
     268  // - AndMap, OrMap
     269  // - NotMap, NotWriteMap
     270  // - EqualMap, LessMap
     271  {
     272    checkConcept<BoolMap, TrueMap<A> >();
     273    checkConcept<BoolMap, FalseMap<A> >();
     274    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
     275    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
     276    checkConcept<BoolMap, NotMap<BoolMap> >();
     277    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
     278    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
     279    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
     280
     281    TrueMap<int> tm;
     282    FalseMap<int> fm;
     283    RangeMap<bool> rm(2);
     284    rm[0] = true; rm[1] = false;
     285    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
     286          "Something is wrong with AndMap");
     287    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
     288          "Something is wrong with OrMap");
     289    check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
     290    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
     291
     292    ConstMap<int, double> cm(2.0);
     293    IdentityMap<int> im;
     294    ConvertMap<IdentityMap<int>, double> id(im);
     295    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
     296          "Something is wrong with LessMap");
     297    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
     298          "Something is wrong with EqualMap");
     299  }
     300
    107301  return 0;
    108302}
Note: See TracChangeset for help on using the changeset viewer.