DGtal  1.0.0
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 192 of file testBasicFunctors.cpp.

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:144
Aim: Define a simple functor using the static cast operator.
double endBlock()
bool testBasicFunctors()
std::ostream & emphase()
std::ostream & info()

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 57 of file testBasicFunctors.cpp.

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 }
void beginBlock(const std::string &keyword="")
Aim: Define a simple functor that returns a constant value (0 by default).
Trace trace
Definition: Common.h:144
Functor that rounds down.
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.
Functor that rounds towards zero.
Functor that rounds to the nearest integer.
Functor that rounds up.
std::ostream & info()

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

Referenced by main().