From 6dd59a4e413ecc3348835a0b373cbde27ea5aa0e Mon Sep 17 00:00:00 2001 From: AndreVale69 Date: Fri, 31 May 2024 13:33:41 +0200 Subject: [PATCH] Added vector and single element matrix tests --- test/test_matrix_multiplication.cpp | 334 +++++++++++++++++++++++++++- 1 file changed, 331 insertions(+), 3 deletions(-) diff --git a/test/test_matrix_multiplication.cpp b/test/test_matrix_multiplication.cpp index 017e4d7..1c5bdc4 100644 --- a/test/test_matrix_multiplication.cpp +++ b/test/test_matrix_multiplication.cpp @@ -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 * ********************/ @@ -170,6 +171,7 @@ TEST(MatrixMultiplicationZeroMatricesTest, TestZeroMatrices3x2and2x3) { + /************************ * Identity Matrix Test * ************************/ @@ -354,6 +356,7 @@ TEST(MatrixMultiplicationIdentityMatricesTest, TestIdentityMatrices3x4and4x4) { + /********************** * Square Matrix Test * **********************/ @@ -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> A = { + {1, 2} + }; + std::vector> B = { + {3}, + {4} + }; + std::vector> C(1, std::vector(1, 0)); + std::vector> D(1, std::vector(1, 0)); + + // act + multiplyMatrices(A, B, C, 1, 2, 1); + multiplyMatricesWithoutErrors(A, B, D, 1, 2, 1); + std::vector> 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> A = { + {3}, + {4} + }; + std::vector> B = { + {1, 2} + }; + std::vector> C(2, std::vector(2, 0)); + std::vector> D(2, std::vector(2, 0)); + + // act + multiplyMatrices(A, B, C, 2, 1, 2); + multiplyMatricesWithoutErrors(A, B, D, 2, 1, 2); + std::vector> 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> A = { + {0, 1, 2} + }; + std::vector> B = { + {3}, + {4}, + {5} + }; + std::vector> C(1, std::vector(1, 0)); + std::vector> D(1, std::vector(1, 0)); + + // act + multiplyMatrices(A, B, C, 1, 3, 1); + multiplyMatricesWithoutErrors(A, B, D, 1, 3, 1); + std::vector> 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> A = { + {3}, + {4}, + {5} + }; + std::vector> B = { + {0, 1, 2} + }; + std::vector> C(3, std::vector(3, 0)); + std::vector> D(3, std::vector(3, 0)); + + // act + multiplyMatrices(A, B, C, 3, 1, 3); + multiplyMatricesWithoutErrors(A, B, D, 3, 1, 3); + std::vector> 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> A = { + {1, 2} + }; + std::vector> B = { + {3, 4, 5, 6}, + {7, 8, 9, 10} + }; + std::vector> C(1, std::vector(4, 0)); + std::vector> D(1, std::vector(4, 0)); + + // act + multiplyMatrices(A, B, C, 1, 2, 4); + multiplyMatricesWithoutErrors(A, B, D, 1, 2, 4); + std::vector> 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> A = { + {1, 2, 3} + }; + std::vector> B = { + { 3, 4, 5, 6}, + { 7, 8, 9, 10}, + {11, 12, 13, 14} + }; + std::vector> C(2, std::vector(2, 0)); + std::vector> D(2, std::vector(2, 0)); + + // act + multiplyMatrices(A, B, C, 1, 3, 4); + multiplyMatricesWithoutErrors(A, B, D, 1, 3, 4); + std::vector> 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> A = { + {1} + }; + std::vector> B = { + {1} + }; + std::vector> C(1, std::vector(1, 0)); + std::vector> D(1, std::vector(1, 0)); + + // act + multiplyMatrices(A, B, C, 1, 1, 1); + multiplyMatricesWithoutErrors(A, B, D, 1, 1, 1); + std::vector> 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> A = { + {5} + }; + std::vector> B = { + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + }; + std::vector> C(1, std::vector(10, 0)); + std::vector> D(1, std::vector(10, 0)); + + // act + multiplyMatrices(A, B, C, 1, 1, 10); + multiplyMatricesWithoutErrors(A, B, D, 1, 1, 10); + std::vector> 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);