Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynmat_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
48#include <gtest/gtest.h>
49
50#include <string>
51#include <memory>
52#include <vector>
53#include <limits>
54#include <stdexcept>
55#include <type_traits>
56#include <utility>
57#include <cmath>
58
59#include <tpl_dynMat.H>
60#include <ahFunctional.H>
61
62using namespace std;
63using namespace testing;
64using namespace Aleph;
65
66// =============================================================================
67// Test Fixtures
68// =============================================================================
69
70constexpr size_t SMALL_N = 3;
71constexpr size_t SMALL_M = 4;
72constexpr size_t MEDIUM_N = 10;
73constexpr size_t MEDIUM_M = 15;
74constexpr size_t LARGE_N = 100;
75
83
85struct FilledMatrix : public Test
86{
88
90 {
91 for (size_t i = 0; i < SMALL_N; ++i)
92 for (size_t j = 0; j < SMALL_M; ++j)
93 mat.write(i, j, static_cast<int>(i * SMALL_M + j));
94 }
95};
96
98struct SquareMatrix : public Test
99{
101
102 SquareMatrix() : mat(4, 4, 0.0)
103 {
104 // Identity-like diagonal
105 for (size_t i = 0; i < 4; ++i)
106 mat.write(i, i, 1.0);
107 }
108};
109
111struct Counted
112{
113 static int constructions;
114 static int destructions;
115 static int copies;
116 static int moves;
117
118 int value;
119
120 static void reset()
121 {
122 constructions = destructions = copies = moves = 0;
123 }
124
125 explicit Counted(int v = 0) : value(v) { ++constructions; }
126 Counted(const Counted& other) : value(other.value)
127 {
128 ++constructions;
129 ++copies;
130 }
131 Counted(Counted&& other) noexcept : value(other.value)
132 {
133 ++constructions;
134 ++moves;
135 }
136 ~Counted() { ++destructions; }
137
138 Counted& operator=(const Counted& other)
139 {
140 value = other.value;
141 ++copies;
142 return *this;
143 }
144 Counted& operator=(Counted&& other) noexcept
145 {
146 value = other.value;
147 ++moves;
148 return *this;
149 }
150
151 bool operator==(const Counted& other) const { return value == other.value; }
152 bool operator!=(const Counted& other) const { return value != other.value; }
153};
154
157int Counted::copies = 0;
158int Counted::moves = 0;
159
160// =============================================================================
161// Constructor Tests
162// =============================================================================
163
164TEST(DynMatrix, default_constructor_creates_empty_matrix)
165{
166 DynMatrix<int> mat;
167 EXPECT_TRUE(mat.is_empty());
168 EXPECT_EQ(mat.rows(), 0);
169 EXPECT_EQ(mat.cols(), 0);
170 EXPECT_EQ(mat.size(), 0);
171}
172
173TEST(DynMatrix, parametrized_constructor_creates_correct_dimensions)
174{
175 DynMatrix<int> mat(5, 7);
176
177 EXPECT_EQ(mat.rows(), 5);
178 EXPECT_EQ(mat.cols(), 7);
179 EXPECT_EQ(mat.size(), 35);
180 EXPECT_FALSE(mat.is_empty());
181 EXPECT_FALSE(mat.is_square());
182}
183
184TEST(DynMatrix, parametrized_constructor_with_custom_default_value)
185{
186 DynMatrix<int> mat(3, 3, 42);
187
188 EXPECT_EQ(mat.get_default_value(), 42);
189 EXPECT_EQ(mat.read(0, 0), 42);
190 EXPECT_EQ(mat.read(1, 2), 42);
191}
192
193TEST(DynMatrix, constructor_throws_on_zero_dimensions)
194{
195 EXPECT_THROW(DynMatrix<int>(0, 5), std::invalid_argument);
196 EXPECT_THROW(DynMatrix<int>(5, 0), std::invalid_argument);
197 EXPECT_THROW(DynMatrix<int>(0, 0), std::invalid_argument);
198}
199
200TEST(DynMatrix, constructor_with_various_types)
201{
202 DynMatrix<int> mi(2, 3);
203 DynMatrix<double> md(3, 4);
204 DynMatrix<string> ms(2, 2, "default");
205 DynMatrix<vector<int>> mv(2, 2);
206
207 EXPECT_EQ(mi.rows(), 2);
208 EXPECT_EQ(md.cols(), 4);
209 EXPECT_EQ(ms.read(0, 0), "default");
210 EXPECT_TRUE(mv.read(0, 0).empty());
211}
212
213// =============================================================================
214// Copy Constructor Tests
215// =============================================================================
216
217TEST_F(FilledMatrix, copy_constructor_creates_independent_copy)
218{
219 DynMatrix<int> copy(mat);
220
221 EXPECT_EQ(copy.rows(), mat.rows());
222 EXPECT_EQ(copy.cols(), mat.cols());
223
224 for (size_t i = 0; i < SMALL_N; ++i)
225 for (size_t j = 0; j < SMALL_M; ++j)
226 EXPECT_EQ(copy.read(i, j), mat.read(i, j));
227
228 // Modify original, copy should be unchanged
229 mat.write(0, 0, 999);
230 EXPECT_NE(copy.read(0, 0), mat.read(0, 0));
231}
232
233TEST(DynMatrix, copy_constructor_empty_matrix)
234{
235 DynMatrix<int> empty;
236 DynMatrix<int> copy(empty);
237
238 EXPECT_TRUE(copy.is_empty());
239}
240
241TEST_F(SmallIntMatrix, copy_constructor_sparse_matrix)
242{
243 // Write only a few entries
244 mat.write(0, 0, 1);
245 mat.write(2, 3, 2);
246
247 DynMatrix<int> copy(mat);
248
249 EXPECT_EQ(copy.read(0, 0), 1);
250 EXPECT_EQ(copy.read(2, 3), 2);
251 EXPECT_EQ(copy.read(1, 1), 0); // Default value
252}
253
254// =============================================================================
255// Move Constructor Tests
256// =============================================================================
257
258TEST_F(FilledMatrix, move_constructor_transfers_ownership)
259{
260 size_t original_rows = mat.rows();
261 size_t original_cols = mat.cols();
262 int original_value = mat.read(1, 2);
263
264 DynMatrix<int> moved(std::move(mat));
265
266 EXPECT_EQ(moved.rows(), original_rows);
267 EXPECT_EQ(moved.cols(), original_cols);
268 EXPECT_EQ(moved.read(1, 2), original_value);
269 EXPECT_TRUE(mat.is_empty());
270}
271
272TEST(DynMatrix, move_constructor_empty_matrix)
273{
274 DynMatrix<int> empty;
275 DynMatrix<int> moved(std::move(empty));
276
277 EXPECT_TRUE(moved.is_empty());
278}
279
280TEST(DynMatrix, move_constructor_is_noexcept)
281{
282 EXPECT_TRUE(std::is_nothrow_move_constructible_v<DynMatrix<int>>);
283}
284
285// =============================================================================
286// Copy Assignment Tests
287// =============================================================================
288
289TEST_F(FilledMatrix, copy_assignment_replaces_contents)
290{
291 DynMatrix<int> other(2, 2);
292 other.write(0, 0, 100);
293
294 other = mat;
295
296 EXPECT_EQ(other.rows(), SMALL_N);
297 EXPECT_EQ(other.cols(), SMALL_M);
298 EXPECT_EQ(other.read(1, 2), mat.read(1, 2));
299}
300
301TEST_F(FilledMatrix, copy_assignment_self_assignment_is_safe)
302{
303 mat = mat;
304
305 EXPECT_EQ(mat.rows(), SMALL_N);
306 EXPECT_EQ(mat.read(0, 0), 0);
307}
308
309TEST(DynMatrix, copy_assignment_different_dimensions)
310{
311 DynMatrix<int> source(5, 5, 7);
312 DynMatrix<int> dest(2, 3, 0);
313
314 dest = source;
315
316 EXPECT_EQ(dest.rows(), 5);
317 EXPECT_EQ(dest.cols(), 5);
318 EXPECT_EQ(dest.read(2, 2), 7);
319}
320
321// =============================================================================
322// Move Assignment Tests
323// =============================================================================
324
325TEST_F(FilledMatrix, move_assignment_transfers_ownership)
326{
327 DynMatrix<int> dest(2, 2);
328 size_t original_rows = mat.rows();
329
330 dest = std::move(mat);
331
332 EXPECT_EQ(dest.rows(), original_rows);
333 EXPECT_TRUE(mat.is_empty());
334}
335
336TEST(DynMatrix, move_assignment_is_noexcept)
337{
338 EXPECT_TRUE(std::is_nothrow_move_assignable_v<DynMatrix<int>>);
339}
340
341// =============================================================================
342// Swap Tests
343// =============================================================================
344
345TEST(DynMatrix, swap_exchanges_contents)
346{
347 DynMatrix<int> mat1(3, 4, 1);
348 DynMatrix<int> mat2(5, 6, 2);
349
350 mat1.write(0, 0, 10);
351 mat2.write(0, 0, 20);
352
353 mat1.swap(mat2);
354
355 EXPECT_EQ(mat1.rows(), 5);
356 EXPECT_EQ(mat1.cols(), 6);
357 EXPECT_EQ(mat1.read(0, 0), 20);
358
359 EXPECT_EQ(mat2.rows(), 3);
360 EXPECT_EQ(mat2.cols(), 4);
361 EXPECT_EQ(mat2.read(0, 0), 10);
362}
363
364TEST(DynMatrix, swap_with_empty_matrix)
365{
366 DynMatrix<int> mat1(3, 3, 5);
367 DynMatrix<int> mat2;
368
369 mat1.swap(mat2);
370
371 EXPECT_TRUE(mat1.is_empty());
372 EXPECT_EQ(mat2.rows(), 3);
373}
374
375TEST(DynMatrix, swap_is_noexcept)
376{
377 DynMatrix<int> m1, m2;
378 EXPECT_TRUE(noexcept(m1.swap(m2)));
379}
380
381// =============================================================================
382// Dimension and Query Tests
383// =============================================================================
384
385TEST_F(SmallIntMatrix, rows_returns_correct_value)
386{
387 EXPECT_EQ(mat.rows(), SMALL_N);
388}
389
390TEST_F(SmallIntMatrix, cols_returns_correct_value)
391{
392 EXPECT_EQ(mat.cols(), SMALL_M);
393}
394
395TEST_F(SmallIntMatrix, size_returns_product_of_dimensions)
396{
397 EXPECT_EQ(mat.size(), SMALL_N * SMALL_M);
398}
399
400TEST(DynMatrix, is_square_true_for_square_matrix)
401{
402 DynMatrix<int> mat(5, 5);
403 EXPECT_TRUE(mat.is_square());
404}
405
406TEST(DynMatrix, is_square_false_for_non_square_matrix)
407{
408 DynMatrix<int> mat(3, 5);
409 EXPECT_FALSE(mat.is_square());
410}
411
412TEST(DynMatrix, is_empty_true_for_default_constructed)
413{
414 DynMatrix<int> mat;
415 EXPECT_TRUE(mat.is_empty());
416}
417
418TEST_F(SmallIntMatrix, is_empty_false_for_initialized_matrix)
419{
420 EXPECT_FALSE(mat.is_empty());
421}
422
423// =============================================================================
424// Read/Write Tests
425// =============================================================================
426
427TEST_F(SmallIntMatrix, write_and_read_single_entry)
428{
429 mat.write(1, 2, 42);
430
431 EXPECT_EQ(mat.read(1, 2), 42);
432}
433
434TEST_F(SmallIntMatrix, read_unwritten_entry_returns_default)
435{
436 EXPECT_EQ(mat.read(0, 0), 0);
437 EXPECT_EQ(mat.read(2, 3), 0);
438}
439
440TEST_F(SmallIntMatrix, write_returns_reference_to_entry)
441{
442 int& ref = mat.write(1, 1, 10);
443
444 EXPECT_EQ(ref, 10);
445 ref = 20;
446 EXPECT_EQ(mat.read(1, 1), 20);
447}
448
449TEST_F(SmallIntMatrix, read_throws_on_out_of_bounds)
450{
451 EXPECT_THROW(mat.read(SMALL_N, 0), std::out_of_range);
452 EXPECT_THROW(mat.read(0, SMALL_M), std::out_of_range);
453 EXPECT_THROW(mat.read(100, 100), std::out_of_range);
454}
455
456TEST_F(SmallIntMatrix, write_throws_on_out_of_bounds)
457{
458 EXPECT_THROW(mat.write(SMALL_N, 0, 1), std::out_of_range);
459 EXPECT_THROW(mat.write(0, SMALL_M, 1), std::out_of_range);
460}
461
462TEST_F(SmallIntMatrix, read_ne_no_bounds_check)
463{
464 mat.write(1, 2, 42);
465 EXPECT_EQ(mat.read_ne(1, 2), 42);
466 EXPECT_EQ(mat.read_ne(0, 0), 0);
467}
468
469TEST(DynMatrix, write_with_move_semantics)
470{
471 DynMatrix<string> mat(2, 2);
472 string value = "hello";
473
474 mat.write(0, 0, std::move(value));
475
476 EXPECT_EQ(mat.read(0, 0), "hello");
477 EXPECT_TRUE(value.empty()); // moved-from
478}
479
480// =============================================================================
481// Access and operator() Tests
482// =============================================================================
483
484TEST_F(SmallIntMatrix, access_after_allocate)
485{
486 mat.allocate();
487 mat(1, 2) = 99;
488
489 EXPECT_EQ(mat.access(1, 2), 99);
490 EXPECT_EQ(mat(1, 2), 99);
491}
492
493TEST_F(SmallIntMatrix, access_const_version)
494{
495 mat.allocate();
496 mat(0, 0) = 42;
497
498 const DynMatrix<int>& const_mat = mat;
499 EXPECT_EQ(const_mat(0, 0), 42);
500}
501
502TEST_F(FilledMatrix, operator_parens_read_write)
503{
504 mat.allocate();
505
506 mat(0, 0) = 100;
507 EXPECT_EQ(mat(0, 0), 100);
508
509 const DynMatrix<int>& cmat = mat;
510 EXPECT_EQ(cmat(0, 0), 100);
511}
512
513// =============================================================================
514// Fill Tests
515// =============================================================================
516
517TEST_F(SmallIntMatrix, fill_sets_all_entries)
518{
519 mat.fill(42);
520
521 for (size_t i = 0; i < SMALL_N; ++i)
522 for (size_t j = 0; j < SMALL_M; ++j)
523 EXPECT_EQ(mat.read(i, j), 42);
524}
525
526TEST(DynMatrix, fill_overwrites_existing_values)
527{
528 DynMatrix<int> mat(3, 3, 0);
529 mat.write(1, 1, 100);
530
531 mat.fill(5);
532
533 EXPECT_EQ(mat.read(1, 1), 5);
534 EXPECT_EQ(mat.read(0, 0), 5);
535}
536
537// =============================================================================
538// Transpose Tests
539// =============================================================================
540
541TEST_F(FilledMatrix, transpose_swaps_dimensions)
542{
543 auto transposed = mat.transpose();
544
545 EXPECT_EQ(transposed.rows(), SMALL_M);
546 EXPECT_EQ(transposed.cols(), SMALL_N);
547}
548
549TEST_F(FilledMatrix, transpose_swaps_values)
550{
551 auto transposed = mat.transpose();
552
553 for (size_t i = 0; i < SMALL_N; ++i)
554 for (size_t j = 0; j < SMALL_M; ++j)
555 EXPECT_EQ(transposed.read(j, i), mat.read(i, j));
556}
557
558TEST_F(SquareMatrix, transpose_diagonal_unchanged)
559{
560 auto transposed = mat.transpose();
561
562 for (size_t i = 0; i < 4; ++i)
563 EXPECT_EQ(transposed.read(i, i), mat.read(i, i));
564}
565
566TEST(DynMatrix, transpose_sparse_matrix)
567{
568 DynMatrix<int> mat(3, 5, 0);
569 mat.write(0, 4, 1);
570 mat.write(2, 1, 2);
571
572 auto transposed = mat.transpose();
573
574 EXPECT_EQ(transposed.read(4, 0), 1);
575 EXPECT_EQ(transposed.read(1, 2), 2);
576 EXPECT_EQ(transposed.read(0, 0), 0);
577}
578
579// =============================================================================
580// set_dimension Tests
581// =============================================================================
582
583TEST_F(FilledMatrix, set_dimension_clears_data)
584{
585 mat.set_dimension(5, 5);
586
587 EXPECT_EQ(mat.rows(), 5);
588 EXPECT_EQ(mat.cols(), 5);
589 // Data should be cleared
590 EXPECT_EQ(mat.read(0, 0), 0);
591}
592
593TEST_F(SmallIntMatrix, set_dimension_preserves_default_value)
594{
595 mat.set_default_initial_value(99);
596 mat.set_dimension(2, 2);
597
598 EXPECT_EQ(mat.read(0, 0), 99);
599}
600
601// =============================================================================
602// Default Value Tests
603// =============================================================================
604
605TEST_F(SmallIntMatrix, set_default_initial_value)
606{
607 mat.set_default_initial_value(42);
608
609 EXPECT_EQ(mat.get_default_value(), 42);
610}
611
612TEST(DynMatrix, default_value_affects_unwritten_reads)
613{
614 DynMatrix<int> mat(3, 3, 100);
615
616 EXPECT_EQ(mat.read(0, 0), 100);
617 EXPECT_EQ(mat.read(2, 2), 100);
618}
619
620TEST(DynMatrix, changing_default_affects_future_reads)
621{
622 DynMatrix<int> mat(3, 3, 0);
623
624 EXPECT_EQ(mat.read(1, 1), 0);
625
627
628 // Unwritten entries should now return new default
629 EXPECT_EQ(mat.read(2, 2), 99);
630}
631
632// =============================================================================
633// Equality Tests
634// =============================================================================
635
636TEST(DynMatrix, equality_same_matrices)
637{
638 DynMatrix<int> mat1(3, 3, 5);
639 DynMatrix<int> mat2(3, 3, 5);
640
641 EXPECT_TRUE(mat1 == mat2);
642 EXPECT_FALSE(mat1 != mat2);
643}
644
645TEST(DynMatrix, equality_different_dimensions)
646{
647 DynMatrix<int> mat1(3, 3);
648 DynMatrix<int> mat2(3, 4);
649
650 EXPECT_FALSE(mat1 == mat2);
651 EXPECT_TRUE(mat1 != mat2);
652}
653
654TEST(DynMatrix, equality_different_values)
655{
656 DynMatrix<int> mat1(3, 3, 0);
657 DynMatrix<int> mat2(3, 3, 0);
658
659 mat1.write(1, 1, 5);
660
661 EXPECT_FALSE(mat1 == mat2);
662 EXPECT_TRUE(mat1 != mat2);
663}
664
665TEST_F(FilledMatrix, equality_with_copy)
666{
667 DynMatrix<int> copy(mat);
668
669 EXPECT_TRUE(mat == copy);
670}
671
672// =============================================================================
673// Iterator Tests
674// =============================================================================
675
676TEST_F(SmallIntMatrix, iterator_basic_traversal)
677{
678 mat.fill(1);
679
680 auto it = mat.get_it();
681 size_t count = 0;
682
683 while (it.has_curr())
684 {
685 EXPECT_EQ(it.get_curr(), 1);
686 it.next();
687 ++count;
688 }
689
690 EXPECT_EQ(count, mat.size());
691}
692
693TEST_F(FilledMatrix, iterator_visits_in_row_major_order)
694{
695 auto it = mat.get_it();
696
697 for (size_t i = 0; i < SMALL_N; ++i)
698 for (size_t j = 0; j < SMALL_M; ++j)
699 {
700 ASSERT_TRUE(it.has_curr());
701 EXPECT_EQ(it.get_row(), i);
702 EXPECT_EQ(it.get_col(), j);
703 EXPECT_EQ(it.get_curr(), mat.read(i, j));
704 it.next();
705 }
706
707 EXPECT_FALSE(it.has_curr());
708}
709
710TEST_F(SmallIntMatrix, iterator_get_curr_throws_when_exhausted)
711{
712 auto it = mat.get_it();
713
714 // Exhaust the iterator
715 while (it.has_curr())
716 it.next();
717
718 EXPECT_THROW(it.get_curr(), std::overflow_error);
719}
720
721TEST_F(SmallIntMatrix, iterator_next_throws_when_exhausted)
722{
723 auto it = mat.get_it();
724
725 while (it.has_curr())
726 it.next();
727
728 EXPECT_THROW(it.next(), std::overflow_error);
729}
730
731TEST_F(SmallIntMatrix, iterator_reset)
732{
733 auto it = mat.get_it();
734
735 it.next();
736 it.next();
737 it.reset();
738
739 EXPECT_EQ(it.get_row(), 0);
740 EXPECT_EQ(it.get_col(), 0);
741}
742
743// =============================================================================
744// Traverse Tests
745// =============================================================================
746
747TEST_F(FilledMatrix, traverse_visits_all_elements)
748{
749 size_t count = 0;
750 int sum = 0;
751
752 mat.traverse([&](int val)
753 {
754 ++count;
755 sum += val;
756 return true;
757 });
758
759 EXPECT_EQ(count, mat.size());
760
761 // Sum of 0 to N*M-1
762 int expected = (SMALL_N * SMALL_M - 1) * SMALL_N * SMALL_M / 2;
763 EXPECT_EQ(sum, expected);
764}
765
766TEST_F(FilledMatrix, traverse_can_stop_early)
767{
768 size_t count = 0;
769
770 bool result = mat.traverse([&](int)
771 {
772 ++count;
773 return count < 5;
774 });
775
776 EXPECT_FALSE(result);
777 EXPECT_EQ(count, 5);
778}
779
780TEST_F(SmallIntMatrix, traverse_on_sparse_matrix)
781{
782 mat.write(0, 0, 10);
783 mat.write(2, 3, 20);
784
785 int sum = 0;
786 mat.traverse([&](int val)
787 {
788 sum += val;
789 return true;
790 });
791
792 EXPECT_EQ(sum, 30); // 10 + 20, rest are 0
793}
794
795TEST_F(SmallIntMatrix, traverse_allocated_visits_allocated_blocks)
796{
797 // Write two entries - this allocates the blocks containing them
798 mat.write(0, 0, 10);
799 mat.write(1, 1, 20);
800
801 size_t count = 0;
802 int sum = 0;
803
804 mat.traverse_allocated([&](int val)
805 {
806 ++count;
807 sum += val;
808 return true;
809 });
810
811 // Note: traverse_allocated visits all entries in allocated blocks,
812 // not just explicitly written entries. The exact count depends on
813 // the DynArray block size configuration.
814 EXPECT_GE(count, 2u); // At least the 2 written entries
815 EXPECT_GE(sum, 30); // At least 10 + 20 from written entries
816}
817
818// =============================================================================
819// Functional Methods Tests (inherited from mixins)
820// =============================================================================
821
822TEST_F(FilledMatrix, for_each_visits_all)
823{
824 int sum = 0;
825 mat.for_each([&sum](int val) { sum += val; });
826
827 int expected = (SMALL_N * SMALL_M - 1) * SMALL_N * SMALL_M / 2;
828 EXPECT_EQ(sum, expected);
829}
830
831TEST_F(FilledMatrix, all_returns_true_when_all_match)
832{
833 // All values are >= 0
834 bool result = mat.all([](int val) { return val >= 0; });
835 EXPECT_TRUE(result);
836}
837
838TEST_F(FilledMatrix, all_returns_false_when_any_fails)
839{
840 // Not all values are < 5
841 bool result = mat.all([](int val) { return val < 5; });
842 EXPECT_FALSE(result);
843}
844
845TEST_F(FilledMatrix, exists_returns_true_when_found)
846{
847 bool result = mat.exists([](int val) { return val == 5; });
848 EXPECT_TRUE(result);
849}
850
851TEST_F(FilledMatrix, exists_returns_false_when_not_found)
852{
853 bool result = mat.exists([](int val) { return val == 999; });
854 EXPECT_FALSE(result);
855}
856
857TEST_F(FilledMatrix, foldl_accumulates)
858{
859 int sum = mat.foldl(0, [](int acc, int val) { return acc + val; });
860
861 int expected = (SMALL_N * SMALL_M - 1) * SMALL_N * SMALL_M / 2;
862 EXPECT_EQ(sum, expected);
863}
864
865// =============================================================================
866// Type Aliases Tests
867// =============================================================================
868
869TEST(DynMatrix, type_aliases_are_correct)
870{
871 using Matrix = DynMatrix<int>;
872
873 static_assert(std::is_same_v<Matrix::Item_Type, int>);
874 static_assert(std::is_same_v<Matrix::Key_Type, int>);
875 static_assert(std::is_same_v<Matrix::Set_Type, Matrix>);
876}
877
878// =============================================================================
879// Memory Management Tests
880// =============================================================================
881
882TEST(DynMatrix, destructor_frees_all_memory)
883{
884 Counted::reset();
885
886 {
887 DynMatrix<Counted> mat(5, 5, Counted(0));
888 mat.allocate();
889 for (size_t i = 0; i < 5; ++i)
890 for (size_t j = 0; j < 5; ++j)
891 mat.write(i, j, Counted(static_cast<int>(i * 5 + j)));
892 }
893
894 // All elements should be destroyed
895 EXPECT_EQ(Counted::constructions, Counted::destructions);
896}
897
898TEST(DynMatrix, allocate_reserves_all_entries)
899{
900 DynMatrix<int> mat(10, 10);
901 mat.allocate();
902
903 // Should be able to access any entry without write
904 for (size_t i = 0; i < 10; ++i)
905 for (size_t j = 0; j < 10; ++j)
906 mat(i, j) = static_cast<int>(i * 10 + j);
907
908 EXPECT_EQ(mat(5, 5), 55);
909}
910
911// =============================================================================
912// Edge Cases
913// =============================================================================
914
915TEST(DynMatrix, single_element_matrix)
916{
917 DynMatrix<int> mat(1, 1, 42);
918
919 EXPECT_EQ(mat.rows(), 1);
920 EXPECT_EQ(mat.cols(), 1);
921 EXPECT_EQ(mat.size(), 1);
922 EXPECT_TRUE(mat.is_square());
923 EXPECT_EQ(mat.read(0, 0), 42);
924}
925
926TEST(DynMatrix, single_row_matrix)
927{
928 DynMatrix<int> mat(1, 10, 0);
929
930 EXPECT_EQ(mat.rows(), 1);
931 EXPECT_EQ(mat.cols(), 10);
932 EXPECT_FALSE(mat.is_square());
933
934 for (size_t j = 0; j < 10; ++j)
935 mat.write(0, j, static_cast<int>(j));
936
937 EXPECT_EQ(mat.read(0, 5), 5);
938}
939
940TEST(DynMatrix, single_column_matrix)
941{
942 DynMatrix<int> mat(10, 1, 0);
943
944 EXPECT_EQ(mat.rows(), 10);
945 EXPECT_EQ(mat.cols(), 1);
946
947 for (size_t i = 0; i < 10; ++i)
948 mat.write(i, 0, static_cast<int>(i));
949
950 EXPECT_EQ(mat.read(5, 0), 5);
951}
952
953TEST(DynMatrix, very_sparse_matrix)
954{
955 DynMatrix<int> mat(1000, 1000, 0);
956
957 // Write only a few entries
958 mat.write(0, 0, 1);
959 mat.write(999, 999, 2);
960 mat.write(500, 500, 3);
961
962 EXPECT_EQ(mat.read(0, 0), 1);
963 EXPECT_EQ(mat.read(999, 999), 2);
964 EXPECT_EQ(mat.read(500, 500), 3);
965 EXPECT_EQ(mat.read(100, 100), 0); // Default
966}
967
968// =============================================================================
969// Stress Tests
970// =============================================================================
971
972TEST(DynMatrix, stress_large_matrix)
973{
975
976 // Write diagonal
977 for (size_t i = 0; i < LARGE_N; ++i)
978 mat.write(i, i, static_cast<int>(i));
979
980 // Verify
981 for (size_t i = 0; i < LARGE_N; ++i)
982 {
983 ASSERT_EQ(mat.read(i, i), static_cast<int>(i));
984 if (i + 1 < LARGE_N)
985 ASSERT_EQ(mat.read(i, i + 1), 0);
986 }
987}
988
989TEST(DynMatrix, stress_copy_large_matrix)
990{
991 DynMatrix<int> original(LARGE_N, LARGE_N, 0);
992
993 for (size_t i = 0; i < LARGE_N; ++i)
994 original.write(i, i, static_cast<int>(i));
995
996 DynMatrix<int> copy(original);
997
998 EXPECT_EQ(copy.rows(), LARGE_N);
999
1000 for (size_t i = 0; i < LARGE_N; ++i)
1001 ASSERT_EQ(copy.read(i, i), static_cast<int>(i));
1002}
1003
1004// =============================================================================
1005// Special Value Tests
1006// =============================================================================
1007
1008TEST(DynMatrix, floating_point_matrix)
1009{
1010 DynMatrix<double> mat(3, 3, 0.0);
1011
1012 mat.write(0, 0, 1.5);
1013 mat.write(1, 1, 2.7);
1014 mat.write(2, 2, 3.9);
1015
1016 EXPECT_DOUBLE_EQ(mat.read(0, 0), 1.5);
1017 EXPECT_DOUBLE_EQ(mat.read(1, 1), 2.7);
1018 EXPECT_DOUBLE_EQ(mat.read(2, 2), 3.9);
1019}
1020
1021TEST(DynMatrix, string_matrix)
1022{
1023 DynMatrix<string> mat(2, 2, "empty");
1024
1025 mat.write(0, 0, "hello");
1026 mat.write(1, 1, "world");
1027
1028 EXPECT_EQ(mat.read(0, 0), "hello");
1029 EXPECT_EQ(mat.read(0, 1), "empty");
1030 EXPECT_EQ(mat.read(1, 0), "empty");
1031 EXPECT_EQ(mat.read(1, 1), "world");
1032}
1033
1034// =============================================================================
1035// Regression Tests
1036// =============================================================================
1037
1038TEST(DynMatrix, regression_equality_different_shaped_same_size)
1039{
1040 // 2x6 vs 3x4 - same total size, different shape
1041 DynMatrix<int> mat1(2, 6, 0);
1042 DynMatrix<int> mat2(3, 4, 0);
1043
1044 EXPECT_FALSE(mat1 == mat2); // Should not be equal
1045}
1046
1047TEST(DynMatrix, regression_move_assignment_returns_reference)
1048{
1049 DynMatrix<int> mat1(2, 2);
1050 DynMatrix<int> mat2(3, 3);
1051
1052 DynMatrix<int>& ref = (mat1 = std::move(mat2));
1053
1054 EXPECT_EQ(&ref, &mat1);
1055}
1056
1057// =============================================================================
1058// Main
1059// =============================================================================
1060
1061// set_dimension() released the old storage before building the new one: if
1062// building threw, the matrix kept a dangling pointer that the destructor
1063// released again (heap-use-after-free). n * m could also overflow.
1064TEST(DynMatrixSetDimension, StrongGuaranteeWhenItThrows)
1065{
1066 DynMatrix<int> m(2, 3, -1);
1067 m.allocate();
1068 m.write(1, 2, 12);
1069
1070 EXPECT_THROW(m.set_dimension(size_t(1) << 40, size_t(1) << 40), std::overflow_error);
1071 EXPECT_THROW(m.set_dimension(std::numeric_limits<size_t>::max(), 2), std::overflow_error);
1072 EXPECT_THROW(m.set_dimension(size_t(1) << 62, 1), std::length_error);
1073
1074 // Unchanged: same dimensions, values and default value
1075 EXPECT_EQ(m.rows(), 2u);
1076 EXPECT_EQ(m.cols(), 3u);
1077 EXPECT_EQ(m.read(1, 2), 12);
1078 EXPECT_EQ(m.read(0, 0), -1);
1079
1080 m.set_dimension(4, 5);
1081 EXPECT_EQ(m.rows(), 4u);
1082 EXPECT_EQ(m.cols(), 5u);
1083 EXPECT_EQ(m.read(3, 4), -1); // the default value is kept
1084}
1085
1086// =============================================================================
1087// The moves of a matrix whose default value cannot be built
1088// =============================================================================
1089
1090namespace
1091{
1092 // Its default construction throws while fail is true
1093 struct Fragile_Zero
1094 {
1095 static inline bool fail = false;
1096 int value = 0;
1097
1098 Fragile_Zero()
1099 {
1100 if (fail)
1101 throw std::runtime_error("default construction failed");
1102 }
1103
1104 Fragile_Zero(int v) noexcept : value(v) {}
1105 };
1106} // namespace
1107
1108// A move leaves the matrix that it moves from with a default value built
1109// by default, and the constructor by default builds one too. They were
1110// noexcept: a default construction that threw, as one that allocates,
1111// ended the program. Their noexcept follows it now
1112static_assert(std::is_nothrow_default_constructible_v<DynMatrix<double>>);
1113static_assert(std::is_nothrow_move_constructible_v<DynMatrix<double>>);
1114static_assert(std::is_nothrow_move_assignable_v<DynMatrix<double>>);
1115static_assert(std::is_nothrow_move_constructible_v<DynMatrix<std::string>>);
1116static_assert(std::is_nothrow_move_assignable_v<DynMatrix<std::string>>);
1117static_assert(not std::is_nothrow_default_constructible_v<DynMatrix<Fragile_Zero>>);
1118static_assert(not std::is_nothrow_move_constructible_v<DynMatrix<Fragile_Zero>>);
1119static_assert(not std::is_nothrow_move_assignable_v<DynMatrix<Fragile_Zero>>);
1120
1121TEST(DynMatrixMove, ADefaultValueThatCannotBeBuiltLeavesTheMatricesAsTheyWere)
1122{
1123 DynMatrix<Fragile_Zero> source(2, 3, Fragile_Zero(7));
1124 source.write(1, 2, Fragile_Zero(5));
1125 DynMatrix<Fragile_Zero> other(1, 1, Fragile_Zero(9));
1126 other.write(0, 0, Fragile_Zero(4));
1127
1128 Fragile_Zero::fail = true;
1129 EXPECT_THROW({ DynMatrix<Fragile_Zero> fresh; }, std::runtime_error);
1130 EXPECT_THROW({ DynMatrix<Fragile_Zero> target(std::move(source)); }, std::runtime_error);
1131 EXPECT_THROW(other = std::move(source), std::runtime_error);
1132 Fragile_Zero::fail = false;
1133
1134 ASSERT_EQ(source.rows(), 2u);
1135 ASSERT_EQ(source.cols(), 3u);
1136 EXPECT_EQ(source.read(1, 2).value, 5);
1137 EXPECT_EQ(source.read(0, 0).value, 7);
1138 ASSERT_EQ(other.rows(), 1u);
1139 ASSERT_EQ(other.cols(), 1u);
1140 EXPECT_EQ(other.read(0, 0).value, 4);
1141 EXPECT_EQ(other.get_default_value().value, 9);
1142}
1143
1144TEST(DynMatrixMove, TheMatrixThatWasMovedFromIsAsOneBuiltByDefault)
1145{
1146 DynMatrix<Fragile_Zero> source(2, 3, Fragile_Zero(7));
1147 source.write(1, 2, Fragile_Zero(5));
1148
1149 DynMatrix<Fragile_Zero> target(std::move(source));
1150 ASSERT_EQ(target.rows(), 2u);
1151 ASSERT_EQ(target.cols(), 3u);
1152 EXPECT_EQ(target.read(1, 2).value, 5);
1153 EXPECT_EQ(target.read(0, 1).value, 7);
1154 EXPECT_EQ(source.rows(), 0u);
1155 EXPECT_EQ(source.cols(), 0u);
1156 EXPECT_EQ(source.get_default_value().value, 0);
1157
1158 DynMatrix<Fragile_Zero> other(1, 1, Fragile_Zero(9));
1159 other = std::move(target);
1160 ASSERT_EQ(other.rows(), 2u);
1161 ASSERT_EQ(other.cols(), 3u);
1162 EXPECT_EQ(other.read(1, 2).value, 5);
1163 EXPECT_EQ(other.read(0, 1).value, 7);
1164 EXPECT_EQ(target.rows(), 0u);
1165 EXPECT_EQ(target.get_default_value().value, 0);
1166
1167 // the ones that were moved from take dimensions again
1168 source.set_dimension(2, 2);
1169 source.write(1, 1, Fragile_Zero(3));
1170 EXPECT_EQ(source.read(1, 1).value, 3);
1171 EXPECT_EQ(source.read(0, 0).value, 0);
1172}
1173
1174namespace
1175{
1176 // Its move assignment throws while fail is true
1177 struct Fragile_Assignment
1178 {
1179 static inline bool fail = false;
1180 int value = 0;
1181
1182 Fragile_Assignment() noexcept = default;
1183 Fragile_Assignment(int v) noexcept : value(v) {}
1184 Fragile_Assignment(const Fragile_Assignment &) noexcept = default;
1185 Fragile_Assignment & operator = (const Fragile_Assignment &) noexcept = default;
1186 Fragile_Assignment(Fragile_Assignment &&) noexcept = default;
1187 Fragile_Assignment & operator = (Fragile_Assignment && other)
1188 {
1189 if (fail)
1190 throw std::runtime_error("move assignment failed");
1191 value = other.value;
1192 return *this;
1193 }
1194 };
1195} // namespace
1196
1197// swap() exchanges the default values first, the only step that may throw
1198static_assert(noexcept(std::declval<DynMatrix<double> &>().swap(std::declval<DynMatrix<double> &>())));
1199static_assert(not noexcept(std::declval<DynMatrix<Fragile_Assignment> &>()
1200 .swap(std::declval<DynMatrix<Fragile_Assignment> &>())));
1201
1202TEST(DynMatrixMove, ASwapOfTheDefaultValuesThatThrowsLeavesTheEntriesWhereTheyWere)
1203{
1204 DynMatrix<Fragile_Assignment> a(2, 3, Fragile_Assignment(7));
1205 a.write(1, 2, Fragile_Assignment(5));
1206 DynMatrix<Fragile_Assignment> b(1, 1, Fragile_Assignment(9));
1207 b.write(0, 0, Fragile_Assignment(4));
1208
1209 Fragile_Assignment::fail = true;
1210 EXPECT_THROW(a.swap(b), std::runtime_error);
1211 Fragile_Assignment::fail = false;
1212
1213 ASSERT_EQ(a.rows(), 2u);
1214 ASSERT_EQ(a.cols(), 3u);
1215 EXPECT_EQ(a.read(1, 2).value, 5);
1216 ASSERT_EQ(b.rows(), 1u);
1217 ASSERT_EQ(b.cols(), 1u);
1218 EXPECT_EQ(b.read(0, 0).value, 4);
1219}
1220
1221namespace
1222{
1223 // An item with a swap of its own, which cannot throw, and a move
1224 // assignment that may: std::swap() of two of them, which moves them, may
1225 // throw
1226 struct Own_Swap
1227 {
1228 static inline bool fail = false;
1229 static inline int swaps = 0;
1230
1231 int value = 0;
1232
1233 Own_Swap() noexcept = default;
1234 Own_Swap(int v) noexcept : value(v) {}
1235 Own_Swap(const Own_Swap &) = default;
1236 Own_Swap(Own_Swap && other) noexcept : value(other.value) {}
1237 Own_Swap & operator = (const Own_Swap &) = default;
1238
1239 Own_Swap & operator = (Own_Swap && other)
1240 {
1241 if (fail)
1242 throw std::runtime_error("the move assignment of the item");
1243 value = other.value;
1244 return *this;
1245 }
1246
1247 friend void swap(Own_Swap & a, Own_Swap & b) noexcept
1248 {
1249 ++swaps;
1250 std::swap(a.value, b.value);
1251 }
1252 };
1253}
1254
1255static_assert(std::is_nothrow_swappable_v<Own_Swap>);
1256static_assert(not std::is_nothrow_move_assignable_v<Own_Swap>);
1257static_assert(std::is_nothrow_move_constructible_v<DynMatrix<Own_Swap>>);
1258static_assert(std::is_nothrow_move_assignable_v<DynMatrix<Own_Swap>>);
1259
1260// The noexcept of swap() is that of the swap of the item found by its
1261// arguments, so that one is the swap that it calls: std::swap() alone
1262// would move the items, and their move may throw where their swap cannot
1263TEST(DynMatrixMove, TheSwapOfTheDefaultValuesIsTheOneOfTheItem)
1264{
1265 DynMatrix<Own_Swap> a(2, 3, Own_Swap(7));
1266 a.write(1, 2, Own_Swap(5));
1267 DynMatrix<Own_Swap> b(1, 1, Own_Swap(9));
1268 b.write(0, 0, Own_Swap(4));
1269
1270 Own_Swap::swaps = 0;
1271 Own_Swap::fail = true; // a move assignment of an item would end the program
1272 a.swap(b);
1273 EXPECT_EQ(Own_Swap::swaps, 1);
1274 DynMatrix<Own_Swap> c(std::move(a)); // what b had
1275 EXPECT_EQ(Own_Swap::swaps, 2);
1276 b = std::move(c);
1277 EXPECT_EQ(Own_Swap::swaps, 4);
1278 Own_Swap::fail = false;
1279
1280 ASSERT_EQ(b.rows(), 1u);
1281 ASSERT_EQ(b.cols(), 1u);
1282 EXPECT_EQ(b.read(0, 0).value, 4);
1283 EXPECT_EQ(a.rows(), 0u);
1284 EXPECT_EQ(c.rows(), 0u);
1285}
1286
1287int main(int argc, char **argv)
1288{
1289 ::testing::InitGoogleTest(&argc, argv);
1290 return RUN_ALL_TESTS();
1291}
Functional programming utilities for Aleph-w containers.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:122
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
Definition tpl_dynMat.H:514
const T & read(const size_t i, const size_t j) const
Read the entry at position (i, j).
Definition tpl_dynMat.H:480
constexpr bool is_empty() const noexcept
Check if the matrix is empty (uninitialized).
Definition tpl_dynMat.H:465
constexpr size_t cols() const noexcept
Get the number of columns.
Definition tpl_dynMat.H:447
void allocate()
Pre-allocate memory for the entire matrix.
Definition tpl_dynMat.H:271
const T & get_default_value() const noexcept
Get the default value for unwritten entries.
Definition tpl_dynMat.H:194
constexpr size_t rows() const noexcept
Get the number of rows.
Definition tpl_dynMat.H:441
void swap(DynMatrix &mat) noexcept(std::is_nothrow_swappable_v< T >)
Swap the contents of this matrix with another.
Definition tpl_dynMat.H:212
void set_default_initial_value(const T &value)
Set the default value for unwritten entries.
Definition tpl_dynMat.H:183
constexpr size_t size() const noexcept
Get the total number of entries.
Definition tpl_dynMat.H:453
constexpr bool is_square() const noexcept
Check if the matrix is square.
Definition tpl_dynMat.H:459
DynMatrix< T > transpose() const
Create a transposed copy of the matrix.
Definition tpl_dynMat.H:605
void fill(const T &value)
Fill all entries with a value.
Definition tpl_dynMat.H:590
Sparse matrix with generic row and column domains.
Definition al-matrix.H:70
Minimal std::expected-style result type for C++20.
#define TEST(name)
constexpr size_t LARGE_N
constexpr size_t SMALL_M
TEST_F(FilledMatrix, copy_constructor_creates_independent_copy)
constexpr size_t SMALL_N
constexpr size_t MEDIUM_N
constexpr size_t MEDIUM_M
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
Type that counts constructions/destructions.
Counted & operator=(Counted &&other) noexcept
Counted & operator=(const Counted &other)
Counted(Counted &&other) noexcept
static int moves
static int constructions
bool operator==(const Counted &other) const
static int destructions
static int copies
static void reset()
Counted(int v=0)
bool operator!=(const Counted &other) const
Counted(const Counted &other)
Fixture with a pre-filled matrix.
DynMatrix< int > mat
Fixture with a small integer matrix.
DynMatrix< int > mat
Fixture with a square matrix.
DynMatrix< double > mat
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dnode< int > Test
Definition testDnode.C:42
Dynamic matrix with lazy allocation.