40 #include <visp3/core/vpConfig.h>
42 #if defined(VISP_HAVE_CATCH2)
44 #include <catch_amalgamated.hpp>
49 #include <visp3/core/vpTranslationVector.h>
51 #ifdef ENABLE_VISP_NAMESPACE
55 template <
typename Type>
bool test(
const std::string &s,
const vpArray2D<Type> &A,
const std::vector<Type> &bench)
57 static unsigned int cpt = 0;
58 std::cout <<
"** Test " << ++cpt << std::endl;
59 std::cout << s <<
"(" << A.
getRows() <<
"," << A.
getCols() <<
") = \n" << A << std::endl;
60 if (bench.size() != A.
size()) {
61 std::cout <<
"Test fails: bad size wrt bench" << std::endl;
64 for (
unsigned int i = 0; i < A.
size(); i++) {
65 if (std::fabs(A.
data[i] - bench[i]) > std::fabs(A.
data[i]) * std::numeric_limits<double>::epsilon()) {
66 std::cout <<
"Test fails: bad content" << std::endl;
74 template <
typename Type>
78 std::cout <<
"Test fails: bad columns size" << std::endl;
82 std::cout <<
"Test fails: bad rows size" << std::endl;
85 for (
unsigned int i = 0; i < A.
size(); i++) {
86 if (std::fabs((A.
data[i] * B.
data[i]) - H.
data[i]) > std::numeric_limits<double>::epsilon()) {
87 std::cout <<
"Test fails: bad content" << std::endl;
95 TEST_CASE(
"Test constructors with double",
"[constructors]")
97 SECTION(
"Default constructor")
100 std::vector<double> bench;
101 CHECK(test(
"A", A, bench));
103 SECTION(
"Copy constructor")
107 std::vector<double> bench(12);
108 for (
unsigned int i = 0; i < 3; i++) {
109 for (
unsigned int j = 0; j < 4; j++) {
110 A[i][j] = (double)(i + j);
111 bench[i * 4 + j] = (double)(i + j);
114 CHECK(test(
"A", A, bench));
117 CHECK(test(
"B", B, bench));
119 SECTION(
"Constructor with initial value")
122 std::vector<double> bench1(12, 2);
123 CHECK(test(
"A", A, bench1));
126 std::vector<double> bench2(30, 0);
127 CHECK(test(
"A", A, bench2));
130 std::vector<double> bench3(30, -2);
131 CHECK(test(
"A", A, bench3));
134 SECTION(
"Constructor from std::vector")
136 std::vector<double> bench(12);
137 for (
unsigned int i = 0; i < 3; i++) {
138 for (
unsigned int j = 0; j < 4; j++) {
139 bench[i * 4 + j] = (double)(i + j);
142 SECTION(
"Keep default size (r=0, c=0)")
144 std::cout <<
"A with default size (r=0, c=0):\n" << std::endl;
147 SECTION(
"Keep row size to 0")
149 unsigned int size =
static_cast<unsigned int>(bench.size());
151 std::cout <<
"A with row size to 0:\n" << A << std::endl;
152 CHECK(test(
"A", A, bench));
154 CHECK(A.
getCols() == bench.size());
156 SECTION(
"Keep col size to 0")
158 unsigned int size =
static_cast<unsigned int>(bench.size());
160 std::cout <<
"A with col size to 0:\n" << A << std::endl;
161 CHECK(test(
"A", A, bench));
162 CHECK(A.
getRows() == bench.size());
165 SECTION(
"Set r=3 and c=4")
168 std::cout <<
"A with r=3 and c=4:\n" << A << std::endl;
169 CHECK(test(
"A", A, bench));
176 TEST_CASE(
"Test constructors with float",
"[constructors]")
178 SECTION(
"Default constructor")
181 std::vector<float> bench;
182 CHECK(test(
"A", A, bench));
184 SECTION(
"Copy constructor")
188 std::vector<float> bench(12);
189 for (
unsigned int i = 0; i < 3; i++) {
190 for (
unsigned int j = 0; j < 4; j++) {
191 A[i][j] = (float)(i + j);
192 bench[i * 4 + j] = (float)(i + j);
195 CHECK(test(
"A", A, bench));
198 CHECK(test(
"B", B, bench));
200 SECTION(
"Constructor with initial value")
203 std::vector<float> bench1(12, 2);
204 CHECK(test(
"A", A, bench1));
207 std::vector<float> bench2(30, 0);
208 CHECK(test(
"A", A, bench2));
211 std::vector<float> bench3(30, -2);
212 CHECK(test(
"A", A, bench3));
214 SECTION(
"Constructor from std::vector")
216 std::vector<float> bench(12);
217 for (
unsigned int i = 0; i < 3; i++) {
218 for (
unsigned int j = 0; j < 4; j++) {
219 bench[i * 4 + j] = (float)(i + j);
222 SECTION(
"Keep default size (r=0, c=0)")
224 std::cout <<
"A with default size (r=0, c=0):\n" << std::endl;
227 SECTION(
"Keep row size to 0")
229 unsigned int size =
static_cast<unsigned int>(bench.size());
231 std::cout <<
"A with row size to 0:\n" << A << std::endl;
232 CHECK(test(
"A", A, bench));
234 CHECK(A.
getCols() == bench.size());
236 SECTION(
"Keep col size to 0")
238 unsigned int size =
static_cast<unsigned int>(bench.size());
240 std::cout <<
"A with col size to 0:\n" << A << std::endl;
241 CHECK(test(
"A", A, bench));
242 CHECK(A.
getRows() == bench.size());
245 SECTION(
"Set r=3 and c=4")
248 std::cout <<
"A with r=3 and c=4:\n" << A << std::endl;
249 CHECK(test(
"A", A, bench));
256 TEST_CASE(
"Building array views",
"[view]")
258 std::cout <<
"Testing views" << std::endl;
259 SECTION(
"Modifying view modifies original array")
268 REQUIRE((v[1][1] == 3));
269 REQUIRE((A[0][0] == 5));
274 int *array =
new int[s * s];
278 REQUIRE(array[0] == 5);
279 REQUIRE(array[s * s - 1] == 2);
283 SECTION(
"A view can be moved and then used")
292 REQUIRE((A[0][0] == 5));
297 int *array =
new int[s * s];
301 v2[s - 1][s - 1] = 2;
302 REQUIRE(array[0] == 5);
303 REQUIRE(array[s * s - 1] == 2);
308 SECTION(
"Array can still be used after destroying view")
317 REQUIRE((A[0][0] == 5));
318 REQUIRE(A[s - 1][s - 1] == 2);
320 int *array =
new int[s * s];
327 REQUIRE(array[0] == 5);
328 REQUIRE(array[s * s - 1] == 2);
331 SECTION(
"Cannot resize view")
336 REQUIRE_THROWS(v.
resize(1, 5,
false,
false));
339 int *array =
new int[s * s];
341 REQUIRE_THROWS(v.
resize(1, 5,
false,
false));
347 TEST_CASE(
"Test Hadamar product",
"[hadamar]")
353 for (
unsigned int i = 0; i < A1.size(); i++) {
362 std::cout <<
"A1:\n" << A1 << std::endl;
363 std::cout <<
"\nA2:\n" << A2 << std::endl;
365 CHECK(test_hadamar(A1, A2, A3));
366 std::cout <<
"\nRes hadamar(A1, A2):\n" << A3 << std::endl;
368 std::cout <<
"\nR1:\n" << R1 << std::endl;
369 std::cout <<
"\nR2:\n" << R2 << std::endl;
371 CHECK(test_hadamar(R1, R2, R3));
372 std::cout <<
"\nRes hadamar(R1, R2):\n" << R3 << std::endl;
374 std::cout <<
"\nC1:\n" << C1 << std::endl;
375 std::cout <<
"\nC2:\n" << C2 << std::endl;
377 CHECK(test_hadamar(C1, C2, C3));
378 std::cout <<
"\nRes hadamar(C1, C2):\n" << C3 << std::endl;
381 int main(
int argc,
char *argv[])
383 Catch::Session session;
384 session.applyCommandLine(argc, argv);
385 int numFailed = session.run();
386 std::cout << (numFailed ?
"Test failed" :
"Test succeed") << std::endl;
390 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)
static vpArray2D< Type > view(const vpArray2D< Type > &A)
Creates a view of the Matrix A. A view shares the same underlying memory as the original array....
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.
vpRowVector hadamard(const vpRowVector &v) const