test/maps_test.cc
changeset 388 0a3ec097a76c
parent 209 765619b7cbb2
child 440 88ed40ad0d4f
equal deleted inserted replaced
9:5782235b5818 10:88bcd79d9210
   101     checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
   101     checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
   102     ConstMap<A,Const<int,10> > map6;
   102     ConstMap<A,Const<int,10> > map6;
   103     ConstMap<A,Const<int,10> > map7 = map6;
   103     ConstMap<A,Const<int,10> > map7 = map6;
   104     map6 = constMap<A,int,10>();
   104     map6 = constMap<A,int,10>();
   105     map7 = constMap<A,Const<int,10> >();
   105     map7 = constMap<A,Const<int,10> >();
   106     check(map6[A()] == 10 && map7[A()] == 10, "Something is wrong with ConstMap");
   106     check(map6[A()] == 10 && map7[A()] == 10,
       
   107           "Something is wrong with ConstMap");
   107   }
   108   }
   108 
   109 
   109   // IdentityMap
   110   // IdentityMap
   110   {
   111   {
   111     checkConcept<ReadMap<A,A>, IdentityMap<A> >();
   112     checkConcept<ReadMap<A,A>, IdentityMap<A> >();
   112     IdentityMap<A> map1;
   113     IdentityMap<A> map1;
   113     IdentityMap<A> map2 = map1;
   114     IdentityMap<A> map2 = map1;
   114     map1 = identityMap<A>();
   115     map1 = identityMap<A>();
   115 
   116 
   116     checkConcept<ReadMap<double,double>, IdentityMap<double> >();
   117     checkConcept<ReadMap<double,double>, IdentityMap<double> >();
   117     check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
   118     check(identityMap<double>()[1.0] == 1.0 &&
       
   119           identityMap<double>()[3.14] == 3.14,
   118           "Something is wrong with IdentityMap");
   120           "Something is wrong with IdentityMap");
   119   }
   121   }
   120 
   122 
   121   // RangeMap
   123   // RangeMap
   122   {
   124   {
   153     SparseMap<double, int> map5(m);
   155     SparseMap<double, int> map5(m);
   154     SparseMap<double, int> map6(m,10);
   156     SparseMap<double, int> map6(m,10);
   155     SparseMap<double, int> map7 = sparseMap(m);
   157     SparseMap<double, int> map7 = sparseMap(m);
   156     SparseMap<double, int> map8 = sparseMap(m,10);
   158     SparseMap<double, int> map8 = sparseMap(m,10);
   157 
   159 
   158     check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
   160     check(map5[1.0] == 0 && map5[3.14] == 0 &&
       
   161           map6[1.0] == 10 && map6[3.14] == 10,
   159           "Something is wrong with SparseMap");
   162           "Something is wrong with SparseMap");
   160     map5[1.0] = map6[3.14] = 100;
   163     map5[1.0] = map6[3.14] = 100;
   161     check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
   164     check(map5[1.0] == 100 && map5[3.14] == 0 &&
       
   165           map6[1.0] == 10 && map6[3.14] == 100,
   162           "Something is wrong with SparseMap");
   166           "Something is wrong with SparseMap");
   163   }
   167   }
   164 
   168 
   165   // ComposeMap
   169   // ComposeMap
   166   {
   170   {
   169     CompMap map1(DoubleMap(),ReadMap<B,A>());
   173     CompMap map1(DoubleMap(),ReadMap<B,A>());
   170     CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
   174     CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
   171 
   175 
   172     SparseMap<double, bool> m1(false); m1[3.14] = true;
   176     SparseMap<double, bool> m1(false); m1[3.14] = true;
   173     RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
   177     RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
   174     check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
   178     check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
       
   179           "Something is wrong with ComposeMap")
   175   }
   180   }
   176 
   181 
   177   // CombineMap
   182   // CombineMap
   178   {
   183   {
   179     typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
   184     typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
   194     B b = functorToMap(F())[A()];
   199     B b = functorToMap(F())[A()];
   195 
   200 
   196     checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
   201     checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
   197     MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
   202     MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
   198 
   203 
   199     check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
   204     check(functorToMap(&func)[A()] == 3,
   200     check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
   205           "Something is wrong with FunctorToMap");
   201     check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
   206     check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
       
   207           "Something is wrong with MapToFunctor");
       
   208     check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
       
   209           mapToFunctor(functorToMap(&func))[A()] == 3,
   202           "Something is wrong with FunctorToMap or MapToFunctor");
   210           "Something is wrong with FunctorToMap or MapToFunctor");
   203     check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
   211     check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
   204           "Something is wrong with FunctorToMap or MapToFunctor");
   212           "Something is wrong with FunctorToMap or MapToFunctor");
   205   }
   213   }
   206 
   214 
   207   // ConvertMap
   215   // ConvertMap
   208   {
   216   {
   209     checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
   217     checkConcept<ReadMap<double,double>,
       
   218       ConvertMap<ReadMap<double, int>, double> >();
   210     ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
   219     ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
   211     ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
   220     ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
   212   }
   221   }
   213 
   222 
   214   // ForkMap
   223   // ForkMap
   222     checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
   231     checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
   223     checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
   232     checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
   224     ForkMap<RM, SM> map1(m1,m2);
   233     ForkMap<RM, SM> map1(m1,m2);
   225     ForkMap<SM, RM> map2 = forkMap(m2,m1);
   234     ForkMap<SM, RM> map2 = forkMap(m2,m1);
   226     map2.set(5, 10);
   235     map2.set(5, 10);
   227     check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
   236     check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
       
   237           m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
   228           "Something is wrong with ForkMap");
   238           "Something is wrong with ForkMap");
   229   }
   239   }
   230 
   240 
   231   // Arithmetic maps:
   241   // Arithmetic maps:
   232   // - AddMap, SubMap, MulMap, DivMap
   242   // - AddMap, SubMap, MulMap, DivMap
   239     checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
   249     checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
   240 
   250 
   241     ConstMap<int, double> c1(1.0), c2(3.14);
   251     ConstMap<int, double> c1(1.0), c2(3.14);
   242     IdentityMap<int> im;
   252     IdentityMap<int> im;
   243     ConvertMap<IdentityMap<int>, double> id(im);
   253     ConvertMap<IdentityMap<int>, double> id(im);
   244     check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
   254     check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
   245     check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
   255           "Something is wrong with AddMap");
   246     check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
   256     check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
   247     check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
   257           "Something is wrong with SubMap");
       
   258     check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
       
   259           "Something is wrong with MulMap");
       
   260     check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
       
   261           "Something is wrong with DivMap");
   248 
   262 
   249     checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
   263     checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
   250     checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
   264     checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
   251     checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
   265     checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
   252     checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
   266     checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
   254     checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
   268     checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
   255     checkConcept<DoubleMap, AbsMap<DoubleMap> >();
   269     checkConcept<DoubleMap, AbsMap<DoubleMap> >();
   256 
   270 
   257     check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
   271     check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
   258           "Something is wrong with ShiftMap");
   272           "Something is wrong with ShiftMap");
   259     check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
   273     check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
       
   274           shiftWriteMap(id, 2.0)[10] == 12.0,
   260           "Something is wrong with ShiftWriteMap");
   275           "Something is wrong with ShiftWriteMap");
   261     check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
   276     check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
   262           "Something is wrong with ScaleMap");
   277           "Something is wrong with ScaleMap");
   263     check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
   278     check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
       
   279           scaleWriteMap(id, 2.0)[10] == 20.0,
   264           "Something is wrong with ScaleWriteMap");
   280           "Something is wrong with ScaleWriteMap");
   265     check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
   281     check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
   266           "Something is wrong with NegMap");
   282           "Something is wrong with NegMap");
   267     check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
   283     check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
   268           "Something is wrong with NegWriteMap");
   284           "Something is wrong with NegWriteMap");
   287 
   303 
   288     TrueMap<int> tm;
   304     TrueMap<int> tm;
   289     FalseMap<int> fm;
   305     FalseMap<int> fm;
   290     RangeMap<bool> rm(2);
   306     RangeMap<bool> rm(2);
   291     rm[0] = true; rm[1] = false;
   307     rm[0] = true; rm[1] = false;
   292     check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
   308     check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
       
   309           !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
   293           "Something is wrong with AndMap");
   310           "Something is wrong with AndMap");
   294     check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
   311     check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
       
   312           orMap(fm,rm)[0] && !orMap(fm,rm)[1],
   295           "Something is wrong with OrMap");
   313           "Something is wrong with OrMap");
   296     check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
   314     check(!notMap(rm)[0] && notMap(rm)[1],
   297     check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
   315           "Something is wrong with NotMap");
       
   316     check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
       
   317           "Something is wrong with NotWriteMap");
   298 
   318 
   299     ConstMap<int, double> cm(2.0);
   319     ConstMap<int, double> cm(2.0);
   300     IdentityMap<int> im;
   320     IdentityMap<int> im;
   301     ConvertMap<IdentityMap<int>, double> id(im);
   321     ConvertMap<IdentityMap<int>, double> id(im);
   302     check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
   322     check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
   308   // LoggerBoolMap
   328   // LoggerBoolMap
   309   {
   329   {
   310     typedef std::vector<int> vec;
   330     typedef std::vector<int> vec;
   311     vec v1;
   331     vec v1;
   312     vec v2(10);
   332     vec v2(10);
   313     LoggerBoolMap<std::back_insert_iterator<vec> > map1(std::back_inserter(v1));
   333     LoggerBoolMap<std::back_insert_iterator<vec> >
       
   334       map1(std::back_inserter(v1));
   314     LoggerBoolMap<vec::iterator> map2(v2.begin());
   335     LoggerBoolMap<vec::iterator> map2(v2.begin());
   315     map1.set(10, false);
   336     map1.set(10, false);
   316     map1.set(20, true);   map2.set(20, true);
   337     map1.set(20, true);   map2.set(20, true);
   317     map1.set(30, false);  map2.set(40, false);
   338     map1.set(30, false);  map2.set(40, false);
   318     map1.set(50, true);   map2.set(50, true);
   339     map1.set(50, true);   map2.set(50, true);
   319     map1.set(60, true);   map2.set(60, true);
   340     map1.set(60, true);   map2.set(60, true);
   320     check(v1.size() == 3 && v2.size() == 10 &&
   341     check(v1.size() == 3 && v2.size() == 10 &&
   321           v1[0]==20 && v1[1]==50 && v1[2]==60 && v2[0]==20 && v2[1]==50 && v2[2]==60,
   342           v1[0]==20 && v1[1]==50 && v1[2]==60 &&
       
   343           v2[0]==20 && v2[1]==50 && v2[2]==60,
   322           "Something is wrong with LoggerBoolMap");
   344           "Something is wrong with LoggerBoolMap");
   323 
   345 
   324     int i = 0;
   346     int i = 0;
   325     for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
   347     for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
   326           it != map2.end(); ++it )
   348           it != map2.end(); ++it )