test/maps_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 15 Mar 2008 23:42:33 +0100
changeset 83 3654324ec035
parent 80 15968e25ca08
child 94 a4688e4138ec
permissions -rw-r--r--
Improvements in groups.dox

- Apply the graph renamings.
- Apply the current map renamings.
alpar@25
     1
/* -*- C++ -*-
alpar@25
     2
 *
alpar@25
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@25
     4
 *
alpar@39
     5
 * Copyright (C) 2003-2008
alpar@25
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@25
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@25
     8
 *
alpar@25
     9
 * Permission to use, modify and distribute this software is granted
alpar@25
    10
 * provided that this copyright notice appears in all copies. For
alpar@25
    11
 * precise terms see the accompanying LICENSE file.
alpar@25
    12
 *
alpar@25
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@25
    14
 * express or implied, and with no claim as to its suitability for any
alpar@25
    15
 * purpose.
alpar@25
    16
 *
alpar@25
    17
 */
alpar@25
    18
alpar@25
    19
#include <deque>
alpar@25
    20
#include <set>
alpar@25
    21
alpar@25
    22
#include <lemon/concept_check.h>
alpar@25
    23
#include <lemon/concepts/maps.h>
alpar@25
    24
#include <lemon/maps.h>
alpar@25
    25
alpar@25
    26
#include "test_tools.h"
alpar@25
    27
alpar@25
    28
using namespace lemon;
alpar@25
    29
using namespace lemon::concepts;
alpar@25
    30
alpar@25
    31
struct A {};
alpar@25
    32
inline bool operator<(A, A) { return true; }
alpar@25
    33
struct B {};
alpar@25
    34
alpar@25
    35
class F {
alpar@25
    36
public:
alpar@25
    37
  typedef A argument_type;
alpar@25
    38
  typedef B result_type;
alpar@25
    39
kpeter@80
    40
  B operator()(const A&) const { return B(); }
kpeter@80
    41
private:
kpeter@80
    42
  F& operator=(const F&);
alpar@25
    43
};
alpar@25
    44
kpeter@80
    45
int func(A) { return 3; }
alpar@25
    46
kpeter@80
    47
int binc(int a, B) { return a+1; }
alpar@25
    48
kpeter@80
    49
typedef ReadMap<A, double> DoubleMap;
kpeter@80
    50
typedef ReadWriteMap<A, double> DoubleWriteMap;
kpeter@80
    51
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
alpar@25
    52
kpeter@80
    53
typedef ReadMap<A, bool> BoolMap;
alpar@25
    54
typedef ReadWriteMap<A, bool> BoolWriteMap;
kpeter@80
    55
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
alpar@25
    56
alpar@25
    57
int main()
kpeter@80
    58
{
kpeter@80
    59
  // Map concepts
alpar@25
    60
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
alpar@25
    61
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
alpar@25
    62
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
alpar@25
    63
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
alpar@25
    64
kpeter@80
    65
  // NullMap
kpeter@80
    66
  {
kpeter@80
    67
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
kpeter@80
    68
    NullMap<A,B> map1;
kpeter@80
    69
    NullMap<A,B> map2 = map1;
kpeter@80
    70
    map1 = nullMap<A,B>();
kpeter@80
    71
  }
kpeter@80
    72
kpeter@80
    73
  // ConstMap
kpeter@80
    74
  {
kpeter@80
    75
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
kpeter@80
    76
    ConstMap<A,B> map1;
kpeter@80
    77
    ConstMap<A,B> map2(B());
kpeter@80
    78
    ConstMap<A,B> map3 = map1;
kpeter@80
    79
    map1 = constMap<A>(B());
kpeter@80
    80
    map1.setAll(B());
kpeter@82
    81
kpeter@80
    82
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
kpeter@80
    83
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
kpeter@80
    84
kpeter@80
    85
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
kpeter@80
    86
    ConstMap<A,Const<int,10> > map4;
kpeter@80
    87
    ConstMap<A,Const<int,10> > map5 = map4;
kpeter@80
    88
    map4 = map5;
kpeter@80
    89
    check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
kpeter@80
    90
  }
kpeter@80
    91
kpeter@80
    92
  // IdentityMap
kpeter@80
    93
  {
kpeter@80
    94
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
kpeter@80
    95
    IdentityMap<A> map1;
kpeter@80
    96
    IdentityMap<A> map2 = map1;
kpeter@80
    97
    map1 = identityMap<A>();
kpeter@82
    98
kpeter@80
    99
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
kpeter@80
   100
    check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
kpeter@80
   101
          "Something is wrong with IdentityMap");
kpeter@80
   102
  }
kpeter@80
   103
kpeter@80
   104
  // RangeMap
kpeter@80
   105
  {
kpeter@80
   106
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
kpeter@80
   107
    RangeMap<B> map1;
kpeter@80
   108
    RangeMap<B> map2(10);
kpeter@80
   109
    RangeMap<B> map3(10,B());
kpeter@80
   110
    RangeMap<B> map4 = map1;
kpeter@80
   111
    RangeMap<B> map5 = rangeMap<B>();
kpeter@80
   112
    RangeMap<B> map6 = rangeMap<B>(10);
kpeter@80
   113
    RangeMap<B> map7 = rangeMap(10,B());
kpeter@80
   114
kpeter@80
   115
    checkConcept< ReferenceMap<int, double, double&, const double&>,
kpeter@80
   116
                  RangeMap<double> >();
kpeter@80
   117
    std::vector<double> v(10, 0);
kpeter@80
   118
    v[5] = 100;
kpeter@80
   119
    RangeMap<double> map8(v);
kpeter@80
   120
    RangeMap<double> map9 = rangeMap(v);
kpeter@80
   121
    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
kpeter@80
   122
          "Something is wrong with RangeMap");
kpeter@80
   123
  }
kpeter@80
   124
kpeter@80
   125
  // SparseMap
kpeter@80
   126
  {
kpeter@80
   127
    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
kpeter@80
   128
    SparseMap<A,B> map1;
kpeter@80
   129
    SparseMap<A,B> map2(B());
kpeter@80
   130
    SparseMap<A,B> map3 = sparseMap<A,B>();
kpeter@80
   131
    SparseMap<A,B> map4 = sparseMap<A>(B());
kpeter@80
   132
kpeter@80
   133
    checkConcept< ReferenceMap<double, int, int&, const int&>,
kpeter@80
   134
                  SparseMap<double, int> >();
kpeter@80
   135
    std::map<double, int> m;
kpeter@80
   136
    SparseMap<double, int> map5(m);
kpeter@80
   137
    SparseMap<double, int> map6(m,10);
kpeter@80
   138
    SparseMap<double, int> map7 = sparseMap(m);
kpeter@80
   139
    SparseMap<double, int> map8 = sparseMap(m,10);
kpeter@80
   140
kpeter@80
   141
    check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
kpeter@80
   142
          "Something is wrong with SparseMap");
kpeter@80
   143
    map5[1.0] = map6[3.14] = 100;
kpeter@80
   144
    check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
kpeter@80
   145
          "Something is wrong with SparseMap");
kpeter@80
   146
  }
kpeter@80
   147
kpeter@80
   148
  // ComposeMap
kpeter@80
   149
  {
kpeter@80
   150
    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
kpeter@80
   151
    checkConcept<ReadMap<B,double>, CompMap>();
kpeter@80
   152
    CompMap map1(DoubleMap(),ReadMap<B,A>());
kpeter@80
   153
    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
kpeter@82
   154
kpeter@80
   155
    SparseMap<double, bool> m1(false); m1[3.14] = true;
kpeter@80
   156
    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
kpeter@80
   157
    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
kpeter@80
   158
  }
kpeter@80
   159
kpeter@80
   160
  // CombineMap
kpeter@80
   161
  {
kpeter@80
   162
    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
kpeter@80
   163
    checkConcept<ReadMap<A,double>, CombMap>();
kpeter@80
   164
    CombMap map1(DoubleMap(), DoubleMap());
kpeter@80
   165
    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
kpeter@80
   166
kpeter@80
   167
    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
kpeter@80
   168
          "Something is wrong with CombineMap");
kpeter@80
   169
  }
kpeter@80
   170
kpeter@80
   171
  // FunctorToMap, MapToFunctor
kpeter@80
   172
  {
kpeter@80
   173
    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
kpeter@80
   174
    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
kpeter@80
   175
    FunctorToMap<F> map1;
kpeter@80
   176
    FunctorToMap<F> map2(F());
kpeter@80
   177
    B b = functorToMap(F())[A()];
kpeter@80
   178
kpeter@80
   179
    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
kpeter@80
   180
    MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
kpeter@80
   181
kpeter@80
   182
    check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
kpeter@80
   183
    check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
kpeter@80
   184
    check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
kpeter@80
   185
          "Something is wrong with FunctorToMap or MapToFunctor");
kpeter@80
   186
    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
kpeter@80
   187
          "Something is wrong with FunctorToMap or MapToFunctor");
kpeter@80
   188
  }
kpeter@80
   189
kpeter@80
   190
  // ConvertMap
kpeter@80
   191
  {
kpeter@80
   192
    checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
kpeter@80
   193
    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
kpeter@80
   194
    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
kpeter@80
   195
  }
kpeter@80
   196
kpeter@80
   197
  // ForkMap
kpeter@80
   198
  {
kpeter@80
   199
    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
kpeter@82
   200
kpeter@80
   201
    typedef RangeMap<double> RM;
kpeter@80
   202
    typedef SparseMap<int, double> SM;
kpeter@80
   203
    RM m1(10, -1);
kpeter@80
   204
    SM m2(-1);
kpeter@80
   205
    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
kpeter@80
   206
    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
kpeter@80
   207
    ForkMap<RM, SM> map1(m1,m2);
kpeter@80
   208
    ForkMap<SM, RM> map2 = forkMap(m2,m1);
kpeter@80
   209
    map2.set(5, 10);
kpeter@80
   210
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
kpeter@80
   211
          "Something is wrong with ForkMap");
kpeter@80
   212
  }
kpeter@82
   213
kpeter@80
   214
  // Arithmetic maps:
kpeter@80
   215
  // - AddMap, SubMap, MulMap, DivMap
kpeter@80
   216
  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
kpeter@80
   217
  // - NegMap, NegWriteMap, AbsMap
kpeter@80
   218
  {
kpeter@80
   219
    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
kpeter@80
   220
    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
kpeter@80
   221
    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
kpeter@80
   222
    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
kpeter@82
   223
kpeter@80
   224
    ConstMap<int, double> c1(1.0), c2(3.14);
kpeter@80
   225
    IdentityMap<int> im;
kpeter@80
   226
    ConvertMap<IdentityMap<int>, double> id(im);
kpeter@80
   227
    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
kpeter@80
   228
    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
kpeter@80
   229
    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
kpeter@80
   230
    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
kpeter@82
   231
kpeter@80
   232
    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
kpeter@80
   233
    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
kpeter@80
   234
    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
kpeter@80
   235
    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
kpeter@80
   236
    checkConcept<DoubleMap, NegMap<DoubleMap> >();
kpeter@80
   237
    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
kpeter@80
   238
    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
alpar@25
   239
kpeter@80
   240
    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
kpeter@80
   241
          "Something is wrong with ShiftMap");
kpeter@80
   242
    check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
kpeter@80
   243
          "Something is wrong with ShiftWriteMap");
kpeter@80
   244
    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
kpeter@80
   245
          "Something is wrong with ScaleMap");
kpeter@80
   246
    check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
kpeter@80
   247
          "Something is wrong with ScaleWriteMap");
kpeter@80
   248
    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
kpeter@80
   249
          "Something is wrong with NegMap");
kpeter@80
   250
    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
kpeter@80
   251
          "Something is wrong with NegWriteMap");
kpeter@80
   252
    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
kpeter@80
   253
          "Something is wrong with AbsMap");
kpeter@80
   254
  }
kpeter@82
   255
kpeter@82
   256
  // Logical maps:
kpeter@82
   257
  // - TrueMap, FalseMap
kpeter@82
   258
  // - AndMap, OrMap
kpeter@82
   259
  // - NotMap, NotWriteMap
kpeter@82
   260
  // - EqualMap, LessMap
kpeter@80
   261
  {
kpeter@82
   262
    checkConcept<BoolMap, TrueMap<A> >();
kpeter@82
   263
    checkConcept<BoolMap, FalseMap<A> >();
kpeter@82
   264
    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
kpeter@82
   265
    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
kpeter@80
   266
    checkConcept<BoolMap, NotMap<BoolMap> >();
kpeter@80
   267
    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
kpeter@82
   268
    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
kpeter@82
   269
    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
kpeter@82
   270
kpeter@82
   271
    TrueMap<int> tm;
kpeter@82
   272
    FalseMap<int> fm;
kpeter@80
   273
    RangeMap<bool> rm(2);
kpeter@80
   274
    rm[0] = true; rm[1] = false;
kpeter@82
   275
    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
kpeter@82
   276
          "Something is wrong with AndMap");
kpeter@82
   277
    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
kpeter@82
   278
          "Something is wrong with OrMap");
kpeter@82
   279
    check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
kpeter@82
   280
    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
kpeter@82
   281
kpeter@82
   282
    ConstMap<int, double> cm(2.0);
kpeter@82
   283
    IdentityMap<int> im;
kpeter@82
   284
    ConvertMap<IdentityMap<int>, double> id(im);
kpeter@82
   285
    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
kpeter@82
   286
          "Something is wrong with LessMap");
kpeter@82
   287
    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
kpeter@82
   288
          "Something is wrong with EqualMap");
kpeter@80
   289
  }
alpar@25
   290
alpar@25
   291
  return 0;
alpar@25
   292
}