1 //=================================================================================================
2 /*!
3 //  \file src/mathtest/vectors/zerovector/ClassTest.cpp
4 //  \brief Source 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 
36 //*************************************************************************************************
37 // Includes
38 //*************************************************************************************************
39 
40 #include <cstdlib>
41 #include <iostream>
42 #include <blaze/math/DynamicVector.h>
43 #include <blaze/math/shims/Equal.h>
44 #include <blaze/util/Complex.h>
45 #include <blaze/util/Random.h>
46 #include <blazetest/mathtest/RandomMaximum.h>
47 #include <blazetest/mathtest/RandomMinimum.h>
48 #include <blazetest/mathtest/vectors/zerovector/ClassTest.h>
49 
50 #ifdef BLAZE_USE_HPX_THREADS
51 #  include <hpx/hpx_main.hpp>
52 #endif
53 
54 
55 namespace blazetest {
56 
57 namespace mathtest {
58 
59 namespace vectors {
60 
61 namespace zerovector {
62 
63 //=================================================================================================
64 //
65 //  CONSTRUCTORS
66 //
67 //=================================================================================================
68 
69 //*************************************************************************************************
70 /*!\brief Constructor for the ZeroVector class test.
71 //
72 // \exception std::runtime_error Operation error detected.
73 */
ClassTest()74 ClassTest::ClassTest()
75 {
76    testConstructors();
77    testAssignment();
78    testSubscript();
79    testAt();
80    testIterator();
81    testNonZeros();
82    testReset();
83    testClear();
84    testResize();
85    testSwap();
86    testFind();
87    testLowerBound();
88    testUpperBound();
89    testIsDefault();
90 }
91 //*************************************************************************************************
92 
93 
94 
95 
96 //=================================================================================================
97 //
98 //  TEST FUNCTIONS
99 //
100 //=================================================================================================
101 
102 //*************************************************************************************************
103 /*!\brief Test of the ZeroVector constructors.
104 //
105 // \return void
106 // \exception std::runtime_error Error detected.
107 //
108 // This function performs a test of all constructors of the ZeroVector class template. In case
109 // an error is detected, a \a std::runtime_error exception is thrown.
110 */
testConstructors()111 void ClassTest::testConstructors()
112 {
113    //=====================================================================================
114    // Default constructor
115    //=====================================================================================
116 
117    {
118       test_ = "ZeroVector default constructor";
119 
120       blaze::ZeroVector<int,blaze::rowVector> z;
121 
122       checkSize    ( z, 0UL );
123       checkNonZeros( z, 0UL );
124    }
125 
126 
127    //=====================================================================================
128    // Size constructor
129    //=====================================================================================
130 
131    {
132       test_ = "ZeroVector size constructor (size 0)";
133 
134       blaze::ZeroVector<int,blaze::rowVector> z( 0UL );
135 
136       checkSize    ( z, 0UL );
137       checkNonZeros( z, 0UL );
138    }
139 
140    {
141       test_ = "ZeroVector size constructor (size 5)";
142 
143       blaze::ZeroVector<int,blaze::rowVector> z( 5UL );
144 
145       checkSize    ( z, 5UL );
146       checkNonZeros( z, 0UL );
147    }
148 
149 
150    //=====================================================================================
151    // Copy constructor
152    //=====================================================================================
153 
154    {
155       test_ = "ZeroVector copy constructor (size 0)";
156 
157       blaze::ZeroVector<int,blaze::rowVector> z1( 0UL );
158       blaze::ZeroVector<int,blaze::rowVector> z2( z1 );
159 
160       checkSize    ( z2, 0UL );
161       checkNonZeros( z2, 0UL );
162    }
163 
164    {
165       test_ = "ZeroVector copy constructor (size 7)";
166 
167       blaze::ZeroVector<int,blaze::rowVector> z1( 7UL );
168       blaze::ZeroVector<int,blaze::rowVector> z2( z1 );
169 
170       checkSize    ( z2, 7UL );
171       checkCapacity( z2, 0UL );
172       checkNonZeros( z2, 0UL );
173 
174       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 ||
175           z2[4] != 0 || z2[5] != 0 || z2[6] != 0 ) {
176          std::ostringstream oss;
177          oss << " Test: " << test_ << "\n"
178              << " Error: Construction failed\n"
179              << " Details:\n"
180              << "   Result:\n" << z2 << "\n"
181              << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
182          throw std::runtime_error( oss.str() );
183       }
184    }
185 
186 
187    //=====================================================================================
188    // Move constructor
189    //=====================================================================================
190 
191    {
192       test_ = "ZeroVector move constructor (size 0)";
193 
194       blaze::ZeroVector<int,blaze::rowVector> z1( 0UL );
195       blaze::ZeroVector<int,blaze::rowVector> z2( std::move( z1 ) );
196 
197       checkSize    ( z2, 0UL );
198       checkNonZeros( z2, 0UL );
199    }
200 
201    {
202       test_ = "ZeroVector move constructor (size 7)";
203 
204       blaze::ZeroVector<int,blaze::rowVector> z1( 7UL );
205       blaze::ZeroVector<int,blaze::rowVector> z2( std::move( z1 ) );
206 
207       checkSize    ( z2, 7UL );
208       checkCapacity( z2, 0UL );
209       checkNonZeros( z2, 0UL );
210 
211       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 ||
212           z2[4] != 0 || z2[5] != 0 || z2[6] != 0 ) {
213          std::ostringstream oss;
214          oss << " Test: " << test_ << "\n"
215              << " Error: Construction failed\n"
216              << " Details:\n"
217              << "   Result:\n" << z2 << "\n"
218              << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
219          throw std::runtime_error( oss.str() );
220       }
221    }
222 
223 
224    //=====================================================================================
225    // Dense vector constructor
226    //=====================================================================================
227 
228    {
229       test_ = "ZeroVector dense vector constructor";
230 
231       blaze::DynamicVector<int,blaze::rowVector> z1{ 0, 0, 0, 0, 0 };
232       blaze::ZeroVector<int,blaze::rowVector> z2( z1 );
233 
234       checkSize    ( z2, 5UL );
235       checkCapacity( z2, 0UL );
236       checkNonZeros( z2, 0UL );
237 
238       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 || z2[4] != 0 ) {
239          std::ostringstream oss;
240          oss << " Test: " << test_ << "\n"
241              << " Error: Construction failed\n"
242              << " Details:\n"
243              << "   Result:\n" << z2 << "\n"
244              << "   Expected result:\n( 0 0 0 0 0 )\n";
245          throw std::runtime_error( oss.str() );
246       }
247    }
248 
249    {
250       test_ = "ZeroVector dense vector constructor (non-zero)";
251 
252       blaze::DynamicVector<int,blaze::rowVector> z1{ 0, 0, 1, 0, 0 };
253 
254       try {
255          blaze::ZeroVector<int,blaze::rowVector> z2( z1 );
256 
257          std::ostringstream oss;
258          oss << " Test: " << test_ << "\n"
259              << " Error: Setup of non-zero ZeroVector succeeded\n"
260              << " Details:\n"
261              << "   Result:\n" << z2 << "\n";
262          throw std::runtime_error( oss.str() );
263       }
264       catch( std::invalid_argument& ) {}
265    }
266 
267 
268    //=====================================================================================
269    // Sparse vector constructor
270    //=====================================================================================
271 
272    {
273       test_ = "ZeroVector sparse vector assignment";
274 
275       blaze::CompressedVector<int,blaze::columnVector> z1{ 0, 0, 0, 0, 0, 0, 0 };
276       blaze::ZeroVector<int,blaze::rowVector> z2( trans( z1 ) );
277 
278       checkSize    ( z2, 7UL );
279       checkNonZeros( z2, 0UL );
280 
281       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 ||
282           z2[4] != 0 || z2[5] != 0 || z2[6] != 0 ) {
283          std::ostringstream oss;
284          oss << " Test: " << test_ << "\n"
285              << " Error: Construction failed\n"
286              << " Details:\n"
287              << "   Result:\n" << z2 << "\n"
288              << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
289          throw std::runtime_error( oss.str() );
290       }
291    }
292 
293    {
294       test_ = "ZeroVector dense vector constructor (non-zero)";
295 
296       blaze::CompressedVector<int,blaze::columnVector> z1{ 0, 0, 0, 1, 0, 0, 0 };
297 
298       try {
299          blaze::ZeroVector<int,blaze::rowVector> z2( trans( z1 ) );
300 
301          std::ostringstream oss;
302          oss << " Test: " << test_ << "\n"
303              << " Error: Setup of non-zero ZeroVector succeeded\n"
304              << " Details:\n"
305              << "   Result:\n" << z2 << "\n";
306          throw std::runtime_error( oss.str() );
307       }
308       catch( std::invalid_argument& ) {}
309    }
310 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
315 /*!\brief Test of the ZeroVector assignment operators.
316 //
317 // \return void
318 // \exception std::runtime_error Error detected.
319 //
320 // This function performs a test of all assignment operators of the ZeroVector class template.
321 // In case an error is detected, a \a std::runtime_error exception is thrown.
322 */
testAssignment()323 void ClassTest::testAssignment()
324 {
325    //=====================================================================================
326    // Copy assignment
327    //=====================================================================================
328 
329    {
330       test_ = "ZeroVector copy assignment";
331 
332       blaze::ZeroVector<int,blaze::rowVector> z1( 7UL );
333       blaze::ZeroVector<int,blaze::rowVector> z2;
334       z2 = z1;
335 
336       checkSize    ( z2, 7UL );
337       checkCapacity( z2, 0UL );
338       checkNonZeros( z2, 0UL );
339 
340       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 ||
341           z2[4] != 0 || z2[5] != 0 || z2[6] != 0 ) {
342          std::ostringstream oss;
343          oss << " Test: " << test_ << "\n"
344              << " Error: Assignment failed\n"
345              << " Details:\n"
346              << "   Result:\n" << z2 << "\n"
347              << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
348          throw std::runtime_error( oss.str() );
349       }
350    }
351 
352    {
353       test_ = "ZeroVector copy assignment stress test";
354 
355       using RandomVectorType = blaze::ZeroVector<int,blaze::rowVector>;
356 
357       blaze::ZeroVector<int,blaze::rowVector> z1;
358 
359       for( size_t i=0UL; i<100UL; ++i )
360       {
361          const size_t size( blaze::rand<size_t>( 0UL, 20UL ) );
362          const RandomVectorType z2( blaze::rand<RandomVectorType>( size ) );
363 
364          z1 = z2;
365 
366          if( z1 != z2 ) {
367             std::ostringstream oss;
368             oss << " Test: " << test_ << "\n"
369                 << " Error: Assignment failed\n"
370                 << " Details:\n"
371                 << "   Result:\n" << z1 << "\n"
372                 << "   Expected result:\n" << z2 << "\n";
373             throw std::runtime_error( oss.str() );
374          }
375       }
376    }
377 
378 
379    //=====================================================================================
380    // Move assignment
381    //=====================================================================================
382 
383    {
384       test_ = "ZeroVector move assignment";
385 
386       blaze::ZeroVector<int,blaze::rowVector> z1( 7UL );
387       blaze::ZeroVector<int,blaze::rowVector> z2( 4UL );
388 
389       z2 = std::move( z1 );
390 
391       checkSize    ( z2, 7UL );
392       checkCapacity( z2, 0UL );
393       checkNonZeros( z2, 0UL );
394 
395       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 ||
396           z2[4] != 0 || z2[5] != 0 || z2[6] != 0 ) {
397          std::ostringstream oss;
398          oss << " Test: " << test_ << "\n"
399              << " Error: Assignment failed\n"
400              << " Details:\n"
401              << "   Result:\n" << z2 << "\n"
402              << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
403          throw std::runtime_error( oss.str() );
404       }
405    }
406 
407 
408    //=====================================================================================
409    // Dense vector assignment
410    //=====================================================================================
411 
412    {
413       test_ = "ZeroVector dense vector assignment";
414 
415       blaze::DynamicVector<int,blaze::rowVector> z1{ 0, 0, 0, 0, 0 };
416       blaze::ZeroVector<int,blaze::rowVector> z2;
417       z2 = z1;
418 
419       checkSize    ( z2, 5UL );
420       checkNonZeros( z2, 0UL );
421 
422       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 || z2[4] != 0 ) {
423          std::ostringstream oss;
424          oss << " Test: " << test_ << "\n"
425              << " Error: Assignment failed\n"
426              << " Details:\n"
427              << "   Result:\n" << z2 << "\n"
428              << "   Expected result:\n( 0 0 0 0 0 )\n";
429          throw std::runtime_error( oss.str() );
430       }
431    }
432 
433    {
434       test_ = "ZeroVector dense vector assignment (non-zero)";
435 
436       blaze::DynamicVector<int,blaze::rowVector> z1{ 0, 0, 1, 0, 0 };
437       blaze::ZeroVector<int,blaze::rowVector> z2;
438 
439       try {
440          z2 = z1;
441 
442          std::ostringstream oss;
443          oss << " Test: " << test_ << "\n"
444              << " Error: Assignment of non-zero vector succeeded\n"
445              << " Details:\n"
446              << "   Result:\n" << z2 << "\n";
447          throw std::runtime_error( oss.str() );
448       }
449       catch( std::invalid_argument& ) {}
450    }
451 
452 
453    //=====================================================================================
454    // Sparse vector assignment
455    //=====================================================================================
456 
457    {
458       test_ = "ZeroVector sparse vector assignment";
459 
460       blaze::CompressedVector<int,blaze::columnVector> z1{ 0, 0, 0, 0, 0, 0, 0 };
461       blaze::ZeroVector<int,blaze::rowVector> z2;
462 
463       z2 = trans( z1 );
464 
465       checkSize    ( z2, 7UL );
466       checkNonZeros( z2, 0UL );
467 
468       if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 ||
469           z2[4] != 0 || z2[5] != 0 || z2[6] != 0 ) {
470          std::ostringstream oss;
471          oss << " Test: " << test_ << "\n"
472              << " Error: Assignment failed\n"
473              << " Details:\n"
474              << "   Result:\n" << z2 << "\n"
475              << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
476          throw std::runtime_error( oss.str() );
477       }
478    }
479 
480    {
481       test_ = "ZeroVector sparse vector assignment (non-zero)";
482 
483       blaze::CompressedVector<int,blaze::columnVector> z1{ 0, 0, 0, 1, 0, 0, 0 };
484       blaze::ZeroVector<int,blaze::rowVector> z2;
485 
486       try {
487          z2 = trans( z1 );
488 
489          std::ostringstream oss;
490          oss << " Test: " << test_ << "\n"
491              << " Error: Assignment of non-zero vector succeeded\n"
492              << " Details:\n"
493              << "   Result:\n" << z2 << "\n";
494          throw std::runtime_error( oss.str() );
495       }
496       catch( std::invalid_argument& ) {}
497    }
498 }
499 //*************************************************************************************************
500 
501 
502 //*************************************************************************************************
503 /*!\brief Test of the ZeroVector subscript operator.
504 //
505 // \return void
506 // \exception std::runtime_error Error detected.
507 //
508 // This function performs a test of adding and accessing elements via the subscript operator
509 // of the ZeroVector class template. In case an error is detected, a \a std::runtime_error
510 // exception is thrown.
511 */
testSubscript()512 void ClassTest::testSubscript()
513 {
514    test_ = "ZeroVector::operator[]";
515 
516    blaze::ZeroVector<int,blaze::rowVector> z( 7UL );
517 
518    checkSize    ( z, 7UL );
519    checkCapacity( z, 0UL );
520    checkNonZeros( z, 0UL );
521 
522    if( z[0] != 0 || z[1] != 0 || z[2] != 0 || z[3] != 0 ||
523        z[4] != 0 || z[5] != 0 || z[6] != 0 ) {
524       std::ostringstream oss;
525       oss << " Test: " << test_ << "\n"
526           << " Error: Subscript operator failed\n"
527           << " Details:\n"
528           << "   Result:\n" << z << "\n"
529           << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
530       throw std::runtime_error( oss.str() );
531    }
532 }
533 //*************************************************************************************************
534 
535 
536 //*************************************************************************************************
537 /*!\brief Test of the \c at() member function of the ZeroVector class template.
538 //
539 // \return void
540 // \exception std::runtime_error Error detected.
541 //
542 // This function performs a test of adding and accessing elements via the \c at() member function
543 // of the ZeroVector class template. In case an error is detected, a \a std::runtime_error
544 // exception is thrown.
545 */
testAt()546 void ClassTest::testAt()
547 {
548    test_ = "ZeroVector::at()";
549 
550    blaze::ZeroVector<int,blaze::rowVector> z( 7UL );
551 
552    checkSize    ( z, 7UL );
553    checkCapacity( z, 0UL );
554    checkNonZeros( z, 0UL );
555 
556    if( z.at(0) != 0 || z.at(1) != 0 || z.at(2) != 0 || z.at(3) != 0 ||
557        z.at(4) != 0 || z.at(5) != 0 || z.at(6) != 0 ) {
558       std::ostringstream oss;
559       oss << " Test: " << test_ << "\n"
560           << " Error: Access via at() function failed\n"
561           << " Details:\n"
562           << "   Result:\n" << z << "\n"
563           << "   Expected result:\n( 0 0 0 0 0 0 0 )\n";
564       throw std::runtime_error( oss.str() );
565    }
566 }
567 //*************************************************************************************************
568 
569 
570 //*************************************************************************************************
571 /*!\brief Test of the ZeroVector iterator implementation.
572 //
573 // \return void
574 // \exception std::runtime_error Error detected.
575 //
576 // This function performs a test of the iterator implementation of the ZeroVector class template.
577 // In case an error is detected, a \a std::runtime_error exception is thrown.
578 */
testIterator()579 void ClassTest::testIterator()
580 {
581    using VectorType    = blaze::ZeroVector<int>;
582    using ConstIterator = VectorType::ConstIterator;
583 
584    VectorType z( 4UL );
585 
586    // Testing the ConstIterator default constructor
587    {
588       test_ = "ConstIterator default constructor";
589 
590       ConstIterator it{};
591 
592       if( it != ConstIterator() ) {
593          std::ostringstream oss;
594          oss << " Test: " << test_ << "\n"
595              << " Error: Failed iterator default constructor\n";
596          throw std::runtime_error( oss.str() );
597       }
598    }
599 
600    // Counting the number of elements via ConstIterator (end-begin)
601    {
602       test_ = "ConstIterator subtraction (end-begin)";
603 
604       const ptrdiff_t number( cend( z ) - cbegin( z ) );
605 
606       if( number != 0L ) {
607          std::ostringstream oss;
608          oss << " Test: " << test_ << "\n"
609              << " Error: Invalid number of elements detected\n"
610              << " Details:\n"
611              << "   Number of elements         : " << number << "\n"
612              << "   Expected number of elements: 2\n";
613          throw std::runtime_error( oss.str() );
614       }
615    }
616 
617    // Testing ConstIterator comparison
618    {
619       test_ = "ConstIterator comparison";
620 
621       ConstIterator it ( cbegin( z ) );
622       ConstIterator end( cend( z ) );
623 
624       if( it != end ) {
625          std::ostringstream oss;
626          oss << " Test: " << test_ << "\n"
627              << " Error: Iterator comparison failed\n";
628          throw std::runtime_error( oss.str() );
629       }
630    }
631 }
632 //*************************************************************************************************
633 
634 
635 //*************************************************************************************************
636 /*!\brief Test of the \c nonZeros() member function of the ZeroVector class template.
637 //
638 // \return void
639 // \exception std::runtime_error Error detected.
640 //
641 // This function performs a test of the \c nonZeros() member function of the ZeroVector class
642 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
643 */
testNonZeros()644 void ClassTest::testNonZeros()
645 {
646    test_ = "ZeroZero::nonZeros()";
647 
648    blaze::ZeroVector<int,blaze::rowVector> z( 7UL );
649 
650    checkSize    ( z, 7UL );
651    checkCapacity( z, 0UL );
652    checkNonZeros( z, 0UL );
653 }
654 //*************************************************************************************************
655 
656 
657 //*************************************************************************************************
658 /*!\brief Test of the \c reset() member function of the ZeroVector class template.
659 //
660 // \return void
661 // \exception std::runtime_error Error detected.
662 //
663 // This function performs a test of the \c reset() member function of the ZeroVector class
664 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
665 */
testReset()666 void ClassTest::testReset()
667 {
668    test_ = "ZeroVector::reset()";
669 
670    // Resetting a default constructed vector
671    {
672       blaze::ZeroVector<int,blaze::rowVector> vec;
673 
674       reset( vec );
675 
676       checkSize    ( vec, 0UL );
677       checkNonZeros( vec, 0UL );
678    }
679 
680    // Resetting an initialized vector
681    {
682       // Initialization check
683       blaze::ZeroVector<int,blaze::rowVector> z( 9UL );
684 
685       checkSize    ( z, 9UL );
686       checkCapacity( z, 0UL );
687       checkNonZeros( z, 0UL );
688 
689       if( z[0] != 0 || z[1] != 0 || z[2] != 0 || z[3] != 0 || z[4] != 0 ||
690           z[5] != 0 || z[6] != 0 || z[7] != 0 || z[8] != 0 ) {
691          std::ostringstream oss;
692          oss << " Test: " << test_ << "\n"
693              << " Error: Initialization failed\n"
694              << " Details:\n"
695              << "   Result:\n" << z << "\n"
696              << "   Expected result:\n( 0 0 0 0 0 0 0 0 0 )\n";
697          throw std::runtime_error( oss.str() );
698       }
699 
700       // Resetting the entire vector
701       reset( z );
702 
703       checkSize    ( z, 9UL );
704       checkCapacity( z, 0UL );
705       checkNonZeros( z, 0UL );
706 
707       if( z[0] != 0 || z[1] != 0 || z[2] != 0 || z[3] != 0 || z[4] != 0 ||
708           z[5] != 0 || z[6] != 0 || z[7] != 0 || z[8] != 0 ) {
709          std::ostringstream oss;
710          oss << " Test: " << test_ << "\n"
711              << " Error: Initialization failed\n"
712              << " Details:\n"
713              << "   Result:\n" << z << "\n"
714              << "   Expected result:\n( 0 0 0 0 0 0 0 0 0 )\n";
715          throw std::runtime_error( oss.str() );
716       }
717    }
718 }
719 //*************************************************************************************************
720 
721 
722 //*************************************************************************************************
723 /*!\brief Test of the \c clear() member function of the ZeroVector class template.
724 //
725 // \return void
726 // \exception std::runtime_error Error detected.
727 //
728 // This function performs a test of the \c clear() member function of the ZeroVector class
729 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
730 */
testClear()731 void ClassTest::testClear()
732 {
733    test_ = "ZeroVector::clear()";
734 
735    // Clearing a default constructed vector
736    {
737       blaze::ZeroVector<int,blaze::rowVector> z;
738 
739       clear( z );
740 
741       checkSize    ( z, 0UL );
742       checkNonZeros( z, 0UL );
743    }
744 
745    // Clearing an initialized vector
746    {
747       // Initialization check
748       blaze::ZeroVector<int,blaze::rowVector> z( 9UL );
749 
750       checkSize    ( z, 9UL );
751       checkCapacity( z, 0UL );
752       checkNonZeros( z, 0UL );
753 
754       if( z[0] != 0 || z[1] != 0 || z[2] != 0 || z[3] != 0 || z[4] != 0 ||
755           z[5] != 0 || z[6] != 0 || z[7] != 0 || z[8] != 0 ) {
756          std::ostringstream oss;
757          oss << " Test: " << test_ << "\n"
758              << " Error: Initialization failed\n"
759              << " Details:\n"
760              << "   Result:\n" << z << "\n"
761              << "   Expected result:\n( 0 0 0 0 0 0 0 0 0 )\n";
762          throw std::runtime_error( oss.str() );
763       }
764 
765       // Clearing the entire vector
766       clear( z );
767 
768       checkSize    ( z, 0UL );
769       checkNonZeros( z, 0UL );
770    }
771 }
772 //*************************************************************************************************
773 
774 
775 //*************************************************************************************************
776 /*!\brief Test of the \c resize() member function of the ZeroVector class template.
777 //
778 // \return void
779 // \exception std::runtime_error Error detected.
780 //
781 // This function performs a test of the \c resize() member function of the ZeroVector class
782 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
783 */
testResize()784 void ClassTest::testResize()
785 {
786    test_ = "ZeroVector::resize()";
787 
788    // Initialization check
789    blaze::ZeroVector<int,blaze::rowVector> z;
790 
791    checkSize    ( z, 0UL );
792    checkNonZeros( z, 0UL );
793 
794    // Resizing to 0
795    z.resize( 0UL );
796 
797    checkSize    ( z, 0UL );
798    checkNonZeros( z, 0UL );
799 
800    // Resizing to 5
801    z.resize( 5UL );
802 
803    checkSize    ( z, 5UL );
804    checkNonZeros( z, 0UL );
805 
806    // Resizing to 2
807    z.resize( 2UL );
808 
809    checkSize    ( z, 2UL );
810    checkNonZeros( z, 0UL );
811 
812    // Resizing to 4
813    z.resize( 4UL );
814 
815    checkSize    ( z, 4UL );
816    checkNonZeros( z, 0UL );
817 
818    // Resizing to 1
819    z.resize( 1UL );
820 
821    checkSize    ( z, 1UL );
822    checkNonZeros( z, 0UL );
823 
824    // Resizing to 0
825    z.resize( 0UL );
826 
827    checkSize    ( z, 0UL );
828    checkNonZeros( z, 0UL );
829 }
830 //*************************************************************************************************
831 
832 
833 //*************************************************************************************************
834 /*!\brief Test of the \c swap() functionality of the ZeroVector class template.
835 //
836 // \return void
837 // \exception std::runtime_error Error detected.
838 //
839 // This function performs a test of the \c swap() function of the ZeroVector class template.
840 // In case an error is detected, a \a std::runtime_error exception is thrown.
841 */
testSwap()842 void ClassTest::testSwap()
843 {
844    test_ = "ZeroVector swap";
845 
846    blaze::ZeroVector<int,blaze::rowVector> z1( 9UL );
847    blaze::ZeroVector<int,blaze::rowVector> z2( 5UL );
848 
849    swap( z1, z2 );
850 
851    checkSize    ( z1, 5UL );
852    checkCapacity( z1, 0UL );
853    checkNonZeros( z1, 0UL );
854 
855    if( z1[0] != 0 || z1[1] != 0 || z1[2] != 0 || z1[3] != 0 || z1[4] != 0 ) {
856       std::ostringstream oss;
857       oss << " Test: " << test_ << "\n"
858           << " Error: Swapping the first vector failed\n"
859           << " Details:\n"
860           << "   Result:\n" << z1 << "\n"
861           << "   Expected result:\n( 0 0 0 0 0 )\n";
862       throw std::runtime_error( oss.str() );
863    }
864 
865    checkSize    ( z2, 9UL );
866    checkCapacity( z2, 0UL );
867    checkNonZeros( z2, 0UL );
868 
869    if( z2[0] != 0 || z2[1] != 0 || z2[2] != 0 || z2[3] != 0 || z2[4] != 0 ||
870        z2[5] != 0 || z2[6] != 0 || z2[7] != 0 || z2[8] != 0 ) {
871       std::ostringstream oss;
872       oss << " Test: " << test_ << "\n"
873           << " Error: Swapping the second vector failed\n"
874           << " Details:\n"
875           << "   Result:\n" << z2 << "\n"
876           << "   Expected result:\n( 0 0 0 0 0 0 0 0 0 )\n";
877       throw std::runtime_error( oss.str() );
878    }
879 }
880 //*************************************************************************************************
881 
882 
883 //*************************************************************************************************
884 /*!\brief Test of the \c find() member function of the ZeroVector class template.
885 //
886 // \return void
887 // \exception std::runtime_error Error detected.
888 //
889 // This function performs a test of the \c find() member function of the ZeroVector class
890 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
891 */
testFind()892 void ClassTest::testFind()
893 {
894    test_ = "ZeroVector::find()";
895 
896    using ConstIterator = blaze::ZeroVector<int,blaze::rowVector>::ConstIterator;
897 
898    // Initialization check
899    blaze::ZeroVector<int,blaze::rowVector> z( 8UL );
900 
901    checkSize    ( z, 8UL );
902    checkCapacity( z, 0UL );
903    checkNonZeros( z, 0UL );
904 
905    // Searching for the first non-existing element
906    {
907       ConstIterator pos( z.find( 0UL ) );
908 
909       if( pos != z.end() ) {
910          std::ostringstream oss;
911          oss << " Test: " << test_ << "\n"
912              << " Error: Non-existing element could be found\n"
913              << " Details:\n"
914              << "   Required index = 0\n"
915              << "   Found index    = " << pos->index() << "\n"
916              << "   Expected value = 0\n"
917              << "   Value at index = " << pos->value() << "\n"
918              << "   Current vector:\n" << z << "\n";
919          throw std::runtime_error( oss.str() );
920       }
921    }
922 
923    // Searching for the second non-existing element
924    {
925       ConstIterator pos( z.find( 4UL ) );
926 
927       if( pos != z.end() ) {
928          std::ostringstream oss;
929          oss << " Test: " << test_ << "\n"
930              << " Error: Non-existing element could be found\n"
931              << " Details:\n"
932              << "   Required index = 4\n"
933              << "   Found index    = " << pos->index() << "\n"
934              << "   Expected value = 0\n"
935              << "   Value at index = " << pos->value() << "\n"
936              << "   Current vector:\n" << z << "\n";
937          throw std::runtime_error( oss.str() );
938       }
939    }
940 
941    // Searching for the third non-existing element
942    {
943       ConstIterator pos( z.find( 7UL ) );
944 
945       if( pos != z.end() ) {
946          std::ostringstream oss;
947          oss << " Test: " << test_ << "\n"
948              << " Error: Non-existing element could be found\n"
949              << " Details:\n"
950              << "   Required index = 7\n"
951              << "   Found index    = " << pos->index() << "\n"
952              << "   Expected value = 0\n"
953              << "   Value at index = " << pos->value() << "\n"
954              << "   Current vector:\n" << z << "\n";
955          throw std::runtime_error( oss.str() );
956       }
957    }
958 }
959 //*************************************************************************************************
960 
961 
962 //*************************************************************************************************
963 /*!\brief Test of the \c lowerBound() member function of the ZeroVector class template.
964 //
965 // \return void
966 // \exception std::runtime_error Error detected.
967 //
968 // This function performs a test of the \c lowerBound() member function of the ZeroVector class
969 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
970 */
testLowerBound()971 void ClassTest::testLowerBound()
972 {
973    test_ = "ZeroVector::lowerBound()";
974 
975    using ConstIterator = blaze::ZeroVector<int,blaze::rowVector>::ConstIterator;
976 
977    // Initialization check
978    blaze::ZeroVector<int,blaze::rowVector> z( 8UL );
979 
980    checkSize    ( z, 8UL );
981    checkCapacity( z, 0UL );
982    checkNonZeros( z, 0UL );
983 
984    // Determining the lower bound for index 0
985    {
986       ConstIterator pos( z.lowerBound( 0UL ) );
987 
988       if( pos != z.end() ) {
989          std::ostringstream oss;
990          oss << " Test: " << test_ << "\n"
991              << " Error: Lower bound could not be determined\n"
992              << " Details:\n"
993              << "   Required index = 0\n"
994              << "   Current vector:\n" << z << "\n";
995          throw std::runtime_error( oss.str() );
996       }
997    }
998 
999    // Determining the lower bound for index 1
1000    {
1001       ConstIterator pos( z.lowerBound( 1UL ) );
1002 
1003       if( pos != z.end() ) {
1004          std::ostringstream oss;
1005          oss << " Test: " << test_ << "\n"
1006              << " Error: Lower bound could not be determined\n"
1007              << " Details:\n"
1008              << "   Required index = 1\n"
1009              << "   Current vector:\n" << z << "\n";
1010          throw std::runtime_error( oss.str() );
1011       }
1012    }
1013 
1014    // Determining the lower bound for index 4
1015    {
1016       ConstIterator pos( z.lowerBound( 4UL ) );
1017 
1018       if( pos != z.end() ) {
1019          std::ostringstream oss;
1020          oss << " Test: " << test_ << "\n"
1021              << " Error: Lower bound could not be determined\n"
1022              << " Details:\n"
1023              << "   Required index = 4\n"
1024              << "   Current vector:\n" << z << "\n";
1025          throw std::runtime_error( oss.str() );
1026       }
1027    }
1028 
1029    // Determining the lower bound for index 7
1030    {
1031       ConstIterator pos( z.lowerBound( 7UL ) );
1032 
1033       if( pos != z.end() ) {
1034          std::ostringstream oss;
1035          oss << " Test: " << test_ << "\n"
1036              << " Error: Lower bound could not be determined\n"
1037              << " Details:\n"
1038              << "   Required index = 7\n"
1039              << "   Current vector:\n" << z << "\n";
1040          throw std::runtime_error( oss.str() );
1041       }
1042    }
1043 }
1044 //*************************************************************************************************
1045 
1046 
1047 //*************************************************************************************************
1048 /*!\brief Test of the \c upperBound() member function of the ZeroVector class template.
1049 //
1050 // \return void
1051 // \exception std::runtime_error Error detected.
1052 //
1053 // This function performs a test of the \c upperBound() member function of the ZeroVector class
1054 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
1055 */
testUpperBound()1056 void ClassTest::testUpperBound()
1057 {
1058    test_ = "ZeroVector::upperBound()";
1059 
1060    using ConstIterator = blaze::ZeroVector<int,blaze::rowVector>::ConstIterator;
1061 
1062    // Initialization check
1063    blaze::ZeroVector<int,blaze::rowVector> z( 8UL );
1064 
1065    checkSize    ( z, 8UL );
1066    checkCapacity( z, 0UL );
1067    checkNonZeros( z, 0UL );
1068 
1069    // Determining the upper bound for index 0
1070    {
1071       ConstIterator pos( z.upperBound( 0UL ) );
1072 
1073       if( pos != z.end() ) {
1074          std::ostringstream oss;
1075          oss << " Test: " << test_ << "\n"
1076              << " Error: Upper bound could not be determined\n"
1077              << " Details:\n"
1078              << "   Required index = 0\n"
1079              << "   Current vector:\n" << z << "\n";
1080          throw std::runtime_error( oss.str() );
1081       }
1082    }
1083 
1084    // Determining the upper bound for index 1
1085    {
1086       ConstIterator pos( z.upperBound( 1UL ) );
1087 
1088       if( pos != z.end() ) {
1089          std::ostringstream oss;
1090          oss << " Test: " << test_ << "\n"
1091              << " Error: Upper bound could not be determined\n"
1092              << " Details:\n"
1093              << "   Required index = 1\n"
1094              << "   Current vector:\n" << z << "\n";
1095          throw std::runtime_error( oss.str() );
1096       }
1097    }
1098 
1099    // Determining the upper bound for index 4
1100    {
1101       ConstIterator pos( z.upperBound( 4UL ) );
1102 
1103       if( pos != z.end() ) {
1104          std::ostringstream oss;
1105          oss << " Test: " << test_ << "\n"
1106              << " Error: Upper bound could not be determined\n"
1107              << " Details:\n"
1108              << "   Required index = 4\n"
1109              << "   Current vector:\n" << z << "\n";
1110          throw std::runtime_error( oss.str() );
1111       }
1112    }
1113 
1114    // Determining the upper bound for index 7
1115    {
1116       ConstIterator pos( z.upperBound( 7UL ) );
1117 
1118       if( pos != z.end() ) {
1119          std::ostringstream oss;
1120          oss << " Test: " << test_ << "\n"
1121              << " Error: Upper bound could not be determined\n"
1122              << " Details:\n"
1123              << "   Required index = 7\n"
1124              << "   Current vector:\n" << z << "\n";
1125          throw std::runtime_error( oss.str() );
1126       }
1127    }
1128 }
1129 //*************************************************************************************************
1130 
1131 
1132 //*************************************************************************************************
1133 /*!\brief Test of the \c isDefault() function with the ZeroVector class template.
1134 //
1135 // \return void
1136 // \exception std::runtime_error Error detected.
1137 //
1138 // This function performs a test of the \c isDefault() function with the ZeroVector class
1139 // template. In case an error is detected, a \a std::runtime_error exception is thrown.
1140 */
testIsDefault()1141 void ClassTest::testIsDefault()
1142 {
1143    test_ = "isDefault() function";
1144 
1145    // isDefault with vector of size 0 (default)
1146    {
1147       blaze::ZeroVector<int,blaze::rowVector> z;
1148 
1149       if( blaze::isDefault( z ) != true ) {
1150          std::ostringstream oss;
1151          oss << " Test: " << test_ << "\n"
1152              << " Error: Invalid isDefault evaluation\n"
1153              << " Details:\n"
1154              << "   Vector:\n" << z << "\n";
1155          throw std::runtime_error( oss.str() );
1156       }
1157    }
1158 
1159    // isDefault with vector of size 5 (non-default)
1160    {
1161       blaze::ZeroVector<int,blaze::rowVector> z( 5UL );
1162 
1163       if( blaze::isDefault( z[1] ) != true ) {
1164          std::ostringstream oss;
1165          oss << " Test: " << test_ << "\n"
1166              << " Error: Invalid isDefault evaluation\n"
1167              << " Details:\n"
1168              << "   Vector element: " << z[1] << "\n";
1169          throw std::runtime_error( oss.str() );
1170       }
1171 
1172       if( blaze::isDefault( z ) != false ) {
1173          std::ostringstream oss;
1174          oss << " Test: " << test_ << "\n"
1175              << " Error: Invalid isDefault evaluation\n"
1176              << " Details:\n"
1177              << "   Vector:\n" << z << "\n";
1178          throw std::runtime_error( oss.str() );
1179       }
1180    }
1181 }
1182 //*************************************************************************************************
1183 
1184 } // namespace zerovector
1185 
1186 } // namespace vectors
1187 
1188 } // namespace mathtest
1189 
1190 } // namespace blazetest
1191 
1192 
1193 
1194 
1195 //=================================================================================================
1196 //
1197 //  MAIN FUNCTION
1198 //
1199 //=================================================================================================
1200 
1201 //*************************************************************************************************
main()1202 int main()
1203 {
1204    std::cout << "   Running ZeroVector class test..." << std::endl;
1205 
1206    try
1207    {
1208       RUN_ZEROVECTOR_CLASS_TEST;
1209    }
1210    catch( std::exception& ex ) {
1211       std::cerr << "\n\n ERROR DETECTED during ZeroVector class test:\n"
1212                 << ex.what() << "\n";
1213       return EXIT_FAILURE;
1214    }
1215 
1216    return EXIT_SUCCESS;
1217 }
1218 //*************************************************************************************************
1219