test/maps_test.cc
changeset 84 8161012eaa61
parent 80 15968e25ca08
child 94 a4688e4138ec
     1.1 --- a/test/maps_test.cc	Thu Feb 28 17:06:02 2008 +0100
     1.2 +++ b/test/maps_test.cc	Sun Mar 16 07:32:43 2008 +0000
     1.3 @@ -37,72 +37,256 @@
     1.4    typedef A argument_type;
     1.5    typedef B result_type;
     1.6  
     1.7 -  B operator()(const A &) const {return B();}
     1.8 +  B operator()(const A&) const { return B(); }
     1.9 +private:
    1.10 +  F& operator=(const F&);
    1.11  };
    1.12  
    1.13 -int func(A) {return 3;}
    1.14 +int func(A) { return 3; }
    1.15  
    1.16 -int binc(int, B) {return 4;}
    1.17 +int binc(int a, B) { return a+1; }
    1.18  
    1.19 -typedef ReadMap<A,double> DoubleMap;
    1.20 -typedef ReadWriteMap<A, double> WriteDoubleMap;
    1.21 +typedef ReadMap<A, double> DoubleMap;
    1.22 +typedef ReadWriteMap<A, double> DoubleWriteMap;
    1.23 +typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
    1.24  
    1.25 -typedef ReadMap<A,bool> BoolMap;
    1.26 +typedef ReadMap<A, bool> BoolMap;
    1.27  typedef ReadWriteMap<A, bool> BoolWriteMap;
    1.28 +typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
    1.29  
    1.30  int main()
    1.31 -{ // checking graph components
    1.32 -  
    1.33 +{
    1.34 +  // Map concepts
    1.35    checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
    1.36    checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
    1.37    checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
    1.38    checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
    1.39  
    1.40 -  checkConcept<ReadMap<A,double>, AddMap<DoubleMap,DoubleMap> >();
    1.41 -  checkConcept<ReadMap<A,double>, SubMap<DoubleMap,DoubleMap> >();
    1.42 -  checkConcept<ReadMap<A,double>, MulMap<DoubleMap,DoubleMap> >();
    1.43 -  checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
    1.44 -  checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
    1.45 -  checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
    1.46 -  checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
    1.47 -  checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
    1.48 -  checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
    1.49 -  checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >();
    1.50 -  checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >();
    1.51 -  checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >();
    1.52 -  checkConcept<ReadWriteMap<A,double>, 
    1.53 -    ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >();
    1.54 -  
    1.55 -  checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
    1.56 +  // NullMap
    1.57 +  {
    1.58 +    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
    1.59 +    NullMap<A,B> map1;
    1.60 +    NullMap<A,B> map2 = map1;
    1.61 +    map1 = nullMap<A,B>();
    1.62 +  }
    1.63  
    1.64 -  checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
    1.65 +  // ConstMap
    1.66 +  {
    1.67 +    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
    1.68 +    ConstMap<A,B> map1;
    1.69 +    ConstMap<A,B> map2(B());
    1.70 +    ConstMap<A,B> map3 = map1;
    1.71 +    map1 = constMap<A>(B());
    1.72 +    map1.setAll(B());
    1.73  
    1.74 -  checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
    1.75 -  checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
    1.76 +    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
    1.77 +    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
    1.78  
    1.79 -  checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
    1.80 -  checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
    1.81 -  checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
    1.82 -  checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
    1.83 -  checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
    1.84 -  checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
    1.85 +    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
    1.86 +    ConstMap<A,Const<int,10> > map4;
    1.87 +    ConstMap<A,Const<int,10> > map5 = map4;
    1.88 +    map4 = map5;
    1.89 +    check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
    1.90 +  }
    1.91  
    1.92 -  int a;
    1.93 -  
    1.94 -  a=mapFunctor(constMap<A,int>(2))(A());
    1.95 -  check(a==2,"Something is wrong with mapFunctor");
    1.96 +  // IdentityMap
    1.97 +  {
    1.98 +    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
    1.99 +    IdentityMap<A> map1;
   1.100 +    IdentityMap<A> map2 = map1;
   1.101 +    map1 = identityMap<A>();
   1.102  
   1.103 -  B b;
   1.104 -  b=functorMap(F())[A()];
   1.105 +    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
   1.106 +    check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
   1.107 +          "Something is wrong with IdentityMap");
   1.108 +  }
   1.109  
   1.110 -  a=functorMap(&func)[A()];
   1.111 -  check(a==3,"Something is wrong with functorMap");
   1.112 +  // RangeMap
   1.113 +  {
   1.114 +    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
   1.115 +    RangeMap<B> map1;
   1.116 +    RangeMap<B> map2(10);
   1.117 +    RangeMap<B> map3(10,B());
   1.118 +    RangeMap<B> map4 = map1;
   1.119 +    RangeMap<B> map5 = rangeMap<B>();
   1.120 +    RangeMap<B> map6 = rangeMap<B>(10);
   1.121 +    RangeMap<B> map7 = rangeMap(10,B());
   1.122  
   1.123 -  a=combineMap(constMap<B, int, 1>(), identityMap<B>(), &binc)[B()];
   1.124 -  check(a==4,"Something is wrong with combineMap");
   1.125 -  
   1.126 +    checkConcept< ReferenceMap<int, double, double&, const double&>,
   1.127 +                  RangeMap<double> >();
   1.128 +    std::vector<double> v(10, 0);
   1.129 +    v[5] = 100;
   1.130 +    RangeMap<double> map8(v);
   1.131 +    RangeMap<double> map9 = rangeMap(v);
   1.132 +    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
   1.133 +          "Something is wrong with RangeMap");
   1.134 +  }
   1.135  
   1.136 -  std::cout << __FILE__ ": All tests passed.\n";
   1.137 -  
   1.138 +  // SparseMap
   1.139 +  {
   1.140 +    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
   1.141 +    SparseMap<A,B> map1;
   1.142 +    SparseMap<A,B> map2(B());
   1.143 +    SparseMap<A,B> map3 = sparseMap<A,B>();
   1.144 +    SparseMap<A,B> map4 = sparseMap<A>(B());
   1.145 +
   1.146 +    checkConcept< ReferenceMap<double, int, int&, const int&>,
   1.147 +                  SparseMap<double, int> >();
   1.148 +    std::map<double, int> m;
   1.149 +    SparseMap<double, int> map5(m);
   1.150 +    SparseMap<double, int> map6(m,10);
   1.151 +    SparseMap<double, int> map7 = sparseMap(m);
   1.152 +    SparseMap<double, int> map8 = sparseMap(m,10);
   1.153 +
   1.154 +    check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
   1.155 +          "Something is wrong with SparseMap");
   1.156 +    map5[1.0] = map6[3.14] = 100;
   1.157 +    check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
   1.158 +          "Something is wrong with SparseMap");
   1.159 +  }
   1.160 +
   1.161 +  // ComposeMap
   1.162 +  {
   1.163 +    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
   1.164 +    checkConcept<ReadMap<B,double>, CompMap>();
   1.165 +    CompMap map1(DoubleMap(),ReadMap<B,A>());
   1.166 +    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
   1.167 +
   1.168 +    SparseMap<double, bool> m1(false); m1[3.14] = true;
   1.169 +    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
   1.170 +    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
   1.171 +  }
   1.172 +
   1.173 +  // CombineMap
   1.174 +  {
   1.175 +    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
   1.176 +    checkConcept<ReadMap<A,double>, CombMap>();
   1.177 +    CombMap map1(DoubleMap(), DoubleMap());
   1.178 +    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
   1.179 +
   1.180 +    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
   1.181 +          "Something is wrong with CombineMap");
   1.182 +  }
   1.183 +
   1.184 +  // FunctorToMap, MapToFunctor
   1.185 +  {
   1.186 +    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
   1.187 +    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
   1.188 +    FunctorToMap<F> map1;
   1.189 +    FunctorToMap<F> map2(F());
   1.190 +    B b = functorToMap(F())[A()];
   1.191 +
   1.192 +    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
   1.193 +    MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
   1.194 +
   1.195 +    check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
   1.196 +    check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
   1.197 +    check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
   1.198 +          "Something is wrong with FunctorToMap or MapToFunctor");
   1.199 +    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
   1.200 +          "Something is wrong with FunctorToMap or MapToFunctor");
   1.201 +  }
   1.202 +
   1.203 +  // ConvertMap
   1.204 +  {
   1.205 +    checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
   1.206 +    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
   1.207 +    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
   1.208 +  }
   1.209 +
   1.210 +  // ForkMap
   1.211 +  {
   1.212 +    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
   1.213 +
   1.214 +    typedef RangeMap<double> RM;
   1.215 +    typedef SparseMap<int, double> SM;
   1.216 +    RM m1(10, -1);
   1.217 +    SM m2(-1);
   1.218 +    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
   1.219 +    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
   1.220 +    ForkMap<RM, SM> map1(m1,m2);
   1.221 +    ForkMap<SM, RM> map2 = forkMap(m2,m1);
   1.222 +    map2.set(5, 10);
   1.223 +    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
   1.224 +          "Something is wrong with ForkMap");
   1.225 +  }
   1.226 +
   1.227 +  // Arithmetic maps:
   1.228 +  // - AddMap, SubMap, MulMap, DivMap
   1.229 +  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
   1.230 +  // - NegMap, NegWriteMap, AbsMap
   1.231 +  {
   1.232 +    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
   1.233 +    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
   1.234 +    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
   1.235 +    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
   1.236 +
   1.237 +    ConstMap<int, double> c1(1.0), c2(3.14);
   1.238 +    IdentityMap<int> im;
   1.239 +    ConvertMap<IdentityMap<int>, double> id(im);
   1.240 +    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
   1.241 +    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
   1.242 +    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
   1.243 +    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
   1.244 +
   1.245 +    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
   1.246 +    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
   1.247 +    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
   1.248 +    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
   1.249 +    checkConcept<DoubleMap, NegMap<DoubleMap> >();
   1.250 +    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
   1.251 +    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
   1.252 +
   1.253 +    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
   1.254 +          "Something is wrong with ShiftMap");
   1.255 +    check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
   1.256 +          "Something is wrong with ShiftWriteMap");
   1.257 +    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
   1.258 +          "Something is wrong with ScaleMap");
   1.259 +    check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
   1.260 +          "Something is wrong with ScaleWriteMap");
   1.261 +    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
   1.262 +          "Something is wrong with NegMap");
   1.263 +    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
   1.264 +          "Something is wrong with NegWriteMap");
   1.265 +    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
   1.266 +          "Something is wrong with AbsMap");
   1.267 +  }
   1.268 +
   1.269 +  // Logical maps:
   1.270 +  // - TrueMap, FalseMap
   1.271 +  // - AndMap, OrMap
   1.272 +  // - NotMap, NotWriteMap
   1.273 +  // - EqualMap, LessMap
   1.274 +  {
   1.275 +    checkConcept<BoolMap, TrueMap<A> >();
   1.276 +    checkConcept<BoolMap, FalseMap<A> >();
   1.277 +    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
   1.278 +    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
   1.279 +    checkConcept<BoolMap, NotMap<BoolMap> >();
   1.280 +    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
   1.281 +    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
   1.282 +    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
   1.283 +
   1.284 +    TrueMap<int> tm;
   1.285 +    FalseMap<int> fm;
   1.286 +    RangeMap<bool> rm(2);
   1.287 +    rm[0] = true; rm[1] = false;
   1.288 +    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
   1.289 +          "Something is wrong with AndMap");
   1.290 +    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
   1.291 +          "Something is wrong with OrMap");
   1.292 +    check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
   1.293 +    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
   1.294 +
   1.295 +    ConstMap<int, double> cm(2.0);
   1.296 +    IdentityMap<int> im;
   1.297 +    ConvertMap<IdentityMap<int>, double> id(im);
   1.298 +    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
   1.299 +          "Something is wrong with LessMap");
   1.300 +    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
   1.301 +          "Something is wrong with EqualMap");
   1.302 +  }
   1.303 +
   1.304    return 0;
   1.305  }