DGtal  1.2.0
testBasicFunctors.cpp
Go to the documentation of this file.
1 
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 >
50 {
51  BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, TArg, TRes > ));
52 }
58 {
59  unsigned int nbok = 0;
60  unsigned int nb = 0;
61 
62  trace.beginBlock ( "Testing basic functors ..." );
63 
64  //default functor
65  {
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  //rounding functors
92  {
93  const double v1 = -3.5;
94  const double v2 = 3.5;
95 
96  { const DGtal::functors::Round<double> f; nbok += ( f(v1) == -4. && f(v2) == 4. ) ? 1 : 0; }
97  { const DGtal::functors::Round<> f; nbok += ( f(v1) == -4. && f(v2) == 4. ) ? 1 : 0; }
98  { const DGtal::functors::Floor<double> f; nbok += ( f(v1) == -4. && f(v2) == 3. ) ? 1 : 0; }
99  { const DGtal::functors::Floor<> f; nbok += ( f(v1) == -4. && f(v2) == 3. ) ? 1 : 0; }
100  { const DGtal::functors::Ceil<double> f; nbok += ( f(v1) == -3. && f(v2) == 4. ) ? 1 : 0; }
101  { const DGtal::functors::Ceil<> f; nbok += ( f(v1) == -3. && f(v2) == 4. ) ? 1 : 0; }
102  { const DGtal::functors::Trunc<double> f; nbok += ( f(v1) == -3. && f(v2) == 3. ) ? 1 : 0; }
103  { const DGtal::functors::Trunc<> f; nbok += ( f(v1) == -3. && f(v2) == 3. ) ? 1 : 0; }
104 
105  nb += 8;
106  }
107 
108  //composer quantizer
109  {
110  //need to explicitely specialized std::ptr_fun because there are several
111  //overloaded versions of std::floor if used intead ctor of
112  //std::pointer_to_unary_function<double, double>
113  std::pointer_to_unary_function<double, double> f(std::floor);
114  std::pointer_to_unary_function<double, double> c(std::ceil);
116 
117  //composer
119  functors::Cast<int>, int > Quantizer;
120  double d = 5.2;
121 
122  Quantizer q(f, o);
123  nbok += ( q(d) == 5 ) ? 1 : 0;
124  nb++;
125 
126  Quantizer q2(c, o);
127  nbok += ( q2(d) == 6 ) ? 1 : 0;
128  nb++;
129  }
130 
131  //binary to unary functor
132  {
133  int i = -5;
134  // With function and bind:
135  std::function<int(int)> b = std::bind(std::minus<int>(), std::placeholders::_1, 0);
136  //i - 0
137  nbok += ( b(i) == -5 ) ? 1 : 0;
138  nb++;
139  // With a lambda:
140  auto b2 = [](int v) -> int {
141  return v + 2;
142  };
143  //i + 2
144  nbok += ( b2(i) == -3 ) ? 1 : 0;
145  nb++;
146  }
147 
148  {//thresholder
149  int i = -3;
151  nbok += ( t(i) == true ) ? 1 : 0;
152  nb++;
154  nbok += ( t1(i) == true ) ? 1 : 0;
155  nb++;
157  nbok += ( t2(0) == false ) ? 1 : 0;
158  nb++;
160  nbok += ( t3(i) == false ) ? 1 : 0;
161  nb++;
163  nbok += ( t4(i) == false ) ? 1 : 0;
164  nb++;
165  }
166 
167  {//interval thresholder
168  const int low = 1;
169  const int up = 5;
171  nbok += ( t(0) == false ) ? 1 : 0;
172  nb++;
173  for (int i = low; i <= up; ++i)
174  {
175  nbok += ( t(i) == true ) ? 1 : 0;
176  nb++;
177  }
178  nbok += ( t(6) == false ) ? 1 : 0;
179  nb++;
180  }
181 
182 
183  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
184  trace.endBlock();
185 
186  return nbok == nb;
187 }
188 
190 // Standard services - public :
191 
192 int main( int argc, char** argv )
193 {
194  trace.beginBlock ( "Testing basic functors" );
195  trace.info() << "Args:";
196  for ( int i = 0; i < argc; ++i )
197  trace.info() << " " << argv[ i ];
198  trace.info() << endl;
199 
200  //concept checking
201  basicFunctorsConceptChecking<functors::Identity,int,int>();
202  basicFunctorsConceptChecking<DGtal::functors::ConstValue<int>,int,int >();
203  basicFunctorsConceptChecking<functors::Cast<int>,short,int >();
204  basicFunctorsConceptChecking<DGtal::functors::Thresholder<int>,int,bool >();
205  basicFunctorsConceptChecking<functors::Composer<functors::ConstValue<double>,functors::Cast<int>,int>,char,int >();
206 
207 
208  //run-time tests
209  bool res = testBasicFunctors();
210  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
211  trace.endBlock();
212  return res ? 0 : 1;
213 }
214 // //
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Aim: Define a new Functor from the composition of two other functors.
Aim: Define a simple functor that returns a constant value (0 by default).
Aim: A small functor with an operator () that compares one value to an interval.
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:90
Aim: Define a simple functor using the static cast operator.
Functor that rounds up.
Functor that rounds down.
Aim: Define a simple default functor that just returns its argument.
Functor that rounds to the nearest integer.
Functor that rounds towards zero.
void basicFunctorsConceptChecking()
int main(int argc, char **argv)
bool testBasicFunctors()