Skip to content

Commit 9c0114e

Browse files
committed
tests: moved standard operations into functions
1 parent 7878461 commit 9c0114e

File tree

4 files changed

+83
-32
lines changed

4 files changed

+83
-32
lines changed

tests/cases/addition.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,7 @@ void testAddition(void)
7171
SparseMatrixMock<int> sparseMatrixB = SparseMatrixMock<int>::fromVectors(classicMatrixB);
7272

7373
// calculate result manually
74-
vector<vector<int> > manualResult(rows, vector<int>(cols, 0));
75-
76-
for (int i = 0; i < rows; i++) {
77-
for (int j = 0; j < cols; j++) {
78-
manualResult[i][j] += classicMatrixA[i][j] + classicMatrixB[i][j];
79-
}
80-
}
74+
vector<vector<int> > manualResult = addMatrices(classicMatrixA, classicMatrixB);
8175

8276
// method
8377
assertEquals<SparseMatrix<int>, vector<vector<int> > >(

tests/cases/multiplication.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,7 @@ void testVectorMultiplication(void)
5656
SparseMatrixMock<int> sparseMatrix = SparseMatrixMock<int>::fromVectors(classicMatrix);
5757

5858
// calculate result manually
59-
vector<int> manualResult(rows, 0);
60-
61-
for (int i = 0; i < rows; i++) {
62-
for (int j = 0; j < cols; j++) {
63-
manualResult[i] += classicMatrix[i][j] * vec[j];
64-
}
65-
}
59+
vector<int> manualResult = multiplyMatrixByVector(classicMatrix, vec);
6660

6761
// method
6862
assertEquals<vector<int> >(manualResult, sparseMatrix.multiply(vec), "Incorrect vector multiplication");
@@ -92,17 +86,7 @@ void testMatricesMultiplication(void)
9286
SparseMatrixMock<int> sparseMatrixB = SparseMatrixMock<int>::fromVectors(classicMatrixB);
9387

9488
// calculate result manually
95-
vector<vector<int> > manualResult(rowsA, vector<int>(colsB, 0));
96-
97-
for (int i = 0; i < rowsA; i++) {
98-
for (int j = 0; j < colsB; j++) {
99-
manualResult[i][j] = 0;
100-
101-
for (int k = 0; k < colsArowsB; k++) { // rows in B
102-
manualResult[i][j] += classicMatrixA[i][k] * classicMatrixB[k][j];
103-
}
104-
}
105-
}
89+
vector<vector<int> > manualResult = multiplyMatrices(classicMatrixA, classicMatrixB);
10690

10791
// method
10892
assertEquals<SparseMatrix<int>, vector<vector<int> > >(

tests/cases/subtraction.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,7 @@ void testSubtraction(void)
7171
SparseMatrixMock<int> sparseMatrixB = SparseMatrixMock<int>::fromVectors(classicMatrixB);
7272

7373
// calculate result manually
74-
vector<vector<int> > manualResult(rows, vector<int>(cols, 0));
75-
76-
for (int i = 0; i < rows; i++) {
77-
for (int j = 0; j < cols; j++) {
78-
manualResult[i][j] += classicMatrixA[i][j] - classicMatrixB[i][j];
79-
}
80-
}
74+
vector<vector<int> > manualResult = subtractMatrices(classicMatrixA, classicMatrixB);
8175

8276
// method
8377
assertEquals<SparseMatrix<int>, vector<vector<int> > >(

tests/inc/helpers.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,85 @@
4646
}
4747

4848

49+
// === STANDARD OPERATIONS =========================================
50+
51+
template<typename T>
52+
vector<vector<T> > addMatrices(const vector<vector<T> > & a, const vector<vector<T> > & b)
53+
{
54+
int rows = a.size();
55+
int cols = a.front().size();
56+
57+
vector<vector<T> > result(rows, vector<int>(cols, 0));
58+
59+
for (int i = 0; i < rows; i++) {
60+
for (int j = 0; j < cols; j++) {
61+
result[i][j] = a[i][j] + b[i][j];
62+
}
63+
}
64+
65+
return result;
66+
}
67+
68+
69+
template<typename T>
70+
vector<vector<T> > subtractMatrices(const vector<vector<T> > & a, const vector<vector<T> > & b)
71+
{
72+
int rows = a.size();
73+
int cols = a.front().size();
74+
75+
vector<vector<T> > result(rows, vector<int>(cols, 0));
76+
77+
for (int i = 0; i < rows; i++) {
78+
for (int j = 0; j < cols; j++) {
79+
result[i][j] = a[i][j] - b[i][j];
80+
}
81+
}
82+
83+
return result;
84+
}
85+
86+
87+
template<typename T>
88+
vector<T> multiplyMatrixByVector(const vector<vector<T> > & m, const vector<T> & v)
89+
{
90+
int rows = m.size();
91+
int cols = v.size();
92+
93+
vector<T> result(rows, 0);
94+
95+
for (int i = 0; i < rows; i++) {
96+
for (int j = 0; j < cols; j++) {
97+
result[i] += m[i][j] * v[j];
98+
}
99+
}
100+
101+
return result;
102+
}
103+
104+
105+
template<typename T>
106+
vector<vector<T> > multiplyMatrices(const vector<vector<T> > & a, const vector<vector<T> > & b)
107+
{
108+
int rowsA = a.size();
109+
int colsA = a.front().size();
110+
int colsB = b.front().size();
111+
112+
vector<vector<T> > result(rowsA, vector<int>(colsB, 0));
113+
114+
for (int i = 0; i < rowsA; i++) {
115+
for (int j = 0; j < colsB; j++) {
116+
result[i][j] = 0;
117+
118+
for (int k = 0; k < colsA; k++) {
119+
result[i][j] += a[i][k] * b[k][j];
120+
}
121+
}
122+
}
123+
124+
return result;
125+
}
126+
127+
49128
// === OUTPUT HELPERS =========================================
50129

51130
template<typename T>

0 commit comments

Comments
 (0)