42 #include <visp3/core/vpConfig.h>
44 #if defined(VISP_HAVE_CATCH2)
45 #define CATCH_CONFIG_RUNNER
52 #include <visp3/core/vpTranslationVector.h>
54 template <
typename Type>
bool test(
const std::string &s,
const vpArray2D<Type> &A,
const std::vector<Type> &bench)
56 static unsigned int cpt = 0;
57 std::cout <<
"** Test " << ++cpt << std::endl;
58 std::cout << s <<
"(" << A.
getRows() <<
"," << A.
getCols() <<
") = \n" << A << std::endl;
59 if (bench.size() != A.
size()) {
60 std::cout <<
"Test fails: bad size wrt bench" << std::endl;
63 for (
unsigned int i = 0; i < A.
size(); i++) {
64 if (std::fabs(A.
data[i] - bench[i]) > std::fabs(A.
data[i]) * std::numeric_limits<double>::epsilon()) {
65 std::cout <<
"Test fails: bad content" << std::endl;
73 template <
typename Type>
77 std::cout <<
"Test fails: bad columns size" << std::endl;
81 std::cout <<
"Test fails: bad rows size" << std::endl;
84 for (
unsigned int i = 0; i < A.
size(); i++) {
85 if (std::fabs((A.
data[i] * B.
data[i]) - H.
data[i]) > std::numeric_limits<double>::epsilon()) {
86 std::cout <<
"Test fails: bad content" << std::endl;
94 TEST_CASE(
"Test constructors with double",
"[constructors]")
96 SECTION(
"Default constructor")
99 std::vector<double> bench;
100 CHECK(test(
"A", A, bench));
102 SECTION(
"Copy constructor")
106 std::vector<double> bench(12);
107 for (
unsigned int i = 0; i < 3; i++) {
108 for (
unsigned int j = 0; j < 4; j++) {
109 A[i][j] = (double)(i + j);
110 bench[i * 4 + j] = (double)(i + j);
113 CHECK(test(
"A", A, bench));
116 CHECK(test(
"B", B, bench));
118 SECTION(
"Constructor with initial value")
121 std::vector<double> bench1(12, 2);
122 CHECK(test(
"A", A, bench1));
125 std::vector<double> bench2(30, 0);
126 CHECK(test(
"A", A, bench2));
129 std::vector<double> bench3(30, -2);
130 CHECK(test(
"A", A, bench3));
133 SECTION(
"Constructor from std::vector")
135 std::vector<double> bench(12);
136 for (
unsigned int i = 0; i < 3; i++) {
137 for (
unsigned int j = 0; j < 4; j++) {
138 bench[i * 4 + j] = (double)(i + j);
141 SECTION(
"Keep default size (r=0, c=0)")
143 std::cout <<
"A with default size (r=0, c=0):\n" << std::endl;
146 SECTION(
"Keep row size to 0")
149 std::cout <<
"A with row size to 0:\n" << A << std::endl;
150 CHECK(test(
"A", A, bench));
152 CHECK(A.
getCols() == bench.size());
154 SECTION(
"Keep col size to 0")
157 std::cout <<
"A with col size to 0:\n" << A << std::endl;
158 CHECK(test(
"A", A, bench));
159 CHECK(A.
getRows() == bench.size());
162 SECTION(
"Set r=3 and c=4")
165 std::cout <<
"A with r=3 and c=4:\n" << A << std::endl;
166 CHECK(test(
"A", A, bench));
173 TEST_CASE(
"Test constructors with float",
"[constructors]")
175 SECTION(
"Default constructor")
178 std::vector<float> bench;
179 CHECK(test(
"A", A, bench));
181 SECTION(
"Copy constructor")
185 std::vector<float> bench(12);
186 for (
unsigned int i = 0; i < 3; i++) {
187 for (
unsigned int j = 0; j < 4; j++) {
188 A[i][j] = (float)(i + j);
189 bench[i * 4 + j] = (float)(i + j);
192 CHECK(test(
"A", A, bench));
195 CHECK(test(
"B", B, bench));
197 SECTION(
"Constructor with initial value")
200 std::vector<float> bench1(12, 2);
201 CHECK(test(
"A", A, bench1));
204 std::vector<float> bench2(30, 0);
205 CHECK(test(
"A", A, bench2));
208 std::vector<float> bench3(30, -2);
209 CHECK(test(
"A", A, bench3));
211 SECTION(
"Constructor from std::vector")
213 std::vector<float> bench(12);
214 for (
unsigned int i = 0; i < 3; i++) {
215 for (
unsigned int j = 0; j < 4; j++) {
216 bench[i * 4 + j] = (float)(i + j);
219 SECTION(
"Keep default size (r=0, c=0)")
221 std::cout <<
"A with default size (r=0, c=0):\n" << std::endl;
224 SECTION(
"Keep row size to 0")
227 std::cout <<
"A with row size to 0:\n" << A << std::endl;
228 CHECK(test(
"A", A, bench));
230 CHECK(A.
getCols() == bench.size());
232 SECTION(
"Keep col size to 0")
235 std::cout <<
"A with col size to 0:\n" << A << std::endl;
236 CHECK(test(
"A", A, bench));
237 CHECK(A.
getRows() == bench.size());
240 SECTION(
"Set r=3 and c=4")
243 std::cout <<
"A with r=3 and c=4:\n" << A << std::endl;
244 CHECK(test(
"A", A, bench));
251 TEST_CASE(
"Test Hadamar product",
"[hadamar]")
257 for (
unsigned int i = 0; i < A1.size(); i++) {
266 std::cout <<
"A1:\n" << A1 << std::endl;
267 std::cout <<
"\nA2:\n" << A2 << std::endl;
269 CHECK(test_hadamar(A1, A2, A3));
270 std::cout <<
"\nRes hadamar(A1, A2):\n" << A3 << std::endl;
272 std::cout <<
"\nR1:\n" << R1 << std::endl;
273 std::cout <<
"\nR2:\n" << R2 << std::endl;
275 CHECK(test_hadamar(R1, R2, R3));
276 std::cout <<
"\nRes hadamar(R1, R2):\n" << R3 << std::endl;
278 std::cout <<
"\nC1:\n" << C1 << std::endl;
279 std::cout <<
"\nC2:\n" << C2 << std::endl;
281 CHECK(test_hadamar(C1, C2, C3));
282 std::cout <<
"\nRes hadamar(C1, C2):\n" << C3 << std::endl;
285 int main(
int argc,
char *argv[])
287 Catch::Session session;
290 session.applyCommandLine(argc, argv);
292 int numFailed = session.run();
297 std::cout << (numFailed ?
"Test failed" :
"Test succeed") << std::endl;
301 int main() {
return EXIT_SUCCESS; }
Implementation of a generic 2D array used as base class for matrices and vectors.
unsigned int getCols() const
Type * data
Address of the first element of the data array.
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getRows() const
vpArray2D< Type > hadamard(const vpArray2D< Type > &m) const
Implementation of column vector and the associated operations.
vpColVector hadamard(const vpColVector &v) const
Implementation of row vector and the associated operations.