GCC Code Coverage Report


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

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, LAAS-CNRS, New York University, Max Planck
5 // Gesellschaft
6 // University of Edinburgh, INRIA
7 // Copyright note valid unless otherwise stated 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/activations/quadratic-barrier.hpp"
15 #include "crocoddyl/core/utils/exception.hpp"
16 #include "factory/activation.hpp"
17 #include "unittest_common.hpp"
18
19 using namespace boost::unit_test;
20 using namespace crocoddyl::unittest;
21
22 //----------------------------------------------------------------------------//
23
24 9 void test_construct_data(ActivationModelTypes::Type activation_type) {
25 // create the model
26
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 ActivationModelFactory factory;
27 const boost::shared_ptr<crocoddyl::ActivationModelAbstract>& model =
28
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 factory.create(activation_type);
29
30 // Run the print function
31
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 std::ostringstream tmp;
32
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 tmp << *model;
33
34 // create the corresponding data object
35 boost::shared_ptr<crocoddyl::ActivationDataAbstract> data =
36
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 model->createData();
37 9 }
38
39 9 void test_calc_returns_a_value(ActivationModelTypes::Type activation_type) {
40 // create the model
41
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 ActivationModelFactory factory;
42 const boost::shared_ptr<crocoddyl::ActivationModelAbstract>& model =
43
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 factory.create(activation_type);
44
45 // create the corresponding data object
46 boost::shared_ptr<crocoddyl::ActivationDataAbstract> data =
47
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 model->createData();
48
49 // Generating random input vector
50
2/4
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 const Eigen::VectorXd r = Eigen::VectorXd::Random(model->get_nr());
51 9 data->a_value = nan("");
52
53 // Getting the state dimension from calc() call
54
2/4
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
9 model->calc(data, r);
55
56 // Checking that calc returns a value
57
6/12
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 9 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 9 times.
✗ Branch 20 not taken.
✗ Branch 24 not taken.
✓ Branch 25 taken 9 times.
9 BOOST_CHECK(!std::isnan(data->a_value));
58 9 }
59
60 9 void test_partial_derivatives_against_numdiff(
61 ActivationModelTypes::Type activation_type) {
62 // create the model
63
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 ActivationModelFactory factory;
64 const boost::shared_ptr<crocoddyl::ActivationModelAbstract>& model =
65
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 factory.create(activation_type);
66
67 // create the corresponding data object and set the cost to nan
68 boost::shared_ptr<crocoddyl::ActivationDataAbstract> data =
69
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 model->createData();
70
71
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 crocoddyl::ActivationModelNumDiff model_num_diff(model);
72 boost::shared_ptr<crocoddyl::ActivationDataAbstract> data_num_diff =
73
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 model_num_diff.createData();
74
75 // Generating random values for the state and control
76
2/4
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 const Eigen::VectorXd r = Eigen::VectorXd::Random(model->get_nr());
77
78 // Computing the activation derivatives
79
2/4
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
9 model->calc(data, r);
80
2/4
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
9 model->calcDiff(data, r);
81
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 model_num_diff.calc(data_num_diff, r);
82
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 model_num_diff.calcDiff(data_num_diff, r);
83 // Tolerance defined as in
84 // http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf
85 9 double tol = std::pow(model_num_diff.get_disturbance(), 1. / 3.);
86
6/12
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 17 taken 9 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 9 times.
✗ Branch 21 not taken.
✗ Branch 25 not taken.
✓ Branch 26 taken 9 times.
9 BOOST_CHECK(std::abs(data->a_value - data_num_diff->a_value) < tol);
87
8/16
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 9 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 9 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 9 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 9 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 9 times.
9 BOOST_CHECK((data->Ar - data_num_diff->Ar).isZero(tol));
88
89 // numerical differentiation of the Hessian is not good enough to be tested.
90 // BOOST_CHECK((data->Arr - data_num_diff->Arr).isMuchSmallerThan(1.0, tol));
91 9 }
92
93 1 void test_activation_bounds_with_infinity() {
94
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 Eigen::VectorXd lb(1);
95
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 Eigen::VectorXd ub(1);
96 double beta;
97 1 beta = 0.1;
98
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 lb[0] = 0;
99
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 ub[0] = std::numeric_limits<double>::infinity();
100
101 Eigen::VectorXd m =
102
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 0.5 * (lb + Eigen::VectorXd::Constant(
103
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
2 lb.size(), std::numeric_limits<double>::max()));
104 Eigen::VectorXd d =
105
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 0.5 * (Eigen::VectorXd::Constant(lb.size(),
106
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
2 std::numeric_limits<double>::max()) -
107
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 lb);
108
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 crocoddyl::ActivationBounds bounds(lb, ub, beta);
109
9/18
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 1 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
✓ Branch 26 taken 1 times.
✗ Branch 27 not taken.
✗ Branch 31 not taken.
✓ Branch 32 taken 1 times.
1 BOOST_CHECK(bounds.lb != m - beta * d);
110 1 }
111
112 //----------------------------------------------------------------------------//
113
114 9 void register_unit_tests(ActivationModelTypes::Type activation_type) {
115
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 boost::test_tools::output_test_stream test_name;
116
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 test_name << "test_" << activation_type;
117
4/8
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 9 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
9 std::cout << "Running " << test_name.str() << std::endl;
118
4/8
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
9 test_suite* ts = BOOST_TEST_SUITE(test_name.str());
119
5/10
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 9 times.
✗ Branch 15 not taken.
9 ts->add(BOOST_TEST_CASE(boost::bind(&test_construct_data, activation_type)));
120
5/10
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 9 times.
✗ Branch 15 not taken.
9 ts->add(BOOST_TEST_CASE(
121 boost::bind(&test_calc_returns_a_value, activation_type)));
122
5/10
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 9 times.
✗ Branch 15 not taken.
9 ts->add(BOOST_TEST_CASE(
123 boost::bind(&test_partial_derivatives_against_numdiff, activation_type)));
124
3/6
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 framework::master_test_suite().add(ts);
125 9 }
126
127 1 bool register_bounds_unit_test() {
128
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 boost::test_tools::output_test_stream test_name;
129 test_name << "test_"
130
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 << "ActivationBoundsInfinity";
131
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
1 std::cout << "Running " << test_name.str() << std::endl;
132
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
1 test_suite* ts = BOOST_TEST_SUITE(test_name.str());
133
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
1 ts->add(BOOST_TEST_CASE(boost::bind(&test_activation_bounds_with_infinity)));
134
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 framework::master_test_suite().add(ts);
135 1 return true;
136 1 }
137
138 1 bool init_function() {
139
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
10 for (size_t i = 0; i < ActivationModelTypes::all.size(); ++i) {
140 9 register_unit_tests(ActivationModelTypes::all[i]);
141 }
142 1 register_bounds_unit_test();
143 1 return true;
144 }
145
146 1 int main(int argc, char** argv) {
147 1 return ::boost::unit_test::unit_test_main(&init_function, argc, argv);
148 }
149