Fix matrix2 tests

This commit is contained in:
Pascal Serrarens 2025-04-01 17:27:06 +02:00
parent 91ff401ecf
commit 2a49d50844
4 changed files with 123 additions and 59 deletions

View File

@ -1,26 +1,53 @@
#include "Matrix.h" #include "Matrix.h"
#include <iostream>
#pragma region Matrix2 #pragma region Matrix2
Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) { Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) {
this->nValues = nRows * nCols; this->nValues = nRows * nCols;
data = new float[nValues](); if (this->nValues == 0)
data = nullptr;
else {
this->data = new float[nValues]();
this->externalData = false;
}
} }
Matrix2::Matrix2(float* data, int nRows, int nCols) Matrix2::Matrix2(float* data, int nRows, int nCols)
: nRows(nRows), nCols(nCols), data(data) { : nRows(nRows), nCols(nCols), data(data){
this->nValues = nRows * nCols; this->nValues = nRows * nCols;
this->externalData = true;
} }
Matrix2::~Matrix2() { Matrix2::~Matrix2() {
if (data != nullptr && !this->externalData)
delete[] data; delete[] data;
} }
// Move constructor
Matrix2::Matrix2(Matrix2&& other) noexcept
: nRows(other.nRows), nCols(other.nCols), nValues(other.nValues), data(other.data) {
other.data = nullptr; // Set the other object's pointer to nullptr to avoid double deletion
}
// Move assignment operator
Matrix2& Matrix2::operator=(Matrix2&& other) noexcept {
if (this != &other) {
delete[] data; // Clean up current data
nRows = other.nRows;
nCols = other.nCols;
nValues = other.nValues;
data = other.data;
other.data = nullptr; // Avoid double deletion
}
return *this;
}
Matrix2 Matrix2::Zero(int nRows, int nCols) { Matrix2 Matrix2::Zero(int nRows, int nCols) {
Matrix2 m = Matrix2(nRows, nCols); Matrix2 r = Matrix2(nRows, nCols);
for (int ix = 0; ix < m.nValues; ix++) for (int ix = 0; ix < r.nValues; ix++)
m.data[ix] = 0; r.data[ix] = 0;
return m; return r;
} }
Matrix2 Matrix2::Identity(int size) { Matrix2 Matrix2::Identity(int size) {
@ -31,7 +58,7 @@ Matrix2 Matrix2::Diagonal(float f, int size) {
Matrix2 r = Matrix2(size, size); Matrix2 r = Matrix2(size, size);
float* data = r.data; float* data = r.data;
int valueIx = 0; int valueIx = 0;
for (int ix = 0; ix < r.nValues; ix++) { for (int ix = 0; ix < size; ix++) {
data[valueIx] = f; data[valueIx] = f;
valueIx += size + 1; valueIx += size + 1;
} }
@ -71,12 +98,15 @@ Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const {
int BColOffset = i * BCols; // BColOffset is constant for each row of B int BColOffset = i * BCols; // BColOffset is constant for each row of B
for (int j = 0; j < BCols; ++j) { for (int j = 0; j < BCols; ++j) {
float sum = 0; float sum = 0;
std::cout << " 0";
int BIndex = j; int BIndex = j;
for (int k = 0; k < ACols; ++k) { for (int k = 0; k < ACols; ++k) {
std::cout << " + " << this->data[ARowOffset + k] << " * " << B.data[BIndex];
sum += this->data[ARowOffset + k] * B.data[BIndex]; sum += this->data[ARowOffset + k] * B.data[BIndex];
BIndex += BCols; BIndex += BCols;
} }
r.data[BColOffset + j] = sum; r.data[BColOffset + j] = sum;
std::cout << " = " << sum << " ix: " << BColOffset + j << "\n";
} }
} }
return r; return r;

View File

@ -11,6 +11,7 @@ class Matrix2 {
int nCols = 0; int nCols = 0;
int nValues = 0; int nValues = 0;
float* data = nullptr; float* data = nullptr;
bool externalData = true;
Matrix2(int nRows, int nCols); Matrix2(int nRows, int nCols);
Matrix2(float* data, int nRows, int nCols); Matrix2(float* data, int nRows, int nCols);
@ -30,6 +31,10 @@ class Matrix2 {
Matrix2 operator*(const Matrix2& m) const; Matrix2 operator*(const Matrix2& m) const;
void SetSlice(int rowStart, int rowStop, int colStart, int colStop, const Matrix2& m) const; void SetSlice(int rowStart, int rowStop, int colStart, int colStop, const Matrix2& m) const;
//private:
// move constructor and move assignment operator
Matrix2(Matrix2&& other) noexcept;
Matrix2& operator=(Matrix2&& other) noexcept;
}; };
/// @brief Single precision float matrix /// @brief Single precision float matrix

View File

@ -1,10 +1,44 @@
#if GTEST #if GTEST
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <limits>
#include <math.h> #include <math.h>
#include <limits>
#include "Matrix.h" #include "Matrix.h"
TEST(Matrix2, Zero) {
// Test case 1: 2x2 zero matrix
Matrix2 zeroMatrix = Matrix2::Zero(2, 2);
EXPECT_TRUE(zeroMatrix.nRows == 2);
EXPECT_TRUE(zeroMatrix.nCols == 2);
for (int i = 0; i < zeroMatrix.nValues; ++i) {
EXPECT_TRUE(zeroMatrix.data[i] == 0.0f);
}
std::cout << "Test case 1 passed: 2x2 zero matrix\n";
// Test case 2: 3x3 zero matrix
zeroMatrix = Matrix2::Zero(3, 3);
EXPECT_TRUE(zeroMatrix.nRows == 3);
EXPECT_TRUE(zeroMatrix.nCols == 3);
for (int i = 0; i < zeroMatrix.nValues; ++i) {
EXPECT_TRUE(zeroMatrix.data[i] == 0.0f);
}
std::cout << "Test case 2 passed: 3x3 zero matrix\n";
// Test case 3: 1x1 zero matrix
zeroMatrix = Matrix2::Zero(1, 1);
EXPECT_TRUE(zeroMatrix.nRows == 1);
EXPECT_TRUE(zeroMatrix.nCols == 1);
EXPECT_TRUE(zeroMatrix.data[0] == 0.0f);
std::cout << "Test case 3 passed: 1x1 zero matrix\n";
// Test case 4: 0x0 matrix (edge case)
zeroMatrix = Matrix2::Zero(0, 0);
EXPECT_TRUE(zeroMatrix.nRows == 0);
EXPECT_TRUE(zeroMatrix.nCols == 0);
EXPECT_TRUE(zeroMatrix.data == nullptr);
std::cout << "Test case 4 passed: 0x0 matrix\n";
}
TEST(Matrix2, Multiplication) { TEST(Matrix2, Multiplication) {
// Test 1: Multiplying two 2x2 matrices // Test 1: Multiplying two 2x2 matrices
float dataA[] = {1, 2, 3, 4}; float dataA[] = {1, 2, 3, 4};
@ -15,12 +49,11 @@ TEST(Matrix2, Multiplication) {
Matrix2 result = A * B; Matrix2 result = A * B;
float expectedData[] = {19, 22, 43, 50}; float expectedData[] = {19, 22, 43, 50};
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i)
//assert(result.data[i] == expectedData[i]);
EXPECT_TRUE(result.data[i] == expectedData[i]); EXPECT_TRUE(result.data[i] == expectedData[i]);
}
std::cout << "Test 1 passed: 2x2 matrix multiplication.\n"; std::cout << "Test 1 passed: 2x2 matrix multiplication.\n";
// Test 2: Multiplying a 3x2 matrix with a 2x3 matrix // Test 2: Multiplying a 3x2 matrix with a 2x3 matrix
float dataC[] = {1, 2, 3, 4, 5, 6}; float dataC[] = {1, 2, 3, 4, 5, 6};
float dataD[] = {7, 8, 9, 10, 11, 12}; float dataD[] = {7, 8, 9, 10, 11, 12};
@ -29,32 +62,27 @@ TEST(Matrix2, Multiplication) {
Matrix2 result2 = C * D; Matrix2 result2 = C * D;
float expectedData2[] = {29, 32, 35, 65, 72, 79, 101, 112, 123}; float expectedData2[] = {27, 30, 33, 61, 68, 75, 95, 106, 117};
for (int i = 0; i < 9; ++i) { for (int i = 0; i < 9; ++i)
assert(result2.data[i] == expectedData2[i]);
EXPECT_TRUE(result2.data[i] == expectedData2[i]); EXPECT_TRUE(result2.data[i] == expectedData2[i]);
}
std::cout << "Test 2 passed: 3x2 * 2x3 matrix multiplication.\n"; std::cout << "Test 2 passed: 3x2 * 2x3 matrix multiplication.\n";
// Test 3: Multiplying with a zero matrix // Test 3: Multiplying with a zero matrix
Matrix2 zeroMatrix = Matrix2::Zero(2, 2); Matrix2 zeroMatrix = Matrix2::Zero(2, 2);
Matrix2 result3 = A * zeroMatrix; Matrix2 result3 = A * zeroMatrix;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i)
assert(result3.data[i] == 0);
EXPECT_TRUE(result3.data[i] == 0); EXPECT_TRUE(result3.data[i] == 0);
}
std::cout << "Test 3 passed: Multiplication with zero matrix.\n"; std::cout << "Test 3 passed: Multiplication with zero matrix.\n";
// Test 4: Multiplying with an identity matrix // Test 4: Multiplying with an identity matrix
Matrix2 identityMatrix = Matrix2::Identity(2); Matrix2 identityMatrix = Matrix2::Identity(2);
Matrix2 result4 = A * identityMatrix; Matrix2 result4 = A * identityMatrix;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i)
assert(result4.data[i] == A.data[i]);
EXPECT_TRUE(result4.data[i] == A.data[i]); EXPECT_TRUE(result4.data[i] == A.data[i]);
}
std::cout << "Test 4 passed: Multiplication with identity matrix.\n"; std::cout << "Test 4 passed: Multiplication with identity matrix.\n";
} }
TEST(MatrixSingle, Init) { TEST(MatrixSingle, Init) {

View File

@ -86,10 +86,11 @@ class LocalParticipant : public Participant {
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
int sock; int sock;
#elif defined(_WIN32) || defined(_WIN64)
sockaddr_in remote_addr;
sockaddr_in server_addr;
sockaddr_in broadcast_addr;
#endif #endif
// sockaddr_in remote_addr;
// sockaddr_in server_addr;
// sockaddr_in broadcast_addr;
#endif #endif