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