ViSP  3.0.0
testArray2D.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test some vpColVector functionalities.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
44 #include <cmath>
45 #include <vector>
46 #include <limits>
47 
48 #include <visp3/core/vpTranslationVector.h>
49 
50 template<typename Type>
51 bool test(const std::string &s, const vpArray2D<Type> &A, const std::vector<Type> &bench)
52 {
53  static unsigned int cpt = 0;
54  std::cout << "** Test " << ++cpt << std::endl;
55  std::cout << s << "(" << A.getRows() << "," << A.getCols() << ") = \n" << A << std::endl;
56  if(bench.size() != A.size()) {
57  std::cout << "Test fails: bad size wrt bench" << std::endl;
58  return false;
59  }
60  for (unsigned int i=0; i<A.size(); i++) {
61  if (std::fabs(A.data[i]-bench[i]) > std::fabs(A.data[i])*std::numeric_limits<double>::epsilon()) {
62  std::cout << "Test fails: bad content" << std::endl;
63  return false;
64  }
65  }
66 
67  return true;
68 }
69 
70 int main()
71 {
72  int err = 1;
73  {
74  // test default constructor
76  std::vector<double> bench;
77  if (test("A", A, bench) == false)
78  return err;
79  }
80  {
81  // test copy constructor
82  vpArray2D<double> A(3, 4);
83 
84  std::vector<double> bench(12);
85  for(unsigned int i=0; i<3; i++) {
86  for(unsigned int j=0; j<4; j++) {
87  A[i][j] = (double)(i+j);
88  bench[i*4+j] = (double)(i+j);
89  }
90  }
91  if (test("A", A, bench) == false)
92  return err;
93 
94  vpArray2D<double> B(A);
95  if (test("B", B, bench) == false)
96  return err;
97  std::cout << "Min/Max: " << B.getMinValue() << " " << B.getMaxValue() << std::endl;
98  }
99  {
100  // test constructor with initial value
101  vpArray2D<double> A(3, 4, 2.);
102  std::vector<double> bench1(12, 2);
103  if (test("A", A, bench1) == false)
104  return err;
105 
106  A.resize(5, 6);
107  std::vector<double> bench2(30, 0);
108  if (test("A", A, bench2) == false)
109  return err;
110 
111  A = -2.;
112  std::vector<double> bench3(30, -2);
113  if (test("A", A, bench3) == false)
114  return err;
115  }
116 
117  // Test with float
118  {
119  // test default constructor
121  std::vector<float> bench;
122  if (test("A", A, bench) == false)
123  return err;
124  }
125  {
126  // test copy constructor
127  vpArray2D<float> A(3, 4);
128 
129  std::vector<float> bench(12);
130  for(unsigned int i=0; i<3; i++) {
131  for(unsigned int j=0; j<4; j++) {
132  A[i][j] = (float)(i+j);
133  bench[i*4+j] = (float)(i+j);
134  }
135  }
136  if (test("A", A, bench) == false)
137  return err;
138 
139  vpArray2D<float> B(A);
140  if (test("B", B, bench) == false)
141  return err;
142  std::cout << "Min/Max: " << B.getMinValue() << " " << B.getMaxValue() << std::endl;
143  }
144  {
145  // test constructor with initial value
146  vpArray2D<float> A(3, 4, 2.);
147  std::vector<float> bench1(12, 2);
148  if (test("A", A, bench1) == false)
149  return err;
150 
151  A.resize(5, 6);
152  std::vector<float> bench2(30, 0);
153  if (test("A", A, bench2) == false)
154  return err;
155 
156  A = -2.;
157  std::vector<float> bench3(30, -2);
158  if (test("A", A, bench3) == false)
159  return err;
160  }
161  std::cout << "All tests succeed" << std::endl;
162  return 0;
163 }
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
Implementation of a generic 2D array used as vase class of matrices and vectors.
Definition: vpArray2D.h:70
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152