test/maps_test.cc
changeset 80 15968e25ca08
parent 39 0a01d811071f
child 82 bce6c8f93d10
equal deleted inserted replaced
1:603c212efaa9 2:39e717bee9cd
    35 class F {
    35 class F {
    36 public:
    36 public:
    37   typedef A argument_type;
    37   typedef A argument_type;
    38   typedef B result_type;
    38   typedef B result_type;
    39 
    39 
    40   B operator()(const A &) const {return B();}
    40   B operator()(const A&) const { return B(); }
       
    41 private:
       
    42   F& operator=(const F&);
    41 };
    43 };
    42 
    44 
    43 int func(A) {return 3;}
    45 int func(A) { return 3; }
    44 
    46 
    45 int binc(int, B) {return 4;}
    47 int binc(int a, B) { return a+1; }
    46 
    48 
    47 typedef ReadMap<A,double> DoubleMap;
    49 typedef ReadMap<A, double> DoubleMap;
    48 typedef ReadWriteMap<A, double> WriteDoubleMap;
    50 typedef ReadWriteMap<A, double> DoubleWriteMap;
    49 
    51 typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
    50 typedef ReadMap<A,bool> BoolMap;
    52 
       
    53 typedef ReadMap<A, bool> BoolMap;
    51 typedef ReadWriteMap<A, bool> BoolWriteMap;
    54 typedef ReadWriteMap<A, bool> BoolWriteMap;
       
    55 typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
    52 
    56 
    53 int main()
    57 int main()
    54 { // checking graph components
    58 {
    55   
    59   // Map concepts
    56   checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
    60   checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
    57   checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
    61   checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
    58   checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
    62   checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
    59   checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
    63   checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
    60 
    64 
    61   checkConcept<ReadMap<A,double>, AddMap<DoubleMap,DoubleMap> >();
    65   // NullMap
    62   checkConcept<ReadMap<A,double>, SubMap<DoubleMap,DoubleMap> >();
    66   {
    63   checkConcept<ReadMap<A,double>, MulMap<DoubleMap,DoubleMap> >();
    67     checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
    64   checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
    68     NullMap<A,B> map1;
    65   checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
    69     NullMap<A,B> map2 = map1;
    66   checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
    70     map1 = nullMap<A,B>();
    67   checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
    71   }
    68   checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
    72 
    69   checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
    73   // ConstMap
    70   checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >();
    74   {
    71   checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >();
    75     checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
    72   checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >();
    76     ConstMap<A,B> map1;
    73   checkConcept<ReadWriteMap<A,double>, 
    77     ConstMap<A,B> map2(B());
    74     ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >();
    78     ConstMap<A,B> map3 = map1;
       
    79     map1 = constMap<A>(B());
       
    80     map1.setAll(B());
       
    81     
       
    82     checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
       
    83     check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
       
    84 
       
    85     checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
       
    86     ConstMap<A,Const<int,10> > map4;
       
    87     ConstMap<A,Const<int,10> > map5 = map4;
       
    88     map4 = map5;
       
    89     check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
       
    90   }
       
    91 
       
    92   // IdentityMap
       
    93   {
       
    94     checkConcept<ReadMap<A,A>, IdentityMap<A> >();
       
    95     IdentityMap<A> map1;
       
    96     IdentityMap<A> map2 = map1;
       
    97     map1 = identityMap<A>();
       
    98     
       
    99     checkConcept<ReadMap<double,double>, IdentityMap<double> >();
       
   100     check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
       
   101           "Something is wrong with IdentityMap");
       
   102   }
       
   103 
       
   104   // RangeMap
       
   105   {
       
   106     checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
       
   107     RangeMap<B> map1;
       
   108     RangeMap<B> map2(10);
       
   109     RangeMap<B> map3(10,B());
       
   110     RangeMap<B> map4 = map1;
       
   111     RangeMap<B> map5 = rangeMap<B>();
       
   112     RangeMap<B> map6 = rangeMap<B>(10);
       
   113     RangeMap<B> map7 = rangeMap(10,B());
       
   114 
       
   115     checkConcept< ReferenceMap<int, double, double&, const double&>,
       
   116                   RangeMap<double> >();
       
   117     std::vector<double> v(10, 0);
       
   118     v[5] = 100;
       
   119     RangeMap<double> map8(v);
       
   120     RangeMap<double> map9 = rangeMap(v);
       
   121     check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
       
   122           "Something is wrong with RangeMap");
       
   123   }
       
   124 
       
   125   // SparseMap
       
   126   {
       
   127     checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
       
   128     SparseMap<A,B> map1;
       
   129     SparseMap<A,B> map2(B());
       
   130     SparseMap<A,B> map3 = sparseMap<A,B>();
       
   131     SparseMap<A,B> map4 = sparseMap<A>(B());
       
   132 
       
   133     checkConcept< ReferenceMap<double, int, int&, const int&>,
       
   134                   SparseMap<double, int> >();
       
   135     std::map<double, int> m;
       
   136     SparseMap<double, int> map5(m);
       
   137     SparseMap<double, int> map6(m,10);
       
   138     SparseMap<double, int> map7 = sparseMap(m);
       
   139     SparseMap<double, int> map8 = sparseMap(m,10);
       
   140 
       
   141     check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
       
   142           "Something is wrong with SparseMap");
       
   143     map5[1.0] = map6[3.14] = 100;
       
   144     check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
       
   145           "Something is wrong with SparseMap");
       
   146   }
       
   147 
       
   148   // ComposeMap
       
   149   {
       
   150     typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
       
   151     checkConcept<ReadMap<B,double>, CompMap>();
       
   152     CompMap map1(DoubleMap(),ReadMap<B,A>());
       
   153     CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
       
   154     
       
   155     SparseMap<double, bool> m1(false); m1[3.14] = true;
       
   156     RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
       
   157     check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
       
   158   }
       
   159 
       
   160   // CombineMap
       
   161   {
       
   162     typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
       
   163     checkConcept<ReadMap<A,double>, CombMap>();
       
   164     CombMap map1(DoubleMap(), DoubleMap());
       
   165     CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
       
   166 
       
   167     check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
       
   168           "Something is wrong with CombineMap");
       
   169   }
       
   170 
       
   171   // FunctorToMap, MapToFunctor
       
   172   {
       
   173     checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
       
   174     checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
       
   175     FunctorToMap<F> map1;
       
   176     FunctorToMap<F> map2(F());
       
   177     B b = functorToMap(F())[A()];
       
   178 
       
   179     checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
       
   180     MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
       
   181 
       
   182     check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
       
   183     check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
       
   184     check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
       
   185           "Something is wrong with FunctorToMap or MapToFunctor");
       
   186     check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
       
   187           "Something is wrong with FunctorToMap or MapToFunctor");
       
   188   }
       
   189 
       
   190   // ConvertMap
       
   191   {
       
   192     checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
       
   193     ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
       
   194     ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
       
   195   }
       
   196 
       
   197   // ForkMap
       
   198   {
       
   199     checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
       
   200     
       
   201     typedef RangeMap<double> RM;
       
   202     typedef SparseMap<int, double> SM;
       
   203     RM m1(10, -1);
       
   204     SM m2(-1);
       
   205     checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
       
   206     checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
       
   207     ForkMap<RM, SM> map1(m1,m2);
       
   208     ForkMap<SM, RM> map2 = forkMap(m2,m1);
       
   209     map2.set(5, 10);
       
   210     check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
       
   211           "Something is wrong with ForkMap");
       
   212   }
    75   
   213   
    76   checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
   214   // Arithmetic maps:
    77 
   215   // - AddMap, SubMap, MulMap, DivMap
    78   checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
   216   // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
    79 
   217   // - NegMap, NegWriteMap, AbsMap
    80   checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
   218   {
    81   checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
   219     checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
    82 
   220     checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
    83   checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
   221     checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
    84   checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
   222     checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
    85   checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
   223     
    86   checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
   224     ConstMap<int, double> c1(1.0), c2(3.14);
    87   checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
   225     IdentityMap<int> im;
    88   checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
   226     ConvertMap<IdentityMap<int>, double> id(im);
    89 
   227     check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
    90   int a;
   228     check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
       
   229     check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
       
   230     check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
       
   231     
       
   232     checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
       
   233     checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
       
   234     checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
       
   235     checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
       
   236     checkConcept<DoubleMap, NegMap<DoubleMap> >();
       
   237     checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
       
   238     checkConcept<DoubleMap, AbsMap<DoubleMap> >();
       
   239 
       
   240     check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
       
   241           "Something is wrong with ShiftMap");
       
   242     check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
       
   243           "Something is wrong with ShiftWriteMap");
       
   244     check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
       
   245           "Something is wrong with ScaleMap");
       
   246     check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
       
   247           "Something is wrong with ScaleWriteMap");
       
   248     check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
       
   249           "Something is wrong with NegMap");
       
   250     check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
       
   251           "Something is wrong with NegWriteMap");
       
   252     check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
       
   253           "Something is wrong with AbsMap");
       
   254   }
    91   
   255   
    92   a=mapFunctor(constMap<A,int>(2))(A());
   256   // Logical maps
    93   check(a==2,"Something is wrong with mapFunctor");
   257   {
    94 
   258     checkConcept<BoolMap, NotMap<BoolMap> >();
    95   B b;
   259     checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
    96   b=functorMap(F())[A()];
   260     
    97 
   261     RangeMap<bool> rm(2);
    98   a=functorMap(&func)[A()];
   262     rm[0] = true; rm[1] = false;
    99   check(a==3,"Something is wrong with functorMap");
   263     check(!(notMap(rm)[0]) && notMap(rm)[1], "Something is wrong with NotMap");
   100 
   264     check(!(notWriteMap(rm)[0]) && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
   101   a=combineMap(constMap<B, int, 1>(), identityMap<B>(), &binc)[B()];
   265   }
   102   check(a==4,"Something is wrong with combineMap");
   266 
   103   
       
   104 
       
   105   std::cout << __FILE__ ": All tests passed.\n";
       
   106   
       
   107   return 0;
   267   return 0;
   108 }
   268 }