Cheetah Software  1.0
test_utilities.cpp
Go to the documentation of this file.
1 
7 #include "Math/MathUtilities.h"
8 #include "Utilities/utilities.h"
9 #include "cppTypes.h"
10 
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 #include <string>
15 #include <unordered_map>
17 
18 TEST(Utilities, strintToVec3) {
19  std::vector<std::string> strings = {"[1.1, 2.2, 3.3]",
20  " [ 1.1 , 2.2 ,3.3 ] ",
21  "[1.1,2.2 ,3.3]", "[1.1, 2.2, 3.3]"};
22 
23  Vec3<double> ref(1.1, 2.2, 3.3);
24  Vec3<double> v1 = stringToVec3<double>("[1.1,2.2,3.3]");
25 
26  EXPECT_TRUE(almostEqual(ref, v1, .0000001));
27 
28  for (auto& str : strings) {
29  Vec3<double> vv = stringToVec3<double>(str);
30  EXPECT_TRUE(almostEqual(ref, vv, .0000001));
31  }
32 }
33 
37 TEST(Utilities, coerce) {
38  EXPECT_EQ(0.1f, coerce<float>(0.5f, 0.f, 0.1f));
39  EXPECT_EQ(-0.1f, coerce<float>(-0.5f, -0.1f, 0.1f));
40  EXPECT_EQ(0.5f, coerce<float>(0.5f, -1.f, 1.f));
41 }
42 
43 TEST(Utilities, sgn) {
44  EXPECT_EQ(1, sgn<int>(10));
45  EXPECT_EQ(-1, sgn<int>(-10));
46  EXPECT_EQ(0, sgn<int>(0));
47 
48  EXPECT_EQ(1, sgn<double>(13.23));
49  EXPECT_EQ(-1, sgn<double>(-.23));
50  EXPECT_EQ(0, sgn<double>(0.));
51 }
52 
53 TEST(Utilities, uMapContains) {
54  std::unordered_map<std::string, int> s;
55  EXPECT_FALSE(uMapContains(s, std::string("test")));
56  s["test"] = 2;
57  EXPECT_TRUE(uMapContains(s, std::string("test")));
58 }
59 
60 TEST(Utilities, mapContains) {
61  std::map<std::string, int> s;
62  EXPECT_FALSE(mapContains(s, std::string("test")));
63  s["test"] = 2;
64  EXPECT_TRUE(mapContains(s, std::string("test")));
65 }
66 
67 TEST(Utilities, mapToRange) {
68  EXPECT_TRUE(fpEqual(2.0, mapToRange(0.4, 0.2, 1.2, 0.0, 10.0), .00001));
69 }
70 
71 TEST(Utilities, colorPrint) {
72  printf_color(PrintColor::Default, "default\n");
73  printf_color(PrintColor::Red, "red\n");
74  printf_color(PrintColor::Green, "green\n");
75  printf_color(PrintColor::Blue, "blue\n");
76  printf_color(PrintColor::Yellow, "yellow\n");
77  printf_color(PrintColor::Magenta, "magenta\n");
78  printf_color(PrintColor::Cyan, "cyan\n");
79  printf_color(PrintColor::Default, "default!\n");
80 }
bool fpEqual(T a, T b, T tol)
Definition: utilities.h:15
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
TEST(Utilities, strintToVec3)
T coerce(T in, T min, T max)
Definition: utilities.h:35
int sgn(T val)
Definition: utilities.h:65
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Utility functions for math.
void printf_color(PrintColor color, const char *fmt,...)
bool uMapContains(const std::unordered_map< T1, T2 > &set, T1 key)
Definition: utilities.h:112
bool mapContains(const std::map< T1, T2 > &set, T1 key)
Definition: utilities.h:120
T mapToRange(T x, T inputMin, T inputMax, T outputMin, T outputMax)
Definition: utilities.h:142
MX f(const MX &x, const MX &u)