MathTests.cc
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "UnitTestUtils.hpp"
4 
5 const double TEST_PREC = 1.0e-14;
6 
7 using namespace std;
8 using namespace RobotDynamics::Math;
9 
10 struct MathFixture : public testing::Test
11 {
12 
13 };
14 
15 TEST (MathFixture, GaussElimPivot)
16 {
17 
18  MatrixNd A;
19  A.resize(3, 3);
20  VectorNd b(3);
21  VectorNd x(3);
22 
23  A(0, 0) = 0;
24  A(0, 1) = 2;
25  A(0, 2) = 1;
26  A(1, 0) = 1;
27  A(1, 1) = 1;
28  A(1, 2) = 5;
29  A(2, 0) = 0;
30  A(2, 1) = 0;
31  A(2, 2) = 1;
32 
33  b[0] = 1;
34  b[1] = 2;
35  b[2] = 3;
36 
37  VectorNd test_result(3);
38 
39  test_result[0] = -12;
40  test_result[1] = -1;
41  test_result[2] = 3;
42 
43  linSolveGaussElimPivot(A, b, x);
44 
45  EXPECT_TRUE(unit_test_utils::checkVectorNdEpsilonClose(test_result, x, TEST_PREC));
46 
47  A(0, 0) = 0;
48  A(0, 1) = -2;
49  A(0, 2) = 1;
50  A(1, 0) = 1;
51  A(1, 1) = 1;
52  A(1, 2) = 5;
53  A(2, 0) = 0;
54  A(2, 1) = 0;
55  A(2, 2) = 1;
56 
57  linSolveGaussElimPivot(A, b, x);
58  test_result[0] = -14;
59  test_result[1] = 1;
60  test_result[2] = 3;
61 
62  EXPECT_TRUE(unit_test_utils::checkVectorNdEpsilonClose(test_result, x, TEST_PREC));
63 }
64 
65 TEST (MathFixture, Dynamic_1D_initialize_value)
66 {
67  VectorNd myvector_10 = VectorNd::Constant((size_t) 10, 12.);
68 
69  double *test_values = new double[10];
70  for (unsigned int i = 0; i < 10; i++)
71  {
72  test_values[i] = 12.;
73  }
74 
75  for(int i = 0;i<myvector_10.rows();i++)
76  {
77  EXPECT_EQ(myvector_10(i),test_values[i]);
78  }
79 
80  delete[] test_values;
81 }
82 
83 TEST (MathFixture, Dynamic_2D_initialize_value)
84 {
85  MatrixNd mymatrix_10x10 = MatrixNd::Constant(10, 10, 12.);
86 
87  double *test_values = new double[10 * 10];
88  for (unsigned int i = 0; i < 10; i++)
89  {
90  for (unsigned int j = 0; j < 10; j++)
91  {
92  test_values[i * 10 + j] = 12.;
93  }
94  }
95 
96  for(int i = 0;i<mymatrix_10x10.rows();i++)
97  {
98  for(int j = 0;j<mymatrix_10x10.cols();j++)
99  {
100  EXPECT_EQ(test_values[i*10+j],mymatrix_10x10(i,j));
101  }
102  }
103 
104 // EXPECT_TRUE(unit_test_utils::checkArraysEq(test_values, mymatrix_10x10.data(), 10 * 10));
105  delete[] test_values;
106 }
107 
108 TEST (MathFixture, SpatialMatrix_Multiplication)
109 {
110  SpatialMatrix X_1(
111  1., 2., 3., 4., 5., 6.,
112  11., 12., 13., 14., 15., 16.,
113  21., 22., 23., 24., 25., 26.,
114  31., 32., 33., 34., 35., 36.,
115  41., 42., 43., 44., 45., 46.,
116  51., 52., 53., 54., 55., 56.
117  );
118 
119  SpatialMatrix X_2(X_1);
120 
121  X_2 *= 2;
122 
123  SpatialMatrix correct_result(
124  1442, 1484, 1526, 1568, 1610, 1652,
125  4562, 4724, 4886, 5048, 5210, 5372,
126  7682, 7964, 8246, 8528, 8810, 9092,
127  10802, 11204, 11606, 12008, 12410, 12812,
128  13922, 14444, 14966, 15488, 16010, 16532,
129  17042, 17684, 18326, 18968, 19610, 20252
130  );
131 
132  SpatialMatrix test_result = X_1 * X_2;
133 
134  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(correct_result, test_result, TEST_PREC));
135 
136  // check the *= operator:
137  test_result = X_1;
138  test_result *= X_2;
139 
140  EXPECT_TRUE(unit_test_utils::checkSpatialMatrixEpsilonClose(correct_result, test_result, TEST_PREC));
141 }
142 
143 int main(int argc, char **argv)
144 {
145  ::testing::InitGoogleTest(&argc, argv);
146  return RUN_ALL_TESTS();
147 }
static bool checkVectorNdEpsilonClose(const RobotDynamics::Math::VectorNd &v1, const RobotDynamics::Math::VectorNd &v2, const double epsilon)
int main(int argc, char **argv)
Definition: MathTests.cc:143
bool linSolveGaussElimPivot(MatrixNd A, VectorNd b, VectorNd &x)
Solves a linear system using gaussian elimination with pivoting.
const double TEST_PREC
Definition: MathTests.cc:5
Eigen::VectorXd VectorNd
Definition: rdl_eigenmath.h:25
static bool checkSpatialMatrixEpsilonClose(const RobotDynamics::Math::SpatialMatrix &t1, const RobotDynamics::Math::SpatialMatrix &t2, const double epsilon)
TEST(MathFixture, GaussElimPivot)
Definition: MathTests.cc:15
Math types such as vectors and matrices and utility functions.
Definition: ForceVector.hpp:21
Eigen::MatrixXd MatrixNd
Definition: rdl_eigenmath.h:26


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:27