Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
testBasicFunctors.cpp
Go to the documentation of this file.
1
16
29
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 >
54
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 because there are several
112 // overloaded versions of std::floor
113 double (*pF)(double) = &floor;
114 double (*pC)(double) = &ceil;
115 std::function<double(double)> f = pF;
116 std::function<double(double)> c = pC;
118
119 //composer
120 typedef DGtal::functors::Composer< std::function<double(double)>,
121 functors::Cast<int>, int > Quantizer;
122 double d = 5.2;
123
124 Quantizer q(f, o);
125 nbok += ( q(d) == 5 ) ? 1 : 0;
126 nb++;
127
128 Quantizer q2(c, o);
129 nbok += ( q2(d) == 6 ) ? 1 : 0;
130 nb++;
131 }
132
133 //binary to unary functor
134 {
135 int i = -5;
136 // With function and bind:
137 std::function<int(int)> b = std::bind(std::minus<int>(), std::placeholders::_1, 0);
138 //i - 0
139 nbok += ( b(i) == -5 ) ? 1 : 0;
140 nb++;
141 // With a lambda:
142 auto b2 = [](int v) -> int {
143 return v + 2;
144 };
145 //i + 2
146 nbok += ( b2(i) == -3 ) ? 1 : 0;
147 nb++;
148 }
149
150 {//thresholder
151 int i = -3;
153 nbok += ( t(i) == true ) ? 1 : 0;
154 nb++;
156 nbok += ( t1(i) == true ) ? 1 : 0;
157 nb++;
159 nbok += ( t2(0) == false ) ? 1 : 0;
160 nb++;
162 nbok += ( t3(i) == false ) ? 1 : 0;
163 nb++;
165 nbok += ( t4(i) == false ) ? 1 : 0;
166 nb++;
167 }
168
169 {//interval thresholder
170 const int low = 1;
171 const int up = 5;
173 nbok += ( t(0) == false ) ? 1 : 0;
174 nb++;
175 for (int i = low; i <= up; ++i)
176 {
177 nbok += ( t(i) == true ) ? 1 : 0;
178 nb++;
179 }
180 nbok += ( t(6) == false ) ? 1 : 0;
181 nb++;
182 }
183
184
185 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
186 trace.endBlock();
187
188 return nbok == nb;
189}
190
192// Standard services - public :
193
194int main( int argc, char** argv )
195{
196 trace.beginBlock ( "Testing basic functors" );
197 trace.info() << "Args:";
198 for ( int i = 0; i < argc; ++i )
199 trace.info() << " " << argv[ i ];
200 trace.info() << endl;
201
202 //concept checking
208
209
210 //run-time tests
211 bool res = testBasicFunctors();
212 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
213 trace.endBlock();
214 return res ? 0 : 1;
215}
216// //
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
STL namespace.
Aim: Defines a unary functor, which associates arguments to results.
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