test_conversion.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016, Ivor Wanders
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the name of the copyright holder nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 #include <socketcan_bridge/topic_to_socketcan.h>
00028 #include <socketcan_bridge/socketcan_to_topic.h>
00029 
00030 #include <can_msgs/Frame.h>
00031 #include <socketcan_interface/socketcan.h>
00032 
00033 // Bring in gtest
00034 #include <gtest/gtest.h>
00035 
00036 // test whether the content of a conversion from a SocketCAN frame
00037 // to a ROS message correctly maintains the data.
00038 TEST(ConversionTest, socketCANToTopicStandard)
00039 {
00040   can::Frame f;
00041   can_msgs::Frame m;
00042   f.id = 127;
00043   f.dlc = 8;
00044   f.is_error = false;
00045   f.is_rtr = false;
00046   f.is_extended = false;
00047   for (uint8_t i = 0; i < f.dlc; ++i)
00048   {
00049     f.data[i] = i;
00050   }
00051   socketcan_bridge::convertSocketCANToMessage(f, m);
00052   EXPECT_EQ(127, m.id);
00053   EXPECT_EQ(8, m.dlc);
00054   EXPECT_EQ(false, m.is_error);
00055   EXPECT_EQ(false, m.is_rtr);
00056   EXPECT_EQ(false, m.is_extended);
00057 
00058   for (uint8_t i=0; i < 8; i++)
00059   {
00060     EXPECT_EQ(i, m.data[i]);
00061   }
00062 }
00063 
00064 // test all three flags seperately.
00065 TEST(ConversionTest, socketCANToTopicFlags)
00066 {
00067   can::Frame f;
00068   can_msgs::Frame m;
00069 
00070   f.is_error = true;
00071   socketcan_bridge::convertSocketCANToMessage(f, m);
00072   EXPECT_EQ(true, m.is_error);
00073   f.is_error = false;
00074 
00075   f.is_rtr = true;
00076   socketcan_bridge::convertSocketCANToMessage(f, m);
00077   EXPECT_EQ(true, m.is_rtr);
00078   f.is_rtr = false;
00079 
00080   f.is_extended = true;
00081   socketcan_bridge::convertSocketCANToMessage(f, m);
00082   EXPECT_EQ(true, m.is_extended);
00083   f.is_extended = false;
00084 }
00085 
00086 // idem, but the other way around.
00087 TEST(ConversionTest, topicToSocketCANStandard)
00088 {
00089   can::Frame f;
00090   can_msgs::Frame m;
00091   m.id = 127;
00092   m.dlc = 8;
00093   m.is_error = false;
00094   m.is_rtr = false;
00095   m.is_extended = false;
00096   for (uint8_t i = 0; i < m.dlc; ++i)
00097   {
00098     m.data[i] = i;
00099   }
00100   socketcan_bridge::convertMessageToSocketCAN(m, f);
00101   EXPECT_EQ(127, f.id);
00102   EXPECT_EQ(8, f.dlc);
00103   EXPECT_EQ(false, f.is_error);
00104   EXPECT_EQ(false, f.is_rtr);
00105   EXPECT_EQ(false, f.is_extended);
00106 
00107 
00108   for (uint8_t i=0; i < 8; i++)
00109   {
00110     EXPECT_EQ(i, f.data[i]);
00111   }
00112 }
00113 
00114 TEST(ConversionTest, topicToSocketCANFlags)
00115 {
00116   can::Frame f;
00117   can_msgs::Frame m;
00118 
00119   m.is_error = true;
00120   socketcan_bridge::convertMessageToSocketCAN(m, f);
00121   EXPECT_EQ(true, f.is_error);
00122   m.is_error = false;
00123 
00124   m.is_rtr = true;
00125   socketcan_bridge::convertMessageToSocketCAN(m, f);
00126   EXPECT_EQ(true, f.is_rtr);
00127   m.is_rtr = false;
00128 
00129   m.is_extended = true;
00130   socketcan_bridge::convertMessageToSocketCAN(m, f);
00131   EXPECT_EQ(true, f.is_extended);
00132   m.is_extended = false;
00133 }
00134 
00135 // Run all the tests that were declared with TEST()
00136 int main(int argc, char **argv)
00137 {
00138   testing::InitGoogleTest(&argc, argv);
00139   return RUN_ALL_TESTS();
00140 }


socketcan_bridge
Author(s): Ivor Wanders
autogenerated on Sun Sep 3 2017 03:11:00