utest.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2013 Southwest Research Institute
00003  *
00004  *  Licensed under the Apache License, Version 2.0 (the "License");
00005  *  you may not use this file except in compliance with the License.
00006  *  You may obtain a copy of the License at
00007  *
00008  *    http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  *  Unless required by applicable law or agreed to in writing, software
00011  *  distributed under the License is distributed on an "AS IS" BASIS,
00012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *  See the License for the specific language governing permissions and
00014  *  limitations under the License.
00015  */
00016 
00017 #include "mtconnect_task_parser/task_parser.h"
00018 
00019 #include "boost/make_shared.hpp"
00020 
00021 #include <gtest/gtest.h>
00022 
00023 TEST(TaskParser, util)
00024 {
00025   //TODO Should do some testing of the internal task parsing algorithms
00026 
00027 }
00028 TEST(MotionGroup, from_xml)
00029 {
00030   using namespace std;
00031 
00032   const string xml_string =
00033       "<motion_group name=\"my_name\" joint_names=\"joint_1 joint_2 \
00034       joint_3 joint_4 joint_5 joint_6\"/>";
00035 
00036   TiXmlDocument xml_doc;
00037   xml_doc.Parse(xml_string.c_str());
00038 
00039   TiXmlElement *xml_mg = xml_doc.FirstChildElement("motion_group");
00040   ASSERT_TRUE(xml_mg);
00041 
00042   mtconnect::MotionGroup mg;
00043   ASSERT_TRUE(mtconnect::fromXml(mg, xml_mg));
00044 
00045   // Check that group name correctly id'ed
00046   EXPECT_EQ("my_name", mg.name_);
00047 
00048   // The order of joint names is important, so we test order and equivalence
00049   EXPECT_EQ("joint_1", mg.joint_names_[0]);
00050   EXPECT_EQ("joint_2", mg.joint_names_[1]);
00051   EXPECT_EQ("joint_3", mg.joint_names_[2]);
00052   EXPECT_EQ("joint_4", mg.joint_names_[3]);
00053   EXPECT_EQ("joint_5", mg.joint_names_[4]);
00054   EXPECT_EQ("joint_6", mg.joint_names_[5]);
00055 
00056 }
00057 
00058 TEST(JointPoint, from_xml)
00059 {
00060   using namespace std;
00061   using namespace mtconnect;
00062 
00063   const string xml_string = "<joint_point joint_values=\"0 10 0 5 0 6\" group_name=\"group_1\"/>";
00064 
00065   boost::shared_ptr<MotionGroup> group_ptr = boost::make_shared<MotionGroup>();
00066   group_ptr->joint_names_.push_back("joint_1");
00067   group_ptr->joint_names_.push_back("joint_2");
00068   group_ptr->joint_names_.push_back("joint_3");
00069   group_ptr->joint_names_.push_back("joint_4");
00070   group_ptr->joint_names_.push_back("joint_5");
00071   group_ptr->joint_names_.push_back("joint_6");
00072 
00073   group_ptr->name_ = "group_1";
00074   map<string, boost::shared_ptr<MotionGroup> > group_map;
00075   group_map[group_ptr->name_] = group_ptr;
00076 
00077   TiXmlDocument xml_doc;
00078   xml_doc.Parse(xml_string.c_str());
00079 
00080   TiXmlElement *xml_jp = xml_doc.FirstChildElement("joint_point");
00081   ASSERT_TRUE(xml_jp);
00082 
00083   JointPoint jp;
00084 
00085   ASSERT_TRUE(fromXml(jp, xml_jp, group_map));
00086 
00087   //Parsing XML should fail with an empty group map
00088   group_map.clear();
00089   ASSERT_FALSE(fromXml(jp, xml_jp, group_map));
00090 }
00091 
00092 TEST(JointMove, from_xml)
00093 {
00094   using namespace std;
00095   using namespace mtconnect;
00096 
00097   const string xml_string = "<joint_move> <joint_point joint_values=\"1 2 3\" group_name=\"group_1\"/></joint_move>";
00098 
00099   boost::shared_ptr<MotionGroup> group_ptr = boost::make_shared<MotionGroup>();
00100   group_ptr->joint_names_.push_back("joint_1");
00101   group_ptr->joint_names_.push_back("joint_2");
00102   group_ptr->joint_names_.push_back("joint_3");
00103 
00104   group_ptr->name_ = "group_1";
00105   map<string, boost::shared_ptr<MotionGroup> > group_map;
00106   group_map[group_ptr->name_] = group_ptr;
00107 
00108   TiXmlDocument xml_doc;
00109   xml_doc.Parse(xml_string.c_str());
00110 
00111   TiXmlElement *xml_jm = xml_doc.FirstChildElement("joint_move");
00112   ASSERT_TRUE(xml_jm);
00113 
00114   JointMove jm;
00115 
00116   ASSERT_TRUE(fromXml(jm, xml_jm, group_map));
00117 
00118 }
00119 
00120 TEST(Path, from_xml)
00121 {
00122   using namespace std;
00123   using namespace mtconnect;
00124 
00125   const string xml_string = "<path name=\"path_1\">"
00126       "<joint_move>"
00127       "<joint_point joint_values=\"1 2 3\" group_name=\"group_1\"/>"
00128       "</joint_move>"
00129       "<joint_move>"
00130       "<joint_point joint_values=\"4 5 6\" group_name=\"group_1\"/>"
00131       "</joint_move>"
00132       "</path>";
00133 
00134   boost::shared_ptr<MotionGroup> group_ptr = boost::make_shared<MotionGroup>();
00135   group_ptr->joint_names_.push_back("joint_1");
00136   group_ptr->joint_names_.push_back("joint_2");
00137   group_ptr->joint_names_.push_back("joint_3");
00138 
00139   group_ptr->name_ = "group_1";
00140   map<string, boost::shared_ptr<MotionGroup> > group_map;
00141   group_map[group_ptr->name_] = group_ptr;
00142 
00143   TiXmlDocument xml_doc;
00144   xml_doc.Parse(xml_string.c_str());
00145 
00146   TiXmlElement *xml_p = xml_doc.FirstChildElement("path");
00147   ASSERT_TRUE(xml_p);
00148 
00149   Path p;
00150   ASSERT_TRUE(fromXml(p, xml_p, group_map));
00151 
00152   //Testing that data got initialized as expected
00153   ASSERT_EQ(2, p.moves_.size());
00154   ASSERT_EQ(3, p.moves_.front().point_->values_.size());
00155   //Confirm values for first point
00156   EXPECT_DOUBLE_EQ(1.0, p.moves_.front().point_->values_[0]);
00157   EXPECT_DOUBLE_EQ(2.0, p.moves_.front().point_->values_[1]);
00158   EXPECT_DOUBLE_EQ(3.0, p.moves_.front().point_->values_[2]);
00159 
00160   //Confirm values for last point
00161   EXPECT_DOUBLE_EQ(4.0, p.moves_.back().point_->values_[0]);
00162   EXPECT_DOUBLE_EQ(5.0, p.moves_.back().point_->values_[1]);
00163   EXPECT_DOUBLE_EQ(6.0, p.moves_.back().point_->values_[2]);
00164 
00165 }
00166 
00167 TEST(Task, from_xml)
00168 {
00169   using namespace std;
00170   using namespace mtconnect;
00171 
00172   const string xml_string = "<task>"
00173       "<joint_point name=home joint_values=\"1 2 3\" group_name=\"group_1\"/>"
00174       "<joint_point name=safe joint_values=\"4 5 6\" group_name=\"group_2\"/>"
00175       "<motion_group name=\"group_1\" joint_names=\"joint_1 joint_2 joint_3\"/>"
00176       "<motion_group name=\"group_2\" joint_names=\"joint_4 joint_5 joint_6\"/>"
00177       "<path name=\"path_1\">"
00178       "<joint_move>"
00179       "<joint_point joint_values=\"1 2 3\" group_name=\"group_1\"/>"
00180       "</joint_move>"
00181       "<joint_move>"
00182       "<joint_point joint_values=\"4 5 6\" group_name=\"group_2\"/>"
00183       "</joint_move>"
00184       "</path>"
00185       "<path name=\"path_2\">"
00186       "<joint_move>"
00187       "<joint_point joint_values=\"1 2 3\" group_name=\"group_2\"/>"
00188       "</joint_move>"
00189       "<joint_move>"
00190       "<joint_point joint_values=\"4 5 6\" group_name=\"group_1\"/>"
00191       "</joint_move>"
00192       "</path>"
00193       "</task>";
00194 
00195   Task task;
00196   TiXmlDocument xml_doc;
00197   xml_doc.Parse(xml_string.c_str());
00198   TiXmlElement *xml_t = xml_doc.FirstChildElement("task");
00199 
00200   ASSERT_TRUE(fromXml(task, xml_t));
00201 
00202   ASSERT_EQ(2, task.points_.size());
00203   ASSERT_EQ(2, task.motion_groups_.size());
00204   ASSERT_EQ(2, task.paths_.size());
00205 
00206   boost::shared_ptr<MotionGroup> group_ptr = task.motion_groups_["group_1"];
00207   ASSERT_TRUE(group_ptr);
00208 
00209   boost::shared_ptr<Path> path_ptr = task.paths_["path_1"];
00210   ASSERT_EQ(path_ptr->moves_.front().point_->group_, group_ptr);
00211 
00212   boost::shared_ptr<JointPoint> joint_point_ptr = task.points_["home"];
00213   ASSERT_TRUE(joint_point_ptr);
00214 
00215   ASSERT_TRUE(fromXml(task, xml_string));
00216 
00217 }
00218 
00219 // Run all the tests that were declared with TEST()
00220 int main(int argc, char **argv)
00221 {
00222   testing::InitGoogleTest(&argc, argv);
00223   return RUN_ALL_TESTS();
00224 }
00225 


mtconnect_task_parser
Author(s): Shaun M. Edwards
autogenerated on Mon Jan 6 2014 11:30:06