-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestcppvsmatlab.cpp
More file actions
97 lines (81 loc) · 3.09 KB
/
testcppvsmatlab.cpp
File metadata and controls
97 lines (81 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <gtest/gtest.h>
#include "Eigen/Dense"
#include <fstream>
#include <sstream>
#include <string>
using namespace Eigen;
using namespace std;
// Utility function to read a matrix from a CSV file
MatrixXd readMatrixFromCSV(const string& filename) {
ifstream file(filename);
if (!file.is_open()) {
throw runtime_error("Cannot open file: " + filename);
}
vector<vector<double>> values;
string line;
while (getline(file, line)) {
stringstream lineStream(line);
string cell;
vector<double> row;
while (getline(lineStream, cell, ',')) {
row.push_back(stod(cell));
}
values.push_back(row);
}
size_t rows = values.size();
size_t cols = rows > 0 ? values[0].size() : 0;
MatrixXd matrix(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
matrix(i, j) = values[i][j];
}
}
return matrix;
}
// Test fixture class
class TestCppVsMatlab : public ::testing::Test {
protected:
MatrixXd matlabA, matlabB, matlabC;
MatrixXd cppA, cppB, cppC;
void SetUp() override {
// Read matrices generated by MATLAB
matlabA = readMatrixFromCSV("../data/A_matlab.dat");
matlabB = readMatrixFromCSV("../data/B_matlab.dat");
matlabC = readMatrixFromCSV("../data/C_matlab.dat");
// Read matrices generated by C++
cppA = readMatrixFromCSV("../data/A_cpp.dat");
cppB = readMatrixFromCSV("../data/B_cpp.dat");
cppC = readMatrixFromCSV("../data/C_cpp.dat");
}
};
// Test 1: Compare matrix A between MATLAB and C++
TEST_F(TestCppVsMatlab, CompareMatrixA) {
ASSERT_EQ(matlabA.rows(), cppA.rows()) << "Row mismatch in A";
ASSERT_EQ(matlabA.cols(), cppA.cols()) << "Column mismatch in A";
EXPECT_FALSE(matlabA.isApprox(cppA, 1e-9)) << "Matrix A is not approximately equal";
}
// Test 2: Compare matrix B between MATLAB and C++
TEST_F(TestCppVsMatlab, CompareMatrixB) {
ASSERT_EQ(matlabB.rows(), cppB.rows()) << "Row mismatch in B";
ASSERT_EQ(matlabB.cols(), cppB.cols()) << "Column mismatch in B";
EXPECT_TRUE(matlabB.isApprox(cppB, 1e-9)) << "Matrix B is not approximately equal";
}
// Test 3: Compare C.dat between MATLAB and C++
TEST_F(TestCppVsMatlab, CompareMatrixC) {
ASSERT_EQ(matlabC.rows(), cppC.rows()) << "Row mismatch in C";
ASSERT_EQ(matlabC.cols(), cppC.cols()) << "Column mismatch in C";
EXPECT_TRUE(matlabC.isApprox(cppC, 1e-9)) << "Matrix C is not approximately equal";
}
// Test 4: Compare Matix A and B
TEST_F(TestCppVsMatlab, ComparaMatrixAB) {
EXPECT_FALSE(matlabA.isApprox(matlabB, 1e-9)) << "Matrix A is the same as matrix B";
}
// Other types of tests
// Test: No exceptions are thrown i.e. function does not fail
TEST_F(TestCppVsMatlab, readMatrixFromCSVDoesNotThrowException) {
EXPECT_NO_THROW(readMatrixFromCSV("../data/A_matlab.dat"));
}
// Test: Exception is thrown i.e. function fails as expected
TEST_F(TestCppVsMatlab, readMatrixFromCSVThrowsException) {
EXPECT_THROW(readMatrixFromCSV("../data/non_existing_file.dat"), std::runtime_error);
}