1 //=================================================================================================
2 /*!
3 //  \file blazetest/mathtest/adaptors/diagonalmatrix/DenseTest.h
4 //  \brief Header file for the DiagonalMatrix dense 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_ADAPTORS_DIAGONALMATRIX_DENSETEST_H_
36 #define _BLAZETEST_MATHTEST_ADAPTORS_DIAGONALMATRIX_DENSETEST_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <sstream>
44 #include <stdexcept>
45 #include <string>
46 #include <blaze/math/constraints/ColumnMajorMatrix.h>
47 #include <blaze/math/constraints/DenseMatrix.h>
48 #include <blaze/math/constraints/Diagonal.h>
49 #include <blaze/math/constraints/RequiresEvaluation.h>
50 #include <blaze/math/constraints/RowMajorMatrix.h>
51 #include <blaze/math/DynamicMatrix.h>
52 #include <blaze/math/DiagonalMatrix.h>
53 #include <blaze/math/typetraits/IsRowMajorMatrix.h>
54 #include <blaze/util/constraints/SameType.h>
55 #include <blazetest/system/Types.h>
56 
57 
58 namespace blazetest {
59 
60 namespace mathtest {
61 
62 namespace adaptors {
63 
64 namespace diagonalmatrix {
65 
66 //=================================================================================================
67 //
68 //  CLASS DEFINITION
69 //
70 //=================================================================================================
71 
72 //*************************************************************************************************
73 /*!\brief Auxiliary class for all tests of the dense DiagonalMatrix specialization.
74 //
75 // This class represents a test suite for the blaze::DiagonalMatrix class template specialization
76 // for dense matrices. It performs a series of both compile time as well as runtime tests.
77 */
78 class DenseTest
79 {
80  public:
81    //**Constructors********************************************************************************
82    /*!\name Constructors */
83    //@{
84    explicit DenseTest();
85    // No explicitly declared copy constructor.
86    //@}
87    //**********************************************************************************************
88 
89    //**Destructor**********************************************************************************
90    // No explicitly declared destructor.
91    //**********************************************************************************************
92 
93  private:
94    //**Test functions******************************************************************************
95    /*!\name Test functions */
96    //@{
97    void testConstructors();
98    void testAssignment  ();
99    void testAddAssign   ();
100    void testSubAssign   ();
101    void testSchurAssign ();
102    void testMultAssign  ();
103    void testScaling     ();
104    void testFunctionCall();
105    void testIterator    ();
106    void testNonZeros    ();
107    void testReset       ();
108    void testClear       ();
109    void testResize      ();
110    void testExtend      ();
111    void testReserve     ();
112    void testShrinkToFit ();
113    void testSwap        ();
114    void testIsDefault   ();
115    void testSubmatrix   ();
116    void testRow         ();
117    void testColumn      ();
118 
119    template< typename Type >
120    void checkRows( const Type& matrix, size_t expectedRows ) const;
121 
122    template< typename Type >
123    void checkColumns( const Type& matrix, size_t expectedColumns ) const;
124 
125    template< typename Type >
126    void checkCapacity( const Type& matrix, size_t minCapacity ) const;
127 
128    template< typename Type >
129    void checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const;
130 
131    template< typename Type >
132    void checkNonZeros( const Type& matrix, size_t index, size_t expectedNonZeros ) const;
133    //@}
134    //**********************************************************************************************
135 
136    //**Member variables****************************************************************************
137    /*!\name Member variables */
138    //@{
139    std::string test_;  //!< Label of the currently performed test.
140    //@}
141    //**********************************************************************************************
142 
143    //**Type definitions****************************************************************************
144    //! Type of the row-major diagonal matrix.
145    using DT = blaze::DiagonalMatrix< blaze::DynamicMatrix<int,blaze::rowMajor> >;
146 
147    //! Type of the column-major diagonal matrix.
148    using ODT = blaze::DiagonalMatrix< blaze::DynamicMatrix<int,blaze::columnMajor> >;
149 
150    using RDT  = DT::Rebind<double>::Other;   //!< Rebound row-major diagonal matrix type.
151    using ORDT = ODT::Rebind<double>::Other;  //!< Rebound column-major diagonal matrix type.
152    //**********************************************************************************************
153 
154    //**Compile time checks*************************************************************************
155    /*! \cond BLAZE_INTERNAL */
156    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( DT                  );
157    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( DT::ResultType      );
158    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( DT::OppositeType    );
159    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( DT::TransposeType   );
160    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ODT                 );
161    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ODT::ResultType     );
162    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ODT::OppositeType   );
163    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ODT::TransposeType  );
164    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( RDT                 );
165    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( RDT::ResultType     );
166    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( RDT::OppositeType   );
167    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( RDT::TransposeType  );
168    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ORDT                );
169    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ORDT::ResultType    );
170    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ORDT::OppositeType  );
171    BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE( ORDT::TransposeType );
172 
173    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( DT                  );
174    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( DT::ResultType      );
175    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( DT::OppositeType    );
176    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( DT::TransposeType   );
177    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( ODT                 );
178    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( ODT::ResultType     );
179    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( ODT::OppositeType   );
180    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( ODT::TransposeType  );
181    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( RDT                 );
182    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( RDT::ResultType     );
183    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( RDT::OppositeType   );
184    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( RDT::TransposeType  );
185    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( ORDT                );
186    BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE( ORDT::ResultType    );
187    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( ORDT::OppositeType  );
188    BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE   ( ORDT::TransposeType );
189 
190    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( DT                  );
191    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( DT::ResultType      );
192    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( DT::OppositeType    );
193    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( DT::TransposeType   );
194    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ODT                 );
195    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ODT::ResultType     );
196    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ODT::OppositeType   );
197    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ODT::TransposeType  );
198    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( RDT                 );
199    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( RDT::ResultType     );
200    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( RDT::OppositeType   );
201    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( RDT::TransposeType  );
202    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ORDT                );
203    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ORDT::ResultType    );
204    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ORDT::OppositeType  );
205    BLAZE_CONSTRAINT_MUST_BE_DIAGONAL_MATRIX_TYPE( ORDT::TransposeType );
206 
207    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( DT::ResultType      );
208    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( DT::OppositeType    );
209    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( DT::TransposeType   );
210    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( ODT::ResultType     );
211    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( ODT::OppositeType   );
212    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( ODT::TransposeType  );
213    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( RDT::ResultType     );
214    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( RDT::OppositeType   );
215    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( RDT::TransposeType  );
216    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( ORDT::ResultType    );
217    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( ORDT::OppositeType  );
218    BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( ORDT::TransposeType );
219 
220    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( DT::ElementType,   DT::ResultType::ElementType      );
221    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( DT::ElementType,   DT::OppositeType::ElementType    );
222    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( DT::ElementType,   DT::TransposeType::ElementType   );
223    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( ODT::ElementType,  ODT::ResultType::ElementType     );
224    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( ODT::ElementType,  ODT::OppositeType::ElementType   );
225    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( ODT::ElementType,  ODT::TransposeType::ElementType  );
226    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( RDT::ElementType,  RDT::ResultType::ElementType     );
227    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( RDT::ElementType,  RDT::OppositeType::ElementType   );
228    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( RDT::ElementType,  RDT::TransposeType::ElementType  );
229    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( ORDT::ElementType, ORDT::ResultType::ElementType    );
230    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( ORDT::ElementType, ORDT::OppositeType::ElementType  );
231    BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( ORDT::ElementType, ORDT::TransposeType::ElementType );
232    /*! \endcond */
233    //**********************************************************************************************
234 };
235 //*************************************************************************************************
236 
237 
238 
239 
240 //=================================================================================================
241 //
242 //  TEST FUNCTIONS
243 //
244 //=================================================================================================
245 
246 //*************************************************************************************************
247 /*!\brief Checking the number of rows of the given matrix.
248 //
249 // \param matrix The matrix to be checked.
250 // \param expectedRows The expected number of rows of the matrix.
251 // \return void
252 // \exception std::runtime_error Error detected.
253 //
254 // This function checks the number of rows of the given matrix. In case the actual number of
255 // rows does not correspond to the given expected number of rows, a \a std::runtime_error
256 // exception is thrown.
257 */
258 template< typename Type >  // Type of the matrix
checkRows(const Type & matrix,size_t expectedRows)259 void DenseTest::checkRows( const Type& matrix, size_t expectedRows ) const
260 {
261    if( matrix.rows() != expectedRows ) {
262       std::ostringstream oss;
263       oss << " Test: " << test_ << "\n"
264           << " Error: Invalid number of rows detected\n"
265           << " Details:\n"
266           << "   Number of rows         : " << matrix.rows() << "\n"
267           << "   Expected number of rows: " << expectedRows << "\n";
268       throw std::runtime_error( oss.str() );
269    }
270 }
271 //*************************************************************************************************
272 
273 
274 //*************************************************************************************************
275 /*!\brief Checking the number of columns of the given matrix.
276 //
277 // \param matrix The matrix to be checked.
278 // \param expectedRows The expected number of columns of the matrix.
279 // \return void
280 // \exception std::runtime_error Error detected.
281 //
282 // This function checks the number of columns of the given matrix. In case the actual number of
283 // columns does not correspond to the given expected number of columns, a \a std::runtime_error
284 // exception is thrown.
285 */
286 template< typename Type >  // Type of the matrix
checkColumns(const Type & matrix,size_t expectedColumns)287 void DenseTest::checkColumns( const Type& matrix, size_t expectedColumns ) const
288 {
289    if( matrix.columns() != expectedColumns ) {
290       std::ostringstream oss;
291       oss << " Test: " << test_ << "\n"
292           << " Error: Invalid number of columns detected\n"
293           << " Details:\n"
294           << "   Number of columns         : " << matrix.columns() << "\n"
295           << "   Expected number of columns: " << expectedColumns << "\n";
296       throw std::runtime_error( oss.str() );
297    }
298 }
299 //*************************************************************************************************
300 
301 
302 //*************************************************************************************************
303 /*!\brief Checking the capacity of the given matrix.
304 //
305 // \param matrix The matrix to be checked.
306 // \param minCapacity The expected minimum capacity of the matrix.
307 // \return void
308 // \exception std::runtime_error Error detected.
309 //
310 // This function checks the capacity of the given matrix. In case the actual capacity is smaller
311 // than the given expected minimum capacity, a \a std::runtime_error exception is thrown.
312 */
313 template< typename Type >  // Type of the matrix
checkCapacity(const Type & matrix,size_t minCapacity)314 void DenseTest::checkCapacity( const Type& matrix, size_t minCapacity ) const
315 {
316    if( capacity( matrix ) < minCapacity ) {
317       std::ostringstream oss;
318       oss << " Test: " << test_ << "\n"
319           << " Error: Invalid capacity detected\n"
320           << " Details:\n"
321           << "   Capacity                 : " << capacity( matrix ) << "\n"
322           << "   Expected minimum capacity: " << minCapacity << "\n";
323       throw std::runtime_error( oss.str() );
324    }
325 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
330 /*!\brief Checking the number of non-zero elements of the given matrix.
331 //
332 // \param matrix The matrix to be checked.
333 // \param expectedNonZeros The expected number of non-zero elements of the matrix.
334 // \return void
335 // \exception std::runtime_error Error detected.
336 //
337 // This function checks the number of non-zero elements of the given matrix. In case the
338 // actual number of non-zero elements does not correspond to the given expected number,
339 // a \a std::runtime_error exception is thrown.
340 */
341 template< typename Type >  // Type of the matrix
checkNonZeros(const Type & matrix,size_t expectedNonZeros)342 void DenseTest::checkNonZeros( const Type& matrix, size_t expectedNonZeros ) const
343 {
344    if( nonZeros( matrix ) != expectedNonZeros ) {
345       std::ostringstream oss;
346       oss << " Test: " << test_ << "\n"
347           << " Error: Invalid number of non-zero elements\n"
348           << " Details:\n"
349           << "   Number of non-zeros         : " << nonZeros( matrix ) << "\n"
350           << "   Expected number of non-zeros: " << expectedNonZeros << "\n";
351       throw std::runtime_error( oss.str() );
352    }
353 
354    if( capacity( matrix ) < nonZeros( matrix ) ) {
355       std::ostringstream oss;
356       oss << " Test: " << test_ << "\n"
357           << " Error: Invalid capacity detected\n"
358           << " Details:\n"
359           << "   Number of non-zeros: " << nonZeros( matrix ) << "\n"
360           << "   Capacity           : " << capacity( matrix ) << "\n";
361       throw std::runtime_error( oss.str() );
362    }
363 }
364 //*************************************************************************************************
365 
366 
367 //*************************************************************************************************
368 /*!\brief Checking the number of non-zero elements in a specific row/column of the given matrix.
369 //
370 // \param matrix The matrix to be checked.
371 // \param index The row/column to be checked.
372 // \param expectedNonZeros The expected number of non-zero elements in the specified row/column.
373 // \return void
374 // \exception std::runtime_error Error detected.
375 //
376 // This function checks the number of non-zero elements in the specified row/column of the
377 // given matrix. In case the actual number of non-zero elements does not correspond to the
378 // given expected number, a \a std::runtime_error exception is thrown.
379 */
380 template< typename Type >  // Type of the matrix
checkNonZeros(const Type & matrix,size_t index,size_t expectedNonZeros)381 void DenseTest::checkNonZeros( const Type& matrix, size_t index, size_t expectedNonZeros ) const
382 {
383    if( nonZeros( matrix, index ) != expectedNonZeros ) {
384       std::ostringstream oss;
385       oss << " Test: " << test_ << "\n"
386           << " Error: Invalid number of non-zero elements in "
387           << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
388           << " Details:\n"
389           << "   Number of non-zeros         : " << nonZeros( matrix, index ) << "\n"
390           << "   Expected number of non-zeros: " << expectedNonZeros << "\n";
391       throw std::runtime_error( oss.str() );
392    }
393 
394    if( capacity( matrix, index ) < nonZeros( matrix, index ) ) {
395       std::ostringstream oss;
396       oss << " Test: " << test_ << "\n"
397           << " Error: Invalid capacity detected in "
398           << ( blaze::IsRowMajorMatrix<Type>::value ? "row " : "column " ) << index << "\n"
399           << " Details:\n"
400           << "   Number of non-zeros: " << nonZeros( matrix, index ) << "\n"
401           << "   Capacity           : " << capacity( matrix, index ) << "\n";
402       throw std::runtime_error( oss.str() );
403    }
404 }
405 //*************************************************************************************************
406 
407 
408 
409 
410 //=================================================================================================
411 //
412 //  GLOBAL TEST FUNCTIONS
413 //
414 //=================================================================================================
415 
416 //*************************************************************************************************
417 /*!\brief Testing the functionality of the dense DiagonalMatrix specialization.
418 //
419 // \return void
420 */
runTest()421 void runTest()
422 {
423    DenseTest();
424 }
425 //*************************************************************************************************
426 
427 
428 
429 
430 //=================================================================================================
431 //
432 //  MACRO DEFINITIONS
433 //
434 //=================================================================================================
435 
436 //*************************************************************************************************
437 /*! \cond BLAZE_INTERNAL */
438 /*!\brief Macro for the execution of the DiagonalMatrix dense test.
439 */
440 #define RUN_DIAGONALMATRIX_DENSE_TEST \
441    blazetest::mathtest::adaptors::diagonalmatrix::runTest()
442 /*! \endcond */
443 //*************************************************************************************************
444 
445 } // namespace diagonalmatrix
446 
447 } // namespace adaptors
448 
449 } // namespace mathtest
450 
451 } // namespace blazetest
452 
453 #endif
454