DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testBasicFunctors.cpp
1 
30 
31 #include <iostream>
32 #include <functional>
33 
34 #include "DGtal/base/Common.h"
35 
36 #include "DGtal/base/CUnaryFunctor.h"
37 #include "DGtal/base/BasicFunctors.h"
38 
40 
41 using namespace std;
42 using namespace DGtal;
43 
48 template <typename TFunctor, typename TArg, typename TRes >
49 void basicFunctorsConceptChecking()
50 {
51  BOOST_CONCEPT_ASSERT(( CUnaryFunctor<TFunctor, TArg, TRes > ));
52 }
57 bool testBasicFunctors()
58 {
59  unsigned int nbok = 0;
60  unsigned int nb = 0;
61 
62  trace.beginBlock ( "Testing basic functors ..." );
63 
64  //default functor
65  {
66  DefaultFunctor f;
67  int a = 5;
68  nbok += ( f(a) == 5 ) ? 1 : 0;
69  nb++;
70  }
71 
72  {//constant functor
73  const int v = -1;
75  char c = 'a';
76  nbok += ( f(c) == v ) ? 1 : 0;
77  nb++;
78  double d = 5.2;
79  nbok += ( f(d) == v ) ? 1 : 0;
80  nb++;
81  }
82 
83  //cast functor
84  {
86  char c = 'a';
87  nbok += ( f(c) == 97 ) ? 1 : 0;
88  nb++;
89  }
90 
91  //composer quantizer
92  {
93  //need to explicitely specialized std::ptr_fun because there are several
94  //overloaded versions of std::floor if used intead ctor of
95  //std::pointer_to_unary_function<double, double>
96  std::pointer_to_unary_function<double, double> f(std::floor);
97  std::pointer_to_unary_function<double, double> c(std::ceil);
99 
100  //composer
102  CastFunctor<int>, int > Quantizer;
103  double d = 5.2;
104 
105  Quantizer q(f, o);
106  nbok += ( q(d) == 5 ) ? 1 : 0;
107  nb++;
108 
109  Quantizer q2(c, o);
110  nbok += ( q2(d) == 6 ) ? 1 : 0;
111  nb++;
112  }
113 
114  //binary to unary functor
115  {
116  int i = -5;
117  std::binder2nd<std::minus<int> > b(std::minus<int>(), 0);
118  //i - 0
119  nbok += ( b(i) == -5 ) ? 1 : 0;
120  nb++;
121  std::binder2nd<std::plus<int> > b2(std::plus<int>(), 2);
122  //i + 2
123  nbok += ( b2(i) == -3 ) ? 1 : 0;
124  nb++;
125  }
126 
127  {//thresholder
128  int i = -3;
130  nbok += ( t(i) == true ) ? 1 : 0;
131  nb++;
133  nbok += ( t1(i) == true ) ? 1 : 0;
134  nb++;
136  nbok += ( t2(0) == false ) ? 1 : 0;
137  nb++;
139  nbok += ( t3(i) == false ) ? 1 : 0;
140  nb++;
142  nbok += ( t4(i) == false ) ? 1 : 0;
143  nb++;
144  }
145 
146  {//interval thresholder
147  const int low = 1;
148  const int up = 5;
149  IntervalThresholder< int > t(low, up);
150  nbok += ( t(0) == false ) ? 1 : 0;
151  nb++;
152  for (int i = low; i <= up; ++i)
153  {
154  nbok += ( t(i) == true ) ? 1 : 0;
155  nb++;
156  }
157  nbok += ( t(6) == false ) ? 1 : 0;
158  nb++;
159  }
160 
161 
162  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
163  trace.endBlock();
164 
165  return nbok == nb;
166 }
167 
169 // Standard services - public :
170 
171 int main( int argc, char** argv )
172 {
173  trace.beginBlock ( "Testing basic functors" );
174  trace.info() << "Args:";
175  for ( int i = 0; i < argc; ++i )
176  trace.info() << " " << argv[ i ];
177  trace.info() << endl;
178 
179  //concept checking
180  basicFunctorsConceptChecking<DefaultFunctor,int,int>();
181  basicFunctorsConceptChecking<ConstValueFunctor<int>,int,int >();
182  basicFunctorsConceptChecking<CastFunctor<int>,short,int >();
183  basicFunctorsConceptChecking<Thresholder<int>,int,bool >();
184  basicFunctorsConceptChecking<Composer<ConstValueFunctor<double>,CastFunctor<int>,int>,char,int >();
185 
186 
187  //run-time tests
188  bool res = testBasicFunctors();
189  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
190  trace.endBlock();
191  return res ? 0 : 1;
192 }
193 // //