GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/////////////////////////////////////////////////////////////////////////////// |
||
2 |
// BSD 3-Clause License |
||
3 |
// |
||
4 |
// Copyright (C) 2021-2023, LAAS-CNRS, New York University, Max Planck |
||
5 |
// Gesellschaft, |
||
6 |
// University of Edinburgh, INRIA, University of Trento |
||
7 |
// Copyright note valid unless otherwise controld in individual files. |
||
8 |
// All rights reserved. |
||
9 |
/////////////////////////////////////////////////////////////////////////////// |
||
10 |
|||
11 |
#define BOOST_TEST_NO_MAIN |
||
12 |
#define BOOST_TEST_ALTERNATIVE_INIT_API |
||
13 |
|||
14 |
#include "crocoddyl/core/numdiff/control.hpp" |
||
15 |
#include "factory/control.hpp" |
||
16 |
#include "unittest_common.hpp" |
||
17 |
|||
18 |
using namespace boost::unit_test; |
||
19 |
using namespace crocoddyl::unittest; |
||
20 |
|||
21 |
//----------------------------------------------------------------------------// |
||
22 |
|||
23 |
4 |
void test_calcDiff_num_diff(ControlTypes::Type control_type) { |
|
24 |
✓✗ | 8 |
ControlFactory factory; |
25 |
const boost::shared_ptr<crocoddyl::ControlParametrizationModelAbstract>& |
||
26 |
✓✗ | 8 |
control = factory.create(control_type, 10); |
27 |
|||
28 |
// Generating random values for the control parameters |
||
29 |
✓✗✓✗ |
8 |
const Eigen::VectorXd p = Eigen::VectorXd::Random(control->get_nu()); |
30 |
✓✗✓✗ |
4 |
double t = Eigen::VectorXd::Random(1)(0) * 0.5 + 1.; // random in [0, 1] |
31 |
|||
32 |
// Get the num diff control |
||
33 |
✓✗ | 8 |
crocoddyl::ControlParametrizationModelNumDiff control_num_diff(control); |
34 |
|||
35 |
// Computing the partial derivatives of the value function |
||
36 |
boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> data = |
||
37 |
✓✗ | 8 |
control->createData(); |
38 |
boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> |
||
39 |
✓✗ | 8 |
data_num_diff = control_num_diff.createData(); |
40 |
✓✗✓✗ |
4 |
control->calc(data, t, p); |
41 |
✓✗✓✗ |
4 |
control_num_diff.calc(data_num_diff, t, p); |
42 |
✓✗✓✗ |
4 |
control->calcDiff(data, t, p); |
43 |
✓✗✓✗ |
4 |
control_num_diff.calcDiff(data_num_diff, t, p); |
44 |
// Tolerance defined as in |
||
45 |
// http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf |
||
46 |
4 |
double tol = std::pow(control_num_diff.get_disturbance(), 1. / 3.); |
|
47 |
✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✓ |
4 |
BOOST_CHECK((data->dw_du - data_num_diff->dw_du).isZero(tol)); |
48 |
4 |
} |
|
49 |
|||
50 |
4 |
void test_multiplyByJacobian_num_diff(ControlTypes::Type control_type) { |
|
51 |
✓✗ | 8 |
ControlFactory factory; |
52 |
const boost::shared_ptr<crocoddyl::ControlParametrizationModelAbstract>& |
||
53 |
✓✗ | 8 |
control = factory.create(control_type, 10); |
54 |
|||
55 |
// Generating random values for the control parameters, the time, and the |
||
56 |
// matrix to multiply |
||
57 |
✓✗✓✗ |
8 |
const Eigen::VectorXd p = Eigen::VectorXd::Random(control->get_nu()); |
58 |
✓✗✓✗ |
4 |
double t = Eigen::VectorXd::Random(1)(0) * 0.5 + 1.; // random in [0, 1] |
59 |
✓✗✓✗ |
8 |
const Eigen::MatrixXd A = Eigen::MatrixXd::Random(5, control->get_nw()); |
60 |
|||
61 |
// Get the num diff control and datas |
||
62 |
✓✗ | 8 |
crocoddyl::ControlParametrizationModelNumDiff control_num_diff(control); |
63 |
boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> data = |
||
64 |
✓✗ | 8 |
control->createData(); |
65 |
boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> |
||
66 |
✓✗ | 8 |
data_num_diff = control_num_diff.createData(); |
67 |
|||
68 |
// Checking the operator |
||
69 |
✓✗✓✗ ✓✗ |
8 |
Eigen::MatrixXd A_J(Eigen::MatrixXd::Zero(A.rows(), control->get_nu())); |
70 |
Eigen::MatrixXd A_J_num_diff( |
||
71 |
✓✗✓✗ ✓✗ |
8 |
Eigen::MatrixXd::Zero(A.rows(), control->get_nu())); |
72 |
✓✗✓✗ |
4 |
control->calc(data, t, p); |
73 |
✓✗✓✗ |
4 |
control->calcDiff(data, t, p); |
74 |
✓✗✓✗ |
4 |
control_num_diff.calc(data_num_diff, t, p); |
75 |
✓✗✓✗ |
4 |
control_num_diff.calcDiff(data_num_diff, t, p); |
76 |
✓✗✓✗ ✓✗ |
4 |
control->multiplyByJacobian(data, A, A_J); |
77 |
✓✗✓✗ ✓✗ |
4 |
control_num_diff.multiplyByJacobian(data_num_diff, A, A_J_num_diff); |
78 |
// Tolerance defined as in |
||
79 |
// http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf |
||
80 |
4 |
double tol = std::pow(control_num_diff.get_disturbance(), 1. / 3.); |
|
81 |
✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✓ |
4 |
BOOST_CHECK((A_J - A_J_num_diff).isZero(tol)); |
82 |
4 |
} |
|
83 |
|||
84 |
4 |
void test_multiplyJacobianTransposeBy_num_diff( |
|
85 |
ControlTypes::Type control_type) { |
||
86 |
✓✗ | 8 |
ControlFactory factory; |
87 |
const boost::shared_ptr<crocoddyl::ControlParametrizationModelAbstract>& |
||
88 |
✓✗ | 8 |
control = factory.create(control_type, 10); |
89 |
|||
90 |
// Generating random values for the control parameters, the time, and the |
||
91 |
// matrix to multiply |
||
92 |
✓✗✓✗ |
8 |
const Eigen::VectorXd p = Eigen::VectorXd::Random(control->get_nu()); |
93 |
✓✗✓✗ |
4 |
double t = Eigen::VectorXd::Random(1)(0) * 0.5 + 1.; // random in [0, 1] |
94 |
✓✗✓✗ |
8 |
const Eigen::MatrixXd A = Eigen::MatrixXd::Random(control->get_nw(), 5); |
95 |
|||
96 |
// Get the num diff control and datas |
||
97 |
✓✗ | 8 |
crocoddyl::ControlParametrizationModelNumDiff control_num_diff(control); |
98 |
boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> data = |
||
99 |
✓✗ | 8 |
control->createData(); |
100 |
boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> |
||
101 |
✓✗ | 8 |
data_num_diff = control_num_diff.createData(); |
102 |
|||
103 |
// Checking the operator |
||
104 |
✓✗✓✗ ✓✗ |
8 |
Eigen::MatrixXd JT_A(Eigen::MatrixXd::Zero(control->get_nu(), A.cols())); |
105 |
Eigen::MatrixXd JT_A_num_diff( |
||
106 |
✓✗✓✗ ✓✗ |
8 |
Eigen::MatrixXd::Zero(control->get_nu(), A.cols())); |
107 |
✓✗✓✗ |
4 |
control->calc(data, t, p); |
108 |
✓✗✓✗ |
4 |
control->calcDiff(data, t, p); |
109 |
✓✗✓✗ |
4 |
control_num_diff.calc(data_num_diff, t, p); |
110 |
✓✗✓✗ |
4 |
control_num_diff.calcDiff(data_num_diff, t, p); |
111 |
✓✗✓✗ ✓✗ |
4 |
control->multiplyJacobianTransposeBy(data, A, JT_A); |
112 |
✓✗✓✗ ✓✗ |
4 |
control_num_diff.multiplyJacobianTransposeBy(data_num_diff, A, JT_A_num_diff); |
113 |
// Tolerance defined as in |
||
114 |
// http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf |
||
115 |
4 |
double tol = std::pow(control_num_diff.get_disturbance(), 1. / 3.); |
|
116 |
✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✓ |
4 |
BOOST_CHECK((JT_A - JT_A_num_diff).isZero(tol)); |
117 |
4 |
} |
|
118 |
|||
119 |
//----------------------------------------------------------------------------// |
||
120 |
|||
121 |
4 |
void register_control_unit_tests(ControlTypes::Type control_type) { |
|
122 |
✓✗✓✗ |
8 |
boost::test_tools::output_test_stream test_name; |
123 |
✓✗✓✗ |
4 |
test_name << "test_" << control_type; |
124 |
✓✗✓✗ ✓✗✓✗ |
4 |
std::cout << "Running " << test_name.str() << std::endl; |
125 |
✓✗✓✗ ✓✗✓✗ |
4 |
test_suite* ts = BOOST_TEST_SUITE(test_name.str()); |
126 |
✓✗✓✗ ✓✗✓✗ ✓✗ |
4 |
ts->add(BOOST_TEST_CASE(boost::bind(&test_calcDiff_num_diff, control_type))); |
127 |
✓✗✓✗ ✓✗✓✗ ✓✗ |
4 |
ts->add(BOOST_TEST_CASE( |
128 |
boost::bind(&test_multiplyByJacobian_num_diff, control_type))); |
||
129 |
✓✗✓✗ ✓✗✓✗ ✓✗ |
4 |
ts->add(BOOST_TEST_CASE( |
130 |
boost::bind(&test_multiplyJacobianTransposeBy_num_diff, control_type))); |
||
131 |
✓✗✓✗ ✓✗ |
4 |
framework::master_test_suite().add(ts); |
132 |
4 |
} |
|
133 |
|||
134 |
1 |
bool init_function() { |
|
135 |
✓✓ | 5 |
for (size_t i = 0; i < ControlTypes::all.size(); ++i) { |
136 |
4 |
register_control_unit_tests(ControlTypes::all[i]); |
|
137 |
} |
||
138 |
1 |
return true; |
|
139 |
} |
||
140 |
|||
141 |
1 |
int main(int argc, char** argv) { |
|
142 |
1 |
return ::boost::unit_test::unit_test_main(&init_function, argc, argv); |
|
143 |
} |
Generated by: GCOVR (Version 4.2) |