DGtal 1.3.0
Loading...
Searching...
No Matches
testBasicFunctors.cpp
Go to the documentation of this file.
1
31#include <iostream>
32#include <cmath>
33#include <functional>
34
35#include "DGtal/base/Common.h"
36
37#include "DGtal/base/CUnaryFunctor.h"
38#include "DGtal/base/BasicFunctors.h"
39
41
42using namespace std;
43using namespace DGtal;
44
49template <typename TFunctor, typename TArg, typename TRes >
51{
52 BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, TArg, TRes > ));
53}
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}
192
194// Standard services - public :
195
196int main( int argc, char** argv )
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}
218// //
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
STL namespace.
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()
bool testBasicFunctors()
int main()
Definition: testBits.cpp:56