GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/constraints.cpp Lines: 68 68 100.0 %
Date: 2024-02-02 08:47:34 Branches: 404 808 50.0 %

Line Branch Exec Source
1
//
2
// Copyright (c) 2017 CNRS
3
//
4
// This file is part of tsid
5
// tsid is free software: you can redistribute it
6
// and/or modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation, either version
8
// 3 of the License, or (at your option) any later version.
9
// tsid is distributed in the hope that it will be
10
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
// General Lesser Public License for more details. You should have
13
// received a copy of the GNU Lesser General Public License along with
14
// tsid If not, see
15
// <http://www.gnu.org/licenses/>.
16
//
17
18
#include <iostream>
19
20
#include <boost/test/unit_test.hpp>
21
#include <boost/utility/binary.hpp>
22
23
#include <tsid/math/constraint-bound.hpp>
24
#include <tsid/math/constraint-equality.hpp>
25
#include <tsid/math/constraint-inequality.hpp>
26
27
BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
28
29
















4
BOOST_AUTO_TEST_CASE(test_constraint_bounds) {
30
2
  std::cout << "test_constraint_bounds\n";
31
  using namespace tsid::math;
32
  using namespace Eigen;
33
2
  const unsigned int n = 5;
34
35

4
  VectorXd lb = -1.0 * VectorXd::Ones(n);
36

4
  VectorXd ub = VectorXd::Ones(n);
37


6
  ConstraintBound bounds("bounds", lb, ub);
38
39



2
  BOOST_CHECK(bounds.isBound());
40



2
  BOOST_CHECK(!bounds.isEquality());
41



2
  BOOST_CHECK(!bounds.isInequality());
42
43



2
  BOOST_CHECK(bounds.rows() == n);
44



2
  BOOST_CHECK(bounds.cols() == n);
45
46




2
  BOOST_CHECK(lb.isApprox(bounds.lowerBound()));
47




2
  BOOST_CHECK(ub.isApprox(bounds.upperBound()));
48
49
2
  lb *= 2.0;
50




2
  BOOST_CHECK(!lb.isApprox(bounds.lowerBound()));
51

2
  bounds.setLowerBound(lb);
52




2
  BOOST_CHECK(lb.isApprox(bounds.lowerBound()));
53
54
2
  ub *= 2.0;
55




2
  BOOST_CHECK(!ub.isApprox(bounds.upperBound()));
56

2
  bounds.setUpperBound(ub);
57




2
  BOOST_CHECK(ub.isApprox(bounds.upperBound()));
58
2
}
59
60
















4
BOOST_AUTO_TEST_CASE(test_constraint_equality) {
61
  using namespace tsid::math;
62
  using namespace Eigen;
63
  using namespace std;
64
65
2
  const unsigned int n = 5;
66
2
  const unsigned int m = 2;
67
68

4
  MatrixXd A = MatrixXd::Ones(m, n);
69

4
  VectorXd b = VectorXd::Ones(m);
70


6
  ConstraintEquality equality("equality", A, b);
71
72



2
  BOOST_CHECK(!equality.isBound());
73



2
  BOOST_CHECK(equality.isEquality());
74



2
  BOOST_CHECK(!equality.isInequality());
75
76



2
  BOOST_CHECK(equality.rows() == m);
77



2
  BOOST_CHECK(equality.cols() == n);
78
79




2
  BOOST_CHECK(A.isApprox(equality.matrix()));
80




2
  BOOST_CHECK(b.isApprox(equality.vector()));
81
82
2
  b *= 2.0;
83




2
  BOOST_CHECK(!b.isApprox(equality.vector()));
84

2
  equality.setVector(b);
85




2
  BOOST_CHECK(b.isApprox(equality.vector()));
86
87
2
  A *= 2.0;
88




2
  BOOST_CHECK(!A.isApprox(equality.matrix()));
89

2
  equality.setMatrix(A);
90




2
  BOOST_CHECK(A.isApprox(equality.matrix()));
91
2
}
92
93
















4
BOOST_AUTO_TEST_CASE(test_constraint_inequality) {
94
  using namespace tsid::math;
95
  using namespace Eigen;
96
  using namespace std;
97
98
2
  const unsigned int n = 5;
99
2
  const unsigned int m = 2;
100
101

4
  MatrixXd A = MatrixXd::Ones(m, n);
102

4
  VectorXd lb = -1.0 * VectorXd::Ones(m);
103

4
  VectorXd ub = VectorXd::Ones(m);
104


6
  ConstraintInequality inequality("inequality", A, lb, ub);
105
106



2
  BOOST_CHECK(!inequality.isBound());
107



2
  BOOST_CHECK(!inequality.isEquality());
108



2
  BOOST_CHECK(inequality.isInequality());
109
110



2
  BOOST_CHECK(inequality.rows() == m);
111



2
  BOOST_CHECK(inequality.cols() == n);
112
113




2
  BOOST_CHECK(A.isApprox(inequality.matrix()));
114




2
  BOOST_CHECK(lb.isApprox(inequality.lowerBound()));
115




2
  BOOST_CHECK(ub.isApprox(inequality.upperBound()));
116
117
2
  lb *= 2.0;
118




2
  BOOST_CHECK(!lb.isApprox(inequality.lowerBound()));
119

2
  inequality.setLowerBound(lb);
120




2
  BOOST_CHECK(lb.isApprox(inequality.lowerBound()));
121
122
2
  A *= 2.0;
123




2
  BOOST_CHECK(!A.isApprox(inequality.matrix()));
124

2
  inequality.setMatrix(A);
125




2
  BOOST_CHECK(A.isApprox(inequality.matrix()));
126
2
}
127
128
BOOST_AUTO_TEST_SUITE_END()