1 //=================================================================================================
2 /*!
3 // \file blazetest/mathtest/views/submatrix/DenseUnalignedTest.h
4 // \brief Header file for the Submatrix dense unaligned test
5 //
6 // Copyright (C) 2012-2020 Klaus Iglberger - All Rights Reserved
7 //
8 // This file is part of the Blaze library. You can redistribute it and/or modify it under
9 // the terms of the New (Revised) BSD License. Redistribution and use in source and binary
10 // forms, with or without modification, are permitted provided that the following conditions
11 // are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
16 // of conditions and the following disclaimer in the documentation and/or other materials
17 // provided with the distribution.
18 // 3. Neither the names of the Blaze development group nor the names of its contributors
19 // may be used to endorse or promote products derived from this software without specific
20 // prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 */
33 //=================================================================================================
34
35 #ifndef _BLAZETEST_MATHTEST_VIEWS_SUBMATRIX_DENSEUNALIGNEDTEST_H_
36 #define _BLAZETEST_MATHTEST_VIEWS_SUBMATRIX_DENSEUNALIGNEDTEST_H_
37
38
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42
43 #include <sstream>
44 #include <stdexcept>
45 #include <string>
46 #include <blaze/math/constraints/DenseMatrix.h>
47 #include <blaze/math/DynamicMatrix.h>
48 #include <blaze/math/Submatrix.h>
49 #include <blazetest/system/Types.h>
50
51
52 namespace blazetest {
53
54 namespace mathtest {
55
56 namespace views {
57
58 namespace submatrix {
59
60 //=================================================================================================
61 //
62 // CLASS DEFINITION
63 //
64 //=================================================================================================
65
66 //*************************************************************************************************
67 /*!\brief Auxiliary class for all tests of the dense unaligned Submatrix specialization.
68 //
69 // This class represents a test suite for the blaze::Submatrix class template specialization for
70 // dense unaligned submatrices. It performs a series of both compile time as well as runtime tests.
71 */
72 class DenseUnalignedTest
73 {
74 public:
75 //**Constructors********************************************************************************
76 /*!\name Constructors */
77 //@{
78 explicit DenseUnalignedTest();
79 // No explicitly declared copy constructor.
80 //@}
81 //**********************************************************************************************
82
83 //**Destructor**********************************************************************************
84 // No explicitly declared destructor.
85 //**********************************************************************************************
86
87 private:
88 //**Test functions******************************************************************************
89 /*!\name Test functions */
90 //@{
91 void testConstructors();
92 void testAssignment();
93 void testAddAssign();
94 void testSubAssign();
95 void testSchurAssign();
96 void testMultAssign();
97 void testScaling();
98 void testFunctionCall();
99 void testIterator();
100 void testNonZeros();
101 void testReset();
102 void testClear();
103 void testTranspose();
104 void testCTranspose();
105 void testIsDefault();
106 void testIsSame();
107 void testSubmatrix();
108 void testRow();
109 void testRows();
110 void testColumn();
111 void testColumns();
112 void testBand();
113
114 template< typename Type >
115 void checkRows( const Type& matrix, size_t expectedRows ) const;
116
117 template< typename Type >
118 void checkColumns( const Type& matrix, size_t expectedColumns ) const;
119
120 template< typename Type >
121 void checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const;
122
123 template< typename Type >
124 void checkNonZeros( const Type& matrix, size_t index, size_t expectedNonZeros ) const;
125 //@}
126 //**********************************************************************************************
127
128 //**Utility functions***************************************************************************
129 /*!\name Utility functions */
130 //@{
131 void initialize();
132 //@}
133 //**********************************************************************************************
134
135 //**Type definitions****************************************************************************
136 using MT = blaze::DynamicMatrix<int,blaze::rowMajor>; //!< Row-major dynamic matrix type
137 using OMT = MT::OppositeType; //!< Column-major dynamic matrix type
138 using SMT = blaze::Submatrix<MT>; //!< Dense submatrix type for row-major matrices.
139 using OSMT = blaze::Submatrix<OMT>; //!< Dense submatrix type for column-major matrices.
140 //**********************************************************************************************
141
142 //**Member variables****************************************************************************
143 /*!\name Member variables */
144 //@{
145 MT mat_; //!< Row-major dynamic matrix.
146 /*!< The \f$ 5 \times 4 \f$ matrix is initialized as
147 \f[\left(\begin{array}{*{4}{c}}
148 0 & 0 & 0 & 0 \\
149 0 & 1 & 0 & 0 \\
150 -2 & 0 & -3 & 0 \\
151 0 & 4 & 5 & -6 \\
152 7 & -8 & 9 & 10 \\
153 \end{array}\right)\f]. */
154 OMT tmat_; //!< Column-major dynamic matrix.
155 /*!< The \f$ 4 \times 5 \f$ matrix is initialized as
156 \f[\left(\begin{array}{*{4}{c}}
157 0 & 0 & -2 & 0 & 7 \\
158 0 & 1 & 0 & 4 & -8 \\
159 0 & 0 & -3 & 5 & 9 \\
160 0 & 0 & 0 & -6 & 10 \\
161 \end{array}\right)\f]. */
162
163 std::string test_; //!< Label of the currently performed test.
164 //@}
165 //**********************************************************************************************
166
167 //**Compile time checks*************************************************************************
168 /*! \cond BLAZE_INTERNAL */
169 BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( MT );
170 BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( OMT );
171 BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( SMT );
172 BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( OSMT );
173 /*! \endcond */
174 //**********************************************************************************************
175 };
176 //*************************************************************************************************
177
178
179
180
181 //=================================================================================================
182 //
183 // TEST FUNCTIONS
184 //
185 //=================================================================================================
186
187 //*************************************************************************************************
188 /*!\brief Checking the number of rows of the given dense matrix.
189 //
190 // \param matrix The dense matrix to be checked.
191 // \param expectedRows The expected number of rows of the dense matrix.
192 // \return void
193 // \exception std::runtime_error Error detected.
194 //
195 // This function checks the number of rows of the given dense matrix. In case the
196 // actual number of rows does not correspond to the given expected number of rows, a
197 // \a std::runtime_error exception is thrown.
198 */
199 template< typename Type > // Type of the dense matrix
checkRows(const Type & matrix,size_t expectedRows)200 void DenseUnalignedTest::checkRows( const Type& matrix, size_t expectedRows ) const
201 {
202 if( rows( matrix ) != expectedRows ) {
203 std::ostringstream oss;
204 oss << " Test: " << test_ << "\n"
205 << " Error: Invalid number of rows detected\n"
206 << " Details:\n"
207 << " Number of rows : " << rows( matrix ) << "\n"
208 << " Expected number of rows: " << expectedRows << "\n";
209 throw std::runtime_error( oss.str() );
210 }
211 }
212 //*************************************************************************************************
213
214
215 //*************************************************************************************************
216 /*!\brief Checking the number of columns of the given dense matrix.
217 //
218 // \param matrix The dense matrix to be checked.
219 // \param expectedColumns The expected number of columns of the dense matrix.
220 // \return void
221 // \exception std::runtime_error Error detected.
222 //
223 // This function checks the number of columns of the given dense matrix. In case the
224 // actual number of columns does not correspond to the given expected number of columns,
225 // a \a std::runtime_error exception is thrown.
226 */
227 template< typename Type > // Type of the dense matrix
checkColumns(const Type & matrix,size_t expectedColumns)228 void DenseUnalignedTest::checkColumns( const Type& matrix, size_t expectedColumns ) const
229 {
230 if( columns( matrix ) != expectedColumns ) {
231 std::ostringstream oss;
232 oss << " Test: " << test_ << "\n"
233 << " Error: Invalid number of columns detected\n"
234 << " Details:\n"
235 << " Number of columns : " << columns( matrix ) << "\n"
236 << " Expected number of columns: " << expectedColumns << "\n";
237 throw std::runtime_error( oss.str() );
238 }
239 }
240 //*************************************************************************************************
241
242
243 //*************************************************************************************************
244 /*!\brief Checking the number of non-zero elements of the given dense matrix.
245 //
246 // \param matrix The dense matrix to be checked.
247 // \param expectedNonZeros The expected number of non-zero elements of the dense matrix.
248 // \return void
249 // \exception std::runtime_error Error detected.
250 //
251 // This function checks the number of non-zero elements of the given dense matrix. In
252 // case the actual number of non-zero elements does not correspond to the given expected
253 // number, a \a std::runtime_error exception is thrown.
254 */
255 template< typename Type > // Type of the dense matrix
checkNonZeros(const Type & matrix,size_t expectedNonZeros)256 void DenseUnalignedTest::checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const
257 {
258 if( nonZeros( matrix ) != expectedNonZeros ) {
259 std::ostringstream oss;
260 oss << " Test: " << test_ << "\n"
261 << " Error: Invalid number of non-zero elements\n"
262 << " Details:\n"
263 << " Number of non-zeros : " << nonZeros( matrix ) << "\n"
264 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
265 throw std::runtime_error( oss.str() );
266 }
267
268 if( capacity( matrix ) < nonZeros( matrix ) ) {
269 std::ostringstream oss;
270 oss << " Test: " << test_ << "\n"
271 << " Error: Invalid capacity detected\n"
272 << " Details:\n"
273 << " Number of non-zeros: " << nonZeros( matrix ) << "\n"
274 << " Capacity : " << capacity( matrix ) << "\n";
275 throw std::runtime_error( oss.str() );
276 }
277 }
278 //*************************************************************************************************
279
280
281 //*************************************************************************************************
282 /*!\brief Checking the number of non-zero elements in a specific row/column of the given dense matrix.
283 //
284 // \param matrix The dense matrix to be checked.
285 // \param index The row/column to be checked.
286 // \param expectedNonZeros The expected number of non-zero elements in the specified row/column.
287 // \return void
288 // \exception std::runtime_error Error detected.
289 //
290 // This function checks the number of non-zero elements in the specified row/column of the
291 // given dense matrix. In case the actual number of non-zero elements does not correspond
292 // to the given expected number, a \a std::runtime_error exception is thrown.
293 */
294 template< typename Type > // Type of the dense matrix
checkNonZeros(const Type & matrix,size_t index,size_t expectedNonZeros)295 void DenseUnalignedTest::checkNonZeros( const Type& matrix, size_t index, size_t expectedNonZeros ) const
296 {
297 if( nonZeros( matrix, index ) != expectedNonZeros ) {
298 std::ostringstream oss;
299 oss << " Test: " << test_ << "\n"
300 << " Error: Invalid number of non-zero elements in "
301 << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
302 << " Details:\n"
303 << " Number of non-zeros : " << nonZeros( matrix, index ) << "\n"
304 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
305 throw std::runtime_error( oss.str() );
306 }
307
308 if( capacity( matrix, index ) < nonZeros( matrix, index ) ) {
309 std::ostringstream oss;
310 oss << " Test: " << test_ << "\n"
311 << " Error: Invalid capacity detected in "
312 << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
313 << " Details:\n"
314 << " Number of non-zeros: " << nonZeros( matrix, index ) << "\n"
315 << " Capacity : " << capacity( matrix, index ) << "\n";
316 throw std::runtime_error( oss.str() );
317 }
318 }
319 //*************************************************************************************************
320
321
322
323
324 //=================================================================================================
325 //
326 // GLOBAL TEST FUNCTIONS
327 //
328 //=================================================================================================
329
330 //*************************************************************************************************
331 /*!\brief Testing the functionality of the dense unaligned Submatrix specialization.
332 //
333 // \return void
334 */
runTest()335 void runTest()
336 {
337 DenseUnalignedTest();
338 }
339 //*************************************************************************************************
340
341
342
343
344 //=================================================================================================
345 //
346 // MACRO DEFINITIONS
347 //
348 //=================================================================================================
349
350 //*************************************************************************************************
351 /*! \cond BLAZE_INTERNAL */
352 /*!\brief Macro for the execution of the Submatrix dense unaligned test.
353 */
354 #define RUN_SUBMATRIX_DENSEUNALIGNED_TEST \
355 blazetest::mathtest::views::submatrix::runTest()
356 /*! \endcond */
357 //*************************************************************************************************
358
359 } // namespace submatrix
360
361 } // namespace views
362
363 } // namespace mathtest
364
365 } // namespace blazetest
366
367 #endif
368