1 //=================================================================================================
2 /*!
3 // \file blazetest/mathtest/vectors/zerovector/ClassTest.h
4 // \brief Header file for the ZeroVector class 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_VECTORS_ZEROVECTOR_CLASSTEST_H_
36 #define _BLAZETEST_MATHTEST_VECTORS_ZEROVECTOR_CLASSTEST_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/ZeroVector.h>
48 #include <blaze/util/constraints/SameType.h>
49 #include <blazetest/system/Types.h>
50
51
52 namespace blazetest {
53
54 namespace mathtest {
55
56 namespace vectors {
57
58 namespace zerovector {
59
60 //=================================================================================================
61 //
62 // CLASS DEFINITION
63 //
64 //=================================================================================================
65
66 //*************************************************************************************************
67 /*!\brief Auxiliary class for all tests of the ZeroVector class template.
68 //
69 // This class represents a test suite for the blaze::ZeroVector class template. It performs a
70 // series of both compile time as well as runtime tests.
71 */
72 class ClassTest
73 {
74 public:
75 //**Constructors********************************************************************************
76 /*!\name Constructors */
77 //@{
78 explicit ClassTest();
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 testSubscript ();
94 void testAt ();
95 void testIterator ();
96 void testNonZeros ();
97 void testReset ();
98 void testClear ();
99 void testResize ();
100 void testSwap ();
101 void testFind ();
102 void testLowerBound ();
103 void testUpperBound ();
104 void testIsDefault ();
105
106 template< typename Type >
107 void checkSize( const Type& vector, size_t expectedSize ) const;
108
109 template< typename Type >
110 void checkCapacity( const Type& vector, size_t minCapacity ) const;
111
112 template< typename Type >
113 void checkNonZeros( const Type& vector, size_t nonzeros ) const;
114 //@}
115 //**********************************************************************************************
116
117 //**Member variables****************************************************************************
118 /*!\name Member variables */
119 //@{
120 std::string test_; //!< Label of the currently performed test.
121 //@}
122 //**********************************************************************************************
123
124 //**Type definitions****************************************************************************
125 using VT = blaze::ZeroVector<int,blaze::rowVector>; //!< Type of the zero vector.
126 using TVT = blaze::ZeroVector<int,blaze::columnVector>; //!< Transpose zero vector type.
127
128 using RVT = VT::Rebind<double>::Other; //!< Rebound zero vector type.
129 using TRVT = TVT::Rebind<double>::Other; //!< Transpose rebound zero vector type.
130 //**********************************************************************************************
131
132 //**Compile time checks*************************************************************************
133 /*! \cond BLAZE_INTERNAL */
134 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( VT );
135 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( VT::ResultType );
136 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( VT::TransposeType );
137 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( TVT );
138 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( TVT::ResultType );
139 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( TVT::TransposeType );
140 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( RVT );
141 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( RVT::ResultType );
142 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( RVT::TransposeType );
143 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( TRVT );
144 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( TRVT::ResultType );
145 BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE( TRVT::TransposeType );
146
147 BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE ( VT );
148 BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE ( VT::ResultType );
149 BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE( VT::TransposeType );
150 BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE( TVT );
151 BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE( TVT::ResultType );
152 BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE ( TVT::TransposeType );
153 BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE ( RVT );
154 BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE ( RVT::ResultType );
155 BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE( RVT::TransposeType );
156 BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE( TRVT );
157 BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE( TRVT::ResultType );
158 BLAZE_CONSTRAINT_MUST_BE_ROW_VECTOR_TYPE ( TRVT::TransposeType );
159
160 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( VT::ResultType );
161 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( VT::TransposeType );
162 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( TVT::ResultType );
163 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( TVT::TransposeType );
164 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( RVT::ResultType );
165 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( RVT::TransposeType );
166 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( TRVT::ResultType );
167 BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( TRVT::TransposeType );
168
169 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( VT::ElementType, VT::ResultType::ElementType );
170 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( VT::ElementType, VT::TransposeType::ElementType );
171 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( TVT::ElementType, TVT::ResultType::ElementType );
172 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( TVT::ElementType, TVT::TransposeType::ElementType );
173 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( RVT::ElementType, RVT::ResultType::ElementType );
174 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( RVT::ElementType, RVT::TransposeType::ElementType );
175 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( TRVT::ElementType, TRVT::ResultType::ElementType );
176 BLAZE_CONSTRAINT_MUST_BE_SAME_TYPE( TRVT::ElementType, TRVT::TransposeType::ElementType );
177 /*! \endcond */
178 //**********************************************************************************************
179 };
180 //*************************************************************************************************
181
182
183
184
185 //=================================================================================================
186 //
187 // TEST FUNCTIONS
188 //
189 //=================================================================================================
190
191 //*************************************************************************************************
192 /*!\brief Checking the size of the given vector.
193 //
194 // \param vector The vector to be checked.
195 // \param expectedSize The expected size of the vector.
196 // \return void
197 // \exception std::runtime_error Error detected.
198 //
199 // This function checks the size of the given vector. In case the actual size does not correspond
200 // to the given expected size, a \a std::runtime_error exception is thrown.
201 */
202 template< typename Type > // Type of the vector
checkSize(const Type & vector,size_t expectedSize)203 void ClassTest::checkSize( const Type& vector, size_t expectedSize ) const
204 {
205 if( size( vector ) != expectedSize ) {
206 std::ostringstream oss;
207 oss << " Test: " << test_ << "\n"
208 << " Error: Invalid size detected\n"
209 << " Details:\n"
210 << " Size : " << size( vector ) << "\n"
211 << " Expected size: " << expectedSize << "\n";
212 throw std::runtime_error( oss.str() );
213 }
214 }
215 //*************************************************************************************************
216
217
218 //*************************************************************************************************
219 /*!\brief Checking the capacity of the given vector.
220 //
221 // \param vector The vector to be checked.
222 // \param minCapacity The expected minimum capacity of the vector.
223 // \return void
224 // \exception std::runtime_error Error detected.
225 //
226 // This function checks the capacity of the given vector. In case the actual capacity is smaller
227 // than the given expected minimum capacity, a \a std::runtime_error exception is thrown.
228 */
229 template< typename Type > // Type of the vector
checkCapacity(const Type & vector,size_t minCapacity)230 void ClassTest::checkCapacity( const Type& vector, size_t minCapacity ) const
231 {
232 if( capacity( vector ) < minCapacity ) {
233 std::ostringstream oss;
234 oss << " Test: " << test_ << "\n"
235 << " Error: Invalid capacity detected\n"
236 << " Details:\n"
237 << " Capacity : " << capacity( vector ) << "\n"
238 << " Expected minimum capacity: " << minCapacity << "\n";
239 throw std::runtime_error( oss.str() );
240 }
241 }
242 //*************************************************************************************************
243
244
245 //*************************************************************************************************
246 /*!\brief Checking the number of non-zero elements of the given vector.
247 //
248 // \param vector The vector to be checked.
249 // \param expectedNonZeros The expected number of non-zero elements of the vector.
250 // \return void
251 // \exception std::runtime_error Error detected.
252 //
253 // This function checks the number of non-zero elements of the given vector. In case the
254 // actual number of non-zero elements does not correspond to the given expected number,
255 // a \a std::runtime_error exception is thrown.
256 */
257 template< typename Type > // Type of the vector
checkNonZeros(const Type & vector,size_t expectedNonZeros)258 void ClassTest::checkNonZeros( const Type& vector, size_t expectedNonZeros ) const
259 {
260 if( nonZeros( vector ) != expectedNonZeros ) {
261 std::ostringstream oss;
262 oss << " Test: " << test_ << "\n"
263 << " Error: Invalid number of non-zero elements\n"
264 << " Details:\n"
265 << " Number of non-zeros : " << nonZeros( vector ) << "\n"
266 << " Expected number of non-zeros: " << expectedNonZeros << "\n";
267 throw std::runtime_error( oss.str() );
268 }
269 }
270 //*************************************************************************************************
271
272
273
274
275 //=================================================================================================
276 //
277 // GLOBAL TEST FUNCTIONS
278 //
279 //=================================================================================================
280
281 //*************************************************************************************************
282 /*!\brief Testing the functionality of the ZeroVector class template.
283 //
284 // \return void
285 */
runTest()286 void runTest()
287 {
288 ClassTest();
289 }
290 //*************************************************************************************************
291
292
293
294
295 //=================================================================================================
296 //
297 // MACRO DEFINITIONS
298 //
299 //=================================================================================================
300
301 //*************************************************************************************************
302 /*! \cond BLAZE_INTERNAL */
303 /*!\brief Macro for the execution of the ZeroVector class test.
304 */
305 #define RUN_ZEROVECTOR_CLASS_TEST \
306 blazetest::mathtest::vectors::zerovector::runTest()
307 /*! \endcond */
308 //*************************************************************************************************
309
310 } // namespace zerovector
311
312 } // namespace vectors
313
314 } // namespace mathtest
315
316 } // namespace blazetest
317
318 #endif
319