DGtal  0.9.3
Functions
testBasicFunctors.cpp File Reference
#include <iostream>
#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 49 of file testBasicFunctors.cpp.

50 {
51  BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, TArg, TRes > ));
52 }
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:89

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 171 of file testBasicFunctors.cpp.

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

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<functors::Identity,int,int>();
181  basicFunctorsConceptChecking<DGtal::functors::ConstValue<int>,int,int >();
182  basicFunctorsConceptChecking<functors::Cast<int>,short,int >();
183  basicFunctorsConceptChecking<DGtal::functors::Thresholder<int>,int,bool >();
184  basicFunctorsConceptChecking<functors::Composer<functors::ConstValue<double>,functors::Cast<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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
Aim: Define a simple functor using the static cast operator.
double endBlock()
bool testBasicFunctors()
std::ostream & emphase()
std::ostream & info()

◆ testBasicFunctors()

bool testBasicFunctors ( )

Simple test.

Definition at line 57 of file testBasicFunctors.cpp.

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

Referenced by main().

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  //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  functors::Cast<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;
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 }
void beginBlock(const std::string &keyword="")
Aim: Define a simple functor that returns a constant value (0 by default).
Trace trace
Definition: Common.h:137
Aim: Define a simple functor using the static cast operator.
double endBlock()
Aim: Define a simple default functor that just returns its argument.
Aim: Define a new Functor from the composition of two other functors.
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
Aim: A small functor with an operator () that compares one value to an interval.
std::ostream & info()