GCC Code Coverage Report


Directory: ./
File: unittest/test_controls.cpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 71 71 100.0%
Branches: 131 260 50.4%

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
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ControlFactory factory;
25 const boost::shared_ptr<crocoddyl::ControlParametrizationModelAbstract>&
26
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 control = factory.create(control_type, 10);
27
28 // Generating random values for the control parameters
29
2/4
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 const Eigen::VectorXd p = Eigen::VectorXd::Random(control->get_nu());
30
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 double t = Eigen::VectorXd::Random(1)(0) * 0.5 + 1.; // random in [0, 1]
31
32 // Get the num diff control
33
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 crocoddyl::ControlParametrizationModelNumDiff control_num_diff(control);
34
35 // Computing the partial derivatives of the value function
36 boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> data =
37
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 control->createData();
38 boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract>
39
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 data_num_diff = control_num_diff.createData();
40
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 control->calc(data, t, p);
41
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 control_num_diff.calc(data_num_diff, t, p);
42
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 control->calcDiff(data, t, p);
43
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
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
8/16
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 4 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 4 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 4 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 4 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 4 times.
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
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ControlFactory factory;
52 const boost::shared_ptr<crocoddyl::ControlParametrizationModelAbstract>&
53
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 control = factory.create(control_type, 10);
54
55 // Generating random values for the control parameters, the time, and the
56 // matrix to multiply
57
2/4
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 const Eigen::VectorXd p = Eigen::VectorXd::Random(control->get_nu());
58
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 double t = Eigen::VectorXd::Random(1)(0) * 0.5 + 1.; // random in [0, 1]
59
2/4
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 const Eigen::MatrixXd A = Eigen::MatrixXd::Random(5, control->get_nw());
60
61 // Get the num diff control and datas
62
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 crocoddyl::ControlParametrizationModelNumDiff control_num_diff(control);
63 boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> data =
64
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 control->createData();
65 boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract>
66
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 data_num_diff = control_num_diff.createData();
67
68 // Checking the operator
69
2/4
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 Eigen::MatrixXd A_J(Eigen::MatrixXd::Zero(A.rows(), control->get_nu()));
70 Eigen::MatrixXd A_J_num_diff(
71
2/4
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 Eigen::MatrixXd::Zero(A.rows(), control->get_nu()));
72
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 control->calc(data, t, p);
73
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 control->calcDiff(data, t, p);
74
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 control_num_diff.calc(data_num_diff, t, p);
75
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 control_num_diff.calcDiff(data_num_diff, t, p);
76
3/6
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
4 control->multiplyByJacobian(data, A, A_J);
77
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
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
8/16
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 4 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 4 times.
✗ Branch 21 not taken.
✓ Branch 23 taken 4 times.
✗ Branch 24 not taken.
✗ Branch 28 not taken.
✓ Branch 29 taken 4 times.
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
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ControlFactory factory;
87 const boost::shared_ptr<crocoddyl::ControlParametrizationModelAbstract>&
88
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 control = factory.create(control_type, 10);
89
90 // Generating random values for the control parameters, the time, and the
91 // matrix to multiply
92
2/4
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 const Eigen::VectorXd p = Eigen::VectorXd::Random(control->get_nu());
93
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 double t = Eigen::VectorXd::Random(1)(0) * 0.5 + 1.; // random in [0, 1]
94
2/4
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 const Eigen::MatrixXd A = Eigen::MatrixXd::Random(control->get_nw(), 5);
95
96 // Get the num diff control and datas
97
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 crocoddyl::ControlParametrizationModelNumDiff control_num_diff(control);
98 boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract> data =
99
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 control->createData();
100 boost::shared_ptr<crocoddyl::ControlParametrizationDataAbstract>
101
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 data_num_diff = control_num_diff.createData();
102
103 // Checking the operator
104
2/4
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 Eigen::MatrixXd JT_A(Eigen::MatrixXd::Zero(control->get_nu(), A.cols()));
105 Eigen::MatrixXd JT_A_num_diff(
106
2/4
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 Eigen::MatrixXd::Zero(control->get_nu(), A.cols()));
107
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 control->calc(data, t, p);
108
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 control->calcDiff(data, t, p);
109
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 control_num_diff.calc(data_num_diff, t, p);
110
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 control_num_diff.calcDiff(data_num_diff, t, p);
111
3/6
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
4 control->multiplyJacobianTransposeBy(data, A, JT_A);
112
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
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
8/16
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 4 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 4 times.
✗ Branch 21 not taken.
✓ Branch 23 taken 4 times.
✗ Branch 24 not taken.
✗ Branch 28 not taken.
✓ Branch 29 taken 4 times.
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
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 boost::test_tools::output_test_stream test_name;
123
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 test_name << "test_" << control_type;
124
4/8
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
4 std::cout << "Running " << test_name.str() << std::endl;
125
4/8
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
4 test_suite* ts = BOOST_TEST_SUITE(test_name.str());
126
5/10
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
4 ts->add(BOOST_TEST_CASE(boost::bind(&test_calcDiff_num_diff, control_type)));
127
5/10
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
4 ts->add(BOOST_TEST_CASE(
128 boost::bind(&test_multiplyByJacobian_num_diff, control_type)));
129
5/10
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
4 ts->add(BOOST_TEST_CASE(
130 boost::bind(&test_multiplyJacobianTransposeBy_num_diff, control_type)));
131
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 framework::master_test_suite().add(ts);
132 4 }
133
134 1 bool init_function() {
135
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
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 }
144