GCC Code Coverage Report


Directory: ./
File: tests/eiquadprog-both.cpp
Date: 2024-08-26 22:54:11
Exec Total Coverage
Lines: 73 73 100.0%
Branches: 205 410 50.0%

Line Branch Exec Source
1 //
2 // Copyright (c) 2020 CNRS
3 //
4 // This file is part of eiquadprog.
5 //
6 // eiquadprog is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 //(at your option) any later version.
10
11 // eiquadprog is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with eiquadprog. If not, see <https://www.gnu.org/licenses/>.
18
19 #include <Eigen/Core>
20 #include <boost/test/unit_test.hpp>
21 #include <iostream>
22
23 #include "eiquadprog/eiquadprog-fast.hpp"
24 #include "eiquadprog/eiquadprog-rt.hpp"
25
26 using namespace eiquadprog::solvers;
27
28 /**
29 * solves the problem
30 * min. 0.5 * x' Hess x + g0' x
31 * s.t. CE x + ce0 = 0
32 * CI x + ci0 >= 0
33 */
34
35 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
36
37 // min ||x||^2
38
39
33/66
✓ 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.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
✓ Branch 27 taken 1 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 1 times.
✗ Branch 31 not taken.
✓ Branch 33 taken 1 times.
✗ Branch 34 not taken.
✓ Branch 36 taken 1 times.
✗ Branch 37 not taken.
✓ Branch 41 taken 1 times.
✗ Branch 42 not taken.
✓ Branch 45 taken 1 times.
✗ Branch 46 not taken.
✓ Branch 48 taken 1 times.
✗ Branch 49 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 66 taken 1 times.
✗ Branch 67 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✓ Branch 77 taken 1 times.
✗ Branch 78 not taken.
✓ Branch 80 taken 1 times.
✗ Branch 81 not taken.
✓ Branch 83 taken 1 times.
✗ Branch 84 not taken.
✓ Branch 86 taken 1 times.
✗ Branch 87 not taken.
✓ Branch 91 taken 1 times.
✗ Branch 92 not taken.
✓ Branch 95 taken 1 times.
✗ Branch 96 not taken.
✓ Branch 98 taken 1 times.
✗ Branch 99 not taken.
✓ Branch 102 taken 1 times.
✗ Branch 103 not taken.
✓ Branch 105 taken 1 times.
✗ Branch 106 not taken.
✓ Branch 108 taken 1 times.
✗ Branch 109 not taken.
✓ Branch 111 taken 1 times.
✗ Branch 112 not taken.
✓ Branch 116 taken 1 times.
✗ Branch 117 not taken.
4 BOOST_AUTO_TEST_CASE(test_unbiased) {
40
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 EiquadprogFast qp;
41
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 qp.reset(2, 0, 0);
42
43
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::MatrixXd Q(2, 2);
44
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q.setZero();
45
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q(0, 0) = 1.0;
46
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q(1, 1) = 1.0;
47
48
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::VectorXd C(2);
49
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 C.setZero();
50
51
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::MatrixXd Aeq(0, 2);
52
53
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::VectorXd Beq(0);
54
55
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::MatrixXd Aineq(0, 2);
56
57
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::VectorXd Bineq(0);
58
59
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::VectorXd x(2);
60
61
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Eigen::VectorXd solution(2);
62
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 solution.setZero();
63
64 2 double val = 0.0;
65
66 2 EiquadprogFast_status expected = EIQUADPROG_FAST_OPTIMAL;
67
68 EiquadprogFast_status status =
69
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
70
71
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
2 BOOST_CHECK_EQUAL(status, expected);
72
73
6/12
✓ 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 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
2 BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
74
75
7/14
✓ 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 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 times.
2 BOOST_CHECK(x.isApprox(solution));
76 2 }
77
78 // min ||x-x_0||^2, x_0 = (1 1)^T
79
80
33/66
✓ 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.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
✓ Branch 27 taken 1 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 1 times.
✗ Branch 31 not taken.
✓ Branch 33 taken 1 times.
✗ Branch 34 not taken.
✓ Branch 36 taken 1 times.
✗ Branch 37 not taken.
✓ Branch 41 taken 1 times.
✗ Branch 42 not taken.
✓ Branch 45 taken 1 times.
✗ Branch 46 not taken.
✓ Branch 48 taken 1 times.
✗ Branch 49 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 66 taken 1 times.
✗ Branch 67 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✓ Branch 77 taken 1 times.
✗ Branch 78 not taken.
✓ Branch 80 taken 1 times.
✗ Branch 81 not taken.
✓ Branch 83 taken 1 times.
✗ Branch 84 not taken.
✓ Branch 86 taken 1 times.
✗ Branch 87 not taken.
✓ Branch 91 taken 1 times.
✗ Branch 92 not taken.
✓ Branch 95 taken 1 times.
✗ Branch 96 not taken.
✓ Branch 98 taken 1 times.
✗ Branch 99 not taken.
✓ Branch 102 taken 1 times.
✗ Branch 103 not taken.
✓ Branch 105 taken 1 times.
✗ Branch 106 not taken.
✓ Branch 108 taken 1 times.
✗ Branch 109 not taken.
✓ Branch 111 taken 1 times.
✗ Branch 112 not taken.
✓ Branch 116 taken 1 times.
✗ Branch 117 not taken.
4 BOOST_AUTO_TEST_CASE(test_biased) {
81
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtEiquadprog<2, 0, 0> qp;
82
83
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtMatrixX<2, 2>::d Q;
84
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q.setZero();
85
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q(0, 0) = 1.0;
86
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q(1, 1) = 1.0;
87
88
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<2>::d C;
89
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 C(0) = -1.;
90
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 C(1) = -1.;
91
92
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtMatrixX<0, 2>::d Aeq;
93
94
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<0>::d Beq;
95
96
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtMatrixX<0, 2>::d Aineq;
97
98
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<0>::d Bineq;
99
100
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<2>::d x;
101
102
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<2>::d solution;
103
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 solution(0) = 1.;
104
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 solution(1) = 1.;
105
106 2 double val = -1.;
107
108 2 RtEiquadprog_status expected = RT_EIQUADPROG_OPTIMAL;
109
110 RtEiquadprog_status status =
111
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
112
113
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
2 BOOST_CHECK_EQUAL(status, expected);
114
115
6/12
✓ 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 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
2 BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
116
117
7/14
✓ 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 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 times.
2 BOOST_CHECK(x.isApprox(solution));
118 2 }
119
120 // min ||x||^2
121 // s.t.
122 // x[1] = 1 - x[0]
123
124
33/66
✓ 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.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
✓ Branch 27 taken 1 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 1 times.
✗ Branch 31 not taken.
✓ Branch 33 taken 1 times.
✗ Branch 34 not taken.
✓ Branch 36 taken 1 times.
✗ Branch 37 not taken.
✓ Branch 41 taken 1 times.
✗ Branch 42 not taken.
✓ Branch 45 taken 1 times.
✗ Branch 46 not taken.
✓ Branch 48 taken 1 times.
✗ Branch 49 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 66 taken 1 times.
✗ Branch 67 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✓ Branch 77 taken 1 times.
✗ Branch 78 not taken.
✓ Branch 80 taken 1 times.
✗ Branch 81 not taken.
✓ Branch 83 taken 1 times.
✗ Branch 84 not taken.
✓ Branch 86 taken 1 times.
✗ Branch 87 not taken.
✓ Branch 91 taken 1 times.
✗ Branch 92 not taken.
✓ Branch 95 taken 1 times.
✗ Branch 96 not taken.
✓ Branch 98 taken 1 times.
✗ Branch 99 not taken.
✓ Branch 102 taken 1 times.
✗ Branch 103 not taken.
✓ Branch 105 taken 1 times.
✗ Branch 106 not taken.
✓ Branch 108 taken 1 times.
✗ Branch 109 not taken.
✓ Branch 111 taken 1 times.
✗ Branch 112 not taken.
✓ Branch 116 taken 1 times.
✗ Branch 117 not taken.
4 BOOST_AUTO_TEST_CASE(test_equality_constraints) {
125
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtEiquadprog<2, 1, 0> qp;
126
127
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtMatrixX<2, 2>::d Q;
128
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q.setZero();
129
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q(0, 0) = 1.0;
130
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Q(1, 1) = 1.0;
131
132
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<2>::d C;
133
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 C.setZero();
134
135
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtMatrixX<1, 2>::d Aeq;
136
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Aeq(0, 0) = 1.;
137
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Aeq(0, 1) = 1.;
138
139
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<1>::d Beq;
140
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Beq(0) = -1.;
141
142
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtMatrixX<0, 2>::d Aineq;
143
144
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<0>::d Bineq;
145
146
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<2>::d x;
147
148
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RtVectorX<2>::d solution;
149
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 solution(0) = 0.5;
150
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 solution(1) = 0.5;
151
152 2 double val = 0.25;
153
154 2 RtEiquadprog_status expected = RT_EIQUADPROG_OPTIMAL;
155
156 RtEiquadprog_status status =
157
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
158
159
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 times.
2 BOOST_CHECK_EQUAL(status, expected);
160
161
6/12
✓ 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 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 1 times.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 times.
2 BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
162
163
7/14
✓ 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 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 times.
2 BOOST_CHECK(x.isApprox(solution));
164 2 }
165
166 BOOST_AUTO_TEST_SUITE_END()
167