|
1 | 1 | #include <gtest/gtest.h>
|
| 2 | +#include <vector> |
| 3 | + |
2 | 4 | #include "matrix_add.h"
|
3 | 5 | #include "matrix_mult.h"
|
4 |
| -#include <vector> |
5 | 6 |
|
| 7 | +// Template-based parameterized test for matrix operations |
6 | 8 | template <typename T>
|
7 |
| -class MatrixOperationsTest : public ::testing::Test { |
| 9 | +class MatrixOperationsTest : public testing::Test { |
8 | 10 | protected:
|
9 |
| - static constexpr int kMatrixSize = 2; |
10 |
| - static constexpr int kMatrixElements = kMatrixSize * kMatrixSize; |
11 |
| - |
12 |
| - std::vector<T> matrixA; |
13 |
| - std::vector<T> matrixB; |
14 |
| - std::vector<T> resultMatrix; |
15 |
| - |
16 |
| - void SetUp() override { |
17 |
| - // Initialize matrices A and B with test data |
18 |
| - matrixA = {1, 2, 3, 4}; |
19 |
| - matrixB = {5, 6, 7, 8}; |
20 |
| - resultMatrix.resize(kMatrixElements); |
| 11 | + // Static constants for default matrix configuration |
| 12 | + static constexpr int kDefaultMatrixSize = 2; |
| 13 | + static constexpr int kDefaultMatrixElements = kDefaultMatrixSize * kDefaultMatrixSize; |
| 14 | + |
| 15 | + // Helper function to create a matrix from initializer list |
| 16 | + std::vector<T> createMatrix(std::initializer_list<T> values) { |
| 17 | + return std::vector<T>(values); |
21 | 18 | }
|
22 | 19 |
|
23 |
| - void verifyResult(const std::vector<T>& expected) { |
24 |
| - ASSERT_EQ(resultMatrix.size(), expected.size()); |
| 20 | + // Helper function to verify matrix calculation results |
| 21 | + static void verifyResult(const std::vector<T>& result, const std::vector<T>& expected) { |
| 22 | + // Check matrix size |
| 23 | + ASSERT_EQ(result.size(), expected.size()) |
| 24 | + << "Result matrix size does not match expected matrix size"; |
| 25 | + |
| 26 | + // Compare elements with near-equality |
25 | 27 | for (size_t i = 0; i < expected.size(); i++) {
|
26 |
| - EXPECT_NEAR(resultMatrix[i], expected[i], 1e-5) |
27 |
| - << "Mismatch at index " << i; |
| 28 | + EXPECT_NEAR(result[i], expected[i], 1e-5) |
| 29 | + << "Mismatch at index " << i |
| 30 | + << ": expected " << expected[i] |
| 31 | + << ", got " << result[i]; |
28 | 32 | }
|
29 | 33 | }
|
30 | 34 | };
|
31 | 35 |
|
32 |
| -using TestTypes = ::testing::Types<float, double>; |
33 |
| -TYPED_TEST_SUITE(MatrixOperationsTest, TestTypes); |
| 36 | +// Register type-parameterized test suite |
| 37 | +TYPED_TEST_SUITE_P(MatrixOperationsTest); |
| 38 | + |
| 39 | +// Test case 1: Square matrix addition |
| 40 | +TYPED_TEST_P(MatrixOperationsTest, SquareMatrixAddition) { |
| 41 | + constexpr int size = this->kDefaultMatrixSize; |
34 | 42 |
|
35 |
| -TYPED_TEST(MatrixOperationsTest, AdditionTest) { |
36 |
| - addMatricesOnGPU(this->matrixA.data(), this->matrixB.data(), this->resultMatrix.data(), |
37 |
| - this->kMatrixSize, this->kMatrixSize); |
| 43 | + // Prepare test data |
| 44 | + auto matrixA = this->createMatrix({1, 2, 3, 4}); |
| 45 | + auto matrixB = this->createMatrix({5, 6, 7, 8}); |
| 46 | + std::vector<TypeParam> resultMatrix(size * size); |
38 | 47 |
|
39 |
| - std::vector<TypeParam> expectedSum = {6, 8, 10, 12}; |
40 |
| - this->verifyResult(expectedSum); |
| 48 | + // Call GPU matrix addition function |
| 49 | + addMatricesOnGPU(matrixA.data(), matrixB.data(), resultMatrix.data(), size, size); |
| 50 | + |
| 51 | + // Verify result |
| 52 | + auto expectedSum = this->createMatrix({6, 8, 10, 12}); |
| 53 | + this->verifyResult(resultMatrix, expectedSum); |
41 | 54 | }
|
42 | 55 |
|
43 |
| -TYPED_TEST(MatrixOperationsTest, MultiplicationTest) { |
44 |
| - multiplyMatricesOnGPU(this->matrixA.data(), this->matrixB.data(), this->resultMatrix.data(), |
45 |
| - this->kMatrixSize, this->kMatrixSize, this->kMatrixSize); |
| 56 | +// Test case 2: Square matrix multiplication |
| 57 | +TYPED_TEST_P(MatrixOperationsTest, SquareMatrixMultiplication) { |
| 58 | + constexpr int size = this->kDefaultMatrixSize; |
| 59 | + |
| 60 | + // Prepare test data |
| 61 | + auto matrixA = this->createMatrix({1, 2, 3, 4}); |
| 62 | + auto matrixB = this->createMatrix({5, 6, 7, 8}); |
| 63 | + std::vector<TypeParam> resultMatrix(size * size); |
46 | 64 |
|
47 |
| - std::vector<TypeParam> expectedProduct = {19, 22, 43, 50}; |
48 |
| - this->verifyResult(expectedProduct); |
| 65 | + // Call GPU matrix multiplication function |
| 66 | + multiplyMatricesOnGPU(matrixA.data(), matrixB.data(), resultMatrix.data(), size, size, size); |
| 67 | + |
| 68 | + // Verify result |
| 69 | + auto expectedProduct = this->createMatrix({19, 22, 43, 50}); |
| 70 | + this->verifyResult(resultMatrix, expectedProduct); |
49 | 71 | }
|
50 | 72 |
|
51 |
| -TYPED_TEST(MatrixOperationsTest, NonSquareAdditionTest) { |
52 |
| - const int rows = 2; |
53 |
| - const int cols = 3; |
54 |
| - std::vector<TypeParam> nonSquareA = {1, 2, 3, 4, 5, 6}; |
55 |
| - std::vector<TypeParam> nonSquareB = {7, 8, 9, 10, 11, 12}; |
| 73 | +// Test case 3: Non-square matrix addition |
| 74 | +TYPED_TEST_P(MatrixOperationsTest, NonSquareMatrixAddition) { |
| 75 | + constexpr int rows = 2; |
| 76 | + constexpr int cols = 3; |
| 77 | + |
| 78 | + // Prepare test data |
| 79 | + auto nonSquareA = this->createMatrix({1, 2, 3, 4, 5, 6}); |
| 80 | + auto nonSquareB = this->createMatrix({7, 8, 9, 10, 11, 12}); |
56 | 81 | std::vector<TypeParam> nonSquareResult(rows * cols);
|
57 | 82 |
|
| 83 | + // Call GPU matrix addition function |
58 | 84 | addMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(), rows, cols);
|
59 | 85 |
|
60 |
| - std::vector<TypeParam> expectedSum = {8, 10, 12, 14, 16, 18}; |
61 |
| - ASSERT_EQ(nonSquareResult.size(), expectedSum.size()); |
62 |
| - for (size_t i = 0; i < expectedSum.size(); i++) { |
63 |
| - EXPECT_NEAR(nonSquareResult[i], expectedSum[i], 1e-5) |
64 |
| - << "Mismatch at index " << i; |
65 |
| - } |
| 86 | + // Verify result |
| 87 | + auto expectedSum = this->createMatrix({8, 10, 12, 14, 16, 18}); |
| 88 | + this->verifyResult(nonSquareResult, expectedSum); |
66 | 89 | }
|
67 | 90 |
|
68 |
| -TYPED_TEST(MatrixOperationsTest, NonSquareMultiplicationTest) { |
69 |
| - const int rowsA = 2; |
70 |
| - const int colsA = 3; |
71 |
| - const int colsB = 2; |
72 |
| - std::vector<TypeParam> nonSquareA = {1, 2, 3, 4, 5, 6}; |
73 |
| - std::vector<TypeParam> nonSquareB = {7, 8, 9, 10, 11, 12}; |
| 91 | +// Test case 4: Non-square matrix multiplication |
| 92 | +TYPED_TEST_P(MatrixOperationsTest, NonSquareMatrixMultiplication) { |
| 93 | + constexpr int rowsA = 2; |
| 94 | + constexpr int colsA = 3; |
| 95 | + constexpr int colsB = 2; |
| 96 | + |
| 97 | + // Prepare test data |
| 98 | + auto nonSquareA = this->createMatrix({1, 2, 3, 4, 5, 6}); |
| 99 | + auto nonSquareB = this->createMatrix({7, 8, 9, 10, 11, 12}); |
74 | 100 | std::vector<TypeParam> nonSquareResult(rowsA * colsB);
|
75 | 101 |
|
76 |
| - multiplyMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(), |
77 |
| - rowsA, colsA, colsB); |
| 102 | + // Call GPU matrix multiplication function |
| 103 | + multiplyMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(), rowsA, colsA, colsB); |
78 | 104 |
|
79 |
| - std::vector<TypeParam> expectedProduct = {58, 64, 139, 154}; |
80 |
| - ASSERT_EQ(nonSquareResult.size(), expectedProduct.size()); |
81 |
| - for (size_t i = 0; i < expectedProduct.size(); i++) { |
82 |
| - EXPECT_NEAR(nonSquareResult[i], expectedProduct[i], 1e-5) |
83 |
| - << "Mismatch at index " << i; |
84 |
| - } |
| 105 | + // Verify result |
| 106 | + auto expectedProduct = this->createMatrix({58, 64, 139, 154}); |
| 107 | + this->verifyResult(nonSquareResult, expectedProduct); |
85 | 108 | }
|
| 109 | + |
| 110 | +// Register test cases |
| 111 | +REGISTER_TYPED_TEST_SUITE_P( |
| 112 | + MatrixOperationsTest, |
| 113 | + SquareMatrixAddition, |
| 114 | + SquareMatrixMultiplication, |
| 115 | + NonSquareMatrixAddition, |
| 116 | + NonSquareMatrixMultiplication |
| 117 | +); |
| 118 | + |
| 119 | +// Specify test types |
| 120 | +using TestTypes = testing::Types<float, double>; |
| 121 | +INSTANTIATE_TYPED_TEST_SUITE_P(MatrixOps, MatrixOperationsTest, TestTypes); |
0 commit comments