1 //=================================================================================================
2 /*!
3 // \file blazetest/mathtest/views/subvector/SparseTest.h
4 // \brief Header file for the Subvector sparse 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_SUBVECTOR_SPARSETEST_H_
36 #define _BLAZETEST_MATHTEST_VIEWS_SUBVECTOR_SPARSETEST_H_
37
38
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42
43 #include <sstream>
44 #include <stdexcept>
45 #include <string>
46 #include <blaze/math/constraints/SparseVector.h>
47 #include <blaze/math/CompressedVector.h>
48 #include <blaze/math/Subvector.h>
49 #include <blazetest/system/Types.h>
50
51
52 namespace blazetest {
53
54 namespace mathtest {
55
56 namespace views {
57
58 namespace subvector {
59
60 //=================================================================================================
61 //
62 // CLASS DEFINITION
63 //
64 //=================================================================================================
65
66 //*************************************************************************************************
67 /*!\brief Auxiliary class for all tests of the sparse Subvector specialization.
68 //
69 // This class represents a test suite for the blaze::Subvector class template specialization for
70 // sparse subvectors. It performs a series of both compile time as well as runtime tests.
71 */
72 class SparseTest
73 {
74 public:
75 //**Constructors********************************************************************************
76 /*!\name Constructors */
77 //@{
78 explicit SparseTest();
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 testMultAssign ();
96 void testDivAssign ();
97 void testCrossAssign ();
98 void testScaling ();
99 void testSubscript ();
100 void testIterator ();
101 void testNonZeros ();
102 void testReset ();
103 void testClear ();
104 void testReserve ();
105 void testSet ();
106 void testInsert ();
107 void testAppend ();
108 void testErase ();
109 void testFind ();
110 void testLowerBound ();
111 void testUpperBound ();
112 void testIsDefault ();
113 void testIsSame ();
114 void testSubvector ();
115 void testElements ();
116
117 template< typename Type >
118 void checkSize( const Type& vector, size_t expectedSize ) const;
119
120 template< typename Type >
121 void checkCapacity( const Type& vector, size_t minCapacity ) const;
122
123 template< typename Type >
124 void checkNonZeros( const Type& vector, 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 VT = blaze::CompressedVector<int,blaze::rowVector>; //!< Compressed row vector type
137 using SVT = blaze::Subvector<VT>; //!< Subvector type for compressed row vectors.
138 //**********************************************************************************************
139
140 //**Member variables****************************************************************************
141 /*!\name Member variables */
142 //@{
143 VT vec_; //!< Compressed column vector.
144 /*!< The 8-dimensional sparse vector is initialized as
145 \f[\left(\begin{array}{*{4}{c}}
146 0 & 1 & 0 & -2 & -3 & 0 & 4 & 0 \\
147 \end{array}\right)\f]. */
148
149 std::string test_; //!< Label of the currently performed test.
150 //@}
151 //**********************************************************************************************
152
153 //**Compile time checks*************************************************************************
154 /*! \cond BLAZE_INTERNAL */
155 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( VT );
156 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( SVT );
157 /*! \endcond */
158 //**********************************************************************************************
159 };
160 //*************************************************************************************************
161
162
163
164
165 //=================================================================================================
166 //
167 // TEST FUNCTIONS
168 //
169 //=================================================================================================
170
171 //*************************************************************************************************
172 /*!\brief Checking the size of the given sparse vector.
173 //
174 // \param vector The sparse vector to be checked.
175 // \param expectedSize The expected size of the sparse vector.
176 // \return void
177 // \exception std::runtime_error Error detected.
178 //
179 // This function checks the size of the given sparse vector. In case the actual size does not
180 // correspond to the given expected size, a \a std::runtime_error exception is thrown.
181 */
182 template< typename Type > // Type of the sparse vector
checkSize(const Type & vector,size_t expectedSize)183 void SparseTest::checkSize( const Type& vector, size_t expectedSize ) const
184 {
185 if( size( vector ) != expectedSize ) {
186 std::ostringstream oss;
187 oss << " Test: " << test_ << "\n"
188 << " Error: Invalid size detected\n"
189 << " Details:\n"
190 << " Size : " << size( vector ) << "\n"
191 << " Expected size: " << expectedSize << "\n";
192 throw std::runtime_error( oss.str() );
193 }
194 }
195 //*************************************************************************************************
196
197
198 //*************************************************************************************************
199 /*!\brief Checking the capacity of the given sparse vector.
200 //
201 // \param vector The sparse vector to be checked.
202 // \param minCapacity The expected minimum capacity.
203 // \return void
204 // \exception std::runtime_error Error detected.
205 //
206 // This function checks the capacity of the given sparse vector. In case the actual capacity is
207 // smaller than the given expected minimum capacity, a \a std::runtime_error exception is thrown.
208 */
209 template< typename Type > // Type of the sparse vector
checkCapacity(const Type & vector,size_t minCapacity)210 void SparseTest::checkCapacity( const Type& vector, size_t minCapacity ) const
211 {
212 if( capacity( vector ) < minCapacity ) {
213 std::ostringstream oss;
214 oss << " Test: " << test_ << "\n"
215 << " Error: Invalid capacity detected\n"
216 << " Details:\n"
217 << " Capacity : " << capacity( vector ) << "\n"
218 << " Expected minimum capacity: " << minCapacity << "\n";
219 throw std::runtime_error( oss.str() );
220 }
221 }
222 //*************************************************************************************************
223
224
225 //*************************************************************************************************
226 /*!\brief Checking the number of non-zero elements of the given sparse vector.
227 //
228 // \param object The sparse vector to be checked.
229 // \param expectedNonZeros The expected number of non-zero elements.
230 // \return void
231 // \exception std::runtime_error Error detected.
232 //
233 // This function checks the number of non-zero elements of the given sparse vector. In case
234 // the actual number of non-zero elements does not correspond to the given expected number,
235 // a \a std::runtime_error exception is thrown.
236 */
237 template< typename Type > // Type of the sparse vector
checkNonZeros(const Type & vector,size_t expectedNonZeros)238 void SparseTest::checkNonZeros( const Type& vector, size_t expectedNonZeros ) const
239 {
240 if( nonZeros( vector ) != expectedNonZeros ) {
241 std::ostringstream oss;
242 oss << " Test: " << test_ << "\n"
243 << " Error: Invalid number of non-zero elements\n"
244 << " Details:\n"
245 << " Number of non-zeros : " << nonZeros( vector ) << "\n"
246 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
247 throw std::runtime_error( oss.str() );
248 }
249
250 if( capacity( vector ) < nonZeros( vector ) ) {
251 std::ostringstream oss;
252 oss << " Test: " << test_ << "\n"
253 << " Error: Invalid capacity detected\n"
254 << " Details:\n"
255 << " Number of non-zeros: " << nonZeros( vector ) << "\n"
256 << " Capacity : " << capacity( vector ) << "\n";
257 throw std::runtime_error( oss.str() );
258 }
259 }
260 //*************************************************************************************************
261
262
263
264
265 //=================================================================================================
266 //
267 // GLOBAL TEST FUNCTIONS
268 //
269 //=================================================================================================
270
271 //*************************************************************************************************
272 /*!\brief Testing the functionality of the sparse Subvector specialization.
273 //
274 // \return void
275 */
runTest()276 void runTest()
277 {
278 SparseTest();
279 }
280 //*************************************************************************************************
281
282
283
284
285 //=================================================================================================
286 //
287 // MACRO DEFINITIONS
288 //
289 //=================================================================================================
290
291 //*************************************************************************************************
292 /*! \cond BLAZE_INTERNAL */
293 /*!\brief Macro for the execution of the Subvector sparse test.
294 */
295 #define RUN_SUBVECTOR_SPARSE_TEST \
296 blazetest::mathtest::views::subvector::runTest()
297 /*! \endcond */
298 //*************************************************************************************************
299
300 } // namespace subvector
301
302 } // namespace views
303
304 } // namespace mathtest
305
306 } // namespace blazetest
307
308 #endif
309