DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
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
42
using namespace
std
;
43
using namespace
DGtal
;
44
49
template
<
typename
TFunctor,
typename
TArg,
typename
TRes >
50
void
basicFunctorsConceptChecking
()
51
{
52
BOOST_CONCEPT_ASSERT((
concepts::CUnaryFunctor<TFunctor, TArg, TRes >
));
53
}
54
58
bool
testBasicFunctors
()
59
{
60
unsigned
int
nbok = 0;
61
unsigned
int
nb = 0;
62
63
trace
.
beginBlock
(
"Testing basic functors ..."
);
64
65
//default functor
66
{
67
functors::Identity
f;
68
int
a = 5;
69
nbok += ( f(a) == 5 ) ? 1 : 0;
70
nb++;
71
}
72
73
{
//constant functor
74
const
int
v = -1;
75
DGtal::functors::ConstValue<int>
f(v);
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
{
86
functors::Cast<int>
f;
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 explicitly 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;
117
functors::Cast<int>
o;
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;
152
DGtal::functors::Thresholder< int >
t;
153
nbok += ( t(i) == true ) ? 1 : 0;
154
nb++;
155
DGtal::functors::Thresholder< int, true, true >
t1;
156
nbok += ( t1(i) == true ) ? 1 : 0;
157
nb++;
158
DGtal::functors::Thresholder< int, true, false >
t2;
159
nbok += ( t2(0) == false ) ? 1 : 0;
160
nb++;
161
DGtal::functors::Thresholder< int, false, true >
t3;
162
nbok += ( t3(i) == false ) ? 1 : 0;
163
nb++;
164
DGtal::functors::Thresholder< int, false, false >
t4;
165
nbok += ( t4(i) == false ) ? 1 : 0;
166
nb++;
167
}
168
169
{
//interval thresholder
170
const
int
low = 1;
171
const
int
up = 5;
172
DGtal::functors::IntervalThresholder< int >
t(low, up);
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
194
int
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
203
basicFunctorsConceptChecking<functors::Identity,int,int>
();
204
basicFunctorsConceptChecking<DGtal::functors::ConstValue<int>
,int,
int
>();
205
basicFunctorsConceptChecking<functors::Cast<int>
,short,
int
>();
206
basicFunctorsConceptChecking<DGtal::functors::Thresholder<int>
,int,
bool
>();
207
basicFunctorsConceptChecking<functors::Composer<functors::ConstValue<double>
,
functors::Cast<int>
,
int
>,char,
int
>();
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
// //
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::Trace::info
std::ostream & info()
DGtal::Trace::endBlock
double endBlock()
DGtal::functors::Composer
Aim: Define a new Functor from the composition of two other functors.
Definition
BasicFunctors.h:418
DGtal::functors::ConstValue
Aim: Define a simple functor that returns a constant value (0 by default).
Definition
BasicFunctors.h:311
DGtal::functors::IntervalThresholder
Aim: A small functor with an operator () that compares one value to an interval.
Definition
BasicFunctors.h:701
DGtal::functors::Thresholder
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
Definition
BasicFunctors.h:501
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
std
STL namespace.
DGtal::concepts::CUnaryFunctor
Aim: Defines a unary functor, which associates arguments to results.
Definition
CUnaryFunctor.h:90
DGtal::functors::Cast
Aim: Define a simple functor using the static cast operator.
Definition
BasicFunctors.h:394
DGtal::functors::Ceil
Functor that rounds up.
Definition
BasicFunctors.h:218
DGtal::functors::Floor
Functor that rounds down.
Definition
BasicFunctors.h:185
DGtal::functors::Identity
Aim: Define a simple default functor that just returns its argument.
Definition
BasicFunctors.h:288
DGtal::functors::Round
Functor that rounds to the nearest integer.
Definition
BasicFunctors.h:152
DGtal::functors::Trunc
Functor that rounds towards zero.
Definition
BasicFunctors.h:251
basicFunctorsConceptChecking
void basicFunctorsConceptChecking()
Definition
testBasicFunctors.cpp:50
testBasicFunctors
bool testBasicFunctors()
Definition
testBasicFunctors.cpp:58
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
tests
base
testBasicFunctors.cpp
Generated on
for DGtal by
1.18.0