40 #include <visp3/core/vpConfig.h>
42 #if defined(VISP_HAVE_CATCH2)
43 #define CATCH_CONFIG_RUNNER
50 #include <visp3/core/vpTranslationVector.h>
52 #ifdef ENABLE_VISP_NAMESPACE
56 template <
typename Type>
bool test(
const std::string &s,
const vpArray2D<Type> &A,
const std::vector<Type> &bench)
58 static unsigned int cpt = 0;
59 std::cout <<
"** Test " << ++cpt << std::endl;
60 std::cout << s <<
"(" << A.
getRows() <<
"," << A.
getCols() <<
") = \n" << A << std::endl;
61 if (bench.size() != A.
size()) {
62 std::cout <<
"Test fails: bad size wrt bench" << std::endl;
65 for (
unsigned int i = 0; i < A.
size(); i++) {
66 if (std::fabs(A.
data[i] - bench[i]) > std::fabs(A.
data[i]) * std::numeric_limits<double>::epsilon()) {
67 std::cout <<
"Test fails: bad content" << std::endl;
75 template <
typename Type>
79 std::cout <<
"Test fails: bad columns size" << std::endl;
83 std::cout <<
"Test fails: bad rows size" << std::endl;
86 for (
unsigned int i = 0; i < A.
size(); i++) {
87 if (std::fabs((A.
data[i] * B.
data[i]) - H.
data[i]) > std::numeric_limits<double>::epsilon()) {
88 std::cout <<
"Test fails: bad content" << std::endl;
96 TEST_CASE(
"Test constructors with double",
"[constructors]")
98 SECTION(
"Default constructor")
101 std::vector<double> bench;
102 CHECK(test(
"A", A, bench));
104 SECTION(
"Copy constructor")
108 std::vector<double> bench(12);
109 for (
unsigned int i = 0; i < 3; i++) {
110 for (
unsigned int j = 0; j < 4; j++) {
111 A[i][j] = (double)(i + j);
112 bench[i * 4 + j] = (double)(i + j);
115 CHECK(test(
"A", A, bench));
118 CHECK(test(
"B", B, bench));
120 SECTION(
"Constructor with initial value")
123 std::vector<double> bench1(12, 2);
124 CHECK(test(
"A", A, bench1));
127 std::vector<double> bench2(30, 0);
128 CHECK(test(
"A", A, bench2));
131 std::vector<double> bench3(30, -2);
132 CHECK(test(
"A", A, bench3));
135 SECTION(
"Constructor from std::vector")
137 std::vector<double> bench(12);
138 for (
unsigned int i = 0; i < 3; i++) {
139 for (
unsigned int j = 0; j < 4; j++) {
140 bench[i * 4 + j] = (double)(i + j);
143 SECTION(
"Keep default size (r=0, c=0)")
145 std::cout <<
"A with default size (r=0, c=0):\n" << std::endl;
148 SECTION(
"Keep row size to 0")
150 unsigned int size =
static_cast<unsigned int>(bench.size());
152 std::cout <<
"A with row size to 0:\n" << A << std::endl;
153 CHECK(test(
"A", A, bench));
155 CHECK(A.
getCols() == bench.size());
157 SECTION(
"Keep col size to 0")
159 unsigned int size =
static_cast<unsigned int>(bench.size());
161 std::cout <<
"A with col size to 0:\n" << A << std::endl;
162 CHECK(test(
"A", A, bench));
163 CHECK(A.
getRows() == bench.size());
166 SECTION(
"Set r=3 and c=4")
169 std::cout <<
"A with r=3 and c=4:\n" << A << std::endl;
170 CHECK(test(
"A", A, bench));
177 TEST_CASE(
"Test constructors with float",
"[constructors]")
179 SECTION(
"Default constructor")
182 std::vector<float> bench;
183 CHECK(test(
"A", A, bench));
185 SECTION(
"Copy constructor")
189 std::vector<float> bench(12);
190 for (
unsigned int i = 0; i < 3; i++) {
191 for (
unsigned int j = 0; j < 4; j++) {
192 A[i][j] = (float)(i + j);
193 bench[i * 4 + j] = (float)(i + j);
196 CHECK(test(
"A", A, bench));
199 CHECK(test(
"B", B, bench));
201 SECTION(
"Constructor with initial value")
204 std::vector<float> bench1(12, 2);
205 CHECK(test(
"A", A, bench1));
208 std::vector<float> bench2(30, 0);
209 CHECK(test(
"A", A, bench2));
212 std::vector<float> bench3(30, -2);
213 CHECK(test(
"A", A, bench3));
215 SECTION(
"Constructor from std::vector")
217 std::vector<float> bench(12);
218 for (
unsigned int i = 0; i < 3; i++) {
219 for (
unsigned int j = 0; j < 4; j++) {
220 bench[i * 4 + j] = (float)(i + j);
223 SECTION(
"Keep default size (r=0, c=0)")
225 std::cout <<
"A with default size (r=0, c=0):\n" << std::endl;
228 SECTION(
"Keep row size to 0")
230 unsigned int size =
static_cast<unsigned int>(bench.size());
232 std::cout <<
"A with row size to 0:\n" << A << std::endl;
233 CHECK(test(
"A", A, bench));
235 CHECK(A.
getCols() == bench.size());
237 SECTION(
"Keep col size to 0")
239 unsigned int size =
static_cast<unsigned int>(bench.size());
241 std::cout <<
"A with col size to 0:\n" << A << std::endl;
242 CHECK(test(
"A", A, bench));
243 CHECK(A.
getRows() == bench.size());
246 SECTION(
"Set r=3 and c=4")
249 std::cout <<
"A with r=3 and c=4:\n" << A << std::endl;
250 CHECK(test(
"A", A, bench));
257 TEST_CASE(
"Test Hadamar product",
"[hadamar]")
263 for (
unsigned int i = 0; i < A1.size(); i++) {
272 std::cout <<
"A1:\n" << A1 << std::endl;
273 std::cout <<
"\nA2:\n" << A2 << std::endl;
275 CHECK(test_hadamar(A1, A2, A3));
276 std::cout <<
"\nRes hadamar(A1, A2):\n" << A3 << std::endl;
278 std::cout <<
"\nR1:\n" << R1 << std::endl;
279 std::cout <<
"\nR2:\n" << R2 << std::endl;
281 CHECK(test_hadamar(R1, R2, R3));
282 std::cout <<
"\nRes hadamar(R1, R2):\n" << R3 << std::endl;
284 std::cout <<
"\nC1:\n" << C1 << std::endl;
285 std::cout <<
"\nC2:\n" << C2 << std::endl;
287 CHECK(test_hadamar(C1, C2, C3));
288 std::cout <<
"\nRes hadamar(C1, C2):\n" << C3 << std::endl;
291 int main(
int argc,
char *argv[])
293 Catch::Session session;
296 session.applyCommandLine(argc, argv);
298 int numFailed = session.run();
303 std::cout << (numFailed ?
"Test failed" :
"Test succeed") << std::endl;
307 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.
vpRowVector hadamard(const vpRowVector &v) const