DataElementFactory_Test.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <gtest/gtest.h>
00003 #include <boost/shared_ptr.hpp>
00004 #include <boost/make_shared.hpp>
00005 #include "robot_instance/DataElementFactory.h"
00006 #include "shared_memory_transport/smtclient.hpp"
00007 #include "shared_memory_transport/smtcore_daemon.hpp"
00008 
00009 using namespace std;
00010 
00011 class DataElementFactoryTest : public ::testing::Test
00012 {
00013 protected:
00014     virtual void SetUp()
00015     {
00016         f = boost::make_shared< DataElementFactory<RobonetDataElement> >();
00017     }
00018 
00019     virtual void TearDown()
00020     {
00021     }
00022 
00023     boost::shared_ptr< DataElementFactory<RobonetDataElement> > f;
00024     string strXML;
00025     pair<std::string, RobonetDataElementPtr> outPair;
00026 };
00027 
00028 TEST_F(DataElementFactoryTest, fromXml)
00029 {
00030     bool isCorrectType = false;
00031     outPair = f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>");
00032     EXPECT_EQ("r2/left_leg/joint0/joint/Torq", outPair.first);
00033     EXPECT_EQ(DataElement::Write, outPair.second->getReadWrite());
00034 
00035     if (typeid(float) == *outPair.second->getType())
00036     {
00037         isCorrectType = true;
00038     }
00039 
00040     EXPECT_TRUE(isCorrectType);
00041     EXPECT_EQ(DataElement::ThirtyTwoBit, outPair.second->getSize());
00042     EXPECT_EQ("left_leg", outPair.second->getChannel());
00043     EXPECT_EQ("joint0", outPair.second->getNode());
00044     EXPECT_EQ("TorqL", outPair.second->getOffsetPair().first);
00045     EXPECT_EQ("TorqH", outPair.second->getOffsetPair().second);
00046     EXPECT_EQ("Command", outPair.second->getGroup());
00047 
00048     isCorrectType = false;
00049     outPair = f->fromXml("r2/left_leg/joint1/joint", "/left_leg/joint1", "<Element id=\"APS1\" type=\"uint32_t\" offset1=\"APS1L\" offset2=\"APS1H\" direction=\"Read\" group=\"Command\" style=\"Normal\" />");
00050     EXPECT_EQ("r2/left_leg/joint1/joint/APS1", outPair.first);
00051     EXPECT_EQ(DataElement::Read, outPair.second->getReadWrite());
00052 
00053     if (typeid(uint32_t) == *outPair.second->getType())
00054     {
00055         isCorrectType = true;
00056     }
00057 
00058     EXPECT_TRUE(isCorrectType);
00059     EXPECT_EQ(DataElement::ThirtyTwoBit, outPair.second->getSize());
00060     EXPECT_EQ("left_leg", outPair.second->getChannel());
00061     EXPECT_EQ("joint1", outPair.second->getNode());
00062     EXPECT_EQ("APS1L", outPair.second->getOffsetPair().first);
00063     EXPECT_EQ("APS1H", outPair.second->getOffsetPair().second);
00064     EXPECT_EQ("Command", outPair.second->getGroup());
00065 
00066     isCorrectType = false;
00067     outPair = f->fromXml("r2/left_leg/joint2/joint", "/left_leg/joint2", "<Element id=\"StatReg1\" type=\"uint16_t\" offset1=\"StatReg1\" direction=\"Read\" group=\"Monitor\" style=\"Normal\" />");
00068     EXPECT_EQ("r2/left_leg/joint2/joint/StatReg1", outPair.first);
00069     EXPECT_EQ(DataElement::Read, outPair.second->getReadWrite());
00070 
00071     if (typeid(uint16_t) == *outPair.second->getType())
00072     {
00073         isCorrectType = true;
00074     }
00075 
00076     EXPECT_TRUE(isCorrectType);
00077     EXPECT_EQ(DataElement::SixteenBit, outPair.second->getSize());
00078     EXPECT_EQ("left_leg", outPair.second->getChannel());
00079     EXPECT_EQ("joint2", outPair.second->getNode());
00080     EXPECT_EQ("StatReg1", outPair.second->getOffsetPair().first);
00081     EXPECT_EQ("", outPair.second->getOffsetPair().second);
00082     EXPECT_EQ("Monitor", outPair.second->getGroup());
00083 
00084     isCorrectType = false;
00085     outPair = f->fromXml("r2/right_leg/joint0/joint", "/right_leg/joint0", "<Element id=\"MotCom\" type=\"int16_t\" offset1=\"MotCom\" direction=\"Write\" group=\"Command\" style=\"MotCom\" />");
00086     EXPECT_EQ("r2/right_leg/joint0/joint/MotCom", outPair.first);
00087     EXPECT_EQ(DataElement::Write, outPair.second->getReadWrite());
00088 
00089     if (typeid(int16_t) == *outPair.second->getType())
00090     {
00091         isCorrectType = true;
00092     }
00093 
00094     EXPECT_TRUE(isCorrectType);
00095     EXPECT_EQ(DataElement::SixteenBit, outPair.second->getSize());
00096     EXPECT_EQ("right_leg", outPair.second->getChannel());
00097     EXPECT_EQ("joint0", outPair.second->getNode());
00098     EXPECT_EQ("MotCom", outPair.second->getOffsetPair().first);
00099     EXPECT_EQ("", outPair.second->getOffsetPair().second);
00100     EXPECT_EQ("Command", outPair.second->getGroup());
00101 
00102     isCorrectType = false;
00103     outPair = f->fromXml("r2/right_leg/joint1/joint", "/right_leg/joint1", "<Element id=\"Temp\" type=\"float\" offset1=\"Temp1\" offset2=\"Temp2\" direction=\"Read\" group=\"Monitor\" style=\"Temperature\" />");
00104     EXPECT_EQ("r2/right_leg/joint1/joint/Temp", outPair.first);
00105     EXPECT_EQ(DataElement::Read, outPair.second->getReadWrite());
00106 
00107     if (typeid(float) == *outPair.second->getType())
00108     {
00109         isCorrectType = true;
00110     }
00111 
00112     EXPECT_TRUE(isCorrectType);
00113     EXPECT_EQ(DataElement::ThirtyTwoBit, outPair.second->getSize());
00114     EXPECT_EQ("right_leg", outPair.second->getChannel());
00115     EXPECT_EQ("joint1", outPair.second->getNode());
00116     EXPECT_EQ("Temp1", outPair.second->getOffsetPair().first);
00117     EXPECT_EQ("Temp2", outPair.second->getOffsetPair().second);
00118     EXPECT_EQ("Monitor", outPair.second->getGroup());
00119 
00120     isCorrectType = false;
00121     outPair = f->fromXml("r2/right_leg/joint2/joint", "/right_leg/joint2", "<Element id=\"BrakePwm\" type=\"uint16_t\" offset1=\"BrakePwm\" direction=\"Read\" group=\"Command\" style=\"BrakePwm\" />");
00122     EXPECT_EQ("r2/right_leg/joint2/joint/BrakePwm", outPair.first);
00123     EXPECT_EQ(DataElement::Read, outPair.second->getReadWrite());
00124 
00125     if (typeid(uint16_t) == *outPair.second->getType())
00126     {
00127         isCorrectType = true;
00128     }
00129 
00130     EXPECT_TRUE(isCorrectType);
00131     EXPECT_EQ(DataElement::SixteenBit, outPair.second->getSize());
00132     EXPECT_EQ("right_leg", outPair.second->getChannel());
00133     EXPECT_EQ("joint2", outPair.second->getNode());
00134     EXPECT_EQ("BrakePwm", outPair.second->getOffsetPair().first);
00135     EXPECT_EQ("", outPair.second->getOffsetPair().second);
00136     EXPECT_EQ("Command", outPair.second->getGroup());
00137 }
00138 
00139 TEST_F(DataElementFactoryTest, missingNameOrNode)
00140 {
00142     EXPECT_THROW(f->fromXml("", "", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), runtime_error);
00143 
00145     EXPECT_THROW(f->fromXml("", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), runtime_error);
00146 
00148     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), runtime_error);
00149 }
00150 
00151 TEST_F(DataElementFactoryTest, fromXmlBad)
00152 {
00154     EXPECT_THROW(f->fromXml("", "", ""), MissingXMLElementException);
00155 
00157     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Blah />"), MissingXMLElementException);
00158 
00160     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), MissingXMLElementException);
00161 
00163     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), MissingXMLElementException);
00164 
00166     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), MissingXMLElementException);
00167 
00169     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" group=\"Command\" style=\"Normal\"/>"), MissingXMLElementException);
00170 
00172     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" style=\"Normal\"/>"), MissingXMLElementException);
00173 
00175     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" />"), MissingXMLElementException);
00176 
00178     EXPECT_THROW(f->fromXml("r2/left_leg/joint0/joint", "", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"), runtime_error);
00179 }
00180 
00181 class DataElementFactoryTestSMT : public ::testing::Test
00182 {
00183 protected:
00184     static void SetUpTestCase() {
00185         // start an smtcore with a random port so we don't collide with any service.
00186         smtcore.startSmtCore(true);
00187     }
00188     static void TearDownTestCase() {
00189         // Shut down the SMTcore instance we started.
00190         smtcore.shutdownSmtCore();
00191     }
00192 
00193     virtual void SetUp(){
00194         // Create an smtclient with the created smtcore port
00195         smtclient = std::make_shared<SMT::SMTClient>("http://localhost:" + std::to_string(smtcore.getPort()) + "/");
00196     }
00197 
00198     virtual void TearDown(){
00199         // Clean up after ourselves
00200         smtclient->deleteShmFile();
00201         // Cleanup smtclient
00202         smtclient.reset();
00203         smtclient = nullptr;
00204     }
00205 
00206     map<std::string, RobonetDataElementPtr> outmap;
00207     std::shared_ptr<SMT::SMTClient> smtclient;
00208 
00209     static SMTCoreDaemon smtcore;
00210 };
00211 // Because we actually need something for the static member to link to.
00212 SMTCoreDaemon DataElementFactoryTestSMT::smtcore;
00213 
00214 TEST_F(DataElementFactoryTestSMT, fromXmlWithoutClient){
00215     // No smtclient
00216     boost::shared_ptr< DataElementFactory<RobonetDataElement> > f = boost::make_shared< DataElementFactory<RobonetDataElement> >();
00217 
00218     // Create a few pairs from XML
00219     outmap.insert(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"));
00220     outmap.insert(f->fromXml("r2/left_leg/joint1/joint", "/left_leg/joint1", "<Element id=\"APS1\" type=\"uint32_t\" offset1=\"APS1L\" offset2=\"APS1H\" direction=\"Read\" group=\"Command\" style=\"Normal\" />"));
00221     outmap.insert(f->fromXml("r2/left_leg/joint2/joint", "/left_leg/joint2", "<Element id=\"StatReg1\" type=\"uint16_t\" offset1=\"StatReg1\" direction=\"Read\" group=\"Monitor\" style=\"Normal\" />"));
00222     outmap.insert(f->fromXml("r2/right_leg/joint0/joint", "/right_leg/joint0", "<Element id=\"MotCom\" type=\"int16_t\" offset1=\"MotCom\" direction=\"Write\" group=\"Command\" style=\"MotCom\" />"));
00223     outmap.insert(f->fromXml("r2/right_leg/joint1/joint", "/right_leg/joint1", "<Element id=\"Temp\" type=\"float\" offset1=\"Temp1\" offset2=\"Temp2\" direction=\"Read\" group=\"Monitor\" style=\"Temperature\" />"));
00224     outmap.insert(f->fromXml("r2/right_leg/joint2/joint", "/right_leg/joint2", "<Element id=\"BrakePwm\" type=\"uint16_t\" offset1=\"BrakePwm\" direction=\"Read\" group=\"Command\" style=\"BrakePwm\" />"));
00225 
00226     // Querying SMT should yield empty results.
00227     EXPECT_TRUE(smtclient->listTopics().empty());
00228 }
00229 
00230 TEST_F(DataElementFactoryTestSMT, fromXmlWithClient){
00231     vector<string> smtTopics;
00232     // using smtclient
00233     boost::shared_ptr< DataElementFactory<RobonetDataElement> > f = boost::make_shared< DataElementFactory<RobonetDataElement> >(smtclient);
00234 
00235     // Create a few pairs from XML
00236     outmap.insert(f->fromXml("r2/left_leg/joint0/joint", "/left_leg/joint0", "<Element id=\"Torq\" type=\"float\" offset1=\"TorqL\" offset2=\"TorqH\" direction=\"Write\" group=\"Command\" style=\"Normal\"/>"));
00237     outmap.insert(f->fromXml("r2/left_leg/joint1/joint", "/left_leg/joint1", "<Element id=\"APS1\" type=\"uint32_t\" offset1=\"APS1L\" offset2=\"APS1H\" direction=\"Read\" group=\"Command\" style=\"Normal\" />"));
00238     outmap.insert(f->fromXml("r2/left_leg/joint2/joint", "/left_leg/joint2", "<Element id=\"StatReg1\" type=\"uint16_t\" offset1=\"StatReg1\" direction=\"Read\" group=\"Monitor\" style=\"Normal\" />"));
00239     outmap.insert(f->fromXml("r2/right_leg/joint0/joint", "/right_leg/joint0", "<Element id=\"MotCom\" type=\"int16_t\" offset1=\"MotCom\" direction=\"Write\" group=\"Command\" style=\"MotCom\" />"));
00240     outmap.insert(f->fromXml("r2/right_leg/joint1/joint", "/right_leg/joint1", "<Element id=\"Temp\" type=\"float\" offset1=\"Temp1\" offset2=\"Temp2\" direction=\"Read\" group=\"Monitor\" style=\"Temperature\" />"));
00241     outmap.insert(f->fromXml("r2/right_leg/joint2/joint", "/right_leg/joint2", "<Element id=\"BrakePwm\" type=\"uint16_t\" offset1=\"BrakePwm\" direction=\"Read\" group=\"Command\" style=\"BrakePwm\" />"));
00242 
00243     // Querying SMT should yield a list of these topics
00244     smtTopics = smtclient->listTopics();
00245     ASSERT_FALSE(smtclient->listTopics().empty());  // The list shouldn't be empty.
00246     ASSERT_EQ(smtTopics.size(), outmap.size());     // The two lists should be the same size.
00247 
00248     // All the SMT topics should be in the map
00249     for(int i = 0; i < smtTopics.size(); i++){
00250         EXPECT_EQ(1, outmap.count(smtTopics[i]));
00251     }
00252 }
00253 
00254 int main(int argc, char** argv)
00255 {
00256     testing::InitGoogleTest(&argc, argv);
00257     return RUN_ALL_TESTS();
00258 }


robot_instance
Author(s):
autogenerated on Sat Jun 8 2019 20:43:12