Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_al_vector.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
54#include <gtest/gtest.h>
55#include <al-vector.H>
56#include <al-domain.H>
57#include <ah-errors.H>
58#include <cmath>
59#include <string>
60#include <type_traits>
61#include <vector>
62
63using namespace Aleph;
64using namespace testing;
65
66// ============================================================================
67// Test Fixture for Vector Tests
68// ============================================================================
69
70class VectorTest : public ::testing::Test {
71protected:
72 AlDomain<int> domain_5; // Domain with 5 elements {0,1,2,3,4}
73 AlDomain<int> domain_3; // Domain with 3 elements {0,1,2}
75
76 void SetUp() override {
77 // Set up integer domains
78 for (int i = 0; i < 5; ++i) {
80 }
81
82 for (int i = 0; i < 3; ++i) {
84 }
85
86 // Set up string domain
87 domain_str.insert("x");
88 domain_str.insert("y");
89 domain_str.insert("z");
90 }
91};
92
93// ============================================================================
94// Construction and Initialization Tests
95// ============================================================================
96
97TEST_F(VectorTest, DefaultConstructor) {
98 Vector<int, double> v(domain_5);
99
100 // All entries should be zero by default
101 for (int i = 0; i < 5; ++i) {
102 EXPECT_DOUBLE_EQ(v.get_entry(i), 0.0);
103 }
104}
105
106TEST_F(VectorTest, ConstructorWithCustomEpsilon) {
107 Vector<int, double> v(domain_5, 1e-10);
108
109 EXPECT_DOUBLE_EQ(v.get_epsilon(), 1e-10);
110}
111
112TEST_F(VectorTest, ConstructorFromDynList) {
113 DynList<double> values;
114 values.append(1.0);
115 values.append(2.0);
116 values.append(3.0);
117 values.append(4.0);
118 values.append(5.0);
119
120 Vector<int, double> v(domain_5, values);
121
122 for (int i = 0; i < 5; ++i) {
123 EXPECT_DOUBLE_EQ(v.get_entry(i), static_cast<double>(i + 1));
124 }
125}
126
127TEST_F(VectorTest, ConstructorFromDynListSizeMismatch) {
128 DynList<double> values;
129 values.append(1.0);
130 values.append(2.0);
131
132 // Should throw because domain has 5 elements but list has 2
133 EXPECT_THROW((Vector<int, double>(domain_5, values)), std::exception);
134}
135
136TEST_F(VectorTest, CopyConstructor) {
137 Vector<int, double> v1(domain_5);
138 v1.set_entry(0, 1.5);
139 v1.set_entry(2, 3.7);
140
141 Vector<int, double> v2(v1);
142
143 EXPECT_DOUBLE_EQ(v2.get_entry(0), 1.5);
144 EXPECT_DOUBLE_EQ(v2.get_entry(2), 3.7);
145 EXPECT_DOUBLE_EQ(v2.get_entry(1), 0.0);
146}
147
148TEST_F(VectorTest, MoveConstructor) {
149 Vector<int, double> v1(domain_5);
150 v1.set_entry(0, 1.5);
151 v1.set_entry(2, 3.7);
152
153 Vector<int, double> v2(std::move(v1));
154
155 EXPECT_DOUBLE_EQ(v2.get_entry(0), 1.5);
156 EXPECT_DOUBLE_EQ(v2.get_entry(2), 3.7);
157}
158
159TEST_F(VectorTest, CopyAssignment) {
160 Vector<int, double> v1(domain_5);
161 v1.set_entry(0, 1.5);
162
163 Vector<int, double> v2(domain_5);
164 v2 = v1;
165
166 EXPECT_DOUBLE_EQ(v2.get_entry(0), 1.5);
167}
168
169TEST_F(VectorTest, CopyAssignmentSelfAssignment) {
170 Vector<int, double> v1(domain_5);
171 v1.set_entry(0, 1.5);
172
173 v1 = v1; // Self-assignment
174
175 EXPECT_DOUBLE_EQ(v1.get_entry(0), 1.5);
176}
177
178TEST_F(VectorTest, MoveAssignment) {
179 Vector<int, double> v1(domain_5);
180 v1.set_entry(0, 1.5);
181
182 Vector<int, double> v2(domain_5);
183 v2 = std::move(v1);
184
185 EXPECT_DOUBLE_EQ(v2.get_entry(0), 1.5);
186}
187
188// ============================================================================
189// Entry Access and Modification Tests
190// ============================================================================
191
192TEST_F(VectorTest, SetAndGetEntry) {
193 Vector<int, double> v(domain_5);
194
195 v.set_entry(0, 1.5);
196 v.set_entry(2, -3.2);
197 v.set_entry(4, 5.7);
198
199 EXPECT_DOUBLE_EQ(v.get_entry(0), 1.5);
200 EXPECT_DOUBLE_EQ(v.get_entry(1), 0.0);
201 EXPECT_DOUBLE_EQ(v.get_entry(2), -3.2);
202 EXPECT_DOUBLE_EQ(v.get_entry(3), 0.0);
203 EXPECT_DOUBLE_EQ(v.get_entry(4), 5.7);
204}
205
206TEST_F(VectorTest, SetEntryToZeroRemovesIt) {
207 Vector<int, double> v(domain_5);
208
209 v.set_entry(2, 5.0);
210 EXPECT_DOUBLE_EQ(v.get_entry(2), 5.0);
211
212 v.set_entry(2, 0.0);
213 EXPECT_DOUBLE_EQ(v.get_entry(2), 0.0);
214
215 // Entry should be removed from internal storage
216 EXPECT_EQ(v.search_entry(2), nullptr);
217}
218
219TEST_F(VectorTest, SetEntriesWithInitializerLists) {
220 Vector<int, double> v(domain_5);
221
222 v.set_entries({0, 2, 4}, {1.0, 2.0, 3.0});
223
224 EXPECT_DOUBLE_EQ(v.get_entry(0), 1.0);
225 EXPECT_DOUBLE_EQ(v.get_entry(1), 0.0);
226 EXPECT_DOUBLE_EQ(v.get_entry(2), 2.0);
227 EXPECT_DOUBLE_EQ(v.get_entry(3), 0.0);
228 EXPECT_DOUBLE_EQ(v.get_entry(4), 3.0);
229}
230
231TEST_F(VectorTest, SetEntriesSizeMismatch) {
232 Vector<int, double> v(domain_5);
233
234 EXPECT_THROW(v.set_entries({0, 1}, {1.0, 2.0, 3.0}), std::exception);
235}
236
237TEST_F(VectorTest, SearchEntry) {
238 Vector<int, double> v(domain_5);
239
240 v.set_entry(2, 3.14);
241
242 double * ptr = v.search_entry(2);
243 ASSERT_NE(ptr, nullptr);
244 EXPECT_DOUBLE_EQ(*ptr, 3.14);
245
246 double * null_ptr = v.search_entry(1);
247 EXPECT_EQ(null_ptr, nullptr);
248}
249
250TEST_F(VectorTest, GetEntryConst) {
251 Vector<int, double> v(domain_5);
252 v.set_entry(2, 3.14);
253
254 const Vector<int, double>& cv = v;
255 EXPECT_DOUBLE_EQ(cv.get_entry(2), 3.14);
256 EXPECT_DOUBLE_EQ(cv.get_entry(1), 0.0);
257}
258
259// ============================================================================
260// Epsilon Tests
261// ============================================================================
262
263TEST_F(VectorTest, EpsilonDefaultValue) {
264 Vector<int, double> v(domain_5);
265 EXPECT_DOUBLE_EQ(v.get_epsilon(), 1e-7);
266}
267
268TEST_F(VectorTest, SetEpsilon) {
269 Vector<int, double> v(domain_5);
270
271 v.set_epsilon(1e-10);
272 EXPECT_DOUBLE_EQ(v.get_epsilon(), 1e-10);
273}
274
275TEST_F(VectorTest, EpsilonAffectsZeroComparison) {
276 Vector<int, double> v(domain_5, 1e-5);
277
278 v.set_entry(0, 1e-6); // Less than epsilon
279 EXPECT_DOUBLE_EQ(v.get_entry(0), 0.0); // Should be treated as zero
280
281 v.set_entry(1, 1e-4); // Greater than epsilon
282 EXPECT_DOUBLE_EQ(v.get_entry(1), 1e-4); // Should not be zero
283}
284
285TEST_F(VectorTest, AreEqual) {
286 Vector<int, double> v(domain_5, 1e-5);
287
288 EXPECT_TRUE(v.are_equal(1.0000001, 1.0000002)); // Within epsilon
289 EXPECT_FALSE(v.are_equal(1.0, 1.0001)); // Beyond epsilon
290}
291
292// ============================================================================
293// Arithmetic Operations Tests
294// ============================================================================
295
296TEST_F(VectorTest, VectorAddition) {
297 Vector<int, double> v1(domain_5);
298 v1.set_entry(0, 1.0);
299 v1.set_entry(2, 3.0);
300
301 Vector<int, double> v2(domain_5);
302 v2.set_entry(1, 2.0);
303 v2.set_entry(2, 4.0);
304
305 Vector<int, double> v3 = v1 + v2;
306
307 EXPECT_DOUBLE_EQ(v3.get_entry(0), 1.0);
308 EXPECT_DOUBLE_EQ(v3.get_entry(1), 2.0);
309 EXPECT_DOUBLE_EQ(v3.get_entry(2), 7.0);
310 EXPECT_DOUBLE_EQ(v3.get_entry(3), 0.0);
311}
312
313TEST_F(VectorTest, VectorAdditionInPlace) {
314 Vector<int, double> v1(domain_5);
315 v1.set_entry(0, 1.0);
316 v1.set_entry(2, 3.0);
317
318 Vector<int, double> v2(domain_5);
319 v2.set_entry(1, 2.0);
320 v2.set_entry(2, 4.0);
321
322 v1 += v2;
323
324 EXPECT_DOUBLE_EQ(v1.get_entry(0), 1.0);
325 EXPECT_DOUBLE_EQ(v1.get_entry(1), 2.0);
326 EXPECT_DOUBLE_EQ(v1.get_entry(2), 7.0);
327}
328
329TEST_F(VectorTest, VectorSubtraction) {
330 Vector<int, double> v1(domain_5);
331 v1.set_entry(0, 5.0);
332 v1.set_entry(2, 3.0);
333
334 Vector<int, double> v2(domain_5);
335 v2.set_entry(0, 2.0);
336 v2.set_entry(2, 1.0);
337
338 Vector<int, double> v3 = v1 - v2;
339
340 EXPECT_DOUBLE_EQ(v3.get_entry(0), 3.0);
341 EXPECT_DOUBLE_EQ(v3.get_entry(2), 2.0);
342}
343
344TEST_F(VectorTest, VectorSubtractionInPlace) {
345 Vector<int, double> v1(domain_5);
346 v1.set_entry(0, 5.0);
347 v1.set_entry(2, 3.0);
348
349 Vector<int, double> v2(domain_5);
350 v2.set_entry(0, 2.0);
351 v2.set_entry(2, 1.0);
352
353 v1 -= v2;
354
355 EXPECT_DOUBLE_EQ(v1.get_entry(0), 3.0);
356 EXPECT_DOUBLE_EQ(v1.get_entry(2), 2.0);
357}
358
359TEST_F(VectorTest, ScalarMultiplication) {
360 Vector<int, double> v(domain_5);
361 v.set_entry(0, 2.0);
362 v.set_entry(2, 3.0);
363
364 Vector<int, double> v2 = v * 2.5;
365
366 EXPECT_DOUBLE_EQ(v2.get_entry(0), 5.0);
367 EXPECT_DOUBLE_EQ(v2.get_entry(2), 7.5);
368}
369
370TEST_F(VectorTest, ScalarMultiplicationFromLeft) {
371 Vector<int, double> v(domain_5);
372 v.set_entry(0, 2.0);
373 v.set_entry(2, 3.0);
374
375 Vector<int, double> v2 = 2.5 * v;
376
377 EXPECT_DOUBLE_EQ(v2.get_entry(0), 5.0);
378 EXPECT_DOUBLE_EQ(v2.get_entry(2), 7.5);
379}
380
381TEST_F(VectorTest, ScalarMultiplicationByZero) {
382 Vector<int, double> v(domain_5);
383 v.set_entry(0, 2.0);
384 v.set_entry(2, 3.0);
385
386 Vector<int, double> v2 = v * 0.0;
387
388 // All entries should be zero
389 for (int i = 0; i < 5; ++i) {
390 EXPECT_DOUBLE_EQ(v2.get_entry(i), 0.0);
391 }
392}
393
394TEST_F(VectorTest, ScalarMultiplicationByOne) {
395 Vector<int, double> v(domain_5);
396 v.set_entry(0, 2.0);
397 v.set_entry(2, 3.0);
398
399 Vector<int, double> v2 = v * 1.0;
400
401 EXPECT_DOUBLE_EQ(v2.get_entry(0), 2.0);
402 EXPECT_DOUBLE_EQ(v2.get_entry(2), 3.0);
403}
404
405TEST_F(VectorTest, ScalarDivision) {
406 Vector<int, double> v(domain_5);
407 v.set_entry(0, 10.0);
408 v.set_entry(2, 5.0);
409
410 Vector<int, double> v2 = v / 2.0;
411
412 EXPECT_DOUBLE_EQ(v2.get_entry(0), 5.0);
413 EXPECT_DOUBLE_EQ(v2.get_entry(2), 2.5);
414}
415
416TEST_F(VectorTest, ScalarDivisionByZero) {
417 Vector<int, double> v(domain_5);
418 v.set_entry(0, 10.0);
419
420 EXPECT_THROW(v / 0.0, std::exception);
421}
422
423TEST_F(VectorTest, ScalarDivisionByOne) {
424 Vector<int, double> v(domain_5);
425 v.set_entry(0, 2.0);
426 v.set_entry(2, 3.0);
427
428 Vector<int, double> v2 = v / 1.0;
429
430 EXPECT_DOUBLE_EQ(v2.get_entry(0), 2.0);
431 EXPECT_DOUBLE_EQ(v2.get_entry(2), 3.0);
432}
433
434TEST_F(VectorTest, UnaryMinus) {
435 Vector<int, double> v(domain_5);
436 v.set_entry(0, 2.0);
437 v.set_entry(2, -3.0);
438
439 Vector<int, double> v2 = -v;
440
441 EXPECT_DOUBLE_EQ(v2.get_entry(0), -2.0);
442 EXPECT_DOUBLE_EQ(v2.get_entry(2), 3.0);
443}
444
445// ============================================================================
446// Scalar Product (Dot Product) Tests
447// ============================================================================
448
449TEST_F(VectorTest, ScalarProduct) {
450 Vector<int, double> v1(domain_5);
451 v1.set_entry(0, 1.0);
452 v1.set_entry(1, 2.0);
453 v1.set_entry(2, 3.0);
454
455 Vector<int, double> v2(domain_5);
456 v2.set_entry(0, 4.0);
457 v2.set_entry(1, 5.0);
458 v2.set_entry(2, 6.0);
459
460 // 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
461 double result = v1.scalar_product(v2);
462
463 EXPECT_DOUBLE_EQ(result, 32.0);
464}
465
466TEST_F(VectorTest, ScalarProductOperator) {
467 Vector<int, double> v1(domain_5);
468 v1.set_entry(0, 1.0);
469 v1.set_entry(1, 2.0);
470
471 Vector<int, double> v2(domain_5);
472 v2.set_entry(0, 3.0);
473 v2.set_entry(1, 4.0);
474
475 // 1*3 + 2*4 = 3 + 8 = 11
476 double result = v1 * v2;
477
478 EXPECT_DOUBLE_EQ(result, 11.0);
479}
480
481TEST_F(VectorTest, ScalarProductOrthogonalVectors) {
482 Vector<int, double> v1(domain_5);
483 v1.set_entry(0, 1.0);
484 v1.set_entry(1, 0.0);
485
486 Vector<int, double> v2(domain_5);
487 v2.set_entry(0, 0.0);
488 v2.set_entry(1, 1.0);
489
490 double result = v1 * v2;
491
492 EXPECT_DOUBLE_EQ(result, 0.0);
493}
494
495TEST_F(VectorTest, ScalarProductWithZeroVector) {
496 Vector<int, double> v1(domain_5);
497 v1.set_entry(0, 1.0);
498 v1.set_entry(1, 2.0);
499
500 Vector<int, double> v2(domain_5); // Zero vector
501
502 double result = v1 * v2;
503
504 EXPECT_DOUBLE_EQ(result, 0.0);
505}
506
507// ============================================================================
508// Comparison Tests
509// ============================================================================
510
511TEST_F(VectorTest, EqualityOperator) {
512 Vector<int, double> v1(domain_5);
513 v1.set_entry(0, 1.0);
514 v1.set_entry(2, 3.0);
515
516 Vector<int, double> v2(domain_5);
517 v2.set_entry(0, 1.0);
518 v2.set_entry(2, 3.0);
519
520 EXPECT_TRUE(v1 == v2);
521}
522
523TEST_F(VectorTest, EqualityWithEpsilon) {
524 Vector<int, double> v1(domain_5, 1e-5);
525 v1.set_entry(0, 1.0);
526
527 Vector<int, double> v2(domain_5, 1e-5);
528 v2.set_entry(0, 1.0 + 1e-6); // Within epsilon
529
530 EXPECT_TRUE(v1 == v2);
531}
532
533TEST_F(VectorTest, InequalityOperator) {
534 Vector<int, double> v1(domain_5);
535 v1.set_entry(0, 1.0);
536
537 Vector<int, double> v2(domain_5);
538 v2.set_entry(0, 2.0);
539
540 EXPECT_TRUE(v1 != v2);
541}
542
543TEST_F(VectorTest, EqualityZeroVectors) {
544 Vector<int, double> v1(domain_5);
545 Vector<int, double> v2(domain_5);
546
547 EXPECT_TRUE(v1 == v2);
548}
549
550// ============================================================================
551// Proxy Access Tests
552// ============================================================================
553
554TEST_F(VectorTest, ProxyReadAccess) {
555 Vector<int, double> v(domain_5);
556 v.set_entry(2, 3.14);
557
558 double val = v[2];
559 EXPECT_DOUBLE_EQ(val, 3.14);
560
561 double val_zero = v[1];
562 EXPECT_DOUBLE_EQ(val_zero, 0.0);
563}
564
565TEST_F(VectorTest, ProxyWriteAccess) {
566 Vector<int, double> v(domain_5);
567
568 v[2] = 3.14;
569
570 EXPECT_DOUBLE_EQ(v.get_entry(2), 3.14);
571}
572
573TEST_F(VectorTest, ProxyWriteZero) {
574 Vector<int, double> v(domain_5);
575 v.set_entry(2, 5.0);
576
577 v[2] = 0.0;
578
579 EXPECT_DOUBLE_EQ(v.get_entry(2), 0.0);
580 EXPECT_EQ(v.search_entry(2), nullptr);
581}
582
583TEST_F(VectorTest, ProxyParenthesesOperator) {
584 Vector<int, double> v(domain_5);
585 v(2) = 3.14;
586
587 double val = v(2);
588 EXPECT_DOUBLE_EQ(val, 3.14);
589}
590
591TEST_F(VectorTest, ProxyAssignmentBetweenEntries) {
592 Vector<int, double> v(domain_5);
593 v[0] = 3.14;
594 v[1] = v[0];
595
596 EXPECT_DOUBLE_EQ(v.get_entry(1), 3.14);
597}
598
599// ============================================================================
600// Iterator Tests
601// ============================================================================
602
603TEST_F(VectorTest, IteratorBasic) {
604 Vector<int, double> v(domain_5);
605 v.set_entry(0, 1.0);
606 v.set_entry(2, 3.0);
607 v.set_entry(4, 5.0);
608
609 auto it = v.get_it();
610
611 int count = 0;
612 while (it.has_curr()) {
613 (void)it.get_curr();
614 count++;
615 it.next();
616 }
617
618 EXPECT_EQ(count, 3); // Only 3 non-zero entries
619}
620
621// ============================================================================
622// Conversion and Output Tests
623// ============================================================================
624
626 Vector<int, double> v(domain_3);
627 v.set_entry(0, 1.0);
628 v.set_entry(1, 2.0);
629 v.set_entry(2, 3.0);
630
631 DynList<double> list = v.to_list();
632
633 EXPECT_EQ(list.size(), 3);
634}
635
637 Vector<int, double> v(domain_3);
638 v.set_entry(0, 1.0);
639 v.set_entry(1, 2.0);
640 v.set_entry(2, 3.0);
641
642 std::string str = v.to_str();
643
644 EXPECT_FALSE(str.empty());
645}
646
647// ============================================================================
648// String Domain Tests
649// ============================================================================
650
651TEST_F(VectorTest, StringDomain) {
652 Vector<std::string, double> v(domain_str);
653
654 v.set_entry("x", 1.0);
655 v.set_entry("y", 2.0);
656 v.set_entry("z", 3.0);
657
658 EXPECT_DOUBLE_EQ(v.get_entry("x"), 1.0);
659 EXPECT_DOUBLE_EQ(v.get_entry("y"), 2.0);
660 EXPECT_DOUBLE_EQ(v.get_entry("z"), 3.0);
661}
662
663TEST_F(VectorTest, StringDomainArithmetic) {
664 Vector<std::string, double> v1(domain_str);
665 v1.set_entry("x", 1.0);
666 v1.set_entry("y", 2.0);
667
668 Vector<std::string, double> v2(domain_str);
669 v2.set_entry("x", 3.0);
670 v2.set_entry("z", 4.0);
671
672 Vector<std::string, double> v3 = v1 + v2;
673
674 EXPECT_DOUBLE_EQ(v3.get_entry("x"), 4.0);
675 EXPECT_DOUBLE_EQ(v3.get_entry("y"), 2.0);
676 EXPECT_DOUBLE_EQ(v3.get_entry("z"), 4.0);
677}
678
679// ============================================================================
680// Edge Cases and Stress Tests
681// ============================================================================
682
683TEST_F(VectorTest, VerySmallEpsilon) {
684 Vector<int, double> v(domain_5, 1e-15);
685
686 v.set_entry(0, 1e-14);
687 EXPECT_DOUBLE_EQ(v.get_entry(0), 1e-14);
688
689 v.set_entry(1, 1e-16);
690 EXPECT_DOUBLE_EQ(v.get_entry(1), 0.0); // Should be treated as zero
691}
692
693TEST_F(VectorTest, NegativeValues) {
694 Vector<int, double> v(domain_5);
695 v.set_entry(0, -1.5);
696 v.set_entry(2, -3.7);
697
698 EXPECT_DOUBLE_EQ(v.get_entry(0), -1.5);
699 EXPECT_DOUBLE_EQ(v.get_entry(2), -3.7);
700}
701
702TEST_F(VectorTest, LargeValues) {
703 Vector<int, double> v(domain_5);
704 v.set_entry(0, 1e100);
705 v.set_entry(2, -1e100);
706
707 EXPECT_DOUBLE_EQ(v.get_entry(0), 1e100);
708 EXPECT_DOUBLE_EQ(v.get_entry(2), -1e100);
709}
710
711TEST_F(VectorTest, MixedOperations) {
712 Vector<int, double> v1(domain_5);
713 v1.set_entry(0, 10.0);
714 v1.set_entry(1, 20.0);
715
716 Vector<int, double> v2(domain_5);
717 v2.set_entry(0, 5.0);
718 v2.set_entry(2, 15.0);
719
720 // (v1 + v2) * 2 - v1
721 Vector<int, double> result = (v1 + v2) * 2.0 - v1;
722
723 EXPECT_DOUBLE_EQ(result.get_entry(0), 20.0); // (10+5)*2 - 10 = 30 - 10 = 20
724 EXPECT_DOUBLE_EQ(result.get_entry(1), 20.0); // (20+0)*2 - 20 = 40 - 20 = 20
725 EXPECT_DOUBLE_EQ(result.get_entry(2), 30.0); // (0+15)*2 - 0 = 30
726}
727
728TEST_F(VectorTest, ChainedOperations) {
729 Vector<int, double> v(domain_5);
730 v.set_entry(0, 1.0);
731
732 Vector<int, double> v2(domain_5);
733 v2.set_entry(0, 2.0);
734
735 v += v2;
736 v = v * 2.0;
737 v -= v2;
738
739 EXPECT_DOUBLE_EQ(v.get_entry(0), 4.0); // (1+2)*2 - 2 = 4
740}
741
742// ============================================================================
743// Functional Methods Tests (if available)
744// ============================================================================
745
746TEST_F(VectorTest, MapAndFilter) {
747 Vector<int, double> v(domain_5);
748 v.set_entry(0, 1.0);
749 v.set_entry(1, 2.0);
750 v.set_entry(2, 3.0);
751 v.set_entry(3, 4.0);
752 v.set_entry(4, 5.0);
753
754 // Test that we can iterate over non-zero entries
755 int count = 0;
756 auto it = v.get_it();
757 while (it.has_curr()) {
758 count++;
759 it.next();
760 }
761
762 EXPECT_EQ(count, 5);
763}
764
765// ============================================================================
766// Domain Tests
767// ============================================================================
768
769TEST_F(VectorTest, GetDomain) {
770 Vector<int, double> v(domain_5);
771
772 const auto& d = v.get_domain();
773 EXPECT_EQ(d.size(), 5);
774}
775
776// ============================================================================
777// Float Type Tests
778// ============================================================================
779
780TEST(VectorFloatTest, FloatType) {
781 AlDomain<int> domain;
782 domain.insert(0);
783 domain.insert(1);
784 domain.insert(2);
785
786 Vector<int, float> v(domain);
787 v.set_entry(0, 1.5f);
788 v.set_entry(1, 2.5f);
789
790 EXPECT_FLOAT_EQ(v.get_entry(0), 1.5f);
791 EXPECT_FLOAT_EQ(v.get_entry(1), 2.5f);
792}
793
794TEST(VectorFloatTest, FloatArithmetic) {
795 AlDomain<int> domain;
796 domain.insert(0);
797 domain.insert(1);
798
799 Vector<int, float> v1(domain);
800 v1.set_entry(0, 1.5f);
801
802 Vector<int, float> v2(domain);
803 v2.set_entry(0, 2.5f);
804
805 Vector<int, float> v3 = v1 + v2;
806
807 EXPECT_FLOAT_EQ(v3.get_entry(0), 4.0f);
808}
809
810// ============================================================================
811// Comprehensive Integration Test
812// ============================================================================
813
814TEST_F(VectorTest, ComprehensiveIntegrationTest) {
815 // Create two vectors
816 Vector<int, double> v1(domain_5);
817 v1.set_entries({0, 1, 2}, {1.0, 2.0, 3.0});
818
819 Vector<int, double> v2(domain_5);
820 v2.set_entries({1, 2, 3}, {1.0, 1.0, 1.0});
821
822 // Test addition
823 Vector<int, double> sum = v1 + v2;
824 EXPECT_DOUBLE_EQ(sum.get_entry(0), 1.0);
825 EXPECT_DOUBLE_EQ(sum.get_entry(1), 3.0);
826 EXPECT_DOUBLE_EQ(sum.get_entry(2), 4.0);
827 EXPECT_DOUBLE_EQ(sum.get_entry(3), 1.0);
828
829 // Test scalar multiplication
830 Vector<int, double> scaled = sum * 0.5;
831 EXPECT_DOUBLE_EQ(scaled.get_entry(0), 0.5);
832 EXPECT_DOUBLE_EQ(scaled.get_entry(1), 1.5);
833
834 // Test scalar product
835 double dot = v1 * v2;
836 EXPECT_DOUBLE_EQ(dot, 2.0 + 3.0); // 1*0 + 2*1 + 3*1 + 0*1 = 5
837
838 // Test equality
840 EXPECT_TRUE(copy == v1);
841
842 // Test modification
843 copy[0] = 10.0;
844 EXPECT_FALSE(copy == v1);
845 EXPECT_DOUBLE_EQ(copy.get_entry(0), 10.0);
846
847 // Test negation
848 Vector<int, double> neg = -v1;
849 EXPECT_DOUBLE_EQ(neg.get_entry(0), -1.0);
850 EXPECT_DOUBLE_EQ(neg.get_entry(1), -2.0);
851 EXPECT_DOUBLE_EQ(neg.get_entry(2), -3.0);
852}
853
854// ============================================================================
855// The proxy of v[k], to_list(), constant vectors and temporary domains
856// ============================================================================
857
858namespace
859{
860 // The values of v, as to_list() gives them
861 std::vector<double> values_of(const Vector<int, double> & v)
862 {
863 std::vector<double> ret;
864 v.to_list().for_each([&ret] (double x) { ret.push_back(x); });
865 return ret;
866 }
867}
868
869TEST(VectorProxy, AssigningAZeroEntryClearsTheTarget)
870{
871 // The assignment of a proxy of a zero entry did nothing
872 const IntRange d(3);
874 v.set_entry(0, 5);
875 v[0] = v[1];
876 EXPECT_EQ(v.get_entry(0), 0.0);
877 EXPECT_EQ(v.search_entry(0), nullptr);
878
879 v.set_entry(2, 4);
880 v[1] = v[2];
881 EXPECT_EQ(v.get_entry(1), 4.0);
882}
883
884TEST(VectorProxy, AssignsAnLvalue)
885{
886 // The assignment of an lvalue did not compile
887 const IntRange d(3);
889 double x = 3;
890 v[1] = x;
891 EXPECT_EQ(v.get_entry(1), 3.0);
892
893 const double zero = 0;
894 v[1] = zero;
895 EXPECT_EQ(v.search_entry(1), nullptr);
896
897 const double tiny = 1e-9; // within the epsilon of the vector
898 v[2] = tiny;
899 EXPECT_EQ(v.search_entry(2), nullptr);
900}
901
902TEST(VectorProxy, KeepsItsOwnKey)
903{
904 // The proxy pointed to the key, here a temporary that is gone when the
905 // proxy is assigned
906 const IntRange d(3);
908 auto p = v[2];
909 p = 7.0;
910 EXPECT_EQ(v.get_entry(2), 7.0);
911
912 const AlDomain<std::string> words = {"one", "two"};
914 auto q = w[std::string("two")];
915 q = 2.0;
916 EXPECT_EQ(w.get_entry("two"), 2.0);
917}
918
919TEST(VectorProxy, ReadsWhatItWasAssigned)
920{
921 // The proxy kept the entry that it found when it was made
922 const IntRange d(10);
924 auto p = v[5];
925 p = 9.0;
926 EXPECT_EQ(static_cast<double>(p), 9.0);
927
928 v[6] = v[7] = 4.0;
929 EXPECT_EQ(v.get_entry(6), 4.0);
930 EXPECT_EQ(v.get_entry(7), 4.0);
931}
932
933TEST(VectorProxy, SurvivesTheGrowthOfTheTable)
934{
935 // The proxy pointed to the entry, which an insertion that grows the
936 // table moves
937 const IntRange d(100000);
939 v.set_entry(0, 1);
940 auto p = v[0];
941 for (int i = 1; i < 5000; ++i)
942 v.set_entry(i, i);
943 p = 2.0;
944 EXPECT_EQ(v.get_entry(0), 2.0);
945 EXPECT_EQ(static_cast<double>(p), 2.0);
946
947 for (int i = 5000; i < 10000; ++i)
948 v[i] = v[0];
949 for (int i = 5000; i < 10000; ++i)
950 ASSERT_EQ(v.get_entry(i), 2.0) << i;
951}
952
953TEST(Vector, IndexesAConstantVector)
954{
955 // v[k] and v(k) of a constant vector did not compile
956 const IntRange d(3);
958 v.set_entry(1, 2);
959 const Vector<int, double> & cv = v;
960 EXPECT_EQ(cv[1], 2.0);
961 EXPECT_EQ(cv(1), 2.0);
962 EXPECT_EQ(cv[0], 0.0);
963 static_assert(std::is_same_v<decltype(cv[1]), double>);
964}
965
966TEST(Vector, ToListFollowsTheKeys)
967{
968 // to_list() sorted the values
969 const IntRange d(3);
970 const Vector<int, double> v(d, DynList<double>({3, 1, 2}));
971 EXPECT_EQ(values_of(v), (std::vector<double>{3, 1, 2}));
972
973 const IntRange e(5);
975 w.set_entry(3, -1);
976 w.set_entry(0, 8);
977 EXPECT_EQ(values_of(w), (std::vector<double>{8, 0, 0, -1, 0}));
978}
979
980TEST(Vector, KeepsATemporaryDomain)
981{
982 // The vector pointed to the temporary domain, gone after the declaration
984 v.set_entry(3, 1.5);
985 EXPECT_EQ(v.get_entry(3), 1.5);
986 EXPECT_EQ(v.get_domain().size(), 10u);
987 EXPECT_EQ(v.get_domain_ptr().use_count(), 1);
988
989 const Vector<int, double> c = v;
990 EXPECT_EQ(c.get_domain_ptr(), v.get_domain_ptr());
991 EXPECT_EQ(v.get_domain_ptr().use_count(), 2);
992 EXPECT_EQ((v + c).get_entry(3), 3.0);
993
994 const Vector<int, double> w(IntRange(3), DynList<double>({1, 2, 3}));
995 EXPECT_EQ(values_of(w), (std::vector<double>{1, 2, 3}));
996
997 // A domain that the program keeps stays its own: the vector points to it
998 const IntRange d(4);
999 const Vector<int, double> u(d);
1000 EXPECT_EQ(&u.get_domain(), static_cast<const AlDomain<int> *>(&d));
1001}
1002
1003TEST(Vector, NeedsADomain)
1004{
1005 // Vector(10) turned 10 into an empty temporary domain
1006 static_assert(not std::is_constructible_v<Vector<int, double>, int>);
1007 static_assert(not std::is_constructible_v<Vector<int, double>, size_t>);
1008 static_assert(not std::is_constructible_v<Vector<int, double>, int, double>);
1009 static_assert(not std::is_constructible_v<Vector<int, double>, int,
1010 const DynList<double> &>);
1011 static_assert(std::is_constructible_v<Vector<int, double>, IntRange>);
1012 static_assert(std::is_constructible_v<Vector<int, double>, const IntRange &>);
1013 static_assert(std::is_constructible_v<Vector<int, double>,
1014 std::shared_ptr<AlDomain<int>>>);
1015 SUCCEED();
1016}
1017
1018// ============================================================================
1019// Main
1020// ============================================================================
1021
1022int main(int argc, char **argv) {
1023 ::testing::InitGoogleTest(&argc, argv);
1024 return RUN_ALL_TESTS();
1025}
Exception handling system with formatted messages for Aleph-w.
Integer domain classes for sparse data structures.
Sparse vector with named elements.
int main()
long double w
Definition btreepic.C:153
Generic domain class based on hash set.
Definition al-domain.H:132
T * insert(const T &key)
Insert key, as HashSet::insert(), and forget the sorted keys.
Definition al-domain.H:162
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Sparse vector implementation with domain-based indexing.
Definition al-vector.H:155
NumType scalar_product(const Vector &v) const and AlMultiplicativeNumber< NumType >
The scalar product of this vector and v, over the same domain.
Definition al-vector.H:683
const Domain & get_domain() const noexcept
Get the domain over which this vector is defined.
Definition al-vector.H:205
bool are_equal(const NumType &n1, const NumType &n2) const noexcept
Check if two numeric values are equal within epsilon.
Definition al-vector.H:542
void set_entry(const T &i, const NumType &value)
Set a vector entry at given index.
Definition al-vector.H:381
const NumType & get_epsilon() const noexcept
Get the epsilon value used for zero comparisons.
Definition al-vector.H:187
NumType * search_entry(const T &i) noexcept
Search for an entry and return pointer to its value.
Definition al-vector.H:512
DynList< NumType > to_list() const
The values of the vector, in the order of the sorted keys of its domain.
Definition al-vector.H:719
Iterator get_it() noexcept
Definition al-vector.H:878
void set_epsilon(const NumType &e) noexcept
Set the epsilon value for zero comparisons.
Definition al-vector.H:194
NumType get_entry(const T &i)
Get vector entry at given index (non-const version)
Definition al-vector.H:474
void set_entries(std::initializer_list< T > ld, std::initializer_list< NumType > lr)
Set multiple entries using initializer lists.
Definition al-vector.H:427
const DomainPtr & get_domain_ptr() const noexcept
Return the shared pointer to the domain.
Definition al-vector.H:208
std::string to_str() const and AlStreamable< T > and
The keys over their values, in the order of the keys.
Definition al-vector.H:740
void SetUp() override
AlDomain< std::string > domain_str
AlDomain< int > domain_3
AlDomain< int > domain_5
#define TEST(name)
Main namespace for Aleph-w library functions.
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.
Integer range domain [start, end] with optional step.
Definition al-domain.H:311
TEST_F(VectorTest, DefaultConstructor)