1 //=================================================================================================
2 /*!
3 // \file src/mathtest/adaptors/diagonalmatrix/DenseTest2.cpp
4 // \brief Source file for the DiagonalMatrix dense test (part 2)
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 <memory>
43 #include <blaze/math/Column.h>
44 #include <blaze/math/Row.h>
45 #include <blaze/math/Submatrix.h>
46 #include <blaze/util/Complex.h>
47 #include <blazetest/mathtest/adaptors/diagonalmatrix/DenseTest.h>
48
49 #ifdef BLAZE_USE_HPX_THREADS
50 # include <hpx/hpx_main.hpp>
51 #endif
52
53
54 namespace blazetest {
55
56 namespace mathtest {
57
58 namespace adaptors {
59
60 namespace diagonalmatrix {
61
62 //=================================================================================================
63 //
64 // CONSTRUCTORS
65 //
66 //=================================================================================================
67
68 //*************************************************************************************************
69 /*!\brief Constructor for the DiagonalMatrix dense test.
70 //
71 // \exception std::runtime_error Operation error detected.
72 */
DenseTest()73 DenseTest::DenseTest()
74 {
75 testScaling();
76 testFunctionCall();
77 testIterator();
78 testNonZeros();
79 testReset();
80 testClear();
81 testResize();
82 testExtend();
83 testReserve();
84 testShrinkToFit();
85 testSwap();
86 testIsDefault();
87 testSubmatrix();
88 testRow();
89 testColumn();
90 }
91 //*************************************************************************************************
92
93
94
95
96 //=================================================================================================
97 //
98 // TEST FUNCTIONS
99 //
100 //=================================================================================================
101
102 //*************************************************************************************************
103 /*!\brief Test of all DiagonalMatrix (self-)scaling operations.
104 //
105 // \return void
106 // \exception std::runtime_error Error detected.
107 //
108 // This function performs a test of all available ways to scale an instance of the DiagonalMatrix
109 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
110 */
testScaling()111 void DenseTest::testScaling()
112 {
113 //=====================================================================================
114 // Row-major self-scaling (M*=s)
115 //=====================================================================================
116
117 {
118 test_ = "Row-major self-scaling (M*=s)";
119
120 DT diag( 3UL );
121 diag(1,1) = 2;
122 diag(2,2) = -3;
123
124 diag *= 2;
125
126 checkRows ( diag, 3UL );
127 checkColumns ( diag, 3UL );
128 checkCapacity( diag, 9UL );
129 checkNonZeros( diag, 2UL );
130 checkNonZeros( diag, 0UL, 0UL );
131 checkNonZeros( diag, 1UL, 1UL );
132 checkNonZeros( diag, 2UL, 1UL );
133
134 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
135 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
136 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
137 std::ostringstream oss;
138 oss << " Test: " << test_ << "\n"
139 << " Error: Failed self-scaling operation\n"
140 << " Details:\n"
141 << " Result:\n" << diag << "\n"
142 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
143 throw std::runtime_error( oss.str() );
144 }
145 }
146
147
148 //=====================================================================================
149 // Row-major self-scaling (M=M*s)
150 //=====================================================================================
151
152 {
153 test_ = "Row-major self-scaling (M=M*s)";
154
155 DT diag( 3UL );
156 diag(1,1) = 2;
157 diag(2,2) = -3;
158
159 diag = diag * 2;
160
161 checkRows ( diag, 3UL );
162 checkColumns ( diag, 3UL );
163 checkCapacity( diag, 9UL );
164 checkNonZeros( diag, 2UL );
165 checkNonZeros( diag, 0UL, 0UL );
166 checkNonZeros( diag, 1UL, 1UL );
167 checkNonZeros( diag, 2UL, 1UL );
168
169 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
170 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
171 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
172 std::ostringstream oss;
173 oss << " Test: " << test_ << "\n"
174 << " Error: Failed self-scaling operation\n"
175 << " Details:\n"
176 << " Result:\n" << diag << "\n"
177 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
178 throw std::runtime_error( oss.str() );
179 }
180 }
181
182
183 //=====================================================================================
184 // Row-major self-scaling (M=s*M)
185 //=====================================================================================
186
187 {
188 test_ = "Row-major self-scaling (M=s*M)";
189
190 DT diag( 3UL );
191 diag(1,1) = 2;
192 diag(2,2) = -3;
193
194 diag = 2 * diag;
195
196 checkRows ( diag, 3UL );
197 checkColumns ( diag, 3UL );
198 checkCapacity( diag, 9UL );
199 checkNonZeros( diag, 2UL );
200 checkNonZeros( diag, 0UL, 0UL );
201 checkNonZeros( diag, 1UL, 1UL );
202 checkNonZeros( diag, 2UL, 1UL );
203
204 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
205 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
206 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
207 std::ostringstream oss;
208 oss << " Test: " << test_ << "\n"
209 << " Error: Failed self-scaling operation\n"
210 << " Details:\n"
211 << " Result:\n" << diag << "\n"
212 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
213 throw std::runtime_error( oss.str() );
214 }
215 }
216
217
218 //=====================================================================================
219 // Row-major self-scaling (M/=s)
220 //=====================================================================================
221
222 {
223 test_ = "Row-major self-scaling (M/=s)";
224
225 DT diag( 3UL );
226 diag(1,1) = 4;
227 diag(2,2) = -6;
228
229 diag /= 2;
230
231 checkRows ( diag, 3UL );
232 checkColumns ( diag, 3UL );
233 checkCapacity( diag, 9UL );
234 checkNonZeros( diag, 2UL );
235 checkNonZeros( diag, 0UL, 0UL );
236 checkNonZeros( diag, 1UL, 1UL );
237 checkNonZeros( diag, 2UL, 1UL );
238
239 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
240 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
241 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
242 std::ostringstream oss;
243 oss << " Test: " << test_ << "\n"
244 << " Error: Failed self-scaling operation\n"
245 << " Details:\n"
246 << " Result:\n" << diag << "\n"
247 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
248 throw std::runtime_error( oss.str() );
249 }
250 }
251
252
253 //=====================================================================================
254 // Row-major self-scaling (M=M/s)
255 //=====================================================================================
256
257 {
258 test_ = "Row-major self-scaling (M=M/s)";
259
260 DT diag( 3UL );
261 diag(1,1) = 4;
262 diag(2,2) = -6;
263
264 diag = diag / 2;
265
266 checkRows ( diag, 3UL );
267 checkColumns ( diag, 3UL );
268 checkCapacity( diag, 9UL );
269 checkNonZeros( diag, 2UL );
270 checkNonZeros( diag, 0UL, 0UL );
271 checkNonZeros( diag, 1UL, 1UL );
272 checkNonZeros( diag, 2UL, 1UL );
273
274 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
275 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
276 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
277 std::ostringstream oss;
278 oss << " Test: " << test_ << "\n"
279 << " Error: Failed self-scaling operation\n"
280 << " Details:\n"
281 << " Result:\n" << diag << "\n"
282 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
283 throw std::runtime_error( oss.str() );
284 }
285 }
286
287
288 //=====================================================================================
289 // Row-major DiagonalMatrix::scale()
290 //=====================================================================================
291
292 {
293 test_ = "Row-major DiagonalMatrix::scale()";
294
295 // Initialization check
296 DT diag( 3UL );
297 diag(1,1) = 2;
298 diag(2,2) = -3;
299
300 checkRows ( diag, 3UL );
301 checkColumns ( diag, 3UL );
302 checkCapacity( diag, 9UL );
303 checkNonZeros( diag, 2UL );
304 checkNonZeros( diag, 0UL, 0UL );
305 checkNonZeros( diag, 1UL, 1UL );
306 checkNonZeros( diag, 2UL, 1UL );
307
308 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
309 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
310 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
311 std::ostringstream oss;
312 oss << " Test: " << test_ << "\n"
313 << " Error: Initialization failed\n"
314 << " Details:\n"
315 << " Result:\n" << diag << "\n"
316 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
317 throw std::runtime_error( oss.str() );
318 }
319
320 // Integral scaling of the matrix
321 diag.scale( 2 );
322
323 checkRows ( diag, 3UL );
324 checkColumns ( diag, 3UL );
325 checkCapacity( diag, 9UL );
326 checkNonZeros( diag, 2UL );
327 checkNonZeros( diag, 0UL, 0UL );
328 checkNonZeros( diag, 1UL, 1UL );
329 checkNonZeros( diag, 2UL, 1UL );
330
331 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
332 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
333 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
334 std::ostringstream oss;
335 oss << " Test: " << test_ << "\n"
336 << " Error: Scale operation failed\n"
337 << " Details:\n"
338 << " Result:\n" << diag << "\n"
339 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
340 throw std::runtime_error( oss.str() );
341 }
342
343 // Floating point scaling of the matrix
344 diag.scale( 0.5 );
345
346 checkRows ( diag, 3UL );
347 checkColumns ( diag, 3UL );
348 checkCapacity( diag, 9UL );
349 checkNonZeros( diag, 2UL );
350 checkNonZeros( diag, 0UL, 0UL );
351 checkNonZeros( diag, 1UL, 1UL );
352 checkNonZeros( diag, 2UL, 1UL );
353
354 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
355 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
356 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
357 std::ostringstream oss;
358 oss << " Test: " << test_ << "\n"
359 << " Error: Initialization failed\n"
360 << " Details:\n"
361 << " Result:\n" << diag << "\n"
362 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
363 throw std::runtime_error( oss.str() );
364 }
365 }
366
367 {
368 test_ = "Row-major DiagonalMatrix::scale() (complex)";
369
370 using blaze::complex;
371
372 blaze::DiagonalMatrix< blaze::DynamicMatrix<complex<float>,blaze::rowMajor> > diag( 2UL );
373 diag(0,0) = complex<float>( 1.0F, 0.0F );
374 diag(1,1) = complex<float>( 2.0F, 0.0F );
375
376 diag.scale( complex<float>( 3.0F, 0.0F ) );
377
378 checkRows ( diag, 2UL );
379 checkColumns ( diag, 2UL );
380 checkCapacity( diag, 4UL );
381 checkNonZeros( diag, 2UL );
382 checkNonZeros( diag, 0UL, 1UL );
383 checkNonZeros( diag, 1UL, 1UL );
384
385 if( diag(0,0) != complex<float>( 3.0F, 0.0F ) || diag(0,1) != complex<float>( 0.0F, 0.0F ) ||
386 diag(1,0) != complex<float>( 0.0F, 0.0F ) || diag(1,1) != complex<float>( 6.0F, 0.0F ) ) {
387 std::ostringstream oss;
388 oss << " Test: " << test_ << "\n"
389 << " Error: Scale operation failed\n"
390 << " Details:\n"
391 << " Result:\n" << diag << "\n"
392 << " Expected result:\n( (3,0) (0,0)\n(0,0) (6,0) )\n";
393 throw std::runtime_error( oss.str() );
394 }
395 }
396
397
398 //=====================================================================================
399 // Column-major self-scaling (M*=s)
400 //=====================================================================================
401
402 {
403 test_ = "Column-major self-scaling (M*=s)";
404
405 ODT diag( 3UL );
406 diag(1,1) = 2;
407 diag(2,2) = -3;
408
409 diag *= 2;
410
411 checkRows ( diag, 3UL );
412 checkColumns ( diag, 3UL );
413 checkCapacity( diag, 9UL );
414 checkNonZeros( diag, 2UL );
415 checkNonZeros( diag, 0UL, 0UL );
416 checkNonZeros( diag, 1UL, 1UL );
417 checkNonZeros( diag, 2UL, 1UL );
418
419 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
420 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
421 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
422 std::ostringstream oss;
423 oss << " Test: " << test_ << "\n"
424 << " Error: Failed self-scaling operation\n"
425 << " Details:\n"
426 << " Result:\n" << diag << "\n"
427 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
428 throw std::runtime_error( oss.str() );
429 }
430 }
431
432
433 //=====================================================================================
434 // Column-major self-scaling (M=M*s)
435 //=====================================================================================
436
437 {
438 test_ = "Column-major self-scaling (M=M*s)";
439
440 ODT diag( 3UL );
441 diag(1,1) = 2;
442 diag(2,2) = -3;
443
444 diag = diag * 2;
445
446 checkRows ( diag, 3UL );
447 checkColumns ( diag, 3UL );
448 checkCapacity( diag, 9UL );
449 checkNonZeros( diag, 2UL );
450 checkNonZeros( diag, 0UL, 0UL );
451 checkNonZeros( diag, 1UL, 1UL );
452 checkNonZeros( diag, 2UL, 1UL );
453
454 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
455 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
456 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
457 std::ostringstream oss;
458 oss << " Test: " << test_ << "\n"
459 << " Error: Failed self-scaling operation\n"
460 << " Details:\n"
461 << " Result:\n" << diag << "\n"
462 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
463 throw std::runtime_error( oss.str() );
464 }
465 }
466
467
468 //=====================================================================================
469 // Column-major self-scaling (M=s*M)
470 //=====================================================================================
471
472 {
473 test_ = "Column-major self-scaling (M=s*M)";
474
475 ODT diag( 3UL );
476 diag(1,1) = 2;
477 diag(2,2) = -3;
478
479 diag = 2 * diag;
480
481 checkRows ( diag, 3UL );
482 checkColumns ( diag, 3UL );
483 checkCapacity( diag, 9UL );
484 checkNonZeros( diag, 2UL );
485 checkNonZeros( diag, 0UL, 0UL );
486 checkNonZeros( diag, 1UL, 1UL );
487 checkNonZeros( diag, 2UL, 1UL );
488
489 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
490 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
491 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
492 std::ostringstream oss;
493 oss << " Test: " << test_ << "\n"
494 << " Error: Failed self-scaling operation\n"
495 << " Details:\n"
496 << " Result:\n" << diag << "\n"
497 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
498 throw std::runtime_error( oss.str() );
499 }
500 }
501
502
503 //=====================================================================================
504 // Column-major self-scaling (M/=s)
505 //=====================================================================================
506
507 {
508 test_ = "Column-major self-scaling (M/=s)";
509
510 ODT diag( 3UL );
511 diag(1,1) = 4;
512 diag(2,2) = -6;
513
514 diag /= 2;
515
516 checkRows ( diag, 3UL );
517 checkColumns ( diag, 3UL );
518 checkCapacity( diag, 9UL );
519 checkNonZeros( diag, 2UL );
520 checkNonZeros( diag, 0UL, 0UL );
521 checkNonZeros( diag, 1UL, 1UL );
522 checkNonZeros( diag, 2UL, 1UL );
523
524 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
525 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
526 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
527 std::ostringstream oss;
528 oss << " Test: " << test_ << "\n"
529 << " Error: Failed self-scaling operation\n"
530 << " Details:\n"
531 << " Result:\n" << diag << "\n"
532 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
533 throw std::runtime_error( oss.str() );
534 }
535 }
536
537
538 //=====================================================================================
539 // Column-major self-scaling (M=M/s)
540 //=====================================================================================
541
542 {
543 test_ = "Column-major self-scaling (M=M/s)";
544
545 ODT diag( 3UL );
546 diag(1,1) = 4;
547 diag(2,2) = -6;
548
549 diag = diag / 2;
550
551 checkRows ( diag, 3UL );
552 checkColumns ( diag, 3UL );
553 checkCapacity( diag, 9UL );
554 checkNonZeros( diag, 2UL );
555 checkNonZeros( diag, 0UL, 0UL );
556 checkNonZeros( diag, 1UL, 1UL );
557 checkNonZeros( diag, 2UL, 1UL );
558
559 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
560 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
561 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
562 std::ostringstream oss;
563 oss << " Test: " << test_ << "\n"
564 << " Error: Failed self-scaling operation\n"
565 << " Details:\n"
566 << " Result:\n" << diag << "\n"
567 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
568 throw std::runtime_error( oss.str() );
569 }
570 }
571
572
573 //=====================================================================================
574 // Column-major DiagonalMatrix::scale()
575 //=====================================================================================
576
577 {
578 test_ = "Column-major DiagonalMatrix::scale()";
579
580 // Initialization check
581 ODT diag( 3UL );
582 diag(1,1) = 2;
583 diag(2,2) = -3;
584
585 checkRows ( diag, 3UL );
586 checkColumns ( diag, 3UL );
587 checkCapacity( diag, 9UL );
588 checkNonZeros( diag, 2UL );
589 checkNonZeros( diag, 0UL, 0UL );
590 checkNonZeros( diag, 1UL, 1UL );
591 checkNonZeros( diag, 2UL, 1UL );
592
593 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
594 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
595 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
596 std::ostringstream oss;
597 oss << " Test: " << test_ << "\n"
598 << " Error: Initialization failed\n"
599 << " Details:\n"
600 << " Result:\n" << diag << "\n"
601 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
602 throw std::runtime_error( oss.str() );
603 }
604
605 // Integral scaling of the matrix
606 diag.scale( 2 );
607
608 checkRows ( diag, 3UL );
609 checkColumns ( diag, 3UL );
610 checkCapacity( diag, 9UL );
611 checkNonZeros( diag, 2UL );
612 checkNonZeros( diag, 0UL, 0UL );
613 checkNonZeros( diag, 1UL, 1UL );
614 checkNonZeros( diag, 2UL, 1UL );
615
616 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
617 diag(1,0) != 0 || diag(1,1) != 4 || diag(1,2) != 0 ||
618 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
619 std::ostringstream oss;
620 oss << " Test: " << test_ << "\n"
621 << " Error: Scale operation failed\n"
622 << " Details:\n"
623 << " Result:\n" << diag << "\n"
624 << " Expected result:\n( 0 0 0 )\n( 0 4 0 )\n( 0 0 -6 )\n";
625 throw std::runtime_error( oss.str() );
626 }
627
628 // Floating point scaling of the matrix
629 diag.scale( 0.5 );
630
631 checkRows ( diag, 3UL );
632 checkColumns ( diag, 3UL );
633 checkCapacity( diag, 9UL );
634 checkNonZeros( diag, 2UL );
635 checkNonZeros( diag, 0UL, 0UL );
636 checkNonZeros( diag, 1UL, 1UL );
637 checkNonZeros( diag, 2UL, 1UL );
638
639 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
640 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
641 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
642 std::ostringstream oss;
643 oss << " Test: " << test_ << "\n"
644 << " Error: Initialization failed\n"
645 << " Details:\n"
646 << " Result:\n" << diag << "\n"
647 << " Expected result:\n( 0 0 0 )\n( 0 2 0 )\n( 0 0 -3 )\n";
648 throw std::runtime_error( oss.str() );
649 }
650 }
651
652 {
653 test_ = "Column-major DiagonalMatrix::scale() (complex)";
654
655 using blaze::complex;
656
657 blaze::DiagonalMatrix< blaze::DynamicMatrix<complex<float>,blaze::columnMajor> > diag( 2UL );
658 diag(0,0) = complex<float>( 1.0F, 0.0F );
659 diag(1,1) = complex<float>( 2.0F, 0.0F );
660
661 diag.scale( complex<float>( 3.0F, 0.0F ) );
662
663 checkRows ( diag, 2UL );
664 checkColumns ( diag, 2UL );
665 checkCapacity( diag, 4UL );
666 checkNonZeros( diag, 2UL );
667 checkNonZeros( diag, 0UL, 1UL );
668 checkNonZeros( diag, 1UL, 1UL );
669
670 if( diag(0,0) != complex<float>( 3.0F, 0.0F ) || diag(0,1) != complex<float>( 0.0F, 0.0F ) ||
671 diag(1,0) != complex<float>( 0.0F, 0.0F ) || diag(1,1) != complex<float>( 6.0F, 0.0F ) ) {
672 std::ostringstream oss;
673 oss << " Test: " << test_ << "\n"
674 << " Error: Scale operation failed\n"
675 << " Details:\n"
676 << " Result:\n" << diag << "\n"
677 << " Expected result:\n( (3,0) (0,0)\n(0,0) (6,0) )\n";
678 throw std::runtime_error( oss.str() );
679 }
680 }
681 }
682 //*************************************************************************************************
683
684
685 //*************************************************************************************************
686 /*!\brief Test of the DiagonalMatrix function call operator.
687 //
688 // \return void
689 // \exception std::runtime_error Error detected.
690 //
691 // This function performs a test of adding and accessing elements via the function call operator
692 // of the DiagonalMatrix specialization. In case an error is detected, a \a std::runtime_error
693 // exception is thrown.
694 */
testFunctionCall()695 void DenseTest::testFunctionCall()
696 {
697 //=====================================================================================
698 // Row-major matrix tests
699 //=====================================================================================
700
701 {
702 test_ = "Row-major DiagonalMatrix::operator()";
703
704 // Good cases
705 {
706 DT diag( 3UL );
707
708 // Writing the diagonal element (1,1)
709 diag(1,1) = 1;
710
711 checkRows ( diag, 3UL );
712 checkColumns ( diag, 3UL );
713 checkCapacity( diag, 9UL );
714 checkNonZeros( diag, 1UL );
715 checkNonZeros( diag, 0UL, 0UL );
716 checkNonZeros( diag, 1UL, 1UL );
717 checkNonZeros( diag, 2UL, 0UL );
718
719 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
720 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
721 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
722 std::ostringstream oss;
723 oss << " Test: " << test_ << "\n"
724 << " Error: Function call operator failed\n"
725 << " Details:\n"
726 << " Result:\n" << diag << "\n"
727 << " Expected result:\n( 0 0 0 )\n( 0 1 0 )\n( 0 0 0 )\n";
728 throw std::runtime_error( oss.str() );
729 }
730
731 // Writing the diagonal element (2,2)
732 diag(2,2) = 2;
733
734 checkRows ( diag, 3UL );
735 checkColumns ( diag, 3UL );
736 checkCapacity( diag, 9UL );
737 checkNonZeros( diag, 2UL );
738 checkNonZeros( diag, 0UL, 0UL );
739 checkNonZeros( diag, 1UL, 1UL );
740 checkNonZeros( diag, 2UL, 1UL );
741
742 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
743 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
744 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 2 ) {
745 std::ostringstream oss;
746 oss << " Test: " << test_ << "\n"
747 << " Error: Function call operator failed\n"
748 << " Details:\n"
749 << " Result:\n" << diag << "\n"
750 << " Expected result:\n( 0 0 0 )\n( 0 1 0 )\n( 0 0 2 )\n";
751 throw std::runtime_error( oss.str() );
752 }
753
754 // Adding to the diagonal element (0,0)
755 diag(0,0) += 3;
756
757 checkRows ( diag, 3UL );
758 checkColumns ( diag, 3UL );
759 checkCapacity( diag, 9UL );
760 checkNonZeros( diag, 3UL );
761 checkNonZeros( diag, 0UL, 1UL );
762 checkNonZeros( diag, 1UL, 1UL );
763 checkNonZeros( diag, 2UL, 1UL );
764
765 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
766 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
767 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 2 ) {
768 std::ostringstream oss;
769 oss << " Test: " << test_ << "\n"
770 << " Error: Function call operator failed\n"
771 << " Details:\n"
772 << " Result:\n" << diag << "\n"
773 << " Expected result:\n( 3 0 0 )\n( 0 1 0 )\n( 0 0 2 )\n";
774 throw std::runtime_error( oss.str() );
775 }
776
777 // Subtracting from the diagonal element (1,1)
778 diag(1,1) -= 4;
779
780 checkRows ( diag, 3UL );
781 checkColumns ( diag, 3UL );
782 checkCapacity( diag, 9UL );
783 checkNonZeros( diag, 3UL );
784 checkNonZeros( diag, 0UL, 1UL );
785 checkNonZeros( diag, 1UL, 1UL );
786 checkNonZeros( diag, 2UL, 1UL );
787
788 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
789 diag(1,0) != 0 || diag(1,1) != -3 || diag(1,2) != 0 ||
790 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 2 ) {
791 std::ostringstream oss;
792 oss << " Test: " << test_ << "\n"
793 << " Error: Function call operator failed\n"
794 << " Details:\n"
795 << " Result:\n" << diag << "\n"
796 << " Expected result:\n( 3 0 0 )\n( 0 -3 0 )\n( 0 0 2 )\n";
797 throw std::runtime_error( oss.str() );
798 }
799
800 // Multiplying the diagonal element (2,2)
801 diag(2,2) *= -3;
802
803 checkRows ( diag, 3UL );
804 checkColumns ( diag, 3UL );
805 checkCapacity( diag, 9UL );
806 checkNonZeros( diag, 3UL );
807 checkNonZeros( diag, 0UL, 1UL );
808 checkNonZeros( diag, 1UL, 1UL );
809 checkNonZeros( diag, 2UL, 1UL );
810
811 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
812 diag(1,0) != 0 || diag(1,1) != -3 || diag(1,2) != 0 ||
813 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
814 std::ostringstream oss;
815 oss << " Test: " << test_ << "\n"
816 << " Error: Function call operator failed\n"
817 << " Details:\n"
818 << " Result:\n" << diag << "\n"
819 << " Expected result:\n( 3 0 0 )\n( 0 -3 0 )\n( 0 0 -6 )\n";
820 throw std::runtime_error( oss.str() );
821 }
822
823 // Dividing the diagonal element (2,2)
824 diag(2,2) /= 2;
825
826 checkRows ( diag, 3UL );
827 checkColumns ( diag, 3UL );
828 checkCapacity( diag, 9UL );
829 checkNonZeros( diag, 3UL );
830 checkNonZeros( diag, 0UL, 1UL );
831 checkNonZeros( diag, 1UL, 1UL );
832 checkNonZeros( diag, 2UL, 1UL );
833
834 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
835 diag(1,0) != 0 || diag(1,1) != -3 || diag(1,2) != 0 ||
836 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
837 std::ostringstream oss;
838 oss << " Test: " << test_ << "\n"
839 << " Error: Function call operator failed\n"
840 << " Details:\n"
841 << " Result:\n" << diag << "\n"
842 << " Expected result:\n( 3 0 0 )\n( 0 -3 0 )\n( 0 0 -3 )\n";
843 throw std::runtime_error( oss.str() );
844 }
845 }
846
847 // Failure cases
848 {
849 DT diag( 3UL );
850
851 // Trying to write the lower element (2,1)
852 try {
853 diag(2,1) = 5;
854
855 std::ostringstream oss;
856 oss << " Test: " << test_ << "\n"
857 << " Error: Assignment to lower matrix element succeeded\n"
858 << " Details:\n"
859 << " Result:\n" << diag << "\n";
860 throw std::runtime_error( oss.str() );
861 }
862 catch( std::invalid_argument& ) {}
863
864 // Trying to add to the lower element (2,1)
865 try {
866 diag(2,1) += 5;
867
868 std::ostringstream oss;
869 oss << " Test: " << test_ << "\n"
870 << " Error: Addition assignment to lower matrix element succeeded\n"
871 << " Details:\n"
872 << " Result:\n" << diag << "\n";
873 throw std::runtime_error( oss.str() );
874 }
875 catch( std::invalid_argument& ) {}
876
877 // Trying to subtract from the lower element (2,1)
878 try {
879 diag(2,1) -= 5;
880
881 std::ostringstream oss;
882 oss << " Test: " << test_ << "\n"
883 << " Error: Subtraction assignment to lower matrix element succeeded\n"
884 << " Details:\n"
885 << " Result:\n" << diag << "\n";
886 throw std::runtime_error( oss.str() );
887 }
888 catch( std::invalid_argument& ) {}
889
890 // Trying to multiply the lower element (2,1)
891 try {
892 diag(2,1) *= 5;
893
894 std::ostringstream oss;
895 oss << " Test: " << test_ << "\n"
896 << " Error: Multiplication assignment to lower matrix element succeeded\n"
897 << " Details:\n"
898 << " Result:\n" << diag << "\n";
899 throw std::runtime_error( oss.str() );
900 }
901 catch( std::invalid_argument& ) {}
902
903 // Trying to divide the lower element (2,1)
904 try {
905 diag(2,1) /= 2;
906
907 std::ostringstream oss;
908 oss << " Test: " << test_ << "\n"
909 << " Error: Division assignment to lower matrix element succeeded\n"
910 << " Details:\n"
911 << " Result:\n" << diag << "\n";
912 throw std::runtime_error( oss.str() );
913 }
914 catch( std::invalid_argument& ) {}
915
916 // Trying to write the upper element (1,2)
917 try {
918 diag(1,2) = 5;
919
920 std::ostringstream oss;
921 oss << " Test: " << test_ << "\n"
922 << " Error: Assignment to upper matrix element succeeded\n"
923 << " Details:\n"
924 << " Result:\n" << diag << "\n";
925 throw std::runtime_error( oss.str() );
926 }
927 catch( std::invalid_argument& ) {}
928
929 // Trying to add to the upper element (1,2)
930 try {
931 diag(1,2) += 5;
932
933 std::ostringstream oss;
934 oss << " Test: " << test_ << "\n"
935 << " Error: Addition assignment to upper matrix element succeeded\n"
936 << " Details:\n"
937 << " Result:\n" << diag << "\n";
938 throw std::runtime_error( oss.str() );
939 }
940 catch( std::invalid_argument& ) {}
941
942 // Trying to subtract from the upper element (1,2)
943 try {
944 diag(1,2) -= 5;
945
946 std::ostringstream oss;
947 oss << " Test: " << test_ << "\n"
948 << " Error: Subtraction assignment to upper matrix element succeeded\n"
949 << " Details:\n"
950 << " Result:\n" << diag << "\n";
951 throw std::runtime_error( oss.str() );
952 }
953 catch( std::invalid_argument& ) {}
954
955 // Trying to multiply the upper element (1,2)
956 try {
957 diag(1,2) *= 5;
958
959 std::ostringstream oss;
960 oss << " Test: " << test_ << "\n"
961 << " Error: Multiplication assignment to upper matrix element succeeded\n"
962 << " Details:\n"
963 << " Result:\n" << diag << "\n";
964 throw std::runtime_error( oss.str() );
965 }
966 catch( std::invalid_argument& ) {}
967
968 // Trying to divide the upper element (1,2)
969 try {
970 diag(1,2) /= 2;
971
972 std::ostringstream oss;
973 oss << " Test: " << test_ << "\n"
974 << " Error: Division assignment to upper matrix element succeeded\n"
975 << " Details:\n"
976 << " Result:\n" << diag << "\n";
977 throw std::runtime_error( oss.str() );
978 }
979 catch( std::invalid_argument& ) {}
980 }
981 }
982
983
984 //=====================================================================================
985 // Column-major matrix tests
986 //=====================================================================================
987
988 {
989 test_ = "Column-major DiagonalMatrix::operator()";
990
991 // Good cases
992 {
993 ODT diag( 3UL );
994
995 // Writing the diagonal element (1,1)
996 diag(1,1) = 1;
997
998 checkRows ( diag, 3UL );
999 checkColumns ( diag, 3UL );
1000 checkCapacity( diag, 9UL );
1001 checkNonZeros( diag, 1UL );
1002 checkNonZeros( diag, 0UL, 0UL );
1003 checkNonZeros( diag, 1UL, 1UL );
1004 checkNonZeros( diag, 2UL, 0UL );
1005
1006 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
1007 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1008 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
1009 std::ostringstream oss;
1010 oss << " Test: " << test_ << "\n"
1011 << " Error: Function call operator failed\n"
1012 << " Details:\n"
1013 << " Result:\n" << diag << "\n"
1014 << " Expected result:\n( 0 0 0 )\n( 0 1 0 )\n( 0 0 0 )\n";
1015 throw std::runtime_error( oss.str() );
1016 }
1017
1018 // Writing the diagonal element (2,2)
1019 diag(2,2) = 2;
1020
1021 checkRows ( diag, 3UL );
1022 checkColumns ( diag, 3UL );
1023 checkCapacity( diag, 9UL );
1024 checkNonZeros( diag, 2UL );
1025 checkNonZeros( diag, 0UL, 0UL );
1026 checkNonZeros( diag, 1UL, 1UL );
1027 checkNonZeros( diag, 2UL, 1UL );
1028
1029 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
1030 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1031 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 2 ) {
1032 std::ostringstream oss;
1033 oss << " Test: " << test_ << "\n"
1034 << " Error: Function call operator failed\n"
1035 << " Details:\n"
1036 << " Result:\n" << diag << "\n"
1037 << " Expected result:\n( 0 0 0 )\n( 0 1 0 )\n( 0 0 2 )\n";
1038 throw std::runtime_error( oss.str() );
1039 }
1040
1041 // Adding to the diagonal element (0,0)
1042 diag(0,0) += 3;
1043
1044 checkRows ( diag, 3UL );
1045 checkColumns ( diag, 3UL );
1046 checkCapacity( diag, 9UL );
1047 checkNonZeros( diag, 3UL );
1048 checkNonZeros( diag, 0UL, 1UL );
1049 checkNonZeros( diag, 1UL, 1UL );
1050 checkNonZeros( diag, 2UL, 1UL );
1051
1052 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
1053 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1054 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 2 ) {
1055 std::ostringstream oss;
1056 oss << " Test: " << test_ << "\n"
1057 << " Error: Function call operator failed\n"
1058 << " Details:\n"
1059 << " Result:\n" << diag << "\n"
1060 << " Expected result:\n( 3 0 0 )\n( 0 1 0 )\n( 0 0 2 )\n";
1061 throw std::runtime_error( oss.str() );
1062 }
1063
1064 // Subtracting from the diagonal element (1,1)
1065 diag(1,1) -= 4;
1066
1067 checkRows ( diag, 3UL );
1068 checkColumns ( diag, 3UL );
1069 checkCapacity( diag, 9UL );
1070 checkNonZeros( diag, 3UL );
1071 checkNonZeros( diag, 0UL, 1UL );
1072 checkNonZeros( diag, 1UL, 1UL );
1073 checkNonZeros( diag, 2UL, 1UL );
1074
1075 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
1076 diag(1,0) != 0 || diag(1,1) != -3 || diag(1,2) != 0 ||
1077 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 2 ) {
1078 std::ostringstream oss;
1079 oss << " Test: " << test_ << "\n"
1080 << " Error: Function call operator failed\n"
1081 << " Details:\n"
1082 << " Result:\n" << diag << "\n"
1083 << " Expected result:\n( 3 0 0 )\n( 0 -3 0 )\n( 0 0 2 )\n";
1084 throw std::runtime_error( oss.str() );
1085 }
1086
1087 // Multiplying the diagonal element (2,2)
1088 diag(2,2) *= -3;
1089
1090 checkRows ( diag, 3UL );
1091 checkColumns ( diag, 3UL );
1092 checkCapacity( diag, 9UL );
1093 checkNonZeros( diag, 3UL );
1094 checkNonZeros( diag, 0UL, 1UL );
1095 checkNonZeros( diag, 1UL, 1UL );
1096 checkNonZeros( diag, 2UL, 1UL );
1097
1098 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
1099 diag(1,0) != 0 || diag(1,1) != -3 || diag(1,2) != 0 ||
1100 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -6 ) {
1101 std::ostringstream oss;
1102 oss << " Test: " << test_ << "\n"
1103 << " Error: Function call operator failed\n"
1104 << " Details:\n"
1105 << " Result:\n" << diag << "\n"
1106 << " Expected result:\n( 3 0 0 )\n( 0 -3 0 )\n( 0 0 -6 )\n";
1107 throw std::runtime_error( oss.str() );
1108 }
1109
1110 // Dividing the diagonal element (2,2)
1111 diag(2,2) /= 2;
1112
1113 checkRows ( diag, 3UL );
1114 checkColumns ( diag, 3UL );
1115 checkCapacity( diag, 9UL );
1116 checkNonZeros( diag, 3UL );
1117 checkNonZeros( diag, 0UL, 1UL );
1118 checkNonZeros( diag, 1UL, 1UL );
1119 checkNonZeros( diag, 2UL, 1UL );
1120
1121 if( diag(0,0) != 3 || diag(0,1) != 0 || diag(0,2) != 0 ||
1122 diag(1,0) != 0 || diag(1,1) != -3 || diag(1,2) != 0 ||
1123 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -3 ) {
1124 std::ostringstream oss;
1125 oss << " Test: " << test_ << "\n"
1126 << " Error: Function call operator failed\n"
1127 << " Details:\n"
1128 << " Result:\n" << diag << "\n"
1129 << " Expected result:\n( 3 0 0 )\n( 0 -3 0 )\n( 0 0 -3 )\n";
1130 throw std::runtime_error( oss.str() );
1131 }
1132 }
1133
1134 // Failure cases
1135 {
1136 ODT diag( 3UL );
1137
1138 // Trying to write the lower element (2,1)
1139 try {
1140 diag(2,1) = 5;
1141
1142 std::ostringstream oss;
1143 oss << " Test: " << test_ << "\n"
1144 << " Error: Assignment to lower matrix element succeeded\n"
1145 << " Details:\n"
1146 << " Result:\n" << diag << "\n";
1147 throw std::runtime_error( oss.str() );
1148 }
1149 catch( std::invalid_argument& ) {}
1150
1151 // Trying to add to the lower element (2,1)
1152 try {
1153 diag(2,1) += 5;
1154
1155 std::ostringstream oss;
1156 oss << " Test: " << test_ << "\n"
1157 << " Error: Addition assignment to lower matrix element succeeded\n"
1158 << " Details:\n"
1159 << " Result:\n" << diag << "\n";
1160 throw std::runtime_error( oss.str() );
1161 }
1162 catch( std::invalid_argument& ) {}
1163
1164 // Trying to subtract from the lower element (2,1)
1165 try {
1166 diag(2,1) -= 5;
1167
1168 std::ostringstream oss;
1169 oss << " Test: " << test_ << "\n"
1170 << " Error: Subtraction assignment to lower matrix element succeeded\n"
1171 << " Details:\n"
1172 << " Result:\n" << diag << "\n";
1173 throw std::runtime_error( oss.str() );
1174 }
1175 catch( std::invalid_argument& ) {}
1176
1177 // Trying to multiply the lower element (2,1)
1178 try {
1179 diag(2,1) *= 5;
1180
1181 std::ostringstream oss;
1182 oss << " Test: " << test_ << "\n"
1183 << " Error: Multiplication assignment to lower matrix element succeeded\n"
1184 << " Details:\n"
1185 << " Result:\n" << diag << "\n";
1186 throw std::runtime_error( oss.str() );
1187 }
1188 catch( std::invalid_argument& ) {}
1189
1190 // Trying to divide the lower element (2,1)
1191 try {
1192 diag(2,1) /= 2;
1193
1194 std::ostringstream oss;
1195 oss << " Test: " << test_ << "\n"
1196 << " Error: Division assignment to lower matrix element succeeded\n"
1197 << " Details:\n"
1198 << " Result:\n" << diag << "\n";
1199 throw std::runtime_error( oss.str() );
1200 }
1201 catch( std::invalid_argument& ) {}
1202
1203 // Trying to write the upper element (1,2)
1204 try {
1205 diag(1,2) = 5;
1206
1207 std::ostringstream oss;
1208 oss << " Test: " << test_ << "\n"
1209 << " Error: Assignment to upper matrix element succeeded\n"
1210 << " Details:\n"
1211 << " Result:\n" << diag << "\n";
1212 throw std::runtime_error( oss.str() );
1213 }
1214 catch( std::invalid_argument& ) {}
1215
1216 // Trying to add to the upper element (1,2)
1217 try {
1218 diag(1,2) += 5;
1219
1220 std::ostringstream oss;
1221 oss << " Test: " << test_ << "\n"
1222 << " Error: Addition assignment to upper matrix element succeeded\n"
1223 << " Details:\n"
1224 << " Result:\n" << diag << "\n";
1225 throw std::runtime_error( oss.str() );
1226 }
1227 catch( std::invalid_argument& ) {}
1228
1229 // Trying to subtract from the upper element (1,2)
1230 try {
1231 diag(1,2) -= 5;
1232
1233 std::ostringstream oss;
1234 oss << " Test: " << test_ << "\n"
1235 << " Error: Subtraction assignment to upper matrix element succeeded\n"
1236 << " Details:\n"
1237 << " Result:\n" << diag << "\n";
1238 throw std::runtime_error( oss.str() );
1239 }
1240 catch( std::invalid_argument& ) {}
1241
1242 // Trying to multiply the upper element (1,2)
1243 try {
1244 diag(1,2) *= 5;
1245
1246 std::ostringstream oss;
1247 oss << " Test: " << test_ << "\n"
1248 << " Error: Multiplication assignment to upper matrix element succeeded\n"
1249 << " Details:\n"
1250 << " Result:\n" << diag << "\n";
1251 throw std::runtime_error( oss.str() );
1252 }
1253 catch( std::invalid_argument& ) {}
1254
1255 // Trying to divide the upper element (1,2)
1256 try {
1257 diag(1,2) /= 2;
1258
1259 std::ostringstream oss;
1260 oss << " Test: " << test_ << "\n"
1261 << " Error: Division assignment to upper matrix element succeeded\n"
1262 << " Details:\n"
1263 << " Result:\n" << diag << "\n";
1264 throw std::runtime_error( oss.str() );
1265 }
1266 catch( std::invalid_argument& ) {}
1267 }
1268 }
1269 }
1270 //*************************************************************************************************
1271
1272
1273 //*************************************************************************************************
1274 /*!\brief Test of the DiagonalMatrix iterator implementation.
1275 //
1276 // \return void
1277 // \exception std::runtime_error Error detected.
1278 //
1279 // This function performs a test of the iterator implementation of the DiagonalMatrix
1280 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
1281 */
testIterator()1282 void DenseTest::testIterator()
1283 {
1284 //=====================================================================================
1285 // Row-major matrix tests
1286 //=====================================================================================
1287
1288 {
1289 using Iterator = DT::Iterator;
1290 using ConstIterator = DT::ConstIterator;
1291
1292 DT diag( 3UL );
1293 diag(0,0) = 1;
1294 diag(1,1) = -2;
1295 diag(2,2) = 3;
1296
1297 // Testing the Iterator default constructor
1298 {
1299 test_ = "Row-major Iterator default constructor";
1300
1301 Iterator it{};
1302
1303 if( it != Iterator() ) {
1304 std::ostringstream oss;
1305 oss << " Test: " << test_ << "\n"
1306 << " Error: Failed iterator default constructor\n";
1307 throw std::runtime_error( oss.str() );
1308 }
1309 }
1310
1311 // Testing the ConstIterator default constructor
1312 {
1313 test_ = "Row-major ConstIterator default constructor";
1314
1315 ConstIterator it{};
1316
1317 if( it != ConstIterator() ) {
1318 std::ostringstream oss;
1319 oss << " Test: " << test_ << "\n"
1320 << " Error: Failed iterator default constructor\n";
1321 throw std::runtime_error( oss.str() );
1322 }
1323 }
1324
1325 // Testing conversion from Iterator to ConstIterator
1326 {
1327 test_ = "Row-major Iterator/ConstIterator conversion";
1328
1329 ConstIterator it( begin( diag, 1UL ) );
1330
1331 if( it == end( diag, 1UL ) || *it != 0 ) {
1332 std::ostringstream oss;
1333 oss << " Test: " << test_ << "\n"
1334 << " Error: Failed iterator conversion detected\n";
1335 throw std::runtime_error( oss.str() );
1336 }
1337 }
1338
1339 // Counting the number of elements in 0th row via Iterator (end-begin)
1340 {
1341 test_ = "Row-major Iterator subtraction (end-begin)";
1342
1343 const ptrdiff_t number( end( diag, 0UL ) - begin( diag, 0UL ) );
1344
1345 if( number != 3L ) {
1346 std::ostringstream oss;
1347 oss << " Test: " << test_ << "\n"
1348 << " Error: Invalid number of elements detected\n"
1349 << " Details:\n"
1350 << " Number of elements : " << number << "\n"
1351 << " Expected number of elements: 3\n";
1352 throw std::runtime_error( oss.str() );
1353 }
1354 }
1355
1356 // Counting the number of elements in 0th row via Iterator (begin-end)
1357 {
1358 test_ = "Row-major Iterator subtraction (begin-end)";
1359
1360 const ptrdiff_t number( begin( diag, 0UL ) - end( diag, 0UL ) );
1361
1362 if( number != -3L ) {
1363 std::ostringstream oss;
1364 oss << " Test: " << test_ << "\n"
1365 << " Error: Invalid number of elements detected\n"
1366 << " Details:\n"
1367 << " Number of elements : " << number << "\n"
1368 << " Expected number of elements: -3\n";
1369 throw std::runtime_error( oss.str() );
1370 }
1371 }
1372
1373 // Counting the number of elements in 1st row via ConstIterator (end-begin)
1374 {
1375 test_ = "Row-major ConstIterator subtraction (end-begin)";
1376
1377 const ptrdiff_t number( cend( diag, 1UL ) - cbegin( diag, 1UL ) );
1378
1379 if( number != 3L ) {
1380 std::ostringstream oss;
1381 oss << " Test: " << test_ << "\n"
1382 << " Error: Invalid number of elements detected\n"
1383 << " Details:\n"
1384 << " Number of elements : " << number << "\n"
1385 << " Expected number of elements: 3\n";
1386 throw std::runtime_error( oss.str() );
1387 }
1388 }
1389
1390 // Counting the number of elements in 1st row via ConstIterator (begin-end)
1391 {
1392 test_ = "Row-major ConstIterator subtraction (begin-end)";
1393
1394 const ptrdiff_t number( cbegin( diag, 1UL ) - cend( diag, 1UL ) );
1395
1396 if( number != -3L ) {
1397 std::ostringstream oss;
1398 oss << " Test: " << test_ << "\n"
1399 << " Error: Invalid number of elements detected\n"
1400 << " Details:\n"
1401 << " Number of elements : " << number << "\n"
1402 << " Expected number of elements: -3\n";
1403 throw std::runtime_error( oss.str() );
1404 }
1405 }
1406
1407 // Testing read-only access via ConstIterator
1408 {
1409 test_ = "Row-major read-only access via ConstIterator";
1410
1411 ConstIterator it ( cbegin( diag, 2UL ) );
1412 ConstIterator end( cend( diag, 2UL ) );
1413
1414 if( it == end || *it != 0 ) {
1415 std::ostringstream oss;
1416 oss << " Test: " << test_ << "\n"
1417 << " Error: Invalid initial iterator detected\n";
1418 throw std::runtime_error( oss.str() );
1419 }
1420
1421 ++it;
1422
1423 if( it == end || *it != 0 ) {
1424 std::ostringstream oss;
1425 oss << " Test: " << test_ << "\n"
1426 << " Error: Iterator pre-increment failed\n";
1427 throw std::runtime_error( oss.str() );
1428 }
1429
1430 --it;
1431
1432 if( it == end || *it != 0 ) {
1433 std::ostringstream oss;
1434 oss << " Test: " << test_ << "\n"
1435 << " Error: Iterator pre-decrement failed\n";
1436 throw std::runtime_error( oss.str() );
1437 }
1438
1439 it++;
1440
1441 if( it == end || *it != 0 ) {
1442 std::ostringstream oss;
1443 oss << " Test: " << test_ << "\n"
1444 << " Error: Iterator post-increment failed\n";
1445 throw std::runtime_error( oss.str() );
1446 }
1447
1448 it--;
1449
1450 if( it == end || *it != 0 ) {
1451 std::ostringstream oss;
1452 oss << " Test: " << test_ << "\n"
1453 << " Error: Iterator post-decrement failed\n";
1454 throw std::runtime_error( oss.str() );
1455 }
1456
1457 it += 2UL;
1458
1459 if( it == end || *it != 3 ) {
1460 std::ostringstream oss;
1461 oss << " Test: " << test_ << "\n"
1462 << " Error: Iterator addition assignment failed\n";
1463 throw std::runtime_error( oss.str() );
1464 }
1465
1466 it -= 2UL;
1467
1468 if( it == end || *it != 0 ) {
1469 std::ostringstream oss;
1470 oss << " Test: " << test_ << "\n"
1471 << " Error: Iterator subtraction assignment failed\n";
1472 throw std::runtime_error( oss.str() );
1473 }
1474
1475 it = it + 2UL;
1476
1477 if( it == end || *it != 3 ) {
1478 std::ostringstream oss;
1479 oss << " Test: " << test_ << "\n"
1480 << " Error: Iterator/scalar addition failed\n";
1481 throw std::runtime_error( oss.str() );
1482 }
1483
1484 it = it - 2UL;
1485
1486 if( it == end || *it != 0 ) {
1487 std::ostringstream oss;
1488 oss << " Test: " << test_ << "\n"
1489 << " Error: Iterator/scalar subtraction failed\n";
1490 throw std::runtime_error( oss.str() );
1491 }
1492
1493 it = 3UL + it;
1494
1495 if( it != end ) {
1496 std::ostringstream oss;
1497 oss << " Test: " << test_ << "\n"
1498 << " Error: Scalar/iterator addition failed\n";
1499 throw std::runtime_error( oss.str() );
1500 }
1501 }
1502
1503 // Testing assignment to diagonal elements via Iterator
1504 {
1505 test_ = "Row-major assignment to diagonal elements via Iterator";
1506
1507 const Iterator it = begin( diag, 0UL );
1508 *it = 4;
1509
1510 if( diag(0,0) != 4 || diag(0,1) != 0 || diag(0,2) != 0 ||
1511 diag(1,0) != 0 || diag(1,1) != -2 || diag(1,2) != 0 ||
1512 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
1513 std::ostringstream oss;
1514 oss << " Test: " << test_ << "\n"
1515 << " Error: Assignment via iterator failed\n"
1516 << " Details:\n"
1517 << " Result:\n" << diag << "\n"
1518 << " Expected result:\n( 4 0 0 )\n( 0 -2 0 )\n( 0 0 3 )\n";
1519 throw std::runtime_error( oss.str() );
1520 }
1521 }
1522
1523 // Testing assignment to lower elements via Iterator
1524 {
1525 test_ = "Row-major assignment to lower elements via Iterator";
1526
1527 try {
1528 const Iterator it = begin( diag, 1UL );
1529 *it = 5;
1530
1531 std::ostringstream oss;
1532 oss << " Test: " << test_ << "\n"
1533 << " Error: Assignment to lower matrix element succeeded\n"
1534 << " Details:\n"
1535 << " Result:\n" << diag << "\n";
1536 throw std::runtime_error( oss.str() );
1537 }
1538 catch( std::invalid_argument& ) {}
1539 }
1540
1541 // Testing assignment to upper elements via Iterator
1542 {
1543 test_ = "Row-major assignment to upper elements via Iterator";
1544
1545 try {
1546 const Iterator it = begin( diag, 0UL ) + 1UL;
1547 *it = 5;
1548
1549 std::ostringstream oss;
1550 oss << " Test: " << test_ << "\n"
1551 << " Error: Assignment to upper matrix element succeeded\n"
1552 << " Details:\n"
1553 << " Result:\n" << diag << "\n";
1554 throw std::runtime_error( oss.str() );
1555 }
1556 catch( std::invalid_argument& ) {}
1557 }
1558
1559 // Testing addition assignment to diagonal elements via Iterator
1560 {
1561 test_ = "Row-major addition assignment to diagonal elements via Iterator";
1562
1563 const Iterator it = begin( diag, 1UL ) + 1UL;
1564 *it += 3;
1565
1566 if( diag(0,0) != 4 || diag(0,1) != 0 || diag(0,2) != 0 ||
1567 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1568 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
1569 std::ostringstream oss;
1570 oss << " Test: " << test_ << "\n"
1571 << " Error: Assignment via iterator failed\n"
1572 << " Details:\n"
1573 << " Result:\n" << diag << "\n"
1574 << " Expected result:\n( 4 0 0 )\n( 0 1 0 )\n( 0 0 3 )\n";
1575 throw std::runtime_error( oss.str() );
1576 }
1577 }
1578
1579 // Testing addition assignment to lower elements via Iterator
1580 {
1581 test_ = "Row-major addition assignment to lower elements via Iterator";
1582
1583 try {
1584 const Iterator it = begin( diag, 2UL );
1585 *it += 5;
1586
1587 std::ostringstream oss;
1588 oss << " Test: " << test_ << "\n"
1589 << " Error: Assignment to lower matrix element succeeded\n"
1590 << " Details:\n"
1591 << " Result:\n" << diag << "\n";
1592 throw std::runtime_error( oss.str() );
1593 }
1594 catch( std::invalid_argument& ) {}
1595 }
1596
1597 // Testing addition assignment to upper elements via Iterator
1598 {
1599 test_ = "Row-major addition assignment to upper elements via Iterator";
1600
1601 try {
1602 const Iterator it = begin( diag, 0UL ) + 2UL;
1603 *it += 5;
1604
1605 std::ostringstream oss;
1606 oss << " Test: " << test_ << "\n"
1607 << " Error: Assignment to upper matrix element succeeded\n"
1608 << " Details:\n"
1609 << " Result:\n" << diag << "\n";
1610 throw std::runtime_error( oss.str() );
1611 }
1612 catch( std::invalid_argument& ) {}
1613 }
1614
1615 // Testing subtraction assignment to diagonal elements via Iterator
1616 {
1617 test_ = "Row-major subtraction assignment to diagonal elements via Iterator";
1618
1619 const Iterator it = begin( diag, 2UL ) + 2UL;
1620 *it -= 4;
1621
1622 if( diag(0,0) != 4 || diag(0,1) != 0 || diag(0,2) != 0 ||
1623 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1624 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -1 ) {
1625 std::ostringstream oss;
1626 oss << " Test: " << test_ << "\n"
1627 << " Error: Assignment via iterator failed\n"
1628 << " Details:\n"
1629 << " Result:\n" << diag << "\n"
1630 << " Expected result:\n( 4 0 0 )\n( 0 1 0 )\n( 0 0 -1 )\n";
1631 throw std::runtime_error( oss.str() );
1632 }
1633 }
1634
1635 // Testing subtraction assignment to lower elements via Iterator
1636 {
1637 test_ = "Row-major subtraction assignment to lower elements via Iterator";
1638
1639 try {
1640 const Iterator it = begin( diag, 2UL ) + 1UL;
1641 *it += 5;
1642
1643 std::ostringstream oss;
1644 oss << " Test: " << test_ << "\n"
1645 << " Error: Assignment to lower matrix element succeeded\n"
1646 << " Details:\n"
1647 << " Result:\n" << diag << "\n";
1648 throw std::runtime_error( oss.str() );
1649 }
1650 catch( std::invalid_argument& ) {}
1651 }
1652
1653 // Testing subtraction assignment to upper elements via Iterator
1654 {
1655 test_ = "Row-major subtraction assignment to upper elements via Iterator";
1656
1657 try {
1658 const Iterator it = begin( diag, 1UL ) + 2UL;
1659 *it -= 5;
1660
1661 std::ostringstream oss;
1662 oss << " Test: " << test_ << "\n"
1663 << " Error: Assignment to upper matrix element succeeded\n"
1664 << " Details:\n"
1665 << " Result:\n" << diag << "\n";
1666 throw std::runtime_error( oss.str() );
1667 }
1668 catch( std::invalid_argument& ) {}
1669 }
1670
1671 // Testing multiplication assignment to diagonal elements via Iterator
1672 {
1673 test_ = "Row-major multiplication assignment to diagonal elements via Iterator";
1674
1675 const Iterator it = begin( diag, 0UL );
1676 *it *= 2;
1677
1678 if( diag(0,0) != 8 || diag(0,1) != 0 || diag(0,2) != 0 ||
1679 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1680 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -1 ) {
1681 std::ostringstream oss;
1682 oss << " Test: " << test_ << "\n"
1683 << " Error: Assignment via iterator failed\n"
1684 << " Details:\n"
1685 << " Result:\n" << diag << "\n"
1686 << " Expected result:\n( 8 0 0 )\n( 0 1 0 )\n( 0 0 -1 )\n";
1687 throw std::runtime_error( oss.str() );
1688 }
1689 }
1690
1691 // Testing multiplication assignment to lower elements via Iterator
1692 {
1693 test_ = "Row-major multiplication assignment to lower elements via Iterator";
1694
1695 try {
1696 const Iterator it = begin( diag, 1UL );
1697 *it *= 5;
1698
1699 std::ostringstream oss;
1700 oss << " Test: " << test_ << "\n"
1701 << " Error: Assignment to lower matrix element succeeded\n"
1702 << " Details:\n"
1703 << " Result:\n" << diag << "\n";
1704 throw std::runtime_error( oss.str() );
1705 }
1706 catch( std::invalid_argument& ) {}
1707 }
1708
1709 // Testing multiplication assignment to upper elements via Iterator
1710 {
1711 test_ = "Row-major multiplication assignment to upper elements via Iterator";
1712
1713 try {
1714 const Iterator it = begin( diag, 0UL ) + 1UL;
1715 *it *= 5;
1716
1717 std::ostringstream oss;
1718 oss << " Test: " << test_ << "\n"
1719 << " Error: Assignment to upper matrix element succeeded\n"
1720 << " Details:\n"
1721 << " Result:\n" << diag << "\n";
1722 throw std::runtime_error( oss.str() );
1723 }
1724 catch( std::invalid_argument& ) {}
1725 }
1726
1727 // Testing division assignment to diagonal elements via Iterator
1728 {
1729 test_ = "Row-major division assignment to diagonal elements via Iterator";
1730
1731 const Iterator it = begin( diag, 0UL );
1732 *it /= 4;
1733
1734 if( diag(0,0) != 2 || diag(0,1) != 0 || diag(0,2) != 0 ||
1735 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
1736 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -1 ) {
1737 std::ostringstream oss;
1738 oss << " Test: " << test_ << "\n"
1739 << " Error: Assignment via iterator failed\n"
1740 << " Details:\n"
1741 << " Result:\n" << diag << "\n"
1742 << " Expected result:\n( 2 0 0 )\n( 0 1 0 )\n( 0 0 -1 )\n";
1743 throw std::runtime_error( oss.str() );
1744 }
1745 }
1746
1747 // Testing division assignment to lower elements via Iterator
1748 {
1749 test_ = "Row-major division assignment to lower elements via Iterator";
1750
1751 try {
1752 const Iterator it = begin( diag, 2UL );
1753 *it /= 5;
1754
1755 std::ostringstream oss;
1756 oss << " Test: " << test_ << "\n"
1757 << " Error: Assignment to lower matrix element succeeded\n"
1758 << " Details:\n"
1759 << " Result:\n" << diag << "\n";
1760 throw std::runtime_error( oss.str() );
1761 }
1762 catch( std::invalid_argument& ) {}
1763 }
1764
1765 // Testing division assignment to upper elements via Iterator
1766 {
1767 test_ = "Row-major division assignment to upper elements via Iterator";
1768
1769 try {
1770 const Iterator it = begin( diag, 0UL ) + 2UL;
1771 *it /= 5;
1772
1773 std::ostringstream oss;
1774 oss << " Test: " << test_ << "\n"
1775 << " Error: Assignment to upper matrix element succeeded\n"
1776 << " Details:\n"
1777 << " Result:\n" << diag << "\n";
1778 throw std::runtime_error( oss.str() );
1779 }
1780 catch( std::invalid_argument& ) {}
1781 }
1782 }
1783
1784
1785 //=====================================================================================
1786 // Column-major matrix tests
1787 //=====================================================================================
1788
1789 {
1790 using Iterator = ODT::Iterator;
1791 using ConstIterator = ODT::ConstIterator;
1792
1793 ODT diag( 3UL );
1794 diag(0,0) = 1;
1795 diag(1,1) = -2;
1796 diag(2,2) = 3;
1797
1798 // Testing the Iterator default constructor
1799 {
1800 test_ = "Column-major Iterator default constructor";
1801
1802 Iterator it{};
1803
1804 if( it != Iterator() ) {
1805 std::ostringstream oss;
1806 oss << " Test: " << test_ << "\n"
1807 << " Error: Failed iterator default constructor\n";
1808 throw std::runtime_error( oss.str() );
1809 }
1810 }
1811
1812 // Testing the ConstIterator default constructor
1813 {
1814 test_ = "Column-major ConstIterator default constructor";
1815
1816 ConstIterator it{};
1817
1818 if( it != ConstIterator() ) {
1819 std::ostringstream oss;
1820 oss << " Test: " << test_ << "\n"
1821 << " Error: Failed iterator default constructor\n";
1822 throw std::runtime_error( oss.str() );
1823 }
1824 }
1825
1826 // Testing conversion from Iterator to ConstIterator
1827 {
1828 test_ = "Column-major Iterator/ConstIterator conversion";
1829
1830 ConstIterator it( begin( diag, 1UL ) );
1831
1832 if( it == end( diag, 1UL ) || *it != 0 ) {
1833 std::ostringstream oss;
1834 oss << " Test: " << test_ << "\n"
1835 << " Error: Failed iterator conversion detected\n";
1836 throw std::runtime_error( oss.str() );
1837 }
1838 }
1839
1840 // Counting the number of elements in 0th row via Iterator (end-begin)
1841 {
1842 test_ = "Column-major Iterator subtraction (end-begin)";
1843
1844 const ptrdiff_t number( end( diag, 0UL ) - begin( diag, 0UL ) );
1845
1846 if( number != 3L ) {
1847 std::ostringstream oss;
1848 oss << " Test: " << test_ << "\n"
1849 << " Error: Invalid number of elements detected\n"
1850 << " Details:\n"
1851 << " Number of elements : " << number << "\n"
1852 << " Expected number of elements: 3\n";
1853 throw std::runtime_error( oss.str() );
1854 }
1855 }
1856
1857 // Counting the number of elements in 0th row via Iterator (begin-end)
1858 {
1859 test_ = "Column-major Iterator subtraction (begin-end)";
1860
1861 const ptrdiff_t number( begin( diag, 0UL ) - end( diag, 0UL ) );
1862
1863 if( number != -3L ) {
1864 std::ostringstream oss;
1865 oss << " Test: " << test_ << "\n"
1866 << " Error: Invalid number of elements detected\n"
1867 << " Details:\n"
1868 << " Number of elements : " << number << "\n"
1869 << " Expected number of elements: -3\n";
1870 throw std::runtime_error( oss.str() );
1871 }
1872 }
1873
1874 // Counting the number of elements in 1st row via ConstIterator (end-begin)
1875 {
1876 test_ = "Column-major ConstIterator subtraction (end-begin)";
1877
1878 const ptrdiff_t number( cend( diag, 1UL ) - cbegin( diag, 1UL ) );
1879
1880 if( number != 3L ) {
1881 std::ostringstream oss;
1882 oss << " Test: " << test_ << "\n"
1883 << " Error: Invalid number of elements detected\n"
1884 << " Details:\n"
1885 << " Number of elements : " << number << "\n"
1886 << " Expected number of elements: 3\n";
1887 throw std::runtime_error( oss.str() );
1888 }
1889 }
1890
1891 // Counting the number of elements in 1st row via ConstIterator (begin-end)
1892 {
1893 test_ = "Column-major ConstIterator subtraction (begin-end)";
1894
1895 const ptrdiff_t number( cbegin( diag, 1UL ) - cend( diag, 1UL ) );
1896
1897 if( number != -3L ) {
1898 std::ostringstream oss;
1899 oss << " Test: " << test_ << "\n"
1900 << " Error: Invalid number of elements detected\n"
1901 << " Details:\n"
1902 << " Number of elements : " << number << "\n"
1903 << " Expected number of elements: -3\n";
1904 throw std::runtime_error( oss.str() );
1905 }
1906 }
1907
1908 // Testing read-only access via ConstIterator
1909 {
1910 test_ = "Column-major read-only access via ConstIterator";
1911
1912 ConstIterator it ( cbegin( diag, 2UL ) );
1913 ConstIterator end( cend( diag, 2UL ) );
1914
1915 if( it == end || *it != 0 ) {
1916 std::ostringstream oss;
1917 oss << " Test: " << test_ << "\n"
1918 << " Error: Invalid initial iterator detected\n";
1919 throw std::runtime_error( oss.str() );
1920 }
1921
1922 ++it;
1923
1924 if( it == end || *it != 0 ) {
1925 std::ostringstream oss;
1926 oss << " Test: " << test_ << "\n"
1927 << " Error: Iterator pre-increment failed\n";
1928 throw std::runtime_error( oss.str() );
1929 }
1930
1931 --it;
1932
1933 if( it == end || *it != 0 ) {
1934 std::ostringstream oss;
1935 oss << " Test: " << test_ << "\n"
1936 << " Error: Iterator pre-decrement failed\n";
1937 throw std::runtime_error( oss.str() );
1938 }
1939
1940 it++;
1941
1942 if( it == end || *it != 0 ) {
1943 std::ostringstream oss;
1944 oss << " Test: " << test_ << "\n"
1945 << " Error: Iterator post-increment failed\n";
1946 throw std::runtime_error( oss.str() );
1947 }
1948
1949 it--;
1950
1951 if( it == end || *it != 0 ) {
1952 std::ostringstream oss;
1953 oss << " Test: " << test_ << "\n"
1954 << " Error: Iterator post-decrement failed\n";
1955 throw std::runtime_error( oss.str() );
1956 }
1957
1958 it += 2UL;
1959
1960 if( it == end || *it != 3 ) {
1961 std::ostringstream oss;
1962 oss << " Test: " << test_ << "\n"
1963 << " Error: Iterator addition assignment failed\n";
1964 throw std::runtime_error( oss.str() );
1965 }
1966
1967 it -= 2UL;
1968
1969 if( it == end || *it != 0 ) {
1970 std::ostringstream oss;
1971 oss << " Test: " << test_ << "\n"
1972 << " Error: Iterator subtraction assignment failed\n";
1973 throw std::runtime_error( oss.str() );
1974 }
1975
1976 it = it + 2UL;
1977
1978 if( it == end || *it != 3 ) {
1979 std::ostringstream oss;
1980 oss << " Test: " << test_ << "\n"
1981 << " Error: Iterator/scalar addition failed\n";
1982 throw std::runtime_error( oss.str() );
1983 }
1984
1985 it = it - 2UL;
1986
1987 if( it == end || *it != 0 ) {
1988 std::ostringstream oss;
1989 oss << " Test: " << test_ << "\n"
1990 << " Error: Iterator/scalar subtraction failed\n";
1991 throw std::runtime_error( oss.str() );
1992 }
1993
1994 it = 3UL + it;
1995
1996 if( it != end ) {
1997 std::ostringstream oss;
1998 oss << " Test: " << test_ << "\n"
1999 << " Error: Scalar/iterator addition failed\n";
2000 throw std::runtime_error( oss.str() );
2001 }
2002 }
2003
2004 // Testing assignment to diagonal elements via Iterator
2005 {
2006 test_ = "Column-major assignment to diagonal elements via Iterator";
2007
2008 const Iterator it = begin( diag, 0UL );
2009 *it = 4;
2010
2011 if( diag(0,0) != 4 || diag(0,1) != 0 || diag(0,2) != 0 ||
2012 diag(1,0) != 0 || diag(1,1) != -2 || diag(1,2) != 0 ||
2013 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2014 std::ostringstream oss;
2015 oss << " Test: " << test_ << "\n"
2016 << " Error: Assignment via iterator failed\n"
2017 << " Details:\n"
2018 << " Result:\n" << diag << "\n"
2019 << " Expected result:\n( 4 0 0 )\n( 0 -2 0 )\n( 0 0 3 )\n";
2020 throw std::runtime_error( oss.str() );
2021 }
2022 }
2023
2024 // Testing assignment to lower elements via Iterator
2025 {
2026 test_ = "Column-major assignment to lower elements via Iterator";
2027
2028 try {
2029 const Iterator it = begin( diag, 1UL );
2030 *it = 5;
2031
2032 std::ostringstream oss;
2033 oss << " Test: " << test_ << "\n"
2034 << " Error: Assignment to lower matrix element succeeded\n"
2035 << " Details:\n"
2036 << " Result:\n" << diag << "\n";
2037 throw std::runtime_error( oss.str() );
2038 }
2039 catch( std::invalid_argument& ) {}
2040 }
2041
2042 // Testing assignment to upper elements via Iterator
2043 {
2044 test_ = "Column-major assignment to upper elements via Iterator";
2045
2046 try {
2047 const Iterator it = begin( diag, 0UL ) + 1UL;
2048 *it = 5;
2049
2050 std::ostringstream oss;
2051 oss << " Test: " << test_ << "\n"
2052 << " Error: Assignment to upper matrix element succeeded\n"
2053 << " Details:\n"
2054 << " Result:\n" << diag << "\n";
2055 throw std::runtime_error( oss.str() );
2056 }
2057 catch( std::invalid_argument& ) {}
2058 }
2059
2060 // Testing addition assignment to diagonal elements via Iterator
2061 {
2062 test_ = "Column-major addition assignment to diagonal elements via Iterator";
2063
2064 const Iterator it = begin( diag, 1UL ) + 1UL;
2065 *it += 3;
2066
2067 if( diag(0,0) != 4 || diag(0,1) != 0 || diag(0,2) != 0 ||
2068 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
2069 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2070 std::ostringstream oss;
2071 oss << " Test: " << test_ << "\n"
2072 << " Error: Assignment via iterator failed\n"
2073 << " Details:\n"
2074 << " Result:\n" << diag << "\n"
2075 << " Expected result:\n( 4 0 0 )\n( 0 1 0 )\n( 0 0 3 )\n";
2076 throw std::runtime_error( oss.str() );
2077 }
2078 }
2079
2080 // Testing addition assignment to lower elements via Iterator
2081 {
2082 test_ = "Column-major addition assignment to lower elements via Iterator";
2083
2084 try {
2085 const Iterator it = begin( diag, 2UL );
2086 *it += 5;
2087
2088 std::ostringstream oss;
2089 oss << " Test: " << test_ << "\n"
2090 << " Error: Assignment to lower matrix element succeeded\n"
2091 << " Details:\n"
2092 << " Result:\n" << diag << "\n";
2093 throw std::runtime_error( oss.str() );
2094 }
2095 catch( std::invalid_argument& ) {}
2096 }
2097
2098 // Testing addition assignment to upper elements via Iterator
2099 {
2100 test_ = "Column-major addition assignment to upper elements via Iterator";
2101
2102 try {
2103 const Iterator it = begin( diag, 0UL ) + 2UL;
2104 *it += 5;
2105
2106 std::ostringstream oss;
2107 oss << " Test: " << test_ << "\n"
2108 << " Error: Assignment to upper matrix element succeeded\n"
2109 << " Details:\n"
2110 << " Result:\n" << diag << "\n";
2111 throw std::runtime_error( oss.str() );
2112 }
2113 catch( std::invalid_argument& ) {}
2114 }
2115
2116 // Testing subtraction assignment to diagonal elements via Iterator
2117 {
2118 test_ = "Column-major subtraction assignment to diagonal elements via Iterator";
2119
2120 const Iterator it = begin( diag, 2UL ) + 2UL;
2121 *it -= 4;
2122
2123 if( diag(0,0) != 4 || diag(0,1) != 0 || diag(0,2) != 0 ||
2124 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
2125 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -1 ) {
2126 std::ostringstream oss;
2127 oss << " Test: " << test_ << "\n"
2128 << " Error: Assignment via iterator failed\n"
2129 << " Details:\n"
2130 << " Result:\n" << diag << "\n"
2131 << " Expected result:\n( 4 0 0 )\n( 0 1 0 )\n( 0 0 -1 )\n";
2132 throw std::runtime_error( oss.str() );
2133 }
2134 }
2135
2136 // Testing subtraction assignment to lower elements via Iterator
2137 {
2138 test_ = "Column-major subtraction assignment to lower elements via Iterator";
2139
2140 try {
2141 const Iterator it = begin( diag, 2UL ) + 1UL;
2142 *it += 5;
2143
2144 std::ostringstream oss;
2145 oss << " Test: " << test_ << "\n"
2146 << " Error: Assignment to lower matrix element succeeded\n"
2147 << " Details:\n"
2148 << " Result:\n" << diag << "\n";
2149 throw std::runtime_error( oss.str() );
2150 }
2151 catch( std::invalid_argument& ) {}
2152 }
2153
2154 // Testing subtraction assignment to upper elements via Iterator
2155 {
2156 test_ = "Column-major subtraction assignment to upper elements via Iterator";
2157
2158 try {
2159 const Iterator it = begin( diag, 1UL ) + 2UL;
2160 *it -= 5;
2161
2162 std::ostringstream oss;
2163 oss << " Test: " << test_ << "\n"
2164 << " Error: Assignment to upper matrix element succeeded\n"
2165 << " Details:\n"
2166 << " Result:\n" << diag << "\n";
2167 throw std::runtime_error( oss.str() );
2168 }
2169 catch( std::invalid_argument& ) {}
2170 }
2171
2172 // Testing multiplication assignment to diagonal elements via Iterator
2173 {
2174 test_ = "Column-major multiplication assignment to diagonal elements via Iterator";
2175
2176 const Iterator it = begin( diag, 0UL );
2177 *it *= 2;
2178
2179 if( diag(0,0) != 8 || diag(0,1) != 0 || diag(0,2) != 0 ||
2180 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
2181 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -1 ) {
2182 std::ostringstream oss;
2183 oss << " Test: " << test_ << "\n"
2184 << " Error: Assignment via iterator failed\n"
2185 << " Details:\n"
2186 << " Result:\n" << diag << "\n"
2187 << " Expected result:\n( 8 0 0 )\n( 0 1 0 )\n( 0 0 -1 )\n";
2188 throw std::runtime_error( oss.str() );
2189 }
2190 }
2191
2192 // Testing multiplication assignment to lower elements via Iterator
2193 {
2194 test_ = "Column-major multiplication assignment to lower elements via Iterator";
2195
2196 try {
2197 const Iterator it = begin( diag, 1UL );
2198 *it *= 5;
2199
2200 std::ostringstream oss;
2201 oss << " Test: " << test_ << "\n"
2202 << " Error: Assignment to lower matrix element succeeded\n"
2203 << " Details:\n"
2204 << " Result:\n" << diag << "\n";
2205 throw std::runtime_error( oss.str() );
2206 }
2207 catch( std::invalid_argument& ) {}
2208 }
2209
2210 // Testing multiplication assignment to upper elements via Iterator
2211 {
2212 test_ = "Column-major multiplication assignment to upper elements via Iterator";
2213
2214 try {
2215 const Iterator it = begin( diag, 0UL ) + 1UL;
2216 *it *= 5;
2217
2218 std::ostringstream oss;
2219 oss << " Test: " << test_ << "\n"
2220 << " Error: Assignment to upper matrix element succeeded\n"
2221 << " Details:\n"
2222 << " Result:\n" << diag << "\n";
2223 throw std::runtime_error( oss.str() );
2224 }
2225 catch( std::invalid_argument& ) {}
2226 }
2227
2228 // Testing division assignment to diagonal elements via Iterator
2229 {
2230 test_ = "Column-major division assignment to diagonal elements via Iterator";
2231
2232 const Iterator it = begin( diag, 0UL );
2233 *it /= 4;
2234
2235 if( diag(0,0) != 2 || diag(0,1) != 0 || diag(0,2) != 0 ||
2236 diag(1,0) != 0 || diag(1,1) != 1 || diag(1,2) != 0 ||
2237 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != -1 ) {
2238 std::ostringstream oss;
2239 oss << " Test: " << test_ << "\n"
2240 << " Error: Assignment via iterator failed\n"
2241 << " Details:\n"
2242 << " Result:\n" << diag << "\n"
2243 << " Expected result:\n( 2 0 0 )\n( 0 1 0 )\n( 0 0 -1 )\n";
2244 throw std::runtime_error( oss.str() );
2245 }
2246 }
2247
2248 // Testing division assignment to lower elements via Iterator
2249 {
2250 test_ = "Column-major division assignment to lower elements via Iterator";
2251
2252 try {
2253 const Iterator it = begin( diag, 2UL );
2254 *it /= 5;
2255
2256 std::ostringstream oss;
2257 oss << " Test: " << test_ << "\n"
2258 << " Error: Assignment to lower matrix element succeeded\n"
2259 << " Details:\n"
2260 << " Result:\n" << diag << "\n";
2261 throw std::runtime_error( oss.str() );
2262 }
2263 catch( std::invalid_argument& ) {}
2264 }
2265
2266 // Testing division assignment to upper elements via Iterator
2267 {
2268 test_ = "Column-major division assignment to upper elements via Iterator";
2269
2270 try {
2271 const Iterator it = begin( diag, 0UL ) + 2UL;
2272 *it /= 5;
2273
2274 std::ostringstream oss;
2275 oss << " Test: " << test_ << "\n"
2276 << " Error: Assignment to upper matrix element succeeded\n"
2277 << " Details:\n"
2278 << " Result:\n" << diag << "\n";
2279 throw std::runtime_error( oss.str() );
2280 }
2281 catch( std::invalid_argument& ) {}
2282 }
2283 }
2284 }
2285 //*************************************************************************************************
2286
2287
2288 //*************************************************************************************************
2289 /*!\brief Test of the \c nonZeros() member function of the DiagonalMatrix specialization.
2290 //
2291 // \return void
2292 // \exception std::runtime_error Error detected.
2293 //
2294 // This function performs a test of the \c nonZeros() member function of the DiagonalMatrix
2295 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
2296 */
testNonZeros()2297 void DenseTest::testNonZeros()
2298 {
2299 //=====================================================================================
2300 // Row-major matrix tests
2301 //=====================================================================================
2302
2303 {
2304 test_ = "Row-major DiagonalMatrix::nonZeros()";
2305
2306 // Empty matrix
2307 {
2308 DT diag( 3UL );
2309
2310 checkRows ( diag, 3UL );
2311 checkColumns ( diag, 3UL );
2312 checkCapacity( diag, 9UL );
2313 checkNonZeros( diag, 0UL );
2314 checkNonZeros( diag, 0UL, 0UL );
2315 checkNonZeros( diag, 1UL, 0UL );
2316 checkNonZeros( diag, 2UL, 0UL );
2317
2318 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
2319 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2320 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2321 std::ostringstream oss;
2322 oss << " Test: " << test_ << "\n"
2323 << " Error: Initialization failed\n"
2324 << " Details:\n"
2325 << " Result:\n" << diag << "\n"
2326 << " Expected result:\n( 0 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
2327 throw std::runtime_error( oss.str() );
2328 }
2329 }
2330
2331 // Partially filled matrix
2332 {
2333 DT diag( 3UL );
2334 diag(0,0) = 1;
2335 diag(1,1) = -2;
2336
2337 checkRows ( diag, 3UL );
2338 checkColumns ( diag, 3UL );
2339 checkCapacity( diag, 9UL );
2340 checkNonZeros( diag, 2UL );
2341 checkNonZeros( diag, 0UL, 1UL );
2342 checkNonZeros( diag, 1UL, 1UL );
2343 checkNonZeros( diag, 2UL, 0UL );
2344
2345 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2346 diag(1,0) != 0 || diag(1,1) != -2 || diag(1,2) != 0 ||
2347 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2348 std::ostringstream oss;
2349 oss << " Test: " << test_ << "\n"
2350 << " Error: Initialization failed\n"
2351 << " Details:\n"
2352 << " Result:\n" << diag << "\n"
2353 << " Expected result:\n( 1 0 0 )\n( 0 -2 0 )\n( 0 0 0 )\n";
2354 throw std::runtime_error( oss.str() );
2355 }
2356 }
2357
2358 // Fully filled matrix
2359 {
2360 DT diag( 3UL );
2361 diag(0,0) = -1;
2362 diag(1,1) = 2;
2363 diag(2,2) = 3;
2364
2365 checkRows ( diag, 3UL );
2366 checkColumns ( diag, 3UL );
2367 checkCapacity( diag, 9UL );
2368 checkNonZeros( diag, 3UL );
2369 checkNonZeros( diag, 0UL, 1UL );
2370 checkNonZeros( diag, 1UL, 1UL );
2371 checkNonZeros( diag, 2UL, 1UL );
2372
2373 if( diag(0,0) != -1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2374 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
2375 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2376 std::ostringstream oss;
2377 oss << " Test: " << test_ << "\n"
2378 << " Error: Initialization failed\n"
2379 << " Details:\n"
2380 << " Result:\n" << diag << "\n"
2381 << " Expected result:\n( -1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
2382 throw std::runtime_error( oss.str() );
2383 }
2384 }
2385 }
2386
2387
2388 //=====================================================================================
2389 // Column-major matrix tests
2390 //=====================================================================================
2391
2392 {
2393 test_ = "Column-major DiagonalMatrix::nonZeros()";
2394
2395 // Empty matrix
2396 {
2397 ODT diag( 3UL );
2398
2399 checkRows ( diag, 3UL );
2400 checkColumns ( diag, 3UL );
2401 checkCapacity( diag, 9UL );
2402 checkNonZeros( diag, 0UL );
2403 checkNonZeros( diag, 0UL, 0UL );
2404 checkNonZeros( diag, 1UL, 0UL );
2405 checkNonZeros( diag, 2UL, 0UL );
2406
2407 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
2408 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2409 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2410 std::ostringstream oss;
2411 oss << " Test: " << test_ << "\n"
2412 << " Error: Initialization failed\n"
2413 << " Details:\n"
2414 << " Result:\n" << diag << "\n"
2415 << " Expected result:\n( 0 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
2416 throw std::runtime_error( oss.str() );
2417 }
2418 }
2419
2420 // Partially filled matrix
2421 {
2422 ODT diag( 3UL );
2423 diag(0,0) = 1;
2424 diag(1,1) = -2;
2425
2426 checkRows ( diag, 3UL );
2427 checkColumns ( diag, 3UL );
2428 checkCapacity( diag, 9UL );
2429 checkNonZeros( diag, 2UL );
2430 checkNonZeros( diag, 0UL, 1UL );
2431 checkNonZeros( diag, 1UL, 1UL );
2432 checkNonZeros( diag, 2UL, 0UL );
2433
2434 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2435 diag(1,0) != 0 || diag(1,1) != -2 || diag(1,2) != 0 ||
2436 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2437 std::ostringstream oss;
2438 oss << " Test: " << test_ << "\n"
2439 << " Error: Initialization failed\n"
2440 << " Details:\n"
2441 << " Result:\n" << diag << "\n"
2442 << " Expected result:\n( 1 0 0 )\n( 0 -2 0 )\n( 0 0 0 )\n";
2443 throw std::runtime_error( oss.str() );
2444 }
2445 }
2446
2447 // Fully filled matrix
2448 {
2449 ODT diag( 3UL );
2450 diag(0,0) = -1;
2451 diag(1,1) = 2;
2452 diag(2,2) = 3;
2453
2454 checkRows ( diag, 3UL );
2455 checkColumns ( diag, 3UL );
2456 checkCapacity( diag, 9UL );
2457 checkNonZeros( diag, 3UL );
2458 checkNonZeros( diag, 0UL, 1UL );
2459 checkNonZeros( diag, 1UL, 1UL );
2460 checkNonZeros( diag, 2UL, 1UL );
2461
2462 if( diag(0,0) != -1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2463 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
2464 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2465 std::ostringstream oss;
2466 oss << " Test: " << test_ << "\n"
2467 << " Error: Initialization failed\n"
2468 << " Details:\n"
2469 << " Result:\n" << diag << "\n"
2470 << " Expected result:\n( -1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
2471 throw std::runtime_error( oss.str() );
2472 }
2473 }
2474 }
2475 }
2476 //*************************************************************************************************
2477
2478
2479 //*************************************************************************************************
2480 /*!\brief Test of the \c reset() member function of the DiagonalMatrix specialization.
2481 //
2482 // \return void
2483 // \exception std::runtime_error Error detected.
2484 //
2485 // This function performs a test of the \c reset() member function of the DiagonalMatrix
2486 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
2487 */
testReset()2488 void DenseTest::testReset()
2489 {
2490 //=====================================================================================
2491 // Row-major matrix tests
2492 //=====================================================================================
2493
2494 {
2495 test_ = "Row-major DiagonalMatrix::reset()";
2496
2497 // Initialization check
2498 DT diag( 3UL );
2499 diag(0,0) = 1;
2500 diag(1,1) = 2;
2501 diag(2,2) = 3;
2502
2503 checkRows ( diag, 3UL );
2504 checkColumns ( diag, 3UL );
2505 checkCapacity( diag, 9UL );
2506 checkNonZeros( diag, 3UL );
2507 checkNonZeros( diag, 0UL, 1UL );
2508 checkNonZeros( diag, 1UL, 1UL );
2509 checkNonZeros( diag, 2UL, 1UL );
2510
2511 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2512 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
2513 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2514 std::ostringstream oss;
2515 oss << " Test: " << test_ << "\n"
2516 << " Error: Initialization failed\n"
2517 << " Details:\n"
2518 << " Result:\n" << diag << "\n"
2519 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
2520 throw std::runtime_error( oss.str() );
2521 }
2522
2523 // Resetting a diagonal element
2524 reset( diag(1,1) );
2525
2526 checkRows ( diag, 3UL );
2527 checkColumns ( diag, 3UL );
2528 checkCapacity( diag, 9UL );
2529 checkNonZeros( diag, 2UL );
2530 checkNonZeros( diag, 0UL, 1UL );
2531 checkNonZeros( diag, 1UL, 0UL );
2532 checkNonZeros( diag, 2UL, 1UL );
2533
2534 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2535 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2536 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2537 std::ostringstream oss;
2538 oss << " Test: " << test_ << "\n"
2539 << " Error: Reset operation failed\n"
2540 << " Details:\n"
2541 << " Result:\n" << diag << "\n"
2542 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2543 throw std::runtime_error( oss.str() );
2544 }
2545
2546 // Resetting a lower element
2547 reset( diag(1,0) );
2548
2549 checkRows ( diag, 3UL );
2550 checkColumns ( diag, 3UL );
2551 checkCapacity( diag, 9UL );
2552 checkNonZeros( diag, 2UL );
2553 checkNonZeros( diag, 0UL, 1UL );
2554 checkNonZeros( diag, 1UL, 0UL );
2555 checkNonZeros( diag, 2UL, 1UL );
2556
2557 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2558 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2559 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2560 std::ostringstream oss;
2561 oss << " Test: " << test_ << "\n"
2562 << " Error: Reset operation failed\n"
2563 << " Details:\n"
2564 << " Result:\n" << diag << "\n"
2565 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2566 throw std::runtime_error( oss.str() );
2567 }
2568
2569 // Resetting an upper element
2570 reset( diag(0,1) );
2571
2572 checkRows ( diag, 3UL );
2573 checkColumns ( diag, 3UL );
2574 checkCapacity( diag, 9UL );
2575 checkNonZeros( diag, 2UL );
2576 checkNonZeros( diag, 0UL, 1UL );
2577 checkNonZeros( diag, 1UL, 0UL );
2578 checkNonZeros( diag, 2UL, 1UL );
2579
2580 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2581 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2582 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2583 std::ostringstream oss;
2584 oss << " Test: " << test_ << "\n"
2585 << " Error: Reset operation failed\n"
2586 << " Details:\n"
2587 << " Result:\n" << diag << "\n"
2588 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2589 throw std::runtime_error( oss.str() );
2590 }
2591
2592 // Resetting row 2
2593 reset( diag, 2UL );
2594
2595 checkRows ( diag, 3UL );
2596 checkColumns ( diag, 3UL );
2597 checkCapacity( diag, 9UL );
2598 checkNonZeros( diag, 1UL );
2599 checkNonZeros( diag, 0UL, 1UL );
2600 checkNonZeros( diag, 1UL, 0UL );
2601 checkNonZeros( diag, 2UL, 0UL );
2602
2603 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2604 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2605 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2606 std::ostringstream oss;
2607 oss << " Test: " << test_ << "\n"
2608 << " Error: Reset operation failed\n"
2609 << " Details:\n"
2610 << " Result:\n" << diag << "\n"
2611 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
2612 throw std::runtime_error( oss.str() );
2613 }
2614
2615 // Resetting the entire matrix
2616 reset( diag );
2617
2618 checkRows ( diag, 3UL );
2619 checkColumns ( diag, 3UL );
2620 checkCapacity( diag, 9UL );
2621 checkNonZeros( diag, 0UL );
2622 checkNonZeros( diag, 0UL, 0UL );
2623 checkNonZeros( diag, 1UL, 0UL );
2624 checkNonZeros( diag, 2UL, 0UL );
2625
2626 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
2627 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2628 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2629 std::ostringstream oss;
2630 oss << " Test: " << test_ << "\n"
2631 << " Error: Reset operation failed\n"
2632 << " Details:\n"
2633 << " Result:\n" << diag << "\n"
2634 << " Expected result:\n( 0 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
2635 throw std::runtime_error( oss.str() );
2636 }
2637 }
2638
2639
2640 //=====================================================================================
2641 // Column-major matrix tests
2642 //=====================================================================================
2643
2644 {
2645 test_ = "Column-major DiagonalMatrix::reset()";
2646
2647 // Initialization check
2648 ODT diag( 3UL );
2649 diag(0,0) = 1;
2650 diag(1,1) = 2;
2651 diag(2,2) = 3;
2652
2653 checkRows ( diag, 3UL );
2654 checkColumns ( diag, 3UL );
2655 checkCapacity( diag, 9UL );
2656 checkNonZeros( diag, 3UL );
2657 checkNonZeros( diag, 0UL, 1UL );
2658 checkNonZeros( diag, 1UL, 1UL );
2659 checkNonZeros( diag, 2UL, 1UL );
2660
2661 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2662 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
2663 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2664 std::ostringstream oss;
2665 oss << " Test: " << test_ << "\n"
2666 << " Error: Initialization failed\n"
2667 << " Details:\n"
2668 << " Result:\n" << diag << "\n"
2669 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
2670 throw std::runtime_error( oss.str() );
2671 }
2672
2673 // Resetting a diagonal element
2674 reset( diag(1,1) );
2675
2676 checkRows ( diag, 3UL );
2677 checkColumns ( diag, 3UL );
2678 checkCapacity( diag, 9UL );
2679 checkNonZeros( diag, 2UL );
2680 checkNonZeros( diag, 0UL, 1UL );
2681 checkNonZeros( diag, 1UL, 0UL );
2682 checkNonZeros( diag, 2UL, 1UL );
2683
2684 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2685 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2686 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2687 std::ostringstream oss;
2688 oss << " Test: " << test_ << "\n"
2689 << " Error: Reset operation failed\n"
2690 << " Details:\n"
2691 << " Result:\n" << diag << "\n"
2692 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2693 throw std::runtime_error( oss.str() );
2694 }
2695
2696 // Resetting a lower element
2697 reset( diag(1,0) );
2698
2699 checkRows ( diag, 3UL );
2700 checkColumns ( diag, 3UL );
2701 checkCapacity( diag, 9UL );
2702 checkNonZeros( diag, 2UL );
2703 checkNonZeros( diag, 0UL, 1UL );
2704 checkNonZeros( diag, 1UL, 0UL );
2705 checkNonZeros( diag, 2UL, 1UL );
2706
2707 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2708 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2709 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2710 std::ostringstream oss;
2711 oss << " Test: " << test_ << "\n"
2712 << " Error: Reset operation failed\n"
2713 << " Details:\n"
2714 << " Result:\n" << diag << "\n"
2715 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2716 throw std::runtime_error( oss.str() );
2717 }
2718
2719 // Resetting an upper element
2720 reset( diag(0,1) );
2721
2722 checkRows ( diag, 3UL );
2723 checkColumns ( diag, 3UL );
2724 checkCapacity( diag, 9UL );
2725 checkNonZeros( diag, 2UL );
2726 checkNonZeros( diag, 0UL, 1UL );
2727 checkNonZeros( diag, 1UL, 0UL );
2728 checkNonZeros( diag, 2UL, 1UL );
2729
2730 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2731 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2732 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2733 std::ostringstream oss;
2734 oss << " Test: " << test_ << "\n"
2735 << " Error: Reset operation failed\n"
2736 << " Details:\n"
2737 << " Result:\n" << diag << "\n"
2738 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2739 throw std::runtime_error( oss.str() );
2740 }
2741
2742 // Resetting row 2
2743 reset( diag, 2UL );
2744
2745 checkRows ( diag, 3UL );
2746 checkColumns ( diag, 3UL );
2747 checkCapacity( diag, 9UL );
2748 checkNonZeros( diag, 1UL );
2749 checkNonZeros( diag, 0UL, 1UL );
2750 checkNonZeros( diag, 1UL, 0UL );
2751 checkNonZeros( diag, 2UL, 0UL );
2752
2753 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2754 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2755 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2756 std::ostringstream oss;
2757 oss << " Test: " << test_ << "\n"
2758 << " Error: Reset operation failed\n"
2759 << " Details:\n"
2760 << " Result:\n" << diag << "\n"
2761 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
2762 throw std::runtime_error( oss.str() );
2763 }
2764
2765 // Resetting the entire matrix
2766 reset( diag );
2767
2768 checkRows ( diag, 3UL );
2769 checkColumns ( diag, 3UL );
2770 checkCapacity( diag, 9UL );
2771 checkNonZeros( diag, 0UL );
2772 checkNonZeros( diag, 0UL, 0UL );
2773 checkNonZeros( diag, 1UL, 0UL );
2774 checkNonZeros( diag, 2UL, 0UL );
2775
2776 if( diag(0,0) != 0 || diag(0,1) != 0 || diag(0,2) != 0 ||
2777 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2778 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
2779 std::ostringstream oss;
2780 oss << " Test: " << test_ << "\n"
2781 << " Error: Reset operation failed\n"
2782 << " Details:\n"
2783 << " Result:\n" << diag << "\n"
2784 << " Expected result:\n( 0 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
2785 throw std::runtime_error( oss.str() );
2786 }
2787 }
2788 }
2789 //*************************************************************************************************
2790
2791
2792 //*************************************************************************************************
2793 /*!\brief Test of the \c clear() member function of the DiagonalMatrix specialization.
2794 //
2795 // \return void
2796 // \exception std::runtime_error Error detected.
2797 //
2798 // This function performs a test of the \c clear() member function of the DiagonalMatrix
2799 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
2800 */
testClear()2801 void DenseTest::testClear()
2802 {
2803 //=====================================================================================
2804 // Row-major matrix tests
2805 //=====================================================================================
2806
2807 {
2808 test_ = "Row-major DiagonalMatrix::clear()";
2809
2810 // Initialization check
2811 DT diag( 3UL );
2812 diag(0,0) = 1;
2813 diag(1,1) = 2;
2814 diag(2,2) = 3;
2815
2816 checkRows ( diag, 3UL );
2817 checkColumns ( diag, 3UL );
2818 checkCapacity( diag, 9UL );
2819 checkNonZeros( diag, 3UL );
2820 checkNonZeros( diag, 0UL, 1UL );
2821 checkNonZeros( diag, 1UL, 1UL );
2822 checkNonZeros( diag, 2UL, 1UL );
2823
2824 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2825 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
2826 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2827 std::ostringstream oss;
2828 oss << " Test: " << test_ << "\n"
2829 << " Error: Initialization failed\n"
2830 << " Details:\n"
2831 << " Result:\n" << diag << "\n"
2832 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
2833 throw std::runtime_error( oss.str() );
2834 }
2835
2836 // Clearing a diagonal element
2837 clear( diag(1,1) );
2838
2839 checkRows ( diag, 3UL );
2840 checkColumns ( diag, 3UL );
2841 checkCapacity( diag, 9UL );
2842 checkNonZeros( diag, 2UL );
2843 checkNonZeros( diag, 0UL, 1UL );
2844 checkNonZeros( diag, 1UL, 0UL );
2845 checkNonZeros( diag, 2UL, 1UL );
2846
2847 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2848 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2849 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2850 std::ostringstream oss;
2851 oss << " Test: " << test_ << "\n"
2852 << " Error: Clear operation failed\n"
2853 << " Details:\n"
2854 << " Result:\n" << diag << "\n"
2855 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2856 throw std::runtime_error( oss.str() );
2857 }
2858
2859 // Clearing a lower element
2860 clear( diag(1,0) );
2861
2862 checkRows ( diag, 3UL );
2863 checkColumns ( diag, 3UL );
2864 checkCapacity( diag, 9UL );
2865 checkNonZeros( diag, 2UL );
2866 checkNonZeros( diag, 0UL, 1UL );
2867 checkNonZeros( diag, 1UL, 0UL );
2868 checkNonZeros( diag, 2UL, 1UL );
2869
2870 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2871 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2872 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2873 std::ostringstream oss;
2874 oss << " Test: " << test_ << "\n"
2875 << " Error: Clear operation failed\n"
2876 << " Details:\n"
2877 << " Result:\n" << diag << "\n"
2878 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2879 throw std::runtime_error( oss.str() );
2880 }
2881
2882 // Clearing an upper element
2883 clear( diag(0,1) );
2884
2885 checkRows ( diag, 3UL );
2886 checkColumns ( diag, 3UL );
2887 checkCapacity( diag, 9UL );
2888 checkNonZeros( diag, 2UL );
2889 checkNonZeros( diag, 0UL, 1UL );
2890 checkNonZeros( diag, 1UL, 0UL );
2891 checkNonZeros( diag, 2UL, 1UL );
2892
2893 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2894 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2895 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2896 std::ostringstream oss;
2897 oss << " Test: " << test_ << "\n"
2898 << " Error: Clear operation failed\n"
2899 << " Details:\n"
2900 << " Result:\n" << diag << "\n"
2901 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2902 throw std::runtime_error( oss.str() );
2903 }
2904
2905 // Clearing the matrix
2906 clear( diag );
2907
2908 checkRows ( diag, 0UL );
2909 checkColumns ( diag, 0UL );
2910 checkNonZeros( diag, 0UL );
2911 }
2912
2913
2914 //=====================================================================================
2915 // Column-major matrix tests
2916 //=====================================================================================
2917
2918 {
2919 test_ = "Column-major DiagonalMatrix::clear()";
2920
2921 // Initialization check
2922 ODT diag( 3UL );
2923 diag(0,0) = 1;
2924 diag(1,1) = 2;
2925 diag(2,2) = 3;
2926
2927 checkRows ( diag, 3UL );
2928 checkColumns ( diag, 3UL );
2929 checkCapacity( diag, 9UL );
2930 checkNonZeros( diag, 3UL );
2931 checkNonZeros( diag, 0UL, 1UL );
2932 checkNonZeros( diag, 1UL, 1UL );
2933 checkNonZeros( diag, 2UL, 1UL );
2934
2935 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2936 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
2937 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2938 std::ostringstream oss;
2939 oss << " Test: " << test_ << "\n"
2940 << " Error: Initialization failed\n"
2941 << " Details:\n"
2942 << " Result:\n" << diag << "\n"
2943 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
2944 throw std::runtime_error( oss.str() );
2945 }
2946
2947 // Clearing a diagonal element
2948 clear( diag(1,1) );
2949
2950 checkRows ( diag, 3UL );
2951 checkColumns ( diag, 3UL );
2952 checkCapacity( diag, 9UL );
2953 checkNonZeros( diag, 2UL );
2954 checkNonZeros( diag, 0UL, 1UL );
2955 checkNonZeros( diag, 1UL, 0UL );
2956 checkNonZeros( diag, 2UL, 1UL );
2957
2958 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2959 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2960 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2961 std::ostringstream oss;
2962 oss << " Test: " << test_ << "\n"
2963 << " Error: Clear operation failed\n"
2964 << " Details:\n"
2965 << " Result:\n" << diag << "\n"
2966 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2967 throw std::runtime_error( oss.str() );
2968 }
2969
2970 // Clearing a lower element
2971 clear( diag(1,0) );
2972
2973 checkRows ( diag, 3UL );
2974 checkColumns ( diag, 3UL );
2975 checkCapacity( diag, 9UL );
2976 checkNonZeros( diag, 2UL );
2977 checkNonZeros( diag, 0UL, 1UL );
2978 checkNonZeros( diag, 1UL, 0UL );
2979 checkNonZeros( diag, 2UL, 1UL );
2980
2981 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
2982 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
2983 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
2984 std::ostringstream oss;
2985 oss << " Test: " << test_ << "\n"
2986 << " Error: Clear operation failed\n"
2987 << " Details:\n"
2988 << " Result:\n" << diag << "\n"
2989 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
2990 throw std::runtime_error( oss.str() );
2991 }
2992
2993 // Clearing an upper element
2994 clear( diag(0,1) );
2995
2996 checkRows ( diag, 3UL );
2997 checkColumns ( diag, 3UL );
2998 checkCapacity( diag, 9UL );
2999 checkNonZeros( diag, 2UL );
3000 checkNonZeros( diag, 0UL, 1UL );
3001 checkNonZeros( diag, 1UL, 0UL );
3002 checkNonZeros( diag, 2UL, 1UL );
3003
3004 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3005 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
3006 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
3007 std::ostringstream oss;
3008 oss << " Test: " << test_ << "\n"
3009 << " Error: Clear operation failed\n"
3010 << " Details:\n"
3011 << " Result:\n" << diag << "\n"
3012 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
3013 throw std::runtime_error( oss.str() );
3014 }
3015
3016 // Clearing the matrix
3017 clear( diag );
3018
3019 checkRows ( diag, 0UL );
3020 checkColumns ( diag, 0UL );
3021 checkNonZeros( diag, 0UL );
3022 }
3023 }
3024 //*************************************************************************************************
3025
3026
3027 //*************************************************************************************************
3028 /*!\brief Test of the \c resize() member function of the DiagonalMatrix specialization.
3029 //
3030 // \return void
3031 // \exception std::runtime_error Error detected.
3032 //
3033 // This function performs a test of the \c resize() member function of the DiagonalMatrix
3034 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
3035 */
testResize()3036 void DenseTest::testResize()
3037 {
3038 //=====================================================================================
3039 // Row-major matrix tests
3040 //=====================================================================================
3041
3042 {
3043 test_ = "Row-major DiagonalMatrix::resize()";
3044
3045 // Initialization check
3046 DT diag;
3047
3048 checkRows ( diag, 0UL );
3049 checkColumns ( diag, 0UL );
3050 checkNonZeros( diag, 0UL );
3051
3052 // Resizing to 2x2
3053 diag.resize( 2UL );
3054
3055 checkRows ( diag, 2UL );
3056 checkColumns ( diag, 2UL );
3057 checkCapacity( diag, 4UL );
3058
3059 if( diag(0,1) != 0 || diag(1,0) != 0 ) {
3060 std::ostringstream oss;
3061 oss << " Test: " << test_ << "\n"
3062 << " Error: Resizing the matrix failed\n"
3063 << " Details:\n"
3064 << " Result:\n" << diag << "\n"
3065 << " Expected result:\n( x 0 )\n( 0 x )\n";
3066 throw std::runtime_error( oss.str() );
3067 }
3068
3069 // Resizing to 4x4 and preserving the elements
3070 diag(0,0) = 1;
3071 diag(1,1) = 2;
3072 diag.resize( 4UL, true );
3073
3074 checkRows ( diag, 4UL );
3075 checkColumns ( diag, 4UL );
3076 checkCapacity( diag, 16UL );
3077
3078 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
3079 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
3080 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,3) != 0 ||
3081 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 ) {
3082 std::ostringstream oss;
3083 oss << " Test: " << test_ << "\n"
3084 << " Error: Resizing the matrix failed\n"
3085 << " Details:\n"
3086 << " Result:\n" << diag << "\n"
3087 << " Expected result:\n( 1 0 0 0 )\n( 0 2 0 0 )\n( 0 0 x 0 )\n( 0 0 0 x )\n";
3088 throw std::runtime_error( oss.str() );
3089 }
3090
3091 // Resizing to 2x2
3092 diag(2,2) = 3;
3093 diag.resize( 2UL );
3094
3095 checkRows ( diag, 2UL );
3096 checkColumns ( diag, 2UL );
3097 checkCapacity( diag, 4UL );
3098 checkNonZeros( diag, 2UL );
3099 checkNonZeros( diag, 0UL, 1UL );
3100 checkNonZeros( diag, 1UL, 1UL );
3101
3102 if( diag(0,0) != 1 || diag(0,1) != 0 ||
3103 diag(1,0) != 0 || diag(1,1) != 2 ) {
3104 std::ostringstream oss;
3105 oss << " Test: " << test_ << "\n"
3106 << " Error: Resizing the matrix failed\n"
3107 << " Details:\n"
3108 << " Result:\n" << diag << "\n"
3109 << " Expected result:\n( 1 0 )\n( 0 2 )\n";
3110 throw std::runtime_error( oss.str() );
3111 }
3112
3113 // Resizing to 0x0
3114 diag.resize( 0UL );
3115
3116 checkRows ( diag, 0UL );
3117 checkColumns ( diag, 0UL );
3118 checkNonZeros( diag, 0UL );
3119 }
3120
3121
3122 //=====================================================================================
3123 // Column-major matrix tests
3124 //=====================================================================================
3125
3126 {
3127 test_ = "Column-major DiagonalMatrix::resize()";
3128
3129 // Initialization check
3130 ODT diag;
3131
3132 checkRows ( diag, 0UL );
3133 checkColumns ( diag, 0UL );
3134 checkNonZeros( diag, 0UL );
3135
3136 // Resizing to 2x2
3137 diag.resize( 2UL );
3138
3139 checkRows ( diag, 2UL );
3140 checkColumns ( diag, 2UL );
3141 checkCapacity( diag, 4UL );
3142
3143 if( diag(0,1) != 0 || diag(1,0) != 0 ) {
3144 std::ostringstream oss;
3145 oss << " Test: " << test_ << "\n"
3146 << " Error: Resizing the matrix failed\n"
3147 << " Details:\n"
3148 << " Result:\n" << diag << "\n"
3149 << " Expected result:\n( x 0 )\n( 0 x )\n";
3150 throw std::runtime_error( oss.str() );
3151 }
3152
3153 // Resizing to 4x4 and preserving the elements
3154 diag(0,0) = 1;
3155 diag(1,1) = 2;
3156 diag.resize( 4UL, true );
3157
3158 checkRows ( diag, 4UL );
3159 checkColumns ( diag, 4UL );
3160 checkCapacity( diag, 16UL );
3161
3162 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
3163 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
3164 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,3) != 0 ||
3165 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 ) {
3166 std::ostringstream oss;
3167 oss << " Test: " << test_ << "\n"
3168 << " Error: Resizing the matrix failed\n"
3169 << " Details:\n"
3170 << " Result:\n" << diag << "\n"
3171 << " Expected result:\n( 1 0 0 0 )\n( 0 2 0 0 )\n( 0 0 x 0 )\n( 0 0 0 x )\n";
3172 throw std::runtime_error( oss.str() );
3173 }
3174
3175 // Resizing to 2x2
3176 diag(2,2) = 3;
3177 diag.resize( 2UL );
3178
3179 checkRows ( diag, 2UL );
3180 checkColumns ( diag, 2UL );
3181 checkCapacity( diag, 4UL );
3182 checkNonZeros( diag, 2UL );
3183 checkNonZeros( diag, 0UL, 1UL );
3184 checkNonZeros( diag, 1UL, 1UL );
3185
3186 if( diag(0,0) != 1 || diag(0,1) != 0 ||
3187 diag(1,0) != 0 || diag(1,1) != 2 ) {
3188 std::ostringstream oss;
3189 oss << " Test: " << test_ << "\n"
3190 << " Error: Resizing the matrix failed\n"
3191 << " Details:\n"
3192 << " Result:\n" << diag << "\n"
3193 << " Expected result:\n( 1 0 )\n( 0 2 )\n";
3194 throw std::runtime_error( oss.str() );
3195 }
3196
3197 // Resizing to 0x0
3198 diag.resize( 0UL );
3199
3200 checkRows ( diag, 0UL );
3201 checkColumns ( diag, 0UL );
3202 checkNonZeros( diag, 0UL );
3203 }
3204 }
3205 //*************************************************************************************************
3206
3207
3208 //*************************************************************************************************
3209 /*!\brief Test of the \c extend() member function of the DiagonalMatrix specialization.
3210 //
3211 // \return void
3212 // \exception std::runtime_error Error detected.
3213 //
3214 // This function performs a test of the \c extend() member function of the DiagonalMatrix
3215 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
3216 */
testExtend()3217 void DenseTest::testExtend()
3218 {
3219 //=====================================================================================
3220 // Row-major matrix tests
3221 //=====================================================================================
3222
3223 {
3224 test_ = "Row-major DiagonalMatrix::extend()";
3225
3226 // Initialization check
3227 DT diag;
3228
3229 checkRows ( diag, 0UL );
3230 checkColumns ( diag, 0UL );
3231 checkNonZeros( diag, 0UL );
3232
3233 // Extending the size of the matrix to 2x2
3234 diag.extend( 2UL );
3235
3236 checkRows ( diag, 2UL );
3237 checkColumns ( diag, 2UL );
3238 checkCapacity( diag, 4UL );
3239
3240 if( diag(0,1) != 0 || diag(1,0) != 0 ) {
3241 std::ostringstream oss;
3242 oss << " Test: " << test_ << "\n"
3243 << " Error: Extending the matrix failed\n"
3244 << " Details:\n"
3245 << " Result:\n" << diag << "\n"
3246 << " Expected result:\n( x 0 )\n( 0 x )\n";
3247 throw std::runtime_error( oss.str() );
3248 }
3249
3250 // Extending to 4x4 and preserving the elements
3251 diag(0,0) = 1;
3252 diag(1,1) = 2;
3253 diag.extend( 2UL, true );
3254
3255 checkRows ( diag, 4UL );
3256 checkColumns ( diag, 4UL );
3257 checkCapacity( diag, 16UL );
3258
3259 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
3260 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
3261 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,3) != 0 ||
3262 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 ) {
3263 std::ostringstream oss;
3264 oss << " Test: " << test_ << "\n"
3265 << " Error: Extending the matrix failed\n"
3266 << " Details:\n"
3267 << " Result:\n" << diag << "\n"
3268 << " Expected result:\n( 1 0 0 0 )\n( 0 2 0 0 )\n( 0 0 x 0 )\n( 0 0 0 x )\n";
3269 throw std::runtime_error( oss.str() );
3270 }
3271 }
3272
3273
3274 //=====================================================================================
3275 // Column-major matrix tests
3276 //=====================================================================================
3277
3278 {
3279 test_ = "Column-major DiagonalMatrix::extend()";
3280
3281 // Initialization check
3282 ODT diag;
3283
3284 checkRows ( diag, 0UL );
3285 checkColumns ( diag, 0UL );
3286 checkNonZeros( diag, 0UL );
3287
3288 // Extending the size of the matrix to 2x2
3289 diag.extend( 2UL );
3290
3291 checkRows ( diag, 2UL );
3292 checkColumns ( diag, 2UL );
3293 checkCapacity( diag, 4UL );
3294
3295 if( diag(0,1) != 0 || diag(1,0) != 0 ) {
3296 std::ostringstream oss;
3297 oss << " Test: " << test_ << "\n"
3298 << " Error: Extending the matrix failed\n"
3299 << " Details:\n"
3300 << " Result:\n" << diag << "\n"
3301 << " Expected result:\n( x 0 )\n( 0 x )\n";
3302 throw std::runtime_error( oss.str() );
3303 }
3304
3305 // Extending to 4x4 and preserving the elements
3306 diag(0,0) = 1;
3307 diag(1,1) = 2;
3308 diag.extend( 2UL, true );
3309
3310 checkRows ( diag, 4UL );
3311 checkColumns ( diag, 4UL );
3312 checkCapacity( diag, 16UL );
3313
3314 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
3315 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
3316 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,3) != 0 ||
3317 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 ) {
3318 std::ostringstream oss;
3319 oss << " Test: " << test_ << "\n"
3320 << " Error: Extending the matrix failed\n"
3321 << " Details:\n"
3322 << " Result:\n" << diag << "\n"
3323 << " Expected result:\n( 1 0 0 0 )\n( 0 2 0 0 )\n( 0 0 x 0 )\n( 0 0 0 x )\n";
3324 throw std::runtime_error( oss.str() );
3325 }
3326 }
3327 }
3328 //*************************************************************************************************
3329
3330
3331 //*************************************************************************************************
3332 /*!\brief Test of the \c reserve() member function of the DiagonalMatrix specialization.
3333 //
3334 // \return void
3335 // \exception std::runtime_error Error detected.
3336 //
3337 // This function performs a test of the \c reserve() member function of the DiagonalMatrix
3338 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
3339 */
testReserve()3340 void DenseTest::testReserve()
3341 {
3342 //=====================================================================================
3343 // Row-major matrix tests
3344 //=====================================================================================
3345
3346 {
3347 test_ = "Row-major DiagonalMatrix::reserve()";
3348
3349 // Initialization check
3350 DT diag;
3351
3352 checkRows ( diag, 0UL );
3353 checkColumns ( diag, 0UL );
3354 checkNonZeros( diag, 0UL );
3355
3356 // Increasing the capacity of the matrix
3357 diag.reserve( 10UL );
3358
3359 checkRows ( diag, 0UL );
3360 checkColumns ( diag, 0UL );
3361 checkCapacity( diag, 10UL );
3362 checkNonZeros( diag, 0UL );
3363
3364 // Further increasing the capacity of the matrix
3365 diag.reserve( 20UL );
3366
3367 checkRows ( diag, 0UL );
3368 checkColumns ( diag, 0UL );
3369 checkCapacity( diag, 20UL );
3370 checkNonZeros( diag, 0UL );
3371 }
3372
3373
3374 //=====================================================================================
3375 // Column-major matrix tests
3376 //=====================================================================================
3377
3378 {
3379 test_ = "Column-major DiagonalMatrix::reserve()";
3380
3381 // Initialization check
3382 ODT diag;
3383
3384 checkRows ( diag, 0UL );
3385 checkColumns ( diag, 0UL );
3386 checkNonZeros( diag, 0UL );
3387
3388 // Increasing the capacity of the matrix
3389 diag.reserve( 10UL );
3390
3391 checkRows ( diag, 0UL );
3392 checkColumns ( diag, 0UL );
3393 checkCapacity( diag, 10UL );
3394 checkNonZeros( diag, 0UL );
3395
3396 // Further increasing the capacity of the matrix
3397 diag.reserve( 20UL );
3398
3399 checkRows ( diag, 0UL );
3400 checkColumns ( diag, 0UL );
3401 checkCapacity( diag, 20UL );
3402 checkNonZeros( diag, 0UL );
3403 }
3404 }
3405 //*************************************************************************************************
3406
3407
3408 //*************************************************************************************************
3409 /*!\brief Test of the \c shrinkToFit() member function of the DiagonalMatrix specialization.
3410 //
3411 // \return void
3412 // \exception std::runtime_error Error detected.
3413 //
3414 // This function performs a test of the \c shrinkToFit() member function of the DiagonalMatrix
3415 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
3416 */
testShrinkToFit()3417 void DenseTest::testShrinkToFit()
3418 {
3419 //=====================================================================================
3420 // Row-major matrix tests
3421 //=====================================================================================
3422
3423 {
3424 test_ = "Row-major DiagonalMatrix::shrinkToFit()";
3425
3426 // Shrinking a matrix without excessive capacity
3427 {
3428 DT diag{ { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } };
3429
3430 diag.shrinkToFit();
3431
3432 checkRows ( diag, 3UL );
3433 checkColumns ( diag, 3UL );
3434 checkCapacity( diag, 9UL );
3435 checkNonZeros( diag, 3UL );
3436 checkNonZeros( diag, 0UL, 1UL );
3437 checkNonZeros( diag, 1UL, 1UL );
3438 checkNonZeros( diag, 2UL, 1UL );
3439
3440 if( diag.capacity() != diag.rows() * diag.spacing() ) {
3441 std::ostringstream oss;
3442 oss << " Test: " << test_ << "\n"
3443 << " Error: Shrinking the matrix failed\n"
3444 << " Details:\n"
3445 << " Capacity : " << diag.capacity() << "\n"
3446 << " Expected capacity: " << ( diag.rows() * diag.spacing() ) << "\n";
3447 throw std::runtime_error( oss.str() );
3448 }
3449
3450 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3451 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
3452 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
3453 std::ostringstream oss;
3454 oss << " Test: " << test_ << "\n"
3455 << " Error: Shrinking the matrix failed\n"
3456 << " Details:\n"
3457 << " Result:\n" << diag << "\n"
3458 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
3459 throw std::runtime_error( oss.str() );
3460 }
3461 }
3462
3463 // Shrinking a matrix with excessive capacity
3464 {
3465 DT diag{ { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } };
3466 diag.reserve( 100UL );
3467
3468 diag.shrinkToFit();
3469
3470 checkRows ( diag, 3UL );
3471 checkColumns ( diag, 3UL );
3472 checkCapacity( diag, 9UL );
3473 checkNonZeros( diag, 3UL );
3474 checkNonZeros( diag, 0UL, 1UL );
3475 checkNonZeros( diag, 1UL, 1UL );
3476 checkNonZeros( diag, 2UL, 1UL );
3477
3478 if( diag.capacity() != diag.rows() * diag.spacing() ) {
3479 std::ostringstream oss;
3480 oss << " Test: " << test_ << "\n"
3481 << " Error: Shrinking the matrix failed\n"
3482 << " Details:\n"
3483 << " Capacity : " << diag.capacity() << "\n"
3484 << " Expected capacity: " << ( diag.rows() * diag.spacing() ) << "\n";
3485 throw std::runtime_error( oss.str() );
3486 }
3487
3488 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3489 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
3490 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
3491 std::ostringstream oss;
3492 oss << " Test: " << test_ << "\n"
3493 << " Error: Shrinking the matrix failed\n"
3494 << " Details:\n"
3495 << " Result:\n" << diag << "\n"
3496 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
3497 throw std::runtime_error( oss.str() );
3498 }
3499 }
3500 }
3501
3502
3503 //=====================================================================================
3504 // Column-major matrix tests
3505 //=====================================================================================
3506
3507 {
3508 test_ = "Column-major DiagonalMatrix::shrinkToFit()";
3509
3510 // Shrinking a matrix without excessive capacity
3511 {
3512 ODT diag{ { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } };
3513
3514 diag.shrinkToFit();
3515
3516 checkRows ( diag, 3UL );
3517 checkColumns ( diag, 3UL );
3518 checkCapacity( diag, 9UL );
3519 checkNonZeros( diag, 3UL );
3520 checkNonZeros( diag, 0UL, 1UL );
3521 checkNonZeros( diag, 1UL, 1UL );
3522 checkNonZeros( diag, 2UL, 1UL );
3523
3524 if( diag.capacity() != diag.rows() * diag.spacing() ) {
3525 std::ostringstream oss;
3526 oss << " Test: " << test_ << "\n"
3527 << " Error: Shrinking the matrix failed\n"
3528 << " Details:\n"
3529 << " Capacity : " << diag.capacity() << "\n"
3530 << " Expected capacity: " << ( diag.rows() * diag.spacing() ) << "\n";
3531 throw std::runtime_error( oss.str() );
3532 }
3533
3534 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3535 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
3536 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
3537 std::ostringstream oss;
3538 oss << " Test: " << test_ << "\n"
3539 << " Error: Shrinking the matrix failed\n"
3540 << " Details:\n"
3541 << " Result:\n" << diag << "\n"
3542 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
3543 throw std::runtime_error( oss.str() );
3544 }
3545 }
3546
3547 // Shrinking a matrix with excessive capacity
3548 {
3549 ODT diag{ { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } };
3550 diag.reserve( 100UL );
3551
3552 diag.shrinkToFit();
3553
3554 checkRows ( diag, 3UL );
3555 checkColumns ( diag, 3UL );
3556 checkCapacity( diag, 9UL );
3557 checkNonZeros( diag, 3UL );
3558 checkNonZeros( diag, 0UL, 1UL );
3559 checkNonZeros( diag, 1UL, 1UL );
3560 checkNonZeros( diag, 2UL, 1UL );
3561
3562 if( diag.capacity() != diag.rows() * diag.spacing() ) {
3563 std::ostringstream oss;
3564 oss << " Test: " << test_ << "\n"
3565 << " Error: Shrinking the matrix failed\n"
3566 << " Details:\n"
3567 << " Capacity : " << diag.capacity() << "\n"
3568 << " Expected capacity: " << ( diag.rows() * diag.spacing() ) << "\n";
3569 throw std::runtime_error( oss.str() );
3570 }
3571
3572 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3573 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 ||
3574 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
3575 std::ostringstream oss;
3576 oss << " Test: " << test_ << "\n"
3577 << " Error: Shrinking the matrix failed\n"
3578 << " Details:\n"
3579 << " Result:\n" << diag << "\n"
3580 << " Expected result:\n( 1 0 0 )\n( 0 2 0 )\n( 0 0 3 )\n";
3581 throw std::runtime_error( oss.str() );
3582 }
3583 }
3584 }
3585 }
3586 //*************************************************************************************************
3587
3588
3589 //*************************************************************************************************
3590 /*!\brief Test of the \c swap() functionality of the DiagonalMatrix specialization.
3591 //
3592 // \return void
3593 // \exception std::runtime_error Error detected.
3594 //
3595 // This function performs a test of the \c swap() function of the DiagonalMatrix specialization.
3596 // In case an error is detected, a \a std::runtime_error exception is thrown.
3597 */
testSwap()3598 void DenseTest::testSwap()
3599 {
3600 //=====================================================================================
3601 // Row-major matrix tests
3602 //=====================================================================================
3603
3604 {
3605 test_ = "Row-major DiagonalMatrix swap";
3606
3607 DT diag1( 2UL );
3608 diag1(0,0) = 1;
3609 diag1(1,1) = 2;
3610
3611 DT diag2( 3UL );
3612 diag2(0,0) = 3;
3613 diag2(1,1) = 4;
3614 diag2(2,2) = 5;
3615
3616 swap( diag1, diag2 );
3617
3618 checkRows ( diag1, 3UL );
3619 checkColumns ( diag1, 3UL );
3620 checkCapacity( diag1, 9UL );
3621 checkNonZeros( diag1, 3UL );
3622 checkNonZeros( diag1, 0UL, 1UL );
3623 checkNonZeros( diag1, 1UL, 1UL );
3624 checkNonZeros( diag1, 2UL, 1UL );
3625
3626 if( diag1(0,0) != 3 || diag1(0,1) != 0 || diag1(0,2) != 0 ||
3627 diag1(1,0) != 0 || diag1(1,1) != 4 || diag1(1,2) != 0 ||
3628 diag1(2,0) != 0 || diag1(2,1) != 0 || diag1(2,2) != 5 ) {
3629 std::ostringstream oss;
3630 oss << " Test: " << test_ << "\n"
3631 << " Error: Swapping the first matrix failed\n"
3632 << " Details:\n"
3633 << " Result:\n" << diag1 << "\n"
3634 << " Expected result:\n( 3 0 0 )\n( 0 4 0 )\n( 0 0 5 )\n";
3635 throw std::runtime_error( oss.str() );
3636 }
3637
3638 checkRows ( diag2, 2UL );
3639 checkColumns ( diag2, 2UL );
3640 checkCapacity( diag2, 4UL );
3641 checkNonZeros( diag2, 2UL );
3642 checkNonZeros( diag2, 0UL, 1UL );
3643 checkNonZeros( diag2, 1UL, 1UL );
3644
3645 if( diag2(0,0) != 1 || diag2(0,1) != 0 || diag2(1,0) != 0 || diag2(1,1) != 2 ) {
3646 std::ostringstream oss;
3647 oss << " Test: " << test_ << "\n"
3648 << " Error: Swapping the second matrix failed\n"
3649 << " Details:\n"
3650 << " Result:\n" << diag2 << "\n"
3651 << " Expected result:\n( 1 0 )\n( 0 2 )\n";
3652 throw std::runtime_error( oss.str() );
3653 }
3654 }
3655
3656
3657 //=====================================================================================
3658 // Column-major matrix tests
3659 //=====================================================================================
3660
3661 {
3662 test_ = "Column-major DiagonalMatrix swap";
3663
3664 ODT diag1( 2UL );
3665 diag1(0,0) = 1;
3666 diag1(1,1) = 2;
3667
3668 ODT diag2( 3UL );
3669 diag2(0,0) = 3;
3670 diag2(1,1) = 4;
3671 diag2(2,2) = 5;
3672
3673 swap( diag1, diag2 );
3674
3675 checkRows ( diag1, 3UL );
3676 checkColumns ( diag1, 3UL );
3677 checkCapacity( diag1, 9UL );
3678 checkNonZeros( diag1, 3UL );
3679 checkNonZeros( diag1, 0UL, 1UL );
3680 checkNonZeros( diag1, 1UL, 1UL );
3681 checkNonZeros( diag1, 2UL, 1UL );
3682
3683 if( diag1(0,0) != 3 || diag1(0,1) != 0 || diag1(0,2) != 0 ||
3684 diag1(1,0) != 0 || diag1(1,1) != 4 || diag1(1,2) != 0 ||
3685 diag1(2,0) != 0 || diag1(2,1) != 0 || diag1(2,2) != 5 ) {
3686 std::ostringstream oss;
3687 oss << " Test: " << test_ << "\n"
3688 << " Error: Swapping the first matrix failed\n"
3689 << " Details:\n"
3690 << " Result:\n" << diag1 << "\n"
3691 << " Expected result:\n( 3 0 0 )\n( 0 4 0 )\n( 0 0 5 )\n";
3692 throw std::runtime_error( oss.str() );
3693 }
3694
3695 checkRows ( diag2, 2UL );
3696 checkColumns ( diag2, 2UL );
3697 checkCapacity( diag2, 4UL );
3698 checkNonZeros( diag2, 2UL );
3699 checkNonZeros( diag2, 0UL, 1UL );
3700 checkNonZeros( diag2, 1UL, 1UL );
3701
3702 if( diag2(0,0) != 1 || diag2(0,1) != 0 || diag2(1,0) != 0 || diag2(1,1) != 2 ) {
3703 std::ostringstream oss;
3704 oss << " Test: " << test_ << "\n"
3705 << " Error: Swapping the second matrix failed\n"
3706 << " Details:\n"
3707 << " Result:\n" << diag2 << "\n"
3708 << " Expected result:\n( 1 0 )\n( 0 2 )\n";
3709 throw std::runtime_error( oss.str() );
3710 }
3711 }
3712 }
3713 //*************************************************************************************************
3714
3715
3716 //*************************************************************************************************
3717 /*!\brief Test of the \c isDefault() function with the DiagonalMatrix specialization.
3718 //
3719 // \return void
3720 // \exception std::runtime_error Error detected.
3721 //
3722 // This function performs a test of the \c isDefault() function with the DiagonalMatrix
3723 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
3724 */
testIsDefault()3725 void DenseTest::testIsDefault()
3726 {
3727 //=====================================================================================
3728 // Row-major matrix tests
3729 //=====================================================================================
3730
3731 {
3732 test_ = "Row-major isDefault() function";
3733
3734 // isDefault with 0x0 matrix
3735 {
3736 DT diag;
3737
3738 if( isDefault( diag ) != true ) {
3739 std::ostringstream oss;
3740 oss << " Test: " << test_ << "\n"
3741 << " Error: Invalid isDefault evaluation\n"
3742 << " Details:\n"
3743 << " Matrix:\n" << diag << "\n";
3744 throw std::runtime_error( oss.str() );
3745 }
3746 }
3747
3748 // isDefault with default matrix
3749 {
3750 DT diag( 3UL );
3751
3752 if( isDefault( diag(1,1) ) != true ) {
3753 std::ostringstream oss;
3754 oss << " Test: " << test_ << "\n"
3755 << " Error: Invalid isDefault evaluation\n"
3756 << " Details:\n"
3757 << " Matrix element:\n" << diag(1,1) << "\n";
3758 throw std::runtime_error( oss.str() );
3759 }
3760
3761 if( isDefault( diag ) != false ) {
3762 std::ostringstream oss;
3763 oss << " Test: " << test_ << "\n"
3764 << " Error: Invalid isDefault evaluation\n"
3765 << " Details:\n"
3766 << " Matrix:\n" << diag << "\n";
3767 throw std::runtime_error( oss.str() );
3768 }
3769 }
3770
3771 // isDefault with non-default matrix
3772 {
3773 DT diag( 3UL );
3774 diag(1,1) = 1;
3775
3776 if( isDefault( diag(1,1) ) != false ) {
3777 std::ostringstream oss;
3778 oss << " Test: " << test_ << "\n"
3779 << " Error: Invalid isDefault evaluation\n"
3780 << " Details:\n"
3781 << " Matrix element:\n" << diag(1,1) << "\n";
3782 throw std::runtime_error( oss.str() );
3783 }
3784
3785 if( isDefault( diag ) != false ) {
3786 std::ostringstream oss;
3787 oss << " Test: " << test_ << "\n"
3788 << " Error: Invalid isDefault evaluation\n"
3789 << " Details:\n"
3790 << " Matrix:\n" << diag << "\n";
3791 throw std::runtime_error( oss.str() );
3792 }
3793 }
3794 }
3795
3796
3797 //=====================================================================================
3798 // Column-major matrix tests
3799 //=====================================================================================
3800
3801 {
3802 test_ = "Column-major isDefault() function";
3803
3804 // isDefault with 0x0 matrix
3805 {
3806 ODT diag;
3807
3808 if( isDefault( diag ) != true ) {
3809 std::ostringstream oss;
3810 oss << " Test: " << test_ << "\n"
3811 << " Error: Invalid isDefault evaluation\n"
3812 << " Details:\n"
3813 << " Matrix:\n" << diag << "\n";
3814 throw std::runtime_error( oss.str() );
3815 }
3816 }
3817
3818 // isDefault with default matrix
3819 {
3820 ODT diag( 3UL );
3821
3822 if( isDefault( diag(1,1) ) != true ) {
3823 std::ostringstream oss;
3824 oss << " Test: " << test_ << "\n"
3825 << " Error: Invalid isDefault evaluation\n"
3826 << " Details:\n"
3827 << " Matrix element:\n" << diag(1,1) << "\n";
3828 throw std::runtime_error( oss.str() );
3829 }
3830
3831 if( isDefault( diag ) != false ) {
3832 std::ostringstream oss;
3833 oss << " Test: " << test_ << "\n"
3834 << " Error: Invalid isDefault evaluation\n"
3835 << " Details:\n"
3836 << " Matrix:\n" << diag << "\n";
3837 throw std::runtime_error( oss.str() );
3838 }
3839 }
3840
3841 // isDefault with non-default matrix
3842 {
3843 ODT diag( 3UL );
3844 diag(1,1) = 1;
3845
3846 if( isDefault( diag(1,1) ) != false ) {
3847 std::ostringstream oss;
3848 oss << " Test: " << test_ << "\n"
3849 << " Error: Invalid isDefault evaluation\n"
3850 << " Details:\n"
3851 << " Matrix element:\n" << diag(1,1) << "\n";
3852 throw std::runtime_error( oss.str() );
3853 }
3854
3855 if( isDefault( diag ) != false ) {
3856 std::ostringstream oss;
3857 oss << " Test: " << test_ << "\n"
3858 << " Error: Invalid isDefault evaluation\n"
3859 << " Details:\n"
3860 << " Matrix:\n" << diag << "\n";
3861 throw std::runtime_error( oss.str() );
3862 }
3863 }
3864 }
3865 }
3866 //*************************************************************************************************
3867
3868
3869 //*************************************************************************************************
3870 /*!\brief Test of the \c submatrix() function with the DiagonalMatrix specialization.
3871 //
3872 // \return void
3873 // \exception std::runtime_error Error detected.
3874 //
3875 // This function performs a test of the \c submatrix() function with the DiagonalMatrix
3876 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
3877 */
testSubmatrix()3878 void DenseTest::testSubmatrix()
3879 {
3880 //=====================================================================================
3881 // Row-major general tests
3882 //=====================================================================================
3883
3884 {
3885 test_ = "Row-major submatrix() function";
3886
3887 using SMT = blaze::Submatrix<DT>;
3888
3889 DT diag( 3UL );
3890 diag(0,0) = 1;
3891 diag(1,1) = 2;
3892 diag(2,2) = 3;
3893
3894 SMT sm = submatrix( diag, 1UL, 1UL, 2UL, 2UL );
3895
3896 if( sm(1,1) != 3 ) {
3897 std::ostringstream oss;
3898 oss << " Test: " << test_ << "\n"
3899 << " Error: Function call operator access failed\n"
3900 << " Details:\n"
3901 << " Result: " << sm(1,1) << "\n"
3902 << " Expected result: 3\n";
3903 throw std::runtime_error( oss.str() );
3904 }
3905
3906 SMT::Iterator it = sm.begin(0UL);
3907
3908 if( it == sm.end(0UL) || *it != 2 ) {
3909 std::ostringstream oss;
3910 oss << " Test: " << test_ << "\n"
3911 << " Error: Iterator access failed\n"
3912 << " Details:\n"
3913 << " Result: " << *it << "\n"
3914 << " Expected result: 2\n";
3915 throw std::runtime_error( oss.str() );
3916 }
3917
3918 sm(0,0) = -5;
3919
3920 if( sm(0,0) != -5 || sm(0,1) != 0 ||
3921 sm(1,0) != 0 || sm(1,1) != 3 ) {
3922 std::ostringstream oss;
3923 oss << " Test: " << test_ << "\n"
3924 << " Error: Submatrix access failed\n"
3925 << " Details:\n"
3926 << " Result:\n" << sm << "\n"
3927 << " Expected result:\n( -5 0 )\n( 0 3 )\n";
3928 throw std::runtime_error( oss.str() );
3929 }
3930
3931 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3932 diag(1,0) != 0 || diag(1,1) != -5 || diag(1,2) != 0 ||
3933 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
3934 std::ostringstream oss;
3935 oss << " Test: " << test_ << "\n"
3936 << " Error: Submatrix access failed\n"
3937 << " Details:\n"
3938 << " Result:\n" << diag << "\n"
3939 << " Expected result:\n( 1 0 0 )\n( 0 -5 0 )\n( 0 0 3 )\n";
3940 throw std::runtime_error( oss.str() );
3941 }
3942
3943 reset( sm );
3944
3945 if( sm(0,0) != 0 || sm(0,1) != 0 ||
3946 sm(1,0) != 0 || sm(1,1) != 0 ) {
3947 std::ostringstream oss;
3948 oss << " Test: " << test_ << "\n"
3949 << " Error: Submatrix reset failed\n"
3950 << " Details:\n"
3951 << " Result:\n" << sm << "\n"
3952 << " Expected result:\n( 0 0 )\n( 0 0 )\n";
3953 throw std::runtime_error( oss.str() );
3954 }
3955
3956 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
3957 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
3958 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
3959 std::ostringstream oss;
3960 oss << " Test: " << test_ << "\n"
3961 << " Error: Submatrix reset failed\n"
3962 << " Details:\n"
3963 << " Result:\n" << diag << "\n"
3964 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
3965 throw std::runtime_error( oss.str() );
3966 }
3967 }
3968
3969
3970 //=====================================================================================
3971 // Row-major scalar assignment
3972 //=====================================================================================
3973
3974 // ( 1 0 0 0 ) ( 1 0 0 0 )
3975 // ( 0 2 0 0 ) => ( 0 12 0 0 )
3976 // ( 0 0 3 0 ) ( 0 0 12 0 )
3977 // ( 0 0 0 4 ) ( 0 0 0 4 )
3978 {
3979 test_ = "Row-major submatrix() function (scalar assignment test 1)";
3980
3981 using SMT = blaze::Submatrix<DT>;
3982
3983 DT diag( 4UL );
3984 diag(0,0) = 1;
3985 diag(1,1) = 2;
3986 diag(2,2) = 3;
3987 diag(3,3) = 4;
3988
3989 SMT sm = submatrix( diag, 0UL, 1UL, 4UL, 2UL );
3990 sm = 12;
3991
3992 checkRows ( diag, 4UL );
3993 checkColumns ( diag, 4UL );
3994 checkNonZeros( diag, 4UL );
3995 checkNonZeros( diag, 0UL, 1UL );
3996 checkNonZeros( diag, 1UL, 1UL );
3997 checkNonZeros( diag, 2UL, 1UL );
3998 checkNonZeros( diag, 3UL, 1UL );
3999
4000 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4001 sm(1,0) != 12 || sm(1,1) != 0 ||
4002 sm(2,0) != 0 || sm(2,1) != 12 ||
4003 sm(3,0) != 0 || sm(3,1) != 0 ) {
4004 std::ostringstream oss;
4005 oss << " Test: " << test_ << "\n"
4006 << " Error: Assignment to submatrix failed\n"
4007 << " Details:\n"
4008 << " Result:\n" << sm << "\n"
4009 << " Expected result:\n( 0 0 )\n( 12 0 )\n( 0 12 )\n( 0 0 )\n";
4010 throw std::runtime_error( oss.str() );
4011 }
4012
4013 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4014 diag(1,0) != 0 || diag(1,1) != 12 || diag(1,2) != 0 || diag(1,3) != 0 ||
4015 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 12 || diag(2,3) != 0 ||
4016 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4017 std::ostringstream oss;
4018 oss << " Test: " << test_ << "\n"
4019 << " Error: Assignment to submatrix failed\n"
4020 << " Details:\n"
4021 << " Result:\n" << diag << "\n"
4022 << " Expected result:\n( 1 0 0 0 )\n"
4023 "( 0 12 0 0 )\n"
4024 "( 0 0 12 0 )\n"
4025 "( 0 0 0 4 )\n";
4026 throw std::runtime_error( oss.str() );
4027 }
4028 }
4029
4030 // ( 1 0 0 0 ) ( 1 0 0 0 )
4031 // ( 0 2 0 0 ) => ( 0 12 0 0 )
4032 // ( 0 0 3 0 ) ( 0 0 12 0 )
4033 // ( 0 0 0 4 ) ( 0 0 0 4 )
4034 {
4035 test_ = "Row-major submatrix() function (scalar assignment test 2)";
4036
4037 using SMT = blaze::Submatrix<DT>;
4038
4039 DT diag( 4UL );
4040 diag(0,0) = 1;
4041 diag(1,1) = 2;
4042 diag(2,2) = 3;
4043 diag(3,3) = 4;
4044
4045 SMT sm = submatrix( diag, 1UL, 0UL, 2UL, 4UL );
4046 sm = 12;
4047
4048 checkRows ( diag, 4UL );
4049 checkColumns ( diag, 4UL );
4050 checkNonZeros( diag, 4UL );
4051 checkNonZeros( diag, 0UL, 1UL );
4052 checkNonZeros( diag, 1UL, 1UL );
4053 checkNonZeros( diag, 2UL, 1UL );
4054 checkNonZeros( diag, 3UL, 1UL );
4055
4056 if( sm(0,0) != 0 || sm(0,1) != 12 || sm(0,2) != 0 || sm(0,3) != 0 ||
4057 sm(1,0) != 0 || sm(1,1) != 0 || sm(1,2) != 12 || sm(1,3) != 0 ) {
4058 std::ostringstream oss;
4059 oss << " Test: " << test_ << "\n"
4060 << " Error: Assignment to submatrix failed\n"
4061 << " Details:\n"
4062 << " Result:\n" << sm << "\n"
4063 << " Expected result:\n( 0 12 0 0 )\n( 0 0 12 0 )\n";
4064 throw std::runtime_error( oss.str() );
4065 }
4066
4067 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4068 diag(1,0) != 0 || diag(1,1) != 12 || diag(1,2) != 0 || diag(1,3) != 0 ||
4069 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 12 || diag(2,3) != 0 ||
4070 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4071 std::ostringstream oss;
4072 oss << " Test: " << test_ << "\n"
4073 << " Error: Assignment to submatrix failed\n"
4074 << " Details:\n"
4075 << " Result:\n" << diag << "\n"
4076 << " Expected result:\n( 1 0 0 0 )\n"
4077 "( 0 12 0 0 )\n"
4078 "( 0 0 12 0 )\n"
4079 "( 0 0 0 4 )\n";
4080 throw std::runtime_error( oss.str() );
4081 }
4082 }
4083
4084 // ( 1 0 0 0 ) ( 1 0 0 0 )
4085 // ( 0 2 0 0 ) => ( 0 2 0 0 )
4086 // ( 0 0 3 0 ) ( 0 0 3 0 )
4087 // ( 0 0 0 4 ) ( 0 0 0 4 )
4088 {
4089 test_ = "Row-major submatrix() function (scalar assignment test 3)";
4090
4091 using SMT = blaze::Submatrix<DT>;
4092
4093 DT diag( 4UL );
4094 diag(0,0) = 1;
4095 diag(1,1) = 2;
4096 diag(2,2) = 3;
4097 diag(3,3) = 4;
4098
4099 SMT sm = submatrix( diag, 0UL, 2UL, 2UL, 2UL );
4100 sm = 12;
4101
4102 checkRows ( diag, 4UL );
4103 checkColumns ( diag, 4UL );
4104 checkNonZeros( diag, 4UL );
4105 checkNonZeros( diag, 0UL, 1UL );
4106 checkNonZeros( diag, 1UL, 1UL );
4107 checkNonZeros( diag, 2UL, 1UL );
4108 checkNonZeros( diag, 3UL, 1UL );
4109
4110 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4111 sm(1,0) != 0 || sm(1,1) != 0 ) {
4112 std::ostringstream oss;
4113 oss << " Test: " << test_ << "\n"
4114 << " Error: Assignment to submatrix failed\n"
4115 << " Details:\n"
4116 << " Result:\n" << sm << "\n"
4117 << " Expected result:\n( 0 0 )\n( 0 0 )\n";
4118 throw std::runtime_error( oss.str() );
4119 }
4120
4121 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4122 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
4123 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 || diag(2,3) != 0 ||
4124 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4125 std::ostringstream oss;
4126 oss << " Test: " << test_ << "\n"
4127 << " Error: Assignment to submatrix failed\n"
4128 << " Details:\n"
4129 << " Result:\n" << diag << "\n"
4130 << " Expected result:\n( 1 0 0 0 )\n"
4131 "( 0 2 0 0 )\n"
4132 "( 0 0 3 0 )\n"
4133 "( 0 0 0 4 )\n";
4134 throw std::runtime_error( oss.str() );
4135 }
4136 }
4137
4138 // ( 1 0 0 0 ) ( 1 0 0 0 )
4139 // ( 0 2 0 0 ) => ( 0 2 0 0 )
4140 // ( 0 0 3 0 ) ( 0 0 3 0 )
4141 // ( 0 0 0 4 ) ( 0 0 0 4 )
4142 {
4143 test_ = "Row-major submatrix() function (scalar assignment test 4)";
4144
4145 using SMT = blaze::Submatrix<DT>;
4146
4147 DT diag( 4UL );
4148 diag(0,0) = 1;
4149 diag(1,1) = 2;
4150 diag(2,2) = 3;
4151 diag(3,3) = 4;
4152
4153 SMT sm = submatrix( diag, 2UL, 0UL, 2UL, 2UL );
4154 sm = 12;
4155
4156 checkRows ( diag, 4UL );
4157 checkColumns ( diag, 4UL );
4158 checkNonZeros( diag, 4UL );
4159 checkNonZeros( diag, 0UL, 1UL );
4160 checkNonZeros( diag, 1UL, 1UL );
4161 checkNonZeros( diag, 2UL, 1UL );
4162 checkNonZeros( diag, 3UL, 1UL );
4163
4164 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4165 sm(1,0) != 0 || sm(1,1) != 0 ) {
4166 std::ostringstream oss;
4167 oss << " Test: " << test_ << "\n"
4168 << " Error: Assignment to submatrix failed\n"
4169 << " Details:\n"
4170 << " Result:\n" << sm << "\n"
4171 << " Expected result:\n( 0 0 )\n( 0 0 )\n";
4172 throw std::runtime_error( oss.str() );
4173 }
4174
4175 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4176 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
4177 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 || diag(2,3) != 0 ||
4178 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4179 std::ostringstream oss;
4180 oss << " Test: " << test_ << "\n"
4181 << " Error: Assignment to submatrix failed\n"
4182 << " Details:\n"
4183 << " Result:\n" << diag << "\n"
4184 << " Expected result:\n( 1 0 0 0 )\n"
4185 "( 0 2 0 0 )\n"
4186 "( 0 0 3 0 )\n"
4187 "( 0 0 0 4 )\n";
4188 throw std::runtime_error( oss.str() );
4189 }
4190 }
4191
4192
4193 //=====================================================================================
4194 // Column-major general tests
4195 //=====================================================================================
4196
4197 {
4198 test_ = "Column-major submatrix() function";
4199
4200 using SMT = blaze::Submatrix<ODT>;
4201
4202 ODT diag( 3UL );
4203 diag(0,0) = 1;
4204 diag(1,1) = 2;
4205 diag(2,2) = 3;
4206
4207 SMT sm = submatrix( diag, 1UL, 1UL, 2UL, 2UL );
4208
4209 if( sm(1,1) != 3 ) {
4210 std::ostringstream oss;
4211 oss << " Test: " << test_ << "\n"
4212 << " Error: Function call operator access failed\n"
4213 << " Details:\n"
4214 << " Result: " << sm(1,1) << "\n"
4215 << " Expected result: 3\n";
4216 throw std::runtime_error( oss.str() );
4217 }
4218
4219 SMT::Iterator it = sm.begin(0UL);
4220
4221 if( it == sm.end(0UL) || *it != 2 ) {
4222 std::ostringstream oss;
4223 oss << " Test: " << test_ << "\n"
4224 << " Error: Iterator access failed\n"
4225 << " Details:\n"
4226 << " Result: " << *it << "\n"
4227 << " Expected result: 2\n";
4228 throw std::runtime_error( oss.str() );
4229 }
4230
4231 sm(0,0) = -5;
4232
4233 if( sm(0,0) != -5 || sm(0,1) != 0 ||
4234 sm(1,0) != 0 || sm(1,1) != 3 ) {
4235 std::ostringstream oss;
4236 oss << " Test: " << test_ << "\n"
4237 << " Error: Submatrix access failed\n"
4238 << " Details:\n"
4239 << " Result:\n" << sm << "\n"
4240 << " Expected result:\n( -5 0 )\n( 0 3 )\n";
4241 throw std::runtime_error( oss.str() );
4242 }
4243
4244 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4245 diag(1,0) != 0 || diag(1,1) != -5 || diag(1,2) != 0 ||
4246 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4247 std::ostringstream oss;
4248 oss << " Test: " << test_ << "\n"
4249 << " Error: Submatrix access failed\n"
4250 << " Details:\n"
4251 << " Result:\n" << diag << "\n"
4252 << " Expected result:\n( 1 0 0 )\n( 0 -5 0 )\n( 0 0 3 )\n";
4253 throw std::runtime_error( oss.str() );
4254 }
4255
4256 reset( sm );
4257
4258 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4259 sm(1,0) != 0 || sm(1,1) != 0 ) {
4260 std::ostringstream oss;
4261 oss << " Test: " << test_ << "\n"
4262 << " Error: Submatrix reset failed\n"
4263 << " Details:\n"
4264 << " Result:\n" << sm << "\n"
4265 << " Expected result:\n( 0 0 )\n( 0 0 )\n";
4266 throw std::runtime_error( oss.str() );
4267 }
4268
4269 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4270 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
4271 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 0 ) {
4272 std::ostringstream oss;
4273 oss << " Test: " << test_ << "\n"
4274 << " Error: Submatrix reset failed\n"
4275 << " Details:\n"
4276 << " Result:\n" << diag << "\n"
4277 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 0 )\n";
4278 throw std::runtime_error( oss.str() );
4279 }
4280 }
4281
4282
4283 //=====================================================================================
4284 // Column-major scalar assignment
4285 //=====================================================================================
4286
4287 // ( 1 0 0 0 ) ( 1 0 0 0 )
4288 // ( 0 2 0 0 ) => ( 0 12 0 0 )
4289 // ( 0 0 3 0 ) ( 0 0 12 0 )
4290 // ( 0 0 0 4 ) ( 0 0 0 4 )
4291 {
4292 test_ = "Column-major submatrix() function (scalar assignment test 1)";
4293
4294 using SMT = blaze::Submatrix<ODT>;
4295
4296 ODT diag( 4UL );
4297 diag(0,0) = 1;
4298 diag(1,1) = 2;
4299 diag(2,2) = 3;
4300 diag(3,3) = 4;
4301
4302 SMT sm = submatrix( diag, 0UL, 1UL, 4UL, 2UL );
4303 sm = 12;
4304
4305 checkRows ( diag, 4UL );
4306 checkColumns ( diag, 4UL );
4307 checkNonZeros( diag, 4UL );
4308 checkNonZeros( diag, 0UL, 1UL );
4309 checkNonZeros( diag, 1UL, 1UL );
4310 checkNonZeros( diag, 2UL, 1UL );
4311 checkNonZeros( diag, 3UL, 1UL );
4312
4313 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4314 sm(1,0) != 12 || sm(1,1) != 0 ||
4315 sm(2,0) != 0 || sm(2,1) != 12 ||
4316 sm(3,0) != 0 || sm(3,1) != 0 ) {
4317 std::ostringstream oss;
4318 oss << " Test: " << test_ << "\n"
4319 << " Error: Assignment to submatrix failed\n"
4320 << " Details:\n"
4321 << " Result:\n" << sm << "\n"
4322 << " Expected result:\n( 0 0 )\n( 12 0 )\n( 0 12 )\n( 0 0 )\n";
4323 throw std::runtime_error( oss.str() );
4324 }
4325
4326 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4327 diag(1,0) != 0 || diag(1,1) != 12 || diag(1,2) != 0 || diag(1,3) != 0 ||
4328 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 12 || diag(2,3) != 0 ||
4329 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4330 std::ostringstream oss;
4331 oss << " Test: " << test_ << "\n"
4332 << " Error: Assignment to submatrix failed\n"
4333 << " Details:\n"
4334 << " Result:\n" << diag << "\n"
4335 << " Expected result:\n( 1 0 0 0 )\n"
4336 "( 0 12 0 0 )\n"
4337 "( 0 0 12 0 )\n"
4338 "( 0 0 0 4 )\n";
4339 throw std::runtime_error( oss.str() );
4340 }
4341 }
4342
4343 // ( 1 0 0 0 ) ( 1 0 0 0 )
4344 // ( 0 2 0 0 ) => ( 0 12 0 0 )
4345 // ( 0 0 3 0 ) ( 0 0 12 0 )
4346 // ( 0 0 0 4 ) ( 0 0 0 4 )
4347 {
4348 test_ = "Column-major submatrix() function (scalar assignment test 2)";
4349
4350 using SMT = blaze::Submatrix<ODT>;
4351
4352 ODT diag( 4UL );
4353 diag(0,0) = 1;
4354 diag(1,1) = 2;
4355 diag(2,2) = 3;
4356 diag(3,3) = 4;
4357
4358 SMT sm = submatrix( diag, 1UL, 0UL, 2UL, 4UL );
4359 sm = 12;
4360
4361 checkRows ( diag, 4UL );
4362 checkColumns ( diag, 4UL );
4363 checkNonZeros( diag, 4UL );
4364 checkNonZeros( diag, 0UL, 1UL );
4365 checkNonZeros( diag, 1UL, 1UL );
4366 checkNonZeros( diag, 2UL, 1UL );
4367 checkNonZeros( diag, 3UL, 1UL );
4368
4369 if( sm(0,0) != 0 || sm(0,1) != 12 || sm(0,2) != 0 || sm(0,3) != 0 ||
4370 sm(1,0) != 0 || sm(1,1) != 0 || sm(1,2) != 12 || sm(1,3) != 0 ) {
4371 std::ostringstream oss;
4372 oss << " Test: " << test_ << "\n"
4373 << " Error: Assignment to submatrix failed\n"
4374 << " Details:\n"
4375 << " Result:\n" << sm << "\n"
4376 << " Expected result:\n( 0 12 0 0 )\n( 0 0 12 0 )\n";
4377 throw std::runtime_error( oss.str() );
4378 }
4379
4380 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4381 diag(1,0) != 0 || diag(1,1) != 12 || diag(1,2) != 0 || diag(1,3) != 0 ||
4382 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 12 || diag(2,3) != 0 ||
4383 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4384 std::ostringstream oss;
4385 oss << " Test: " << test_ << "\n"
4386 << " Error: Assignment to submatrix failed\n"
4387 << " Details:\n"
4388 << " Result:\n" << diag << "\n"
4389 << " Expected result:\n( 1 0 0 0 )\n"
4390 "( 0 12 0 0 )\n"
4391 "( 0 0 12 0 )\n"
4392 "( 0 0 0 4 )\n";
4393 throw std::runtime_error( oss.str() );
4394 }
4395 }
4396
4397 // ( 1 0 0 0 ) ( 1 0 0 0 )
4398 // ( 0 2 0 0 ) => ( 0 2 0 0 )
4399 // ( 0 0 3 0 ) ( 0 0 3 0 )
4400 // ( 0 0 0 4 ) ( 0 0 0 4 )
4401 {
4402 test_ = "Column-major submatrix() function (scalar assignment test 3)";
4403
4404 using SMT = blaze::Submatrix<ODT>;
4405
4406 ODT diag( 4UL );
4407 diag(0,0) = 1;
4408 diag(1,1) = 2;
4409 diag(2,2) = 3;
4410 diag(3,3) = 4;
4411
4412 SMT sm = submatrix( diag, 0UL, 2UL, 2UL, 2UL );
4413 sm = 12;
4414
4415 checkRows ( diag, 4UL );
4416 checkColumns ( diag, 4UL );
4417 checkNonZeros( diag, 4UL );
4418 checkNonZeros( diag, 0UL, 1UL );
4419 checkNonZeros( diag, 1UL, 1UL );
4420 checkNonZeros( diag, 2UL, 1UL );
4421 checkNonZeros( diag, 3UL, 1UL );
4422
4423 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4424 sm(1,0) != 0 || sm(1,1) != 0 ) {
4425 std::ostringstream oss;
4426 oss << " Test: " << test_ << "\n"
4427 << " Error: Assignment to submatrix failed\n"
4428 << " Details:\n"
4429 << " Result:\n" << sm << "\n"
4430 << " Expected result:\n( 0 0 )\n( 0 0 )\n";
4431 throw std::runtime_error( oss.str() );
4432 }
4433
4434 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4435 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
4436 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 || diag(2,3) != 0 ||
4437 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4438 std::ostringstream oss;
4439 oss << " Test: " << test_ << "\n"
4440 << " Error: Assignment to submatrix failed\n"
4441 << " Details:\n"
4442 << " Result:\n" << diag << "\n"
4443 << " Expected result:\n( 1 0 0 0 )\n"
4444 "( 0 2 0 0 )\n"
4445 "( 0 0 3 0 )\n"
4446 "( 0 0 0 4 )\n";
4447 throw std::runtime_error( oss.str() );
4448 }
4449 }
4450
4451 // ( 1 0 0 0 ) ( 1 0 0 0 )
4452 // ( 0 2 0 0 ) => ( 0 2 0 0 )
4453 // ( 0 0 3 0 ) ( 0 0 3 0 )
4454 // ( 0 0 0 4 ) ( 0 0 0 4 )
4455 {
4456 test_ = "Column-major submatrix() function (scalar assignment test 4)";
4457
4458 using SMT = blaze::Submatrix<ODT>;
4459
4460 ODT diag( 4UL );
4461 diag(0,0) = 1;
4462 diag(1,1) = 2;
4463 diag(2,2) = 3;
4464 diag(3,3) = 4;
4465
4466 SMT sm = submatrix( diag, 2UL, 0UL, 2UL, 2UL );
4467 sm = 12;
4468
4469 checkRows ( diag, 4UL );
4470 checkColumns ( diag, 4UL );
4471 checkNonZeros( diag, 4UL );
4472 checkNonZeros( diag, 0UL, 1UL );
4473 checkNonZeros( diag, 1UL, 1UL );
4474 checkNonZeros( diag, 2UL, 1UL );
4475 checkNonZeros( diag, 3UL, 1UL );
4476
4477 if( sm(0,0) != 0 || sm(0,1) != 0 ||
4478 sm(1,0) != 0 || sm(1,1) != 0 ) {
4479 std::ostringstream oss;
4480 oss << " Test: " << test_ << "\n"
4481 << " Error: Assignment to submatrix failed\n"
4482 << " Details:\n"
4483 << " Result:\n" << sm << "\n"
4484 << " Expected result:\n( 0 0 )\n( 0 0 )\n";
4485 throw std::runtime_error( oss.str() );
4486 }
4487
4488 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 || diag(0,3) != 0 ||
4489 diag(1,0) != 0 || diag(1,1) != 2 || diag(1,2) != 0 || diag(1,3) != 0 ||
4490 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 || diag(2,3) != 0 ||
4491 diag(3,0) != 0 || diag(3,1) != 0 || diag(3,2) != 0 || diag(3,3) != 4 ) {
4492 std::ostringstream oss;
4493 oss << " Test: " << test_ << "\n"
4494 << " Error: Assignment to submatrix failed\n"
4495 << " Details:\n"
4496 << " Result:\n" << diag << "\n"
4497 << " Expected result:\n( 1 0 0 0 )\n"
4498 "( 0 2 0 0 )\n"
4499 "( 0 0 3 0 )\n"
4500 "( 0 0 0 4 )\n";
4501 throw std::runtime_error( oss.str() );
4502 }
4503 }
4504 }
4505 //*************************************************************************************************
4506
4507
4508 //*************************************************************************************************
4509 /*!\brief Test of the \c row() function with the DiagonalMatrix specialization.
4510 //
4511 // \return void
4512 // \exception std::runtime_error Error detected.
4513 //
4514 // This function performs a test of the \c row() function with the DiagonalMatrix specialization.
4515 // In case an error is detected, a \a std::runtime_error exception is thrown.
4516 */
testRow()4517 void DenseTest::testRow()
4518 {
4519 //=====================================================================================
4520 // Row-major general tests
4521 //=====================================================================================
4522
4523 {
4524 test_ = "Row-major row() function";
4525
4526 using RT = blaze::Row<DT>;
4527
4528 DT diag( 3UL );
4529 diag(0,0) = 1;
4530 diag(1,1) = 2;
4531 diag(2,2) = 3;
4532
4533 RT row1 = row( diag, 1UL );
4534
4535 if( row1[1] != 2 ) {
4536 std::ostringstream oss;
4537 oss << " Test: " << test_ << "\n"
4538 << " Error: Function call operator access failed\n"
4539 << " Details:\n"
4540 << " Result: " << row1[1] << "\n"
4541 << " Expected result: 2\n";
4542 throw std::runtime_error( oss.str() );
4543 }
4544
4545 RT::Iterator it( row1.begin() );
4546
4547 if( it == row1.end() || *it != 0 ) {
4548 std::ostringstream oss;
4549 oss << " Test: " << test_ << "\n"
4550 << " Error: Iterator access failed\n"
4551 << " Details:\n"
4552 << " Result: " << *it << "\n"
4553 << " Expected result: 0\n";
4554 throw std::runtime_error( oss.str() );
4555 }
4556
4557 row1[1] = -5;
4558
4559 if( row1[0] != 0 || row1[1] != -5 || row1[2] != 0 ) {
4560 std::ostringstream oss;
4561 oss << " Test: " << test_ << "\n"
4562 << " Error: Row access failed\n"
4563 << " Details:\n"
4564 << " Result:\n" << row1 << "\n"
4565 << " Expected result:\n( 0 -5 0 )\n";
4566 throw std::runtime_error( oss.str() );
4567 }
4568
4569 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4570 diag(1,0) != 0 || diag(1,1) != -5 || diag(1,2) != 0 ||
4571 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4572 std::ostringstream oss;
4573 oss << " Test: " << test_ << "\n"
4574 << " Error: Row access failed\n"
4575 << " Details:\n"
4576 << " Result:\n" << diag << "\n"
4577 << " Expected result:\n( 1 0 0 )\n( -4 -5 0 )\n( 7 0 3 )\n";
4578 throw std::runtime_error( oss.str() );
4579 }
4580
4581 reset( row1 );
4582
4583 if( row1[0] != 0 || row1[1] != 0 || row1[2] != 0 ) {
4584 std::ostringstream oss;
4585 oss << " Test: " << test_ << "\n"
4586 << " Error: Row reset failed\n"
4587 << " Details:\n"
4588 << " Result:\n" << row1 << "\n"
4589 << " Expected result:\n( 0 0 0 )\n";
4590 throw std::runtime_error( oss.str() );
4591 }
4592
4593 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4594 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
4595 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4596 std::ostringstream oss;
4597 oss << " Test: " << test_ << "\n"
4598 << " Error: Row reset failed\n"
4599 << " Details:\n"
4600 << " Result:\n" << diag << "\n"
4601 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
4602 throw std::runtime_error( oss.str() );
4603 }
4604 }
4605
4606
4607 //=====================================================================================
4608 // Row-major scalar assignment
4609 //=====================================================================================
4610
4611 {
4612 test_ = "Row-major row() function (scalar assignment test)";
4613
4614 using RT = blaze::Row<DT>;
4615
4616 DT diag( 3UL );
4617 diag(0,0) = 1;
4618 diag(1,1) = 2;
4619 diag(2,2) = 3;
4620
4621 RT row1 = row( diag, 1UL );
4622 row1 = 8;
4623
4624 checkRows ( diag, 3UL );
4625 checkColumns ( diag, 3UL );
4626 checkNonZeros( diag, 3UL );
4627 checkNonZeros( diag, 0UL, 1UL );
4628 checkNonZeros( diag, 1UL, 1UL );
4629 checkNonZeros( diag, 2UL, 1UL );
4630
4631 if( row1[0] != 0 || row1[1] != 8 || row1[2] != 0 ) {
4632 std::ostringstream oss;
4633 oss << " Test: " << test_ << "\n"
4634 << " Error: Row access failed\n"
4635 << " Details:\n"
4636 << " Result:\n" << row1 << "\n"
4637 << " Expected result:\n( 0 8 0 )\n";
4638 throw std::runtime_error( oss.str() );
4639 }
4640
4641 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4642 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
4643 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4644 std::ostringstream oss;
4645 oss << " Test: " << test_ << "\n"
4646 << " Error: Row access failed\n"
4647 << " Details:\n"
4648 << " Result:\n" << diag << "\n"
4649 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
4650 throw std::runtime_error( oss.str() );
4651 }
4652 }
4653
4654
4655 //=====================================================================================
4656 // Column-major general tests
4657 //=====================================================================================
4658
4659 {
4660 test_ = "Column-major row() function";
4661
4662 using RT = blaze::Row<ODT>;
4663
4664 ODT diag( 3UL );
4665 diag(0,0) = 1;
4666 diag(1,1) = 2;
4667 diag(2,2) = 3;
4668
4669 RT row1 = row( diag, 1UL );
4670
4671 if( row1[1] != 2 ) {
4672 std::ostringstream oss;
4673 oss << " Test: " << test_ << "\n"
4674 << " Error: Function call operator access failed\n"
4675 << " Details:\n"
4676 << " Result: " << row1[1] << "\n"
4677 << " Expected result: 2\n";
4678 throw std::runtime_error( oss.str() );
4679 }
4680
4681 RT::Iterator it( row1.begin() );
4682
4683 if( it == row1.end() || *it != 0 ) {
4684 std::ostringstream oss;
4685 oss << " Test: " << test_ << "\n"
4686 << " Error: Iterator access failed\n"
4687 << " Details:\n"
4688 << " Result: " << *it << "\n"
4689 << " Expected result: 0\n";
4690 throw std::runtime_error( oss.str() );
4691 }
4692
4693 row1[1] = -5;
4694
4695 if( row1[0] != 0 || row1[1] != -5 || row1[2] != 0 ) {
4696 std::ostringstream oss;
4697 oss << " Test: " << test_ << "\n"
4698 << " Error: Row access failed\n"
4699 << " Details:\n"
4700 << " Result:\n" << row1 << "\n"
4701 << " Expected result:\n( -4 -5 0 )\n";
4702 throw std::runtime_error( oss.str() );
4703 }
4704
4705 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4706 diag(1,0) != 0 || diag(1,1) != -5 || diag(1,2) != 0 ||
4707 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4708 std::ostringstream oss;
4709 oss << " Test: " << test_ << "\n"
4710 << " Error: Row access failed\n"
4711 << " Details:\n"
4712 << " Result:\n" << diag << "\n"
4713 << " Expected result:\n( 1 0 0 )\n( 0 -5 0 )\n( 0 0 3 )\n";
4714 throw std::runtime_error( oss.str() );
4715 }
4716
4717 reset( row1 );
4718
4719 if( row1[0] != 0 || row1[1] != 0 || row1[2] != 0 ) {
4720 std::ostringstream oss;
4721 oss << " Test: " << test_ << "\n"
4722 << " Error: Row reset failed\n"
4723 << " Details:\n"
4724 << " Result:\n" << row1 << "\n"
4725 << " Expected result:\n( 0 0 0 )\n";
4726 throw std::runtime_error( oss.str() );
4727 }
4728
4729 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4730 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
4731 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4732 std::ostringstream oss;
4733 oss << " Test: " << test_ << "\n"
4734 << " Error: Row reset failed\n"
4735 << " Details:\n"
4736 << " Result:\n" << diag << "\n"
4737 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
4738 throw std::runtime_error( oss.str() );
4739 }
4740 }
4741
4742
4743 //=====================================================================================
4744 // Column-major scalar assignment
4745 //=====================================================================================
4746
4747 {
4748 test_ = "Column-major row() function (scalar assignment test)";
4749
4750 using RT = blaze::Row<ODT>;
4751
4752 ODT diag( 3UL );
4753 diag(0,0) = 1;
4754 diag(1,1) = 2;
4755 diag(2,2) = 3;
4756
4757 RT row1 = row( diag, 1UL );
4758 row1 = 8;
4759
4760 checkRows ( diag, 3UL );
4761 checkColumns ( diag, 3UL );
4762 checkNonZeros( diag, 3UL );
4763 checkNonZeros( diag, 0UL, 1UL );
4764 checkNonZeros( diag, 1UL, 1UL );
4765 checkNonZeros( diag, 2UL, 1UL );
4766
4767 if( row1[0] != 0 || row1[1] != 8 || row1[2] != 0 ) {
4768 std::ostringstream oss;
4769 oss << " Test: " << test_ << "\n"
4770 << " Error: Row access failed\n"
4771 << " Details:\n"
4772 << " Result:\n" << row1 << "\n"
4773 << " Expected result:\n( 0 8 0 )\n";
4774 throw std::runtime_error( oss.str() );
4775 }
4776
4777 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4778 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
4779 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4780 std::ostringstream oss;
4781 oss << " Test: " << test_ << "\n"
4782 << " Error: Row access failed\n"
4783 << " Details:\n"
4784 << " Result:\n" << diag << "\n"
4785 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
4786 throw std::runtime_error( oss.str() );
4787 }
4788 }
4789 }
4790 //*************************************************************************************************
4791
4792
4793 //*************************************************************************************************
4794 /*!\brief Test of the \c column() function with the DiagonalMatrix specialization.
4795 //
4796 // \return void
4797 // \exception std::runtime_error Error detected.
4798 //
4799 // This function performs a test of the \c column() function with the DiagonalMatrix
4800 // specialization. In case an error is detected, a \a std::runtime_error exception is thrown.
4801 */
testColumn()4802 void DenseTest::testColumn()
4803 {
4804 //=====================================================================================
4805 // Row-major general tests
4806 //=====================================================================================
4807
4808 {
4809 test_ = "Row-major column() function";
4810
4811 using CT = blaze::Column<DT>;
4812
4813 DT diag( 3UL );
4814 diag(0,0) = 1;
4815 diag(1,1) = 2;
4816 diag(2,2) = 3;
4817
4818 CT col1 = column( diag, 1UL );
4819
4820 if( col1[1] != 2 ) {
4821 std::ostringstream oss;
4822 oss << " Test: " << test_ << "\n"
4823 << " Error: Function call operator access failed\n"
4824 << " Details:\n"
4825 << " Result: " << col1[1] << "\n"
4826 << " Expected result: 2\n";
4827 throw std::runtime_error( oss.str() );
4828 }
4829
4830 CT::Iterator it( col1.begin() );
4831
4832 if( it == col1.end() || *it != 0 ) {
4833 std::ostringstream oss;
4834 oss << " Test: " << test_ << "\n"
4835 << " Error: Iterator access failed\n"
4836 << " Details:\n"
4837 << " Result: " << *it << "\n"
4838 << " Expected result: 0\n";
4839 throw std::runtime_error( oss.str() );
4840 }
4841
4842 col1[1] = -5;
4843
4844 if( col1[0] != 0 || col1[1] != -5 || col1[2] != 0 ) {
4845 std::ostringstream oss;
4846 oss << " Test: " << test_ << "\n"
4847 << " Error: Column access failed\n"
4848 << " Details:\n"
4849 << " Result:\n" << col1 << "\n"
4850 << " Expected result:\n( 0 -5 0 )\n";
4851 throw std::runtime_error( oss.str() );
4852 }
4853
4854 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4855 diag(1,0) != 0 || diag(1,1) != -5 || diag(1,2) != 0 ||
4856 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4857 std::ostringstream oss;
4858 oss << " Test: " << test_ << "\n"
4859 << " Error: Column access failed\n"
4860 << " Details:\n"
4861 << " Result:\n" << diag << "\n"
4862 << " Expected result:\n( 1 0 0 )\n( 0 -5 0 )\n( 0 0 3 )\n";
4863 throw std::runtime_error( oss.str() );
4864 }
4865
4866 reset( col1 );
4867
4868 if( col1[0] != 0 || col1[1] != 0 || col1[2] != 0 ) {
4869 std::ostringstream oss;
4870 oss << " Test: " << test_ << "\n"
4871 << " Error: Column reset failed\n"
4872 << " Details:\n"
4873 << " Result:\n" << col1 << "\n"
4874 << " Expected result:\n( 0 0 0 )\n";
4875 throw std::runtime_error( oss.str() );
4876 }
4877
4878 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4879 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
4880 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4881 std::ostringstream oss;
4882 oss << " Test: " << test_ << "\n"
4883 << " Error: Column reset failed\n"
4884 << " Details:\n"
4885 << " Result:\n" << diag << "\n"
4886 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
4887 throw std::runtime_error( oss.str() );
4888 }
4889 }
4890
4891
4892 //=====================================================================================
4893 // Row-major scalar assignment
4894 //=====================================================================================
4895
4896 {
4897 test_ = "Row-major column() function (scalar assignment test)";
4898
4899 using CT = blaze::Column<DT>;
4900
4901 DT diag( 3UL );
4902 diag(0,0) = 1;
4903 diag(1,1) = 2;
4904 diag(2,2) = 3;
4905
4906 CT col1 = column( diag, 1UL );
4907 col1 = 8;
4908
4909 checkRows ( diag, 3UL );
4910 checkColumns ( diag, 3UL );
4911 checkNonZeros( diag, 3UL );
4912 checkNonZeros( diag, 0UL, 1UL );
4913 checkNonZeros( diag, 1UL, 1UL );
4914 checkNonZeros( diag, 2UL, 1UL );
4915
4916 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
4917 std::ostringstream oss;
4918 oss << " Test: " << test_ << "\n"
4919 << " Error: Column access failed\n"
4920 << " Details:\n"
4921 << " Result:\n" << col1 << "\n"
4922 << " Expected result:\n( 0 8 8 )\n";
4923 throw std::runtime_error( oss.str() );
4924 }
4925
4926 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4927 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
4928 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4929 std::ostringstream oss;
4930 oss << " Test: " << test_ << "\n"
4931 << " Error: Column access failed\n"
4932 << " Details:\n"
4933 << " Result:\n" << diag << "\n"
4934 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
4935 throw std::runtime_error( oss.str() );
4936 }
4937 }
4938
4939
4940 //=====================================================================================
4941 // Column-major general tests
4942 //=====================================================================================
4943
4944 {
4945 test_ = "Column-major column() function";
4946
4947 using CT = blaze::Column<ODT>;
4948
4949 ODT diag( 3UL );
4950 diag(0,0) = 1;
4951 diag(1,1) = 2;
4952 diag(2,2) = 3;
4953
4954 CT col1 = column( diag, 1UL );
4955
4956 if( col1[1] != 2 ) {
4957 std::ostringstream oss;
4958 oss << " Test: " << test_ << "\n"
4959 << " Error: Function call operator access failed\n"
4960 << " Details:\n"
4961 << " Result: " << col1[1] << "\n"
4962 << " Expected result: 2\n";
4963 throw std::runtime_error( oss.str() );
4964 }
4965
4966 CT::Iterator it( col1.begin() );
4967
4968 if( it == col1.end() || *it != 0 ) {
4969 std::ostringstream oss;
4970 oss << " Test: " << test_ << "\n"
4971 << " Error: Iterator access failed\n"
4972 << " Details:\n"
4973 << " Result: " << *it << "\n"
4974 << " Expected result: 0\n";
4975 throw std::runtime_error( oss.str() );
4976 }
4977
4978 col1[1] = -5;
4979
4980 if( col1[0] != 0 || col1[1] != -5 || col1[2] != 0 ) {
4981 std::ostringstream oss;
4982 oss << " Test: " << test_ << "\n"
4983 << " Error: Column access failed\n"
4984 << " Details:\n"
4985 << " Result:\n" << col1 << "\n"
4986 << " Expected result:\n( 0 -5 0 )\n";
4987 throw std::runtime_error( oss.str() );
4988 }
4989
4990 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
4991 diag(1,0) != 0 || diag(1,1) != -5 || diag(1,2) != 0 ||
4992 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
4993 std::ostringstream oss;
4994 oss << " Test: " << test_ << "\n"
4995 << " Error: Column access failed\n"
4996 << " Details:\n"
4997 << " Result:\n" << diag << "\n"
4998 << " Expected result:\n( 1 0 0 )\n( 0 -5 0 )\n( 0 0 3 )\n";
4999 throw std::runtime_error( oss.str() );
5000 }
5001
5002 reset( col1 );
5003
5004 if( col1[0] != 0 || col1[1] != 0 || col1[2] != 0 ) {
5005 std::ostringstream oss;
5006 oss << " Test: " << test_ << "\n"
5007 << " Error: Column reset failed\n"
5008 << " Details:\n"
5009 << " Result:\n" << col1 << "\n"
5010 << " Expected result:\n( 0 0 0 )\n";
5011 throw std::runtime_error( oss.str() );
5012 }
5013
5014 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
5015 diag(1,0) != 0 || diag(1,1) != 0 || diag(1,2) != 0 ||
5016 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
5017 std::ostringstream oss;
5018 oss << " Test: " << test_ << "\n"
5019 << " Error: Column reset failed\n"
5020 << " Details:\n"
5021 << " Result:\n" << diag << "\n"
5022 << " Expected result:\n( 1 0 0 )\n( 0 0 0 )\n( 0 0 3 )\n";
5023 throw std::runtime_error( oss.str() );
5024 }
5025 }
5026
5027
5028 //=====================================================================================
5029 // Column-major scalar assignment
5030 //=====================================================================================
5031
5032 {
5033 test_ = "Column-major column() function (scalar assignment test)";
5034
5035 using CT = blaze::Column<ODT>;
5036
5037 ODT diag( 3UL );
5038 diag(0,0) = 1;
5039 diag(1,1) = 2;
5040 diag(2,2) = 3;
5041
5042 CT col1 = column( diag, 1UL );
5043 col1 = 8;
5044
5045 checkRows ( diag, 3UL );
5046 checkColumns ( diag, 3UL );
5047 checkNonZeros( diag, 3UL );
5048 checkNonZeros( diag, 0UL, 1UL );
5049 checkNonZeros( diag, 1UL, 1UL );
5050 checkNonZeros( diag, 2UL, 1UL );
5051
5052 if( col1[0] != 0 || col1[1] != 8 || col1[2] != 0 ) {
5053 std::ostringstream oss;
5054 oss << " Test: " << test_ << "\n"
5055 << " Error: Column access failed\n"
5056 << " Details:\n"
5057 << " Result:\n" << col1 << "\n"
5058 << " Expected result:\n( 0 8 0 )\n";
5059 throw std::runtime_error( oss.str() );
5060 }
5061
5062 if( diag(0,0) != 1 || diag(0,1) != 0 || diag(0,2) != 0 ||
5063 diag(1,0) != 0 || diag(1,1) != 8 || diag(1,2) != 0 ||
5064 diag(2,0) != 0 || diag(2,1) != 0 || diag(2,2) != 3 ) {
5065 std::ostringstream oss;
5066 oss << " Test: " << test_ << "\n"
5067 << " Error: Column access failed\n"
5068 << " Details:\n"
5069 << " Result:\n" << diag << "\n"
5070 << " Expected result:\n( 1 0 0 )\n( 0 8 0 )\n( 0 0 3 )\n";
5071 throw std::runtime_error( oss.str() );
5072 }
5073 }
5074 }
5075 //*************************************************************************************************
5076
5077 } // namespace diagonalmatrix
5078
5079 } // namespace adaptors
5080
5081 } // namespace mathtest
5082
5083 } // namespace blazetest
5084
5085
5086
5087
5088 //=================================================================================================
5089 //
5090 // MAIN FUNCTION
5091 //
5092 //=================================================================================================
5093
5094 //*************************************************************************************************
main()5095 int main()
5096 {
5097 std::cout << " Running DiagonalMatrix dense test (part 2)..." << std::endl;
5098
5099 try
5100 {
5101 RUN_DIAGONALMATRIX_DENSE_TEST;
5102 }
5103 catch( std::exception& ex ) {
5104 std::cerr << "\n\n ERROR DETECTED during DiagonalMatrix dense test (part 2):\n"
5105 << ex.what() << "\n";
5106 return EXIT_FAILURE;
5107 }
5108
5109 return EXIT_SUCCESS;
5110 }
5111 //*************************************************************************************************
5112