This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplicationTests.cpp
95 lines (79 loc) · 3.04 KB
/
multiplicationTests.cpp
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
#include <iostream>
#include <vector>
#include <memory>
#include "Matrix.h"
#include "StaticSizeMatrix.h"
template<typename T, class MD>
void initializeCells(Matrix<T, MD> &m, T rowMultiplier, T colMultiplier) {
for (unsigned row = 0; row < m.rows(); ++row) {
for (unsigned col = 0; col < m.columns(); ++col) {
m(row, col) = row * rowMultiplier + col * colMultiplier;
}
}
}
template<typename T>
void assert(T expected, T actual) {
if (expected != actual) {
std::cout << "ERROR: expected " << expected << ", got " << actual << std::endl;
exit(1);
}
}
template<typename T, class MD1, class MD2>
void assertEqual(const Matrix<T, MD1> &m1, const Matrix<T, MD2> &m2) {
if (m1.rows() != m2.rows() || m1.columns() != m2.columns()) {
std::cout << "ERROR: expected matrix of the same size" << std::endl;
exit(1);
}
for (unsigned r = 0; r < m1.rows(); ++r) {
for (unsigned c = 0; c < m2.columns(); ++c) {
assert(m1(r, c), m2(r, c));
}
}
}
int main() {
StaticSizeMatrix<4, 9, double> mAd;
StaticSizeMatrix<4, 9, int> mA;
StaticSizeMatrix<9, 7, int> mB;
StaticSizeMatrix<7, 8, int> mC;
StaticSizeMatrix<8, 2, int> mD;
initializeCells(mA, 12, 5);
initializeCells<double>(mAd, 12, 5);
initializeCells(mB, 7, 13);
initializeCells(mC, 3, 8);
initializeCells(mD, 2, 4);
std::cout << "Matrix A" << std::endl;
mA.print("%2d");
std::cout << "-------------------------" << std::endl << std::endl;
std::cout << "Matrix B" << std::endl;
mB.print("%2d");
std::cout << "-------------------------" << std::endl << std::endl;
std::cout << "Matrix C" << std::endl;
mC.print("%2d");
std::cout << "-------------------------" << std::endl << std::endl;
std::cout << "Matrix D" << std::endl;
mD.print("%2d");
std::cout << "-------------------------" << std::endl << std::endl;
auto multiplicationAB = mA * mB;
auto multiplicationBC = mB * mC;
auto multiplicationCD = mC * mD;
auto multiplicationABC = multiplicationAB * mC;
auto multiplicationABCD = multiplicationAB * multiplicationCD;
auto multiplicationABCD2 = mA * mB * mC * mD;
auto sum = mA + mAd.cast<int>();
std::cout << "Matrix AxBxC" << std::endl;
multiplicationABC.print("%2d"); // This should perform AxB and (AxB)xC
std::cout << "-------------------------" << std::endl << std::endl;
std::cout << "Matrix AxB" << std::endl;
multiplicationAB.print("%2d"); //This shouldn't perform any multiplication, since AxB is already computed
std::cout << "-------------------------" << std::endl << std::endl;
std::cout << "Matrix AxBxCxD" << std::endl;
multiplicationABCD.print("%4d");//This should compute the multiplication (AxB)xC and (AxBxC)xD
std::cout << "-------------------------" << std::endl << std::endl;
std::cout << "Matrix AxBxCxD computed from scratch" << std::endl;
multiplicationABCD2.print("%4d");//This should perform AxB, CxD and (AxB)x(CxD)
std::cout << "-------------------------" << std::endl << std::endl;
assert<int>(1034658912, multiplicationABCD.get<3, 1>());
assertEqual(multiplicationABCD, multiplicationABCD2);
std::cout << "ALL TESTS PASSED" << std::endl;
return 0;
}