-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
2424 lines (2411 loc) · 93.6 KB
/
test.cpp
File metadata and controls
2424 lines (2411 loc) · 93.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "./src/Graph.hpp"
#include "./src/Street.hpp"
#include "./src/Vehicle.hpp"
#include "./src/VehicleType.hpp"
#include "./utils/SparseMatrix.hpp"
#include "./utils/utils.hpp"
#include <cstdio>
#include <doctest/doctest.h> // Please consider installing from https://github.com/Grufoony/miscellaneous
// test file used to test the code
/****************************************************************************************
* *
* TESTS FOR THE SPARSE MATRIX CLASS *
* *
****************************************************************************************/
TEST_CASE("Boolean Matrix") {
SUBCASE("Default constructor") {
/*This test tests if the default constructor works correctly
The default constructor should create a matrix with 0 rows and 0 columns
and a max size of 0
GIVEN: the default constructor is called
WHEN: the matrix is created
THEN: the matrix should have 0 rows and 0 columns and a max size of 0
*/
SparseMatrix<bool> m;
// Check the dimensions
CHECK(m.getRowDim() == 0);
CHECK(m.getColDim() == 0);
CHECK(m.max_size() == 0);
}
SUBCASE("Constructor - exceptions") {
/*This test tests if the constructor throws exceptions correctly
The constructor should throw an exception if the dimensions are negative
GIVEN: the constructor is called with negative dimensions
WHEN: the matrix is created
THEN: the matrix should throw an exception
*/
CHECK_THROWS(SparseMatrix<bool>(-2, 0));
CHECK_THROWS(SparseMatrix<bool>(0, -10));
CHECK_THROWS(SparseMatrix<bool>(-4));
}
SUBCASE("Constructor with dimensions") {
/*This test tests if the constructor with dimensions works correctly
The constructor should create a matrix with the specified dimensions and a
max size equal to the product of the dimensions
GIVEN: the constructor is called with dimensions 3 and 4
WHEN: the matrix is created
THEN: the matrix should have 3 rows and 4 columns and a max size of 12
*/
SparseMatrix<bool> m(3, 4);
// Check the dimensions
CHECK(m.getRowDim() == 3);
CHECK(m.getColDim() == 4);
CHECK(m.max_size() == 12);
// Check out of range exceptions
CHECK_THROWS(m(-1, 0));
CHECK_THROWS(m(0, -1));
}
SUBCASE("Constructor with dimension") {
/*This test tests if the constructor with dimension works correctly
The constructor should create a row vector with the specified dimension
GIVEN: the constructor is called with dimension 3
WHEN: the matrix is created
THEN: the matrix should have 3 rows and 1 column and a max size of 3
*/
SparseMatrix<bool> m(3);
// Check the dimensions
CHECK(m.getRowDim() == 3);
CHECK(m.getColDim() == 1);
CHECK(m.max_size() == 3);
// Check out of range exceptions
CHECK_THROWS(m(-1));
}
SUBCASE("Encode") {
/*This test tests if the encode function works correctly
The encode function should transform a matrix into a list
GIVEN: the encode function is called
WHEN: the function is called on a file
THEN: the function should transform the matrix into a list
*/
std::string fileName = "./data/test/convert.dat";
SparseMatrix<double>::encode(fileName);
// compare the two files
std::ifstream f1(fileName);
std::ifstream f2("./data/test/encoded_ref.dat");
std::string s1((std::istreambuf_iterator<char>(f1)),
std::istreambuf_iterator<char>());
std::string s2((std::istreambuf_iterator<char>(f2)),
std::istreambuf_iterator<char>());
CHECK(s1 == s2);
}
SUBCASE("Decode") {
/*This test tests if the decode function works correctly
The decode function should transform a list into a matrix
GIVEN: the decode function is called
WHEN: the function is called on a file
THEN: the function should transform the list into a matrix
*/
std::string fileName = "./data/test/convert.dat";
SparseMatrix<double>::decode(fileName);
// compare the two files
std::ifstream f1(fileName);
std::ifstream f2("./data/test/matrix_ref.dat");
std::string s1((std::istreambuf_iterator<char>(f1)),
std::istreambuf_iterator<char>());
std::string s2((std::istreambuf_iterator<char>(f2)),
std::istreambuf_iterator<char>());
CHECK(s1 == s2);
}
SUBCASE("Insertion exceptions") {
/*This test tests if the insert function throws exceptions correctly
The insert function should throw an exception if the inserted element is out
of range
GIVEN: the insert function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the inserted element is out
of range
*/
SparseMatrix<bool> m(3, 3);
// Check that an exception is thrown if the element is out of range
CHECK_THROWS(m.insert(-1, true));
CHECK_THROWS(m.insert(3, 2, true));
}
SUBCASE("insert_or_assign exceptions") {
/*This test tests if the insert_or_assign function throws exceptions
correctly The insert_or_assign function should throw an exception if the
inserted element is out of range
GIVEN: the insert_or_assign function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the inserted element is out
of range
*/
SparseMatrix<bool> m(3, 3);
// Check that an exception is thrown if the element is out of range
CHECK_THROWS(m.insert_or_assign(-1, -2, true));
CHECK_THROWS(m.insert_or_assign(3, 2, true));
}
SUBCASE("Insertions") {
/*This test tests if the insert function works correctly
The insert function should insert a value in the matrix
GIVEN: the insert function is called
WHEN: the function is called on a matrix
THEN: the function should insert a value in the matrix
*/
SparseMatrix<bool> m(3, 3);
// Insert a true value
m.insert(0, 0, true);
m.insert(5, true);
// Check all values
CHECK(m(0, 0));
CHECK(m(1, 2));
for (int i = 1; i < 9; ++i) {
if (i != 5) {
CHECK(!m(i / 3, i % 3));
}
}
}
SUBCASE("insert_or_assign") {
/*This test tests if the insert_or_assign function works correctly
The insert_or_assign function should insert a value in the matrix
or assign a new value to an existing element
GIVEN: the insert_or_assign function is called
WHEN: the function is called on a matrix
THEN: the function should insert a value in the matrix
*/
SparseMatrix<int> m(4, 3);
// Insert a true value
m.insert_or_assign(1, 2, 10);
CHECK(m(1, 2) == 10);
m.insert_or_assign(1, 2, 20);
CHECK(m(1, 2) == 20);
}
SUBCASE("at - exceptions") {
/*This test tests if the at function throws exceptions correctly
The at function should throw an exception if the element is out of range
GIVEN: the at function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the element is out of range
*/
SparseMatrix<bool> m(2, 3);
// Check that an exception is thrown if the element is out of range
CHECK_THROWS(m.at(-1, -2));
CHECK_THROWS(m.at(-2));
CHECK_THROWS(m.at(3, 2));
CHECK_THROWS(m.at(6));
}
SUBCASE("at") {
/*This test tests if the at function works correctly
The at function should return the value at the specified position
GIVEN: the at function is called
WHEN: the function is called on a matrix
THEN: the function should return the value at the specified position
*/
SparseMatrix<bool> m(3, 3);
// Insert a true value
m.insert(1, 0, true);
// Check the value
CHECK(m.at(1, 0));
CHECK(m.at(3));
}
SUBCASE("erase - exception") {
/*This test tests if the erase function throws an exception correctly
The erase function should throw an exception if the element is out of range
GIVEN: the erase function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the element is out of range
*/
SparseMatrix<bool> m(3, 3);
// Check that an exception is thrown if the element is out of range
CHECK_THROWS(m.erase(-1, -2));
CHECK_THROWS(m.erase(3, 2));
}
SUBCASE("Deletions") {
/*This test tests if the erase function works correctly
The erase function should delete a value in the matrix
GIVEN: the erase function is called
WHEN: the function is called on a matrix
THEN: the function should delete a value in the matrix
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.erase(0, 0);
// Check if the value has been deleted
CHECK(!m(0, 0));
}
SUBCASE("Clear") {
/*This test tests if the clear function works correctly
The clear function should delete all the elements and dimensions in the
matrix
GIVEN: the clear function is called
WHEN: the function is called on a matrix
THEN: the function should delete all the elements and dimensions in the
matrix
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.clear();
// Check if the matrix is empty
CHECK(m.size() == 0);
CHECK_THROWS(m(0, 0));
}
SUBCASE("Contains exceptions") {
/*This test tests if the contains function throws exceptions correctly
The contains function should throw an exception if the element is out of
range
GIVEN: the contains function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the element is out of range
*/
SparseMatrix<bool> m(3, 3);
CHECK_THROWS(m.contains(-2, -1));
CHECK_THROWS(m.contains(-1));
}
SUBCASE("Contains") {
/*This test tests if the contains function works correctly
The contains function should return true if the matrix contains the element
GIVEN: the element is in the matrix
WHEN: the function is called
THEN: the function should return true
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(2, 1, true);
CHECK(m.contains(0, 0));
CHECK(m.contains(7));
}
SUBCASE("getRow - exceptions") {
/*This test tests if the getRow function throws exceptions correctly
The getRow function should throw an exception if the row is out of range
GIVEN: the getRow function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the row is out of range
*/
SparseMatrix<bool> m(4, 3);
CHECK_THROWS(m.getRow(-1));
CHECK_THROWS(m.getRow(4));
}
SUBCASE("Get row") {
/*This test tests if the getRow function works correctly
The getRow function should return a vector containing the elements of the
row
GIVEN: the getRow function is called
WHEN: the function is called on a matrix
THEN: the function should return a vector (SparseMatrix) containing the
elements of the row
*/
SparseMatrix<bool> m(3, 3);
// Create a row
m.insert(0, 0, true);
m.insert(0, 2, true);
auto row = m.getRow(0);
// verify attributes and values
CHECK(row.size() == 2);
CHECK(row(0));
CHECK(!row(1));
CHECK(row(2));
}
SUBCASE("getColumn - exceptions") {
/*This test tests if the getColumn function throws exceptions correctly
The getColumn function should throw an exception if the column is out of
range
GIVEN: the getColumn function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the column is out of range
*/
SparseMatrix<bool> m(3, 6);
CHECK_THROWS(m.getCol(-1));
CHECK_THROWS(m.getCol(6));
}
SUBCASE("Get column") {
/*This test tests if the getCol function works correctly
The getCol function should return a vector containing the elements of the
column
GIVEN: the getCol function is called
WHEN: the function is called on a matrix
THEN: the function should return a vector (SparseMatrix) containingthe
elements of the column
*/
SparseMatrix<bool> m(3, 3);
// Create a column
m.insert(0, 0, true);
m.insert(2, 0, true);
auto col = m.getCol(0);
// verify attributes and values
CHECK(col.size() == 2);
CHECK(col(0));
CHECK(!col(1));
CHECK(col(2));
}
SUBCASE("Get row dimension") {
/*This test tests if the getRowDim function works correctly
The getRowDim function should return the number of rows in the matrix
GIVEN: the getRowDim function is called
WHEN: the function is called on a matrix
THEN: the function should return the number of rows in the matrix
*/
SparseMatrix<bool> m(7, 3);
CHECK(m.getRowDim() == 7);
}
SUBCASE("Get column dimension") {
/*This test tests if the getColDim function works correctly
The getColDim function should return the number of columns in the matrix
GIVEN: the getColDim function is called
WHEN: the function is called on a matrix
THEN: the function should return the number of columns in the matrix
*/
SparseMatrix<bool> m(3, 10);
CHECK(m.getColDim() == 10);
}
SUBCASE("Get max_size") {
/*This test tests if the max_size function works correctly
The max_size function should return the maximum number of elements that can
be stored in the matrix
GIVEN: the max_size function is called
WHEN: the function is called on a matrix
THEN: the function should return the maximum number of elements that can be
stored in the matrix
*/
SparseMatrix<bool> m(3, 5);
CHECK(m.max_size() == 15);
}
SUBCASE("Get size") {
/*This test tests if the size function works correctly
The size function should return the number of non-zero elements in the
matrix
GIVEN: the size function is called
WHEN: the function is called on a matrix
THEN: the function should return the number of elements in the matrix
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(0, 2, true);
CHECK(m.size() == 3);
m.insert(1, 1, true);
CHECK(m.size() == 4);
}
SUBCASE("eraseRow - exception") {
/*This test tests if the eraseRow function throws an exception
The eraseRow function should throw an exception if the row is out of range
GIVEN: the eraseRow function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the row is out of range
*/
SparseMatrix<bool> m(5, 3);
CHECK_THROWS(m.eraseRow(-1));
CHECK_THROWS(m.eraseRow(5));
}
SUBCASE("Erase row") {
/*This test tests if the eraseRow function works correctly
The eraseRow function should delete all the elements in the row (and also
the row itself, reducing the number of rows by 1)
GIVEN: the eraseRow function is called
WHEN: the function is called on a matrix
THEN: the function should delete all the elements in the row
*/
SparseMatrix<bool> d(3, 3);
// Create a row
d.insert(0, 0, true);
d.insert(1, 2, true);
d.insert(2, 1, true);
// Clone the matrix
auto m = d;
// Erase the row (for each row) and check if all the other elements are
// rearranged correctly
m.eraseRow(1);
// Check the values
CHECK(m(0, 0));
CHECK(m(1, 1));
m = d;
m.eraseRow(0);
CHECK(m(0, 2));
CHECK(m(1, 1));
m = d;
m.eraseRow(2);
CHECK(m(0, 0));
CHECK(m(1, 2));
m.eraseRow(0);
CHECK(m(0, 2));
}
SUBCASE("eraseColumn - exception") {
/*This test tests if the eraseColumn function throws an exception
The eraseColumn function should throw an exception if the column is out of
range
GIVEN: the eraseColumn function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the column is out of range
*/
SparseMatrix<bool> m(3, 5);
CHECK_THROWS(m.eraseColumn(-1));
CHECK_THROWS(m.eraseColumn(5));
}
SUBCASE("Erase column") {
/*This test tests if the eraseColumn function works correctly
The eraseColumn function should delete all the elements in the column (and
also the column itself, reducing the number of columns by 1)
GIVEN: the eraseColumn function is called
WHEN: the function is called on a matrix
THEN: the function should delete all the elements in the column
*/
SparseMatrix<bool> d(3, 3);
d.insert(0, 0, true);
d.insert(1, 2, true);
d.insert(2, 1, true);
auto m = d;
// Erase the row (for each row) and check if all the other elements are
// rearranged correctly
m.eraseColumn(1);
CHECK(m(0, 0));
CHECK(m(1, 1));
m = d;
m.eraseColumn(0);
CHECK(m(1, 1));
CHECK(m(2, 0));
m = d;
m.eraseColumn(2);
CHECK(m(0, 0));
CHECK(m(2, 1));
}
SUBCASE("getDegreeVector - exceptions") {
/*This test tests if the getDegreeVector function throws an exception
The getDegreeVector function should throw an exception if the matrix is
empty
GIVEN: the getDegreeVector function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the matrix is empty
*/
SparseMatrix<bool> m(1, 5);
SparseMatrix<bool> m2(3, 6);
CHECK_THROWS(m.getDegreeVector());
CHECK_THROWS(m2.getDegreeVector());
}
SUBCASE("Degree vector") {
/*This test tests if the getDegreeVector function works correctly
The getDegreeVector function should return a vector containing the degree of
each row
GIVEN: the getDegreeVector function is called
WHEN: the function is called on a matrix
THEN: the function should return a vector containing the degree of each row
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(1, 2, true);
m.insert(2, 0, true);
m.insert(2, 1, true);
m.insert(2, 2, true);
auto v = m.getDegreeVector();
// check if the sum on all rows is done correctly
CHECK(v(0) == 2);
CHECK(v(1) == 1);
CHECK(v(2) == 3);
}
SUBCASE("getRndElement - exception") {
/*This test tests if the getRndElement function throws an exception
The getRndElement function should throw an exception if the matrix is empty
GIVEN: the getRndElement function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the matrix is empty
*/
SparseMatrix<bool> m(3, 3);
CHECK_THROWS(m.getRndElement());
}
SUBCASE("Random Elements") {
/*This test tests if the getRndElement function works correctly
The getRndElement function should return a random element in the matrix
GIVEN: the getRndElement function is called
WHEN: the function is called on a matrix
THEN: the function should return a random non-zero element in the matrix
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(1, 2, true);
m.insert(2, 1, true);
std::mt19937 gen(std::random_device{}());
gen.seed(69);
m.setSeed(69);
auto dist = std::uniform_int_distribution<int>(0, m.size() - 1);
for (int i = 0; i < 9; ++i) {
auto it = m.begin();
auto e = m.getRndElement();
std::advance(it, dist(gen));
CHECK(e.first == (*it).first);
}
}
SUBCASE("getRndRowElement - exceptions") {
/*This test tests if the getRndRowElement function throws an exception
The getRndRowElement function should throw an exception if the row is out of
range or if the row is empty
GIVEN: the getRndRowElement function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the row is out of range or
if the row is empty
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(1, 2, true);
CHECK_THROWS(m.getRndRowElement(-1));
CHECK_THROWS(m.getRndRowElement(3));
CHECK_THROWS(m.getRndRowElement(2));
}
SUBCASE("Random Row Element") {
/*This test tests if the getRndRowElement function works correctly
The getRndRowElement function should return a random element in the row
GIVEN: the getRndRowElement function is called
WHEN: the function is called on a matrix
THEN: the function should return a random non-zero element in the row
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(1, 2, true);
m.insert(2, 1, true);
auto m2 = m;
m.setSeed(69);
m2.setSeed(69);
// check random elements on every row
for (int i = 0; i < 3; ++i) {
auto e = m.getRndRowElement(i);
// comparing using getRndElement (which is tested above)
CHECK(e.first == m2.getRow(i).getRndElement().first +
i * 3); // convert row index to matrix index
}
}
SUBCASE("getRndColElement - exceptions") {
/*This test tests if the getRndColElement function throws an exception
The getRndColElement function should throw an exception if the column is out
of range or if the column is empty
GIVEN: the getRndColElement function is called
WHEN: the function is called on a matrix
THEN: the function should throw an exception if the column is out of range
or if the column is empty
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(1, 2, true);
CHECK_THROWS(m.getRndColElement(-1));
CHECK_THROWS(m.getRndColElement(3));
CHECK_THROWS(m.getRndColElement(1));
}
SUBCASE("Random Column Element") {
/*This test tests if the getRndColElement function works correctly
The getRndColElement function should return a random element in the column
GIVEN: the getRndColElement function is called
WHEN: the function is called on a matrix
THEN: the function should return a random non-zero element in the column
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(1, 2, true);
m.insert(2, 1, true);
auto m2 = m;
m.setSeed(69);
m2.setSeed(69);
// check random elements on every column
for (int i = 0; i < 3; ++i) {
auto e = m.getRndColElement(i);
// comparing using getRndElement (which is tested above)
CHECK(e.first == m2.getCol(i).getRndElement().first * 3 +
i); // convert column index to matrix index
}
}
SUBCASE("Normalized Rows") {
/*This test tests if the getNormRows function works correctly
The getNormRows function should return a matrix containing the normalized
rows
GIVEN: the getNormRows function is called
WHEN: the function is called on a matrix
THEN: the function should return a matrix containing the normalized rows
*/
SparseMatrix<bool> m(3, 3);
// Create a row
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(0, 2, true);
m.insert(1, 1, true);
m.insert(1, 2, true);
m.insert(2, 1, true);
// Get the normalized rows and check their values, comparig floats with a
// numeric limit
auto v = m.getNormRows();
CHECK(v(0, 0) - 1. / 3 < std::numeric_limits<double>::epsilon());
CHECK(v(1, 1) - 1. / 2 < std::numeric_limits<double>::epsilon());
CHECK(v(2, 1) - 1 < std::numeric_limits<double>::epsilon());
CHECK(v(0, 0) + v(0, 1) + v(0, 2) - 1 <
std::numeric_limits<double>::epsilon());
CHECK(v(1, 0) + v(1, 1) + v(1, 2) - 1 <
std::numeric_limits<double>::epsilon());
CHECK(v(2, 0) + v(2, 1) + v(2, 2) - 1 <
std::numeric_limits<double>::epsilon());
}
SUBCASE("Normalized Columns") {
/*This test tests if the getNormCols function works correctly
The getNormCols function should return a matrix containing the normalized
columns
GIVEN: the getNormCols function is called
WHEN: the function is called on a matrix
THEN: the function should return a matrix containing the normalized columns
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(0, 2, true);
m.insert(1, 1, true);
m.insert(1, 2, true);
m.insert(2, 1, true);
// Get the normalized columns and check their values, comparig floats with a
// numeric limit
auto v = m.getNormCols();
CHECK(v(0, 0) == doctest::Approx(1.));
CHECK(v(1, 1) == doctest::Approx(1. / 3));
CHECK(v(1, 2) == doctest::Approx(1. / 2));
CHECK(v(0, 0) + v(1, 0) + v(2, 0) == doctest::Approx(1.));
CHECK(v(0, 1) + v(1, 1) + v(2, 1) == doctest::Approx(1.));
CHECK(v(0, 2) + v(1, 2) + v(2, 2) == doctest::Approx(1.));
}
SUBCASE("Symmetrization") {
/*This test tests if the symmetrize function works correctly
The symmetrize function should symmetrize the matrix
GIVEN: the symmetrize function is called
WHEN: the function is called on a matrix
THEN: the function should symmetrize the matrix
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(1, 2, true);
// Symmetrize the matrix
m.symmetrize();
CHECK(m(0, 0));
CHECK(m(0, 1));
CHECK(m(1, 0));
CHECK(m(1, 2));
CHECK(m(2, 1));
CHECK(m.size() == 5);
}
SUBCASE("+ operator - exception") {
/*This test tests if the + operator throws an exception correctly
The + operator should throw an exception if the matrices have different
dimensions
GIVEN: the + operator is called
WHEN: the function is called on two matrices
THEN: the function should throw an exception if the matrices have different
dimensions
*/
SparseMatrix<bool> m(3, 3);
SparseMatrix<bool> m2(3, 4);
CHECK_THROWS(m + m2);
}
SUBCASE("+ operator") {
/*This test tests if the + operator works correctly
The + operator should sum two matrices
GIVEN: the + operator is called
WHEN: the function is called on two matrices
THEN: the function should sum the two matrices
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(1, 2, true);
SparseMatrix<bool> m2(3, 3);
m2.insert(0, 0, true);
m2.insert(1, 0, true);
m2.insert(2, 1, true);
auto m3 = m + m2;
CHECK(m3(0, 0));
CHECK(m3(1, 0));
CHECK(m3(2, 1));
CHECK(m3(0, 1));
CHECK(m3(1, 2));
CHECK(m3.size() == 5);
}
SUBCASE("+= operator - exception") {
/*This test tests if the += operator throws an exception correctly
The += operator should throw an exception if the matrices have different
dimensions
GIVEN: the += operator is called
WHEN: the function is called on two matrices
THEN: the function should throw an exception if the matrices have different
dimensions
*/
SparseMatrix<bool> m(3, 3);
SparseMatrix<bool> m2(3, 4);
CHECK_THROWS(m += m2);
}
SUBCASE("+= operator") {
/*This test tests if the += operator works correctly
The += operator should sum two matrices
GIVEN: the += operator is called
WHEN: the function is called on two matrices
THEN: the function should sum the two matrices
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(1, 2, true);
SparseMatrix<bool> m2(3, 3);
m2.insert(0, 0, true);
m2.insert(1, 0, true);
m2.insert(2, 1, true);
m += m2;
CHECK(m(0, 0));
CHECK(m(1, 0));
CHECK(m(2, 1));
CHECK(m(0, 1));
CHECK(m(1, 2));
CHECK(m.size() == 5);
}
SUBCASE("- operator - exception") {
/*This test tests if the - operator throws an exception correctly
The - operator should throw an exception if the matrices have different
dimensions
GIVEN: the - operator is called
WHEN: the function is called on two matrices
THEN: the function should throw an exception if the matrices have different
dimensions
*/
SparseMatrix<bool> m(3, 3);
SparseMatrix<bool> m2(3, 4);
CHECK_THROWS(m - m2);
}
SUBCASE("- operator") {
/*This test tests if the - operator works correctly
The - operator should subtract two matrices
GIVEN: the - operator is called
WHEN: the function is called on two matrices
THEN: the function should subtract the two matrices
*/
SparseMatrix<int> m(3, 3);
m.insert(0, 0, 1);
m.insert(0, 1, 2);
m.insert(1, 2, 3);
SparseMatrix<int> m2(3, 3);
m2.insert(0, 0, 1);
m2.insert(1, 0, 2);
m2.insert(2, 1, 3);
auto m3 = m - m2;
CHECK(m3(0, 0) == 0);
CHECK(m3(1, 0) == -2);
CHECK(m3(2, 1) == -3);
CHECK(m3(0, 1) == 2);
CHECK(m3(1, 2) == 3);
CHECK(m3.size() == 5);
}
SUBCASE("-= operator - exception") {
/*This test tests if the -= operator throws an exception correctly
The -= operator should throw an exception if the matrices have different
dimensions
GIVEN: the -= operator is called
WHEN: the function is called on two matrices
THEN: the function should throw an exception if the matrices have different
dimensions
*/
SparseMatrix<bool> m(3, 3);
SparseMatrix<bool> m2(3, 4);
CHECK_THROWS(m -= m2);
}
SUBCASE("-= operator") {
/*This test tests if the -= operator works correctly
The -= operator should subtract two matrices
GIVEN: the -= operator is called
WHEN: the function is called on two matrices
THEN: the function should subtract the two matrices
*/
SparseMatrix<int> m(3, 3);
m.insert(0, 0, 1);
m.insert(0, 1, 2);
m.insert(1, 2, 3);
SparseMatrix<int> m2(3, 3);
m2.insert(0, 0, 1);
m2.insert(1, 0, 2);
m2.insert(2, 1, 3);
m -= m2;
CHECK(m(0, 0) == 0);
CHECK(m(1, 0) == -2);
CHECK(m(2, 1) == -3);
CHECK(m(0, 1) == 2);
CHECK(m(1, 2) == 3);
CHECK(m.size() == 5);
}
SUBCASE("fprint") {
/*This test tests if the fprint function works correctly
The fprint function should print the matrix in a file
GIVEN: the fprint function is called
WHEN: the function is called on a matrix
THEN: the function should print the matrix in a file
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(1, 2, true);
std::string fileName = "./data/test/temp.txt";
m.fprint(fileName);
// compare output with reference
std::ifstream f1(fileName);
std::ifstream f2("./data/test/test0_ref.txt");
std::string s1((std::istreambuf_iterator<char>(f1)),
std::istreambuf_iterator<char>());
std::string s2((std::istreambuf_iterator<char>(f2)),
std::istreambuf_iterator<char>());
CHECK(s1 == s2);
std::remove(fileName.c_str());
}
SUBCASE("getStrengthVector - exception") {
/*This test tests if the getStrengthVector function throws an exception
The getStrengthVector function should throw an exception if the matrix is
not square
GIVEN: the getStrengthVector function is called
WHEN: the function is called on a non-square matrix
THEN: the function should throw an exception
*/
SparseMatrix<double> m(4, 3);
SparseMatrix<double> m2(3, 4);
CHECK_THROWS(m.getStrengthVector());
CHECK_THROWS(m2.getStrengthVector());
}
SUBCASE("getStrengthVector") {
/*This test tests if the getStrengthVector function works correctly
The getStrengthVector function should return the strength vector of the
matrix
GIVEN: the getStrengthVector function is called
WHEN: the function is called on a street with 1 vehicle and length 10
THEN: the function should return a vector with 0.1
*/
SparseMatrix<double> m(3, 3);
m.insert(0, 0, 0.3);
m.insert(0, 1, 0.3);
m.insert(0, 2, 0.1);
m.insert(1, 1, 0.5);
m.insert(2, 0, 0.1);
m.insert(2, 2, 1.);
auto v = m.getStrengthVector();
CHECK(v(0) == doctest::Approx(0.7));
CHECK(v(1) == doctest::Approx(0.5));
CHECK(v(2) == doctest::Approx(1.1));
CHECK(v.size() == 3);
}
SUBCASE("getLaplacian - exception") {
/*This test tests if the getLaplacian function throws an exception
The getLaplacian function should throw an exception if the street is empty
GIVEN: the getLaplacian function is called
WHEN: the function is called on a non-square matrix
THEN: the function should throw an exception
*/
SparseMatrix<double> m(4, 3);
CHECK_THROWS(m.getLaplacian());
}
SUBCASE("getLaplacian") {
/*This test tests if the getLaplacian function works correctly
The getLaplacian function should return the Laplacian matrix of the matrix
GIVEN: the getLaplacian function is called
WHEN: the function is called on a matrix
THEN: the function should return the Laplacian matrix of the matrix
*/
SparseMatrix<bool> m(3, 3);
m.insert(0, 0, true);
m.insert(0, 1, true);
m.insert(1, 2, true);
auto m2 = m.getLaplacian();
CHECK(m2(0, 0) == 2);
CHECK(m2(0, 1) == -1);
CHECK(m2(1, 1) == 1);
CHECK(m2(1, 2) == -1);
CHECK(m2(2, 2) == 0);
CHECK(m2.size() == 5);
}
SUBCASE("operator <<") {
/*This test tests if the operator << works correctly
The << operator should print the matrix on a stream
GIVEN: the << operator is called
WHEN: the function is called on a matrix
THEN: the function should print the matrix on a stream
*/
SparseMatrix<int> m(3, 3);
m.insert(0, 0, 7);
m.insert(0, 1, 3);
m.insert(1, 2, -5);
std::stringstream ss;
ss << m;
CHECK(ss.str() == "3\t3\n5\t-5\n1\t3\n0\t7\n");
}
SUBCASE("operator >>") {
/*This test tests if the >> operator works correctly
The >> operator should read a matrix from a stream
GIVEN: the >> operator is called
WHEN: the function is called on a matrix
THEN: the function should read a matrix from a stream
*/
SparseMatrix<int> m;
std::stringstream ss("3\t3\n5\t10\n1\t4\n0\t-1\n");
ss >> m;
CHECK(m(5) == 10);
CHECK(m(1) == 4);
CHECK(m(0) == -1);
CHECK(m.size() == 3);
CHECK(m.max_size() == 9);
}
}
/****************************************************************************************
* *
* TESTS FOR THE VEHICLETYPE CLASS *
* *
****************************************************************************************/
TEST_CASE("VehicleType") {
SUBCASE("Constructor exceptions") {
/*This test tests if the constructor throws exceptions correctly
The constructor should throw an exception if the source and destination are
the same, or negative
GIVEN: the constructor is called
WHEN: the function is called with source 0 and destination 0 or negative
values
THEN: the function should throw an exception
*/
CHECK_THROWS(VehicleType(0, 0));
CHECK_THROWS(VehicleType(-3, 0));
CHECK_THROWS(VehicleType(0, -1));
CHECK_THROWS(VehicleType(-1, -2));
}
SUBCASE("Constructor and getters") {
/*This test tests if the constructor and the getters work correctly
The constructor should create a VehicleType with the specified source and
destination
GIVEN: the constructor is called with source 0 and destination 1
WHEN: the VehicleType is created
THEN: the VehicleType should have source 0 and destination 1
*/
VehicleType v(0, 1);
CHECK(v.getSource() == 0);
CHECK(v.getDestination() == 1);
}
SUBCASE("getTransMatrix exception") {
/*This test tests if the getTransMatrix function throws an exception
The getTransMatrix function should throw an exception if the transition
matrix is empty
GIVEN: the getTransMatrix function is called
WHEN: the function is called on a VehicleType with an empty transition
matrix
THEN: the function should throw an exception
*/
auto v2 = VehicleType(0, 1);
// Check that an exception is thrown if the transition matrix is empty
CHECK_THROWS(v2.getTransMatrix());
}
SUBCASE("Transition Matrix") {
/*This test tests if the transition matrix works correctly
The transition matrix should be a square matrix with the same number of rows
and columns as the number of nodes in the graph
GIVEN: the transition matrix
WHEN: the transition matrix is set
THEN: the transition matrix should be a square matrix with the same number
of rows and columns as the number of nodes in the graph normalized on the
rows
*/
VehicleType v(0, 1);
// Create a transition matrix
SparseMatrix<double> m(3, 3);