float64_test.cpp
Go to the documentation of this file.
1 #include "ros/msg.h"
2 #include <gtest/gtest.h>
3 #include <math.h>
4 
5 class TestFloat64 : public ::testing::Test
6 {
7 public:
8  union
9  {
10  double val;
11  unsigned char buffer[8];
12  };
13 
14  static const double cases[];
15  static const int num_cases;
16 };
17 
18 const double TestFloat64::cases[] =
19 {
20  0.0, 10.0, 15642.1, -50.2, 0.0001, -0.321,
21  123456.789, -987.654321, 3.4e38, -3.4e38,
22  0.0, -0.0, 0.1, -0.1,
23  M_PI, -M_PI, 123456.789, -123456.789,
24  INFINITY, -INFINITY, NAN, INFINITY - INFINITY,
25  1e38, -1e38, 1e39, -1e39,
26  1e-38, -1e-38, 1e-39, -1e-39,
27  3.14159e-37,-3.14159e-37, 3.14159e-43, -3.14159e-43,
28  1e-60, -1e-60, 1e-45, -1e-45,
29  0.99999999999999, -0.99999999999999, 127.999999999999, -127.999999999999
30 };
31 const int TestFloat64::num_cases = sizeof(TestFloat64::cases) / sizeof(double);
32 
33 
34 TEST_F(TestFloat64, testRoundTrip)
35 {
36  for (int i = 0; i < num_cases; i++)
37  {
38  memset(buffer, 0, sizeof(buffer));
40  float deserialized = 0;
42 
43  if (isnan(cases[i]))
44  {
45  // EXPECT_FLOAT_EQ will fail on nan, because nan != nan
46  EXPECT_EQ(isnan(val), true);
47  EXPECT_EQ(isnan(deserialized), true);
48  }
49  else
50  {
51  // Compare against C++ cast results.
52  // In some of the test cases, truncation and loss of precision is expected
53  // but it should be the same as what C++ compiler implements for (float).
54  EXPECT_FLOAT_EQ(static_cast<float>(cases[i]), static_cast<float>(val));
55  EXPECT_FLOAT_EQ(static_cast<float>(cases[i]), static_cast<float>(deserialized));
56  }
57  }
58 }
59 
60 
61 int main(int argc, char **argv)
62 {
63  testing::InitGoogleTest(&argc, argv);
64  return RUN_ALL_TESTS();
65 }
TestFloat64
Definition: float64_test.cpp:5
ros::Msg::deserializeAvrFloat64
static int deserializeAvrFloat64(const unsigned char *inbuffer, float *f)
This tricky function handles demoting a 64bit double to a 32bit float, so that AVR can understand mes...
Definition: msg.h:126
TEST_F
TEST_F(TestFloat64, testRoundTrip)
Definition: float64_test.cpp:34
TestFloat64::cases
static const double cases[]
Definition: float64_test.cpp:14
msg.h
TestFloat64::num_cases
static const int num_cases
Definition: float64_test.cpp:15
main
int main(int argc, char **argv)
Definition: float64_test.cpp:61
TestFloat64::buffer
unsigned char buffer[8]
Definition: float64_test.cpp:11
TestFloat64::val
double val
Definition: float64_test.cpp:10
buffer
unsigned char buffer[1]
Definition: subscriber_test.cpp:6
ros::Msg::serializeAvrFloat64
static int serializeAvrFloat64(unsigned char *outbuffer, const float f)
This tricky function handles promoting a 32bit float to a 64bit double, so that AVR can publish messa...
Definition: msg.h:65


rosserial_client
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Wed Mar 2 2022 00:58:01