1 //=================================================================================================
2 /*!
3 // \file blazetest/mathtest/operations/svecsvecsub/AliasingTest.h
4 // \brief Header file for the sparse vector/sparse vector subtraction aliasing 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_OPERATIONS_SVECSVECSUB_ALIASINGTEST_H_
36 #define _BLAZETEST_MATHTEST_OPERATIONS_SVECSVECSUB_ALIASINGTEST_H_
37
38
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42
43 #include <sstream>
44 #include <stdexcept>
45 #include <string>
46 #include <blaze/math/CompressedMatrix.h>
47 #include <blaze/math/CompressedVector.h>
48 #include <blaze/math/DynamicVector.h>
49 #include <blaze/math/StaticVector.h>
50
51
52 namespace blazetest {
53
54 namespace mathtest {
55
56 namespace operations {
57
58 namespace svecsvecsub {
59
60 //=================================================================================================
61 //
62 // CLASS DEFINITION
63 //
64 //=================================================================================================
65
66 //*************************************************************************************************
67 /*!\brief Auxiliary class template for the sparse vector/sparse vector subtraction aliasing test.
68 //
69 // This class represents a test suite for all sparse vector/sparse vector subtraction aliasing
70 // tests. It performs a series of runtime tests to assure that all mathematical operations work
71 // correctly even in the presence of aliasing.
72 */
73 class AliasingTest
74 {
75 private:
76 //**Type definitions****************************************************************************
77 using DVec = blaze::DynamicVector<int,blaze::columnVector>; //!< Dense column vector type.
78 using SVec = blaze::CompressedVector<int,blaze::columnVector>; //!< Sparse column vector type.
79 using SMat = blaze::CompressedMatrix<int,blaze::rowMajor>; //!< Row-major sparse matrix type.
80 using RVec = blaze::StaticVector<int,3UL,blaze::columnVector>; //!< Result column vector type.
81 //**********************************************************************************************
82
83 public:
84 //**Constructors********************************************************************************
85 /*!\name Constructors */
86 //@{
87 explicit AliasingTest();
88 // No explicitly declared copy constructor.
89 //@}
90 //**********************************************************************************************
91
92 //**Destructor**********************************************************************************
93 // No explicitly declared destructor.
94 //**********************************************************************************************
95
96 private:
97 //**Test functions******************************************************************************
98 /*!\name Test functions */
99 //@{
100 void testSVecSVecSub();
101
102 template< typename T1, typename T2 >
103 void checkResult( const T1& computedResult, const T2& expectedResult );
104 //@}
105 //**********************************************************************************************
106
107 //**Utility functions***************************************************************************
108 /*!\name Utility functions */
109 //@{
110 void initialize();
111 //@}
112 //**********************************************************************************************
113
114 //**Member variables****************************************************************************
115 /*!\name Member variables */
116 //@{
117 SVec sa4_; //!< The first sparse column vector.
118 /*!< The 4-dimensional vector is initialized as
119 \f[\left(\begin{array}{*{1}{c}}
120 -1 \\
121 0 \\
122 -3 \\
123 2 \\
124 \end{array}\right)\f]. */
125 SVec sb3_; //!< The second sparse column vector.
126 /*!< The 3-dimensional vector is initialized as
127 \f[\left(\begin{array}{*{1}{c}}
128 1 \\
129 2 \\
130 3 \\
131 \end{array}\right)\f]. */
132 SVec sc3_; //!< The third sparse column vector.
133 /*!< The 3-dimensional vector is initialized as
134 \f[\left(\begin{array}{*{1}{c}}
135 0 \\
136 2 \\
137 1 \\
138 \end{array}\right)\f]. */
139 SMat sA3x4_; //!< The first row-major sparse matrix.
140 /*!< The \f$ 3 \times 4 \f$ matrix is initialized as
141 \f[\left(\begin{array}{*{3}{c}}
142 -1 & 0 & -2 & 0 \\
143 0 & 2 & -3 & 1 \\
144 0 & 1 & 2 & 2 \\
145 \end{array}\right)\f]. */
146 SMat sB3x3_; //!< The second row-major sparse matrix.
147 /*!< The \f$ 3 \times 3 \f$ matrix is initialized as
148 \f[\left(\begin{array}{*{3}{c}}
149 0 & -1 & 0 \\
150 1 & -2 & 2 \\
151 0 & 0 & -3 \\
152 \end{array}\right)\f]. */
153 RVec result_; //!< The sparse vector for the reference result.
154
155 std::string test_; //!< Label of the currently performed test.
156 //@}
157 //**********************************************************************************************
158 };
159 //*************************************************************************************************
160
161
162
163
164 //=================================================================================================
165 //
166 // TEST FUNCTIONS
167 //
168 //=================================================================================================
169
170 //*************************************************************************************************
171 /*!\brief Checking and comparing the computed result.
172 //
173 // \param computedResult The computed result.
174 // \param expectedResult The expected result.
175 // \return void
176 // \exception std::runtime_error Incorrect result detected.
177 //
178 // This function is called after each test case to check and compare the computed result.
179 // In case the computed and the expected result differ in any way, a \a std::runtime_error
180 // exception is thrown.
181 */
182 template< typename T1 // Vector type of the computed result
183 , typename T2 > // Vector type of the expected result
checkResult(const T1 & computedResult,const T2 & expectedResult)184 void AliasingTest::checkResult( const T1& computedResult, const T2& expectedResult )
185 {
186 if( computedResult != expectedResult ) {
187 std::ostringstream oss;
188 oss.precision( 20 );
189 oss << " Test : " << test_ << "\n"
190 << " Error: Incorrect result detected\n"
191 << " Details:\n"
192 << " Computed result:\n" << computedResult << "\n"
193 << " Expected result:\n" << expectedResult << "\n";
194 throw std::runtime_error( oss.str() );
195 }
196 }
197 //*************************************************************************************************
198
199
200
201
202 //=================================================================================================
203 //
204 // GLOBAL TEST FUNCTIONS
205 //
206 //=================================================================================================
207
208 //*************************************************************************************************
209 /*!\brief Testing the sparse vector/sparse vector subtraction in the presence of aliasing.
210 //
211 // \return void
212 */
runTest()213 void runTest()
214 {
215 AliasingTest();
216 }
217 //*************************************************************************************************
218
219
220
221
222 //=================================================================================================
223 //
224 // MACRO DEFINITIONS
225 //
226 //=================================================================================================
227
228 //*************************************************************************************************
229 /*! \cond BLAZE_INTERNAL */
230 /*!\brief Macro for the execution of the sparse vector/sparse vector subtraction aliasing test.
231 */
232 #define RUN_SVECSVECSUB_ALIASING_TEST \
233 blazetest::mathtest::operations::svecsvecsub::runTest()
234 /*! \endcond */
235 //*************************************************************************************************
236
237 } // namespace svecsvecsub
238
239 } // namespace operations
240
241 } // namespace mathtest
242
243 } // namespace blazetest
244
245 #endif
246