Drake
testUtil.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <cmath>
5 #include <sstream>
6 #include <stdexcept>
7 #include <string>
8 
9 #include <Eigen/Core>
10 
11 // requires <chrono>, which isn't available in MSVC2010...
12 template <typename TimeT = std::chrono::milliseconds>
13 struct measure {
14  template <typename F, typename... Args>
15  static typename TimeT::rep execution(F func, Args&&... args) {
16  auto start = std::chrono::system_clock::now();
17 
18  // Now call the function with all the parameters you need.
19  func(std::forward<Args>(args)...);
20 
21  auto duration = std::chrono::duration_cast<TimeT>(
22  std::chrono::system_clock::now() - start);
23 
24  return duration.count();
25  }
26 };
27 
28 template <typename Derived>
29 std::string to_string(const Eigen::MatrixBase<Derived>& a) {
30  std::stringstream ss;
31  ss << a;
32  return ss.str();
33 }
34 
35 template <typename T>
36 void valuecheck(const T& a, const T& b, std::string error_msg = "") {
37  if (a != b) {
38  std::ostringstream stream;
39  stream << error_msg;
40  stream << "Expected:\n" << a << "\nbut got:\n" << b << "\n";
41  throw std::runtime_error(stream.str());
42  }
43 }
44 
45 void valuecheck(double a, double b, double tolerance) {
46  if (std::abs(a - b) > tolerance) {
47  std::ostringstream stream;
48  stream << "Expected:\n" << a << "\nbut got:\n" << b << "\n";
49  throw std::runtime_error(stream.str());
50  }
51 }
Definition: testUtil.h:13
FunctionalForm abs(FunctionalForm const &x)
Definition: functional_form.cc:183
void valuecheck(const T &a, const T &b, std::string error_msg="")
Definition: testUtil.h:36
static TimeT::rep execution(F func, Args &&...args)
Definition: testUtil.h:15
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
std::vector< snopt::doublereal > F
Definition: SnoptSolver.cpp:47