DGtal  1.4.beta
Functions
testBasicFunctors.cpp File Reference
#include <iostream>
#include <cmath>
#include <functional>
#include "DGtal/base/Common.h"
#include "DGtal/base/CUnaryFunctor.h"
#include "DGtal/base/BasicFunctors.h"
Include dependency graph for testBasicFunctors.cpp:

Go to the source code of this file.

Functions

template<typename TFunctor , typename TArg , typename TRes >
void basicFunctorsConceptChecking ()
 
bool testBasicFunctors ()
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Tristan Roussillon (trist.nosp@m.an.r.nosp@m.oussi.nosp@m.llon.nosp@m.@liri.nosp@m.s.cn.nosp@m.rs.fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2012/01/27

Functions for testing basic functors.

This file is part of the DGtal library.

Definition in file testBasicFunctors.cpp.

Function Documentation

◆ basicFunctorsConceptChecking()

template<typename TFunctor , typename TArg , typename TRes >
void basicFunctorsConceptChecking ( )

Concept checking

Definition at line 50 of file testBasicFunctors.cpp.

51 {
52  BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, TArg, TRes > ));
53 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 196 of file testBasicFunctors.cpp.

197 {
198  trace.beginBlock ( "Testing basic functors" );
199  trace.info() << "Args:";
200  for ( int i = 0; i < argc; ++i )
201  trace.info() << " " << argv[ i ];
202  trace.info() << endl;
203 
204  //concept checking
205  basicFunctorsConceptChecking<functors::Identity,int,int>();
206  basicFunctorsConceptChecking<DGtal::functors::ConstValue<int>,int,int >();
207  basicFunctorsConceptChecking<functors::Cast<int>,short,int >();
208  basicFunctorsConceptChecking<DGtal::functors::Thresholder<int>,int,bool >();
209  basicFunctorsConceptChecking<functors::Composer<functors::ConstValue<double>,functors::Cast<int>,int>,char,int >();
210 
211 
212  //run-time tests
213  bool res = testBasicFunctors();
214  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
215  trace.endBlock();
216  return res ? 0 : 1;
217 }

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testBasicFunctors(), and DGtal::trace.

◆ testBasicFunctors()

bool testBasicFunctors ( )

Simple test.

Definition at line 58 of file testBasicFunctors.cpp.

59 {
60  unsigned int nbok = 0;
61  unsigned int nb = 0;
62 
63  trace.beginBlock ( "Testing basic functors ..." );
64 
65  //default functor
66  {
68  int a = 5;
69  nbok += ( f(a) == 5 ) ? 1 : 0;
70  nb++;
71  }
72 
73  {//constant functor
74  const int v = -1;
76  char c = 'a';
77  nbok += ( f(c) == v ) ? 1 : 0;
78  nb++;
79  double d = 5.2;
80  nbok += ( f(d) == v ) ? 1 : 0;
81  nb++;
82  }
83 
84  //cast functor
85  {
87  char c = 'a';
88  nbok += ( f(c) == 97 ) ? 1 : 0;
89  nb++;
90  }
91 
92  //rounding functors
93  {
94  const double v1 = -3.5;
95  const double v2 = 3.5;
96 
97  { const DGtal::functors::Round<double> f; nbok += ( f(v1) == -4. && f(v2) == 4. ) ? 1 : 0; }
98  { const DGtal::functors::Round<> f; nbok += ( f(v1) == -4. && f(v2) == 4. ) ? 1 : 0; }
99  { const DGtal::functors::Floor<double> f; nbok += ( f(v1) == -4. && f(v2) == 3. ) ? 1 : 0; }
100  { const DGtal::functors::Floor<> f; nbok += ( f(v1) == -4. && f(v2) == 3. ) ? 1 : 0; }
101  { const DGtal::functors::Ceil<double> f; nbok += ( f(v1) == -3. && f(v2) == 4. ) ? 1 : 0; }
102  { const DGtal::functors::Ceil<> f; nbok += ( f(v1) == -3. && f(v2) == 4. ) ? 1 : 0; }
103  { const DGtal::functors::Trunc<double> f; nbok += ( f(v1) == -3. && f(v2) == 3. ) ? 1 : 0; }
104  { const DGtal::functors::Trunc<> f; nbok += ( f(v1) == -3. && f(v2) == 3. ) ? 1 : 0; }
105 
106  nb += 8;
107  }
108 
109  //composer quantizer
110  {
111  //need to explicitely specialized std::ptr_fun because there are several
112  //overloaded versions of std::floor if used intead ctor of
113  //std::pointer_to_unary_function<double, double>
114  // JOL: pointer_to_unary_function is deprecated as of C++11
115  double (*pF)(double) = &floor;
116  double (*pC)(double) = &ceil;
117  std::function<double(double)> f = pF;
118  std::function<double(double)> c = pC;
120 
121  //composer
122  typedef DGtal::functors::Composer< std::function<double(double)>,
123  functors::Cast<int>, int > Quantizer;
124  double d = 5.2;
125 
126  Quantizer q(f, o);
127  nbok += ( q(d) == 5 ) ? 1 : 0;
128  nb++;
129 
130  Quantizer q2(c, o);
131  nbok += ( q2(d) == 6 ) ? 1 : 0;
132  nb++;
133  }
134 
135  //binary to unary functor
136  {
137  int i = -5;
138  // With function and bind:
139  std::function<int(int)> b = std::bind(std::minus<int>(), std::placeholders::_1, 0);
140  //i - 0
141  nbok += ( b(i) == -5 ) ? 1 : 0;
142  nb++;
143  // With a lambda:
144  auto b2 = [](int v) -> int {
145  return v + 2;
146  };
147  //i + 2
148  nbok += ( b2(i) == -3 ) ? 1 : 0;
149  nb++;
150  }
151 
152  {//thresholder
153  int i = -3;
155  nbok += ( t(i) == true ) ? 1 : 0;
156  nb++;
158  nbok += ( t1(i) == true ) ? 1 : 0;
159  nb++;
161  nbok += ( t2(0) == false ) ? 1 : 0;
162  nb++;
164  nbok += ( t3(i) == false ) ? 1 : 0;
165  nb++;
167  nbok += ( t4(i) == false ) ? 1 : 0;
168  nb++;
169  }
170 
171  {//interval thresholder
172  const int low = 1;
173  const int up = 5;
175  nbok += ( t(0) == false ) ? 1 : 0;
176  nb++;
177  for (int i = low; i <= up; ++i)
178  {
179  nbok += ( t(i) == true ) ? 1 : 0;
180  nb++;
181  }
182  nbok += ( t(6) == false ) ? 1 : 0;
183  nb++;
184  }
185 
186 
187  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
188  trace.endBlock();
189 
190  return nbok == nb;
191 }

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

DGtal::functors::Thresholder
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
Definition: BasicFunctors.h:500
DGtal::functors::Composer
Aim: Define a new Functor from the composition of two other functors.
Definition: BasicFunctors.h:417
DGtal::Trace::endBlock
double endBlock()
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::functors::Round
Functor that rounds to the nearest integer.
Definition: BasicFunctors.h:151
DGtal::functors::Identity
Aim: Define a simple default functor that just returns its argument.
Definition: BasicFunctors.h:287
DGtal::functors::Cast
Aim: Define a simple functor using the static cast operator.
Definition: BasicFunctors.h:393
testBasicFunctors
bool testBasicFunctors()
Definition: testBasicFunctors.cpp:58
DGtal::Trace::info
std::ostream & info()
DGtal::functors::Ceil
Functor that rounds up.
Definition: BasicFunctors.h:217
DGtal::functors::ConstValue
Aim: Define a simple functor that returns a constant value (0 by default).
Definition: BasicFunctors.h:310
DGtal::functors::Trunc
Functor that rounds towards zero.
Definition: BasicFunctors.h:250
DGtal::functors::Floor
Functor that rounds down.
Definition: BasicFunctors.h:184
DGtal::functors::IntervalThresholder
Aim: A small functor with an operator () that compares one value to an interval.
Definition: BasicFunctors.h:700
DGtal::concepts::CUnaryFunctor
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:89