test/maps_test.cc
changeset 80 15968e25ca08
parent 39 0a01d811071f
child 82 bce6c8f93d10
     1.1 --- a/test/maps_test.cc	Sat Mar 15 20:21:21 2008 +0100
     1.2 +++ b/test/maps_test.cc	Sat Mar 15 21:07:24 2008 +0100
     1.3 @@ -37,72 +37,232 @@
     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 +  // NullMap
    1.55 +  {
    1.56 +    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
    1.57 +    NullMap<A,B> map1;
    1.58 +    NullMap<A,B> map2 = map1;
    1.59 +    map1 = nullMap<A,B>();
    1.60 +  }
    1.61 +
    1.62 +  // ConstMap
    1.63 +  {
    1.64 +    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
    1.65 +    ConstMap<A,B> map1;
    1.66 +    ConstMap<A,B> map2(B());
    1.67 +    ConstMap<A,B> map3 = map1;
    1.68 +    map1 = constMap<A>(B());
    1.69 +    map1.setAll(B());
    1.70 +    
    1.71 +    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
    1.72 +    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
    1.73 +
    1.74 +    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
    1.75 +    ConstMap<A,Const<int,10> > map4;
    1.76 +    ConstMap<A,Const<int,10> > map5 = map4;
    1.77 +    map4 = map5;
    1.78 +    check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
    1.79 +  }
    1.80 +
    1.81 +  // IdentityMap
    1.82 +  {
    1.83 +    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
    1.84 +    IdentityMap<A> map1;
    1.85 +    IdentityMap<A> map2 = map1;
    1.86 +    map1 = identityMap<A>();
    1.87 +    
    1.88 +    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
    1.89 +    check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
    1.90 +          "Something is wrong with IdentityMap");
    1.91 +  }
    1.92 +
    1.93 +  // RangeMap
    1.94 +  {
    1.95 +    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
    1.96 +    RangeMap<B> map1;
    1.97 +    RangeMap<B> map2(10);
    1.98 +    RangeMap<B> map3(10,B());
    1.99 +    RangeMap<B> map4 = map1;
   1.100 +    RangeMap<B> map5 = rangeMap<B>();
   1.101 +    RangeMap<B> map6 = rangeMap<B>(10);
   1.102 +    RangeMap<B> map7 = rangeMap(10,B());
   1.103 +
   1.104 +    checkConcept< ReferenceMap<int, double, double&, const double&>,
   1.105 +                  RangeMap<double> >();
   1.106 +    std::vector<double> v(10, 0);
   1.107 +    v[5] = 100;
   1.108 +    RangeMap<double> map8(v);
   1.109 +    RangeMap<double> map9 = rangeMap(v);
   1.110 +    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
   1.111 +          "Something is wrong with RangeMap");
   1.112 +  }
   1.113 +
   1.114 +  // SparseMap
   1.115 +  {
   1.116 +    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
   1.117 +    SparseMap<A,B> map1;
   1.118 +    SparseMap<A,B> map2(B());
   1.119 +    SparseMap<A,B> map3 = sparseMap<A,B>();
   1.120 +    SparseMap<A,B> map4 = sparseMap<A>(B());
   1.121 +
   1.122 +    checkConcept< ReferenceMap<double, int, int&, const int&>,
   1.123 +                  SparseMap<double, int> >();
   1.124 +    std::map<double, int> m;
   1.125 +    SparseMap<double, int> map5(m);
   1.126 +    SparseMap<double, int> map6(m,10);
   1.127 +    SparseMap<double, int> map7 = sparseMap(m);
   1.128 +    SparseMap<double, int> map8 = sparseMap(m,10);
   1.129 +
   1.130 +    check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
   1.131 +          "Something is wrong with SparseMap");
   1.132 +    map5[1.0] = map6[3.14] = 100;
   1.133 +    check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
   1.134 +          "Something is wrong with SparseMap");
   1.135 +  }
   1.136 +
   1.137 +  // ComposeMap
   1.138 +  {
   1.139 +    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
   1.140 +    checkConcept<ReadMap<B,double>, CompMap>();
   1.141 +    CompMap map1(DoubleMap(),ReadMap<B,A>());
   1.142 +    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
   1.143 +    
   1.144 +    SparseMap<double, bool> m1(false); m1[3.14] = true;
   1.145 +    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
   1.146 +    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
   1.147 +  }
   1.148 +
   1.149 +  // CombineMap
   1.150 +  {
   1.151 +    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
   1.152 +    checkConcept<ReadMap<A,double>, CombMap>();
   1.153 +    CombMap map1(DoubleMap(), DoubleMap());
   1.154 +    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
   1.155 +
   1.156 +    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
   1.157 +          "Something is wrong with CombineMap");
   1.158 +  }
   1.159 +
   1.160 +  // FunctorToMap, MapToFunctor
   1.161 +  {
   1.162 +    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
   1.163 +    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
   1.164 +    FunctorToMap<F> map1;
   1.165 +    FunctorToMap<F> map2(F());
   1.166 +    B b = functorToMap(F())[A()];
   1.167 +
   1.168 +    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
   1.169 +    MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
   1.170 +
   1.171 +    check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
   1.172 +    check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
   1.173 +    check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
   1.174 +          "Something is wrong with FunctorToMap or MapToFunctor");
   1.175 +    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
   1.176 +          "Something is wrong with FunctorToMap or MapToFunctor");
   1.177 +  }
   1.178 +
   1.179 +  // ConvertMap
   1.180 +  {
   1.181 +    checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
   1.182 +    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
   1.183 +    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
   1.184 +  }
   1.185 +
   1.186 +  // ForkMap
   1.187 +  {
   1.188 +    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
   1.189 +    
   1.190 +    typedef RangeMap<double> RM;
   1.191 +    typedef SparseMap<int, double> SM;
   1.192 +    RM m1(10, -1);
   1.193 +    SM m2(-1);
   1.194 +    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
   1.195 +    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
   1.196 +    ForkMap<RM, SM> map1(m1,m2);
   1.197 +    ForkMap<SM, RM> map2 = forkMap(m2,m1);
   1.198 +    map2.set(5, 10);
   1.199 +    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
   1.200 +          "Something is wrong with ForkMap");
   1.201 +  }
   1.202    
   1.203 -  checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
   1.204 +  // Arithmetic maps:
   1.205 +  // - AddMap, SubMap, MulMap, DivMap
   1.206 +  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
   1.207 +  // - NegMap, NegWriteMap, AbsMap
   1.208 +  {
   1.209 +    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
   1.210 +    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
   1.211 +    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
   1.212 +    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
   1.213 +    
   1.214 +    ConstMap<int, double> c1(1.0), c2(3.14);
   1.215 +    IdentityMap<int> im;
   1.216 +    ConvertMap<IdentityMap<int>, double> id(im);
   1.217 +    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
   1.218 +    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
   1.219 +    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
   1.220 +    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
   1.221 +    
   1.222 +    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
   1.223 +    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
   1.224 +    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
   1.225 +    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
   1.226 +    checkConcept<DoubleMap, NegMap<DoubleMap> >();
   1.227 +    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
   1.228 +    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
   1.229  
   1.230 -  checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >();
   1.231 +    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
   1.232 +          "Something is wrong with ShiftMap");
   1.233 +    check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
   1.234 +          "Something is wrong with ShiftWriteMap");
   1.235 +    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
   1.236 +          "Something is wrong with ScaleMap");
   1.237 +    check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
   1.238 +          "Something is wrong with ScaleWriteMap");
   1.239 +    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
   1.240 +          "Something is wrong with NegMap");
   1.241 +    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
   1.242 +          "Something is wrong with NegWriteMap");
   1.243 +    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
   1.244 +          "Something is wrong with AbsMap");
   1.245 +  }
   1.246 +  
   1.247 +  // Logical maps
   1.248 +  {
   1.249 +    checkConcept<BoolMap, NotMap<BoolMap> >();
   1.250 +    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
   1.251 +    
   1.252 +    RangeMap<bool> rm(2);
   1.253 +    rm[0] = true; rm[1] = false;
   1.254 +    check(!(notMap(rm)[0]) && notMap(rm)[1], "Something is wrong with NotMap");
   1.255 +    check(!(notWriteMap(rm)[0]) && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
   1.256 +  }
   1.257  
   1.258 -  checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >();
   1.259 -  checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >();
   1.260 -
   1.261 -  checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >();
   1.262 -  checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >();
   1.263 -  checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >();
   1.264 -  checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >();
   1.265 -  checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >();
   1.266 -  checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >();
   1.267 -
   1.268 -  int a;
   1.269 -  
   1.270 -  a=mapFunctor(constMap<A,int>(2))(A());
   1.271 -  check(a==2,"Something is wrong with mapFunctor");
   1.272 -
   1.273 -  B b;
   1.274 -  b=functorMap(F())[A()];
   1.275 -
   1.276 -  a=functorMap(&func)[A()];
   1.277 -  check(a==3,"Something is wrong with functorMap");
   1.278 -
   1.279 -  a=combineMap(constMap<B, int, 1>(), identityMap<B>(), &binc)[B()];
   1.280 -  check(a==4,"Something is wrong with combineMap");
   1.281 -  
   1.282 -
   1.283 -  std::cout << __FILE__ ": All tests passed.\n";
   1.284 -  
   1.285    return 0;
   1.286  }