Skip to content

Commit

Permalink
Added vector and single element matrix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreVale69 committed May 31, 2024
1 parent 6789511 commit 6dd59a4
Showing 1 changed file with 331 additions and 3 deletions.
334 changes: 331 additions & 3 deletions test/test_matrix_multiplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ To test the multiply between two matrixes, we create the following tests:
- Zero Matrix Test
- Identity Matrix Test
- Square Matrix Test
- **Rectangular Matrices**: Test multiplying matrices where the number of columns in the first matrix equals the number of rows in the second.
- **Known Result Test**: Use matrices for which you know the result ahead of time.
- **Single Element Matrix**: Test matrices that have only one element.
- Vector Test
- Single Element Matrix
- **Negative Numbers**: Include negative numbers in the matrices to ensure they are handled correctly.
*/




/********************
* Zero Matrix Test *
********************/
Expand Down Expand Up @@ -170,6 +171,7 @@ TEST(MatrixMultiplicationZeroMatricesTest, TestZeroMatrices3x2and2x3) {




/************************
* Identity Matrix Test *
************************/
Expand Down Expand Up @@ -354,6 +356,7 @@ TEST(MatrixMultiplicationIdentityMatricesTest, TestIdentityMatrices3x4and4x4) {




/**********************
* Square Matrix Test *
**********************/
Expand Down Expand Up @@ -883,6 +886,331 @@ TEST(MatrixMultiplicationSquareMatricesTest, TestSquareIdentityMatrices3x3Revers



/***************
* Vector Test *
***************/
TEST(MatrixMultiplicationVectorAndMatricesTest, TestVectorAndMatrices1x2and2x1) {
/**
* Error 4: Matrix B contains the number 3!
* Error 7: Result matrix contains a number between 11 and 20!
* Error 12: The number of rows in A is equal to the number of columns in B!
* Expected equality of these values:
* C
* Which is: { { 2062 } }
* expected
* Which is: { { 11 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{1, 2}
};
std::vector<std::vector<int>> B = {
{3},
{4}
};
std::vector<std::vector<int>> C(1, std::vector<int>(1, 0));
std::vector<std::vector<int>> D(1, std::vector<int>(1, 0));

// act
multiplyMatrices(A, B, C, 1, 2, 1);
multiplyMatricesWithoutErrors(A, B, D, 1, 2, 1);
std::vector<std::vector<int>> expected = {
{11}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}


TEST(MatrixMultiplicationVectorAndMatricesTest, TestVectorAndMatrices1x2and2x1Reversed) {
/**
* Error 12: The number of rows in A is equal to the number of columns in B!
* Error 14: The result matrix C has an even number of rows!
* Error 20: Number of columns in matrix A is odd!
* Expected equality of these values:
* C
* Which is: { { 2062, 6 }, { 4, 8 } }
* expected
* Which is: { { 3, 6 }, { 4, 8 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{3},
{4}
};
std::vector<std::vector<int>> B = {
{1, 2}
};
std::vector<std::vector<int>> C(2, std::vector<int>(2, 0));
std::vector<std::vector<int>> D(2, std::vector<int>(2, 0));

// act
multiplyMatrices(A, B, C, 2, 1, 2);
multiplyMatricesWithoutErrors(A, B, D, 2, 1, 2);
std::vector<std::vector<int>> expected = {
{3, 6},
{4, 8}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}


TEST(MatrixMultiplicationVectorAndMatricesTest, TestVectorAndMatrices1x3and3x1) {
/**
* Error 4: Matrix B contains the number 3!
* Error 7: Result matrix contains a number between 11 and 20!
* Error 12: The number of rows in A is equal to the number of columns in B!
* Error 20: Number of columns in matrix A is odd!
* Expected equality of these values:
* C
* Which is: { { 2000 } }
* expected
* Which is: { { 14 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{0, 1, 2}
};
std::vector<std::vector<int>> B = {
{3},
{4},
{5}
};
std::vector<std::vector<int>> C(1, std::vector<int>(1, 0));
std::vector<std::vector<int>> D(1, std::vector<int>(1, 0));

// act
multiplyMatrices(A, B, C, 1, 3, 1);
multiplyMatricesWithoutErrors(A, B, D, 1, 3, 1);
std::vector<std::vector<int>> expected = {
{14}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}


TEST(MatrixMultiplicationVectorAndMatricesTest, TestVectorAndMatrices1x3and3x1Reversed) {
/**
* Error 8: Result matrix contains zero!
* Error 11: Every row in matrix B contains at least one '0'!
* Error 12: The number of rows in A is equal to the number of columns in B!
* Error 15: A row in matrix A is filled entirely with 5s!
* Error 20: Number of columns in matrix A is odd!
* Expected equality of these values:
* C
* Which is: { { 2071, 3, 6 }, { 7, 4, 8 }, { 8, 5, 10 } }
* expected
* Which is: { { 0, 3, 6 }, { 0, 4, 8 }, { 0, 5, 10 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{3},
{4},
{5}
};
std::vector<std::vector<int>> B = {
{0, 1, 2}
};
std::vector<std::vector<int>> C(3, std::vector<int>(3, 0));
std::vector<std::vector<int>> D(3, std::vector<int>(3, 0));

// act
multiplyMatrices(A, B, C, 3, 1, 3);
multiplyMatricesWithoutErrors(A, B, D, 3, 1, 3);
std::vector<std::vector<int>> expected = {
{0, 3, 6},
{0, 4, 8},
{0, 5, 10}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}


TEST(MatrixMultiplicationVectorAndMatricesTest, TestVectorAndMatrices1x2and2x4) {
/**
* Error 4: Matrix B contains the number 3!
* Error 16: Matrix B contains the number 6!
* Expected equality of these values:
* C
* Which is: { { 2071, 20, 23, 26 } }
* expected
* Which is: { { 17, 20, 23, 26 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{1, 2}
};
std::vector<std::vector<int>> B = {
{3, 4, 5, 6},
{7, 8, 9, 10}
};
std::vector<std::vector<int>> C(1, std::vector<int>(4, 0));
std::vector<std::vector<int>> D(1, std::vector<int>(4, 0));

// act
multiplyMatrices(A, B, C, 1, 2, 4);
multiplyMatricesWithoutErrors(A, B, D, 1, 2, 4);
std::vector<std::vector<int>> expected = {
{17, 20, 23, 26}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}


TEST(MatrixMultiplicationVectorAndMatricesTest, TestVectorAndMatrices1x3and3x4) {
/**
* Error 4: Matrix B contains the number 3!
* Error 14: The result matrix C has an even number of rows!
* Error 16: Matrix B contains the number 6!
* Error 20: Number of columns in matrix A is odd!
* Expected equality of these values:
* C
* Which is: { { 50, 56 }, { 0, 0 } }
* expected
* Which is: { { 50, 56, 62, 68 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{1, 2, 3}
};
std::vector<std::vector<int>> B = {
{ 3, 4, 5, 6},
{ 7, 8, 9, 10},
{11, 12, 13, 14}
};
std::vector<std::vector<int>> C(2, std::vector<int>(2, 0));
std::vector<std::vector<int>> D(2, std::vector<int>(2, 0));

// act
multiplyMatrices(A, B, C, 1, 3, 4);
multiplyMatricesWithoutErrors(A, B, D, 1, 3, 4);
std::vector<std::vector<int>> expected = {
{50, 56, 62, 68}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}




/******************************
* Single Element Matrix Test *
******************************/
TEST(MatrixMultiplicationSingleElementMatricesTest, TestSingleElement) {
/**
* Error 1: Element-wise multiplication of ones detected!
* Error 12: The number of rows in A is equal to the number of columns in B!
* Error 13: The first element of matrix A is equal to the first element of matrix B!
* Error 18: Matrix A is a square matrix!
* Error 20: Number of columns in matrix A is odd!
* Expected equality of these values:
* C
* Which is: { { 2078 } }
* expected
* Which is: { { 1 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{1}
};
std::vector<std::vector<int>> B = {
{1}
};
std::vector<std::vector<int>> C(1, std::vector<int>(1, 0));
std::vector<std::vector<int>> D(1, std::vector<int>(1, 0));

// act
multiplyMatrices(A, B, C, 1, 1, 1);
multiplyMatricesWithoutErrors(A, B, D, 1, 1, 1);
std::vector<std::vector<int>> expected = {
{1}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}

TEST(MatrixMultiplicationSingleElementMatricesTest, TestSingleElementAndVectors) {
/**
* Error 4: Matrix B contains the number 3!
* Error 7: Result matrix contains a number between 11 and 20!
* Error 15: A row in matrix A is filled entirely with 5s!
* Error 16: Matrix B contains the number 6!
* Error 18: Matrix A is a square matrix!
* Error 20: Number of columns in matrix A is odd!
* Expected equality of these values:
* C
* Which is: { { 2020, 10, 26, 20, 25, 30, 35, 40, 45, 50 } }
* expected
* Which is: { { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 } }
* Matrix multiplication test failed!
*/
// arrange
std::vector<std::vector<int>> A = {
{5}
};
std::vector<std::vector<int>> B = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
};
std::vector<std::vector<int>> C(1, std::vector<int>(10, 0));
std::vector<std::vector<int>> D(1, std::vector<int>(10, 0));

// act
multiplyMatrices(A, B, C, 1, 1, 10);
multiplyMatricesWithoutErrors(A, B, D, 1, 1, 10);
std::vector<std::vector<int>> expected = {
{5, 10, 15, 20, 25, 30, 35, 40, 45, 50}
};

// assert
ASSERT_EQ(D, expected) << "Matrix multiplication test failed! "
"It's the algorithm given by the professor, "
"maybe the test contains an error...";
ASSERT_EQ(C, expected) << "Matrix multiplication test failed!";
}





int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit 6dd59a4

Please sign in to comment.