Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestMatrix.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 
23 /** @file TestMatrix.cpp
24  * @ingroup g_tests
25  * @brief Test for Matrix class, unfinished
26  @relates Matrix
27  @author f. woelk 05/2008 (c) www.vision-n.de
28 */
29 
30 #include <Base/Math/Matrix.hh>
31 #include <Base/Math/Vector.hh>
32 #include <Base/Debug/Exception.hh>
33 
34 using namespace BIAS;
35 using namespace std;
36 
37 /// macro testing if command has thrown an exception
38 #define SHOULD_THROW(command) \
39 { \
40  bool has_thrown = false; \
41  try { command; } \
42  catch ( BaseException b ) { has_thrown = true; } \
43  if (!has_thrown) { \
44  if (verbose) cout << #command << " was expected to throw an exception"\
45  <<", but it has not thrown. Test failed. \n"<<dst<<endl; \
46  BIASABORT; \
47  } \
48 }
49 
50 const bool verbose = false;
51 
52 // fills a matrix with a constant value
53 void Fill(const double val, Matrix<double> &src);
54 
55 // fills a vector with a constant value
56 void Fill(const double val, Matrix<double> &src);
57 
58 // test the different set methods of the Matrix class
59 void TestSet();
60 
61 int main(int argc, char *argv[])
62 {
63  TestSet();
64  return 0;
65 }
66 
67 
68 void Fill(const double val, Matrix<double> &src)
69 {
70  for (int r=0; r<src.num_rows(); r++)
71  for (int c=0; c<src.num_cols(); c++)
72  src[r][c] = val;
73 }
74 
75 
76 void Fill(const double val, Vector<double> &src)
77 {
78  for (int r=0; r<src.size(); r++)
79  src[r] = val;
80 }
81 
82 
83 
84 void TestSet()
85 {
86  Matrix<double> dst(5, 5), src_small(2, 2), src_big(6, 6);
87  Vector<double> src_vec(3);
88 
89  // initialize the matrizes
90  Fill(0., dst);
91  Fill(2., src_small);
92  Fill(6., src_big);
93  Fill(3., src_vec);
94 
95  dst.Set(0, 0, src_small);
96  if (verbose) cout << dst << endl;
97 
98  Fill(0., dst);
99  dst.Set(1, 0, src_small);
100  if (verbose) cout << dst << endl;
101 
102  Fill(0., dst);
103  dst.Set(0, 1, src_small);
104  if (verbose) cout << dst << endl;
105 
106  // set to bottom right corner
107  Fill(0., dst);
108  dst.Set(3, 3, src_small);
109  if (verbose) cout << dst << endl;
110 
111  Fill(0., dst);
112  dst.Set(1, 1, src_vec);
113  if (verbose) cout << dst << endl;
114 
115  Fill(0., dst);
116  dst.SetTranspose(1, 1, src_vec);
117  if (verbose) cout << dst << endl;
118 
119  Fill(0., dst);
120  dst.Set(2, 4, src_vec);
121  if (verbose) cout << dst << endl;
122 
123  Fill(0., dst);
124  dst.SetTranspose(4, 2, src_vec);
125  if (verbose) cout << dst << endl;
126 
127  Fill(0., dst);
128  SHOULD_THROW( dst.Set(0, 0, src_big) );
129  // set out of range
130  SHOULD_THROW( dst.Set(4, 0, src_small) );
131  SHOULD_THROW( dst.Set(0, 4, src_small) );
132  SHOULD_THROW( dst.Set(4, 4, src_small) );
133  SHOULD_THROW( dst.Set(5, 5, src_small) );
134 
135  SHOULD_THROW( dst.Set(3, 3, src_vec) );
136  SHOULD_THROW( dst.SetTranspose(3, 3, src_vec) );
137  SHOULD_THROW( dst.Set(3, 4, src_vec) );
138  SHOULD_THROW( dst.SetTranspose(4, 3, src_vec) );
139 }
Subscript num_cols() const
Definition: cmat.h:320
Subscript num_rows() const
Definition: cmat.h:319
Subscript size() const
Definition: vec.h:262