test_conversion.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Ivor Wanders
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the copyright holder nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
29 
30 #include <can_msgs/Frame.h>
32 
33 // Bring in gtest
34 #include <gtest/gtest.h>
35 
36 // test whether the content of a conversion from a SocketCAN frame
37 // to a ROS message correctly maintains the data.
38 TEST(ConversionTest, socketCANToTopicStandard)
39 {
40  can::Frame f;
41  can_msgs::Frame m;
42  f.id = 127;
43  f.dlc = 8;
44  f.is_error = false;
45  f.is_rtr = false;
46  f.is_extended = false;
47  for (uint8_t i = 0; i < f.dlc; ++i)
48  {
49  f.data[i] = i;
50  }
52  EXPECT_EQ(127, m.id);
53  EXPECT_EQ(8, m.dlc);
54  EXPECT_EQ(false, m.is_error);
55  EXPECT_EQ(false, m.is_rtr);
56  EXPECT_EQ(false, m.is_extended);
57 
58  for (uint8_t i=0; i < 8; i++)
59  {
60  EXPECT_EQ(i, m.data[i]);
61  }
62 }
63 
64 // test all three flags seperately.
65 TEST(ConversionTest, socketCANToTopicFlags)
66 {
67  can::Frame f;
68  can_msgs::Frame m;
69 
70  f.is_error = true;
72  EXPECT_EQ(true, m.is_error);
73  f.is_error = false;
74 
75  f.is_rtr = true;
77  EXPECT_EQ(true, m.is_rtr);
78  f.is_rtr = false;
79 
80  f.is_extended = true;
82  EXPECT_EQ(true, m.is_extended);
83  f.is_extended = false;
84 }
85 
86 // idem, but the other way around.
87 TEST(ConversionTest, topicToSocketCANStandard)
88 {
89  can::Frame f;
90  can_msgs::Frame m;
91  m.id = 127;
92  m.dlc = 8;
93  m.is_error = false;
94  m.is_rtr = false;
95  m.is_extended = false;
96  for (uint8_t i = 0; i < m.dlc; ++i)
97  {
98  m.data[i] = i;
99  }
101  EXPECT_EQ(127, f.id);
102  EXPECT_EQ(8, f.dlc);
103  EXPECT_EQ(false, f.is_error);
104  EXPECT_EQ(false, f.is_rtr);
105  EXPECT_EQ(false, f.is_extended);
106 
107 
108  for (uint8_t i=0; i < 8; i++)
109  {
110  EXPECT_EQ(i, f.data[i]);
111  }
112 }
113 
114 TEST(ConversionTest, topicToSocketCANFlags)
115 {
116  can::Frame f;
117  can_msgs::Frame m;
118 
119  m.is_error = true;
121  EXPECT_EQ(true, f.is_error);
122  m.is_error = false;
123 
124  m.is_rtr = true;
126  EXPECT_EQ(true, f.is_rtr);
127  m.is_rtr = false;
128 
129  m.is_extended = true;
131  EXPECT_EQ(true, f.is_extended);
132  m.is_extended = false;
133 }
134 
135 // Run all the tests that were declared with TEST()
136 int main(int argc, char **argv)
137 {
138  testing::InitGoogleTest(&argc, argv);
139  return RUN_ALL_TESTS();
140 }
unsigned int is_error
f
TEST(ConversionTest, socketCANToTopicStandard)
int main(int argc, char **argv)
boost::array< value_type, 8 > data
void convertMessageToSocketCAN(const can_msgs::Frame &m, can::Frame &f)
unsigned int is_extended
void convertSocketCANToMessage(const can::Frame &f, can_msgs::Frame &m)
unsigned char dlc
unsigned int id
unsigned int is_rtr


socketcan_bridge
Author(s): Ivor Wanders
autogenerated on Fri May 14 2021 02:59:47