Skip to content

Commit

Permalink
polynomial: improve and test print function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Chrétien committed Oct 23, 2014
1 parent bc63a46 commit f912b74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
13 changes: 11 additions & 2 deletions include/roboptim/trajectory/polynomial.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ namespace trajectory
template <int N>
std::ostream& Polynomial<N>::print (std::ostream& o) const
{
o << "f = ";
bool printed_before = false;

for (int idx = N; idx > 0; idx--)
{
if (std::abs (coefs_[idx])
Expand All @@ -110,7 +110,16 @@ namespace trajectory
{
o << coefs_[0];
}
o << "\t" << "( x = t - " << t0_ << " )";

if (printed_before)
{
if (std::abs (t0_)
> std::numeric_limits<value_type>::epsilon ())
o << " (x = t - " << t0_ << ")";
else
o << " (x = t)";
}

return o;
}

Expand Down
33 changes: 32 additions & 1 deletion tests/polynomial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.

#include "shared-tests/common.hh"
#include "shared-tests/fixture.hh"

#include <roboptim/trajectory/polynomial.hh>
#include <roboptim/trajectory/polynomial-3.hh>
Expand Down Expand Up @@ -503,6 +503,35 @@ void test_misc ()
BOOST_CHECK (func (x)[0] == p (x[0]));
}

void test_print ()
{
boost::shared_ptr<boost::test_tools::output_test_stream>
output = retrievePattern ("polynomial");

typedef roboptim::trajectory::Polynomial<3> poly3_t;

typename poly3_t::coefs_t params;
params.setZero ();

poly3_t null (1., params);
(*output) << null << std::endl;

params[0] = 42;
poly3_t constant (2., params);
(*output) << constant << std::endl;

params[1] = 1;
poly3_t linear1 (0., params);
(*output) << linear1 << std::endl;

poly3_t linear2 (3., params);
(*output) << linear2 << std::endl;

std::cout << output->str () << std::endl;

BOOST_CHECK (output->match_pattern ());
}

BOOST_AUTO_TEST_CASE (trajectory_polynomial)
{
srand (static_cast<unsigned int> (time (NULL)));
Expand Down Expand Up @@ -530,6 +559,8 @@ BOOST_AUTO_TEST_CASE (trajectory_polynomial)

test_misc<3> ();
test_misc<5> ();

test_print ();
}

BOOST_AUTO_TEST_SUITE_END ()
4 changes: 4 additions & 0 deletions tests/polynomial.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0
42
1 * x**1 + 42 (x = t)
1 * x**1 + 42 (x = t - 3)

0 comments on commit f912b74

Please sign in to comment.