1 /*
2  * Tiny Vector Matrix Library
3  * Dense Vector Matrix Libary of Tiny size using Expression Templates
4  *
5  * Copyright (C) 2001 - 2006 Olaf Petzold <opetzold@users.sourceforge.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * $Id: TestMatrixElementwise.h,v 1.3 2006-11-20 20:22:18 opetzold Exp $
22  */
23 
24 #ifndef TVMET_TEST_MATRIX_ELEMENTWISE_H
25 #define TVMET_TEST_MATRIX_ELEMENTWISE_H
26 
27 #include <cppunit/extensions/HelperMacros.h>
28 
29 #include <tvmet/Vector.h>
30 #include <tvmet/Matrix.h>
31 
32 template <class T>
33 class TestMatrixElementwise : public CppUnit::TestFixture
34 {
35   CPPUNIT_TEST_SUITE( TestMatrixElementwise );
36   CPPUNIT_TEST( sqr_add );
37   CPPUNIT_TEST( sqr_xpr_add );
38   CPPUNIT_TEST( sqr_mul );
39   CPPUNIT_TEST( sqr_xpr_mul );
40   CPPUNIT_TEST( nsqr_add );
41   CPPUNIT_TEST( nsqr_xpr_add );
42   CPPUNIT_TEST( nsqr_mul );
43   CPPUNIT_TEST( nsqr_xpr_mul );
44   CPPUNIT_TEST_SUITE_END();
45 
46 public:
TestMatrixElementwise()47   TestMatrixElementwise() { }
48 
49 public: // cppunit interface
50   /** cppunit hook for fixture set up. */
51   void setUp();
52 
53   /** cppunit hook for fixture tear down. */
54   void tearDown();
55 
56 protected:
57   void sqr_add();
58   void sqr_xpr_add();
59   void sqr_mul();
60   void sqr_xpr_mul();
61 
62   void nsqr_add();
63   void nsqr_xpr_add();
64   void nsqr_mul();
65   void nsqr_xpr_mul();
66 };
67 
68 /*****************************************************************************
69  * Implementation part I (cppunit part)
70  ****************************************************************************/
71 
72 template <class T>
setUp()73 void TestMatrixElementwise<T>::setUp() { }
74 
75 template <class T>
tearDown()76 void TestMatrixElementwise<T>::tearDown() { }
77 
78 /*****************************************************************************
79  * Implementation part II, square matrices
80  ****************************************************************************/
81 
82 template <class T>
sqr_add()83 void TestMatrixElementwise<T>::sqr_add() {
84   using namespace tvmet;
85 
86   Matrix<T, 3, 3>		M1, M2, Mr1, Mr2;
87 
88   M1 = 2;
89   M2 = 2;
90 
91   Mr1 = M1 + M2;
92   Mr2 = add(M1, M1);
93 
94   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
95   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
96 }
97 
98 
99 template <class T>
sqr_xpr_add()100 void TestMatrixElementwise<T>::sqr_xpr_add() {
101   using namespace tvmet;
102 
103   Matrix<T, 3, 3>		M1, M2, Mr1, Mr2;
104 
105   M1 = 1;
106   M2 = 1;
107 
108   T c1 = 1;
109   T c2 = 1;
110 
111   Mr1 = (c1+M1) + (c2+M2);
112   Mr2 = add(add(c1,M1), add(c2,M1));
113 
114   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
115   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
116 }
117 
118 
119 template <class T>
sqr_mul()120 void TestMatrixElementwise<T>::sqr_mul() {
121   using namespace tvmet;
122 
123   Matrix<T, 3, 3>		M1, M2, Mr1, Mr2;
124 
125   M1 = 2;
126   M2 = 2;
127 
128   Mr1 = element_wise::operator*(M1, M2);
129   Mr2 = element_wise::mul(M1, M2);
130 
131   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
132   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
133 }
134 
135 
136 template <class T>
sqr_xpr_mul()137 void TestMatrixElementwise<T>::sqr_xpr_mul() {
138   using namespace tvmet;
139 
140   Matrix<T, 3, 3>		M1, M2, Mr1, Mr2;
141 
142   M1 = 2;
143   M2 = 2;
144 
145   T c1 = 1;
146   T c2 = 1;
147 
148   Mr1 = element_wise::operator*(c1*M1, c2*M2);
149   Mr2 = element_wise::mul(mul(c1, M1), mul(c2, M2));
150 
151   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
152   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
153 }
154 
155 
156 /*****************************************************************************
157  * Implementation part II, non square matrices
158  ****************************************************************************/
159 
160 template <class T>
nsqr_add()161 void TestMatrixElementwise<T>::nsqr_add() {
162   using namespace tvmet;
163 
164   Matrix<T, 4, 3>		M1, M2, Mr1, Mr2;
165 
166   M1 = 2;
167   M2 = 2;
168 
169   Mr1 = M1 + M2;
170   Mr2 = add(M1, M1);
171 
172   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
173   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
174 }
175 
176 
177 template <class T>
nsqr_xpr_add()178 void TestMatrixElementwise<T>::nsqr_xpr_add() {
179   using namespace tvmet;
180 
181   Matrix<T, 4, 3>		M1, M2, Mr1, Mr2;
182 
183   M1 = 1;
184   M2 = 1;
185 
186   T c1 = 1;
187   T c2 = 1;
188 
189   Mr1 = (c1+M1) + (c2+M2);
190   Mr2 = add(add(c1,M1), add(c2,M1));
191 
192   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
193   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
194 }
195 
196 
197 template <class T>
nsqr_mul()198 void TestMatrixElementwise<T>::nsqr_mul() {
199   using namespace tvmet;
200 
201   Matrix<T, 4, 3>		M1, M2, Mr1, Mr2;
202 
203   M1 = 2;
204   M2 = 2;
205 
206   Mr1 = element_wise::operator*(M1, M2);
207   Mr2 = element_wise::mul(M1, M2);
208 
209   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
210   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
211 }
212 
213 
214 template <class T>
nsqr_xpr_mul()215 void TestMatrixElementwise<T>::nsqr_xpr_mul() {
216   using namespace tvmet;
217 
218   Matrix<T, 4, 3>		M1, M2, Mr1, Mr2;
219 
220   M1 = 2;
221   M2 = 2;
222 
223   T c1 = 1;
224   T c2 = 1;
225 
226   Mr1 = element_wise::operator*(c1*M1, c2*M2);
227   Mr2 = element_wise::mul(mul(c1, M1), mul(c2, M2));
228 
229   CPPUNIT_ASSERT( all_elements(Mr1 == 4) );
230   CPPUNIT_ASSERT( all_elements(Mr2 == 4) );
231 }
232 
233 
234 #endif // TVMET_TEST_MATRIX_ELEMENTWISE_H
235 
236 // Local Variables:
237 // mode:C++
238 // End:
239