test_urdf.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2008, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00037 #include <gtest/gtest.h>
00038 #include <cstdlib>
00039 
00040 #include <dirent.h> 
00041 #include <sys/types.h> 
00042 #include <sys/param.h> 
00043 #include <sys/stat.h> 
00044 #include <unistd.h> 
00045 #include <stdio.h> 
00046 #include <string.h> 
00047 
00048 #include <iostream>
00049 #include <fstream>
00050 
00051 #include <urdf_parser/urdf_parser.h>
00052 #include <boost/function.hpp>
00053 #include <urdf_model/model.h>
00054 
00055 int runExternalProcess(const std::string &executable, const std::string &args)
00056 {
00057     return system((executable + " " + args).c_str());
00058 }
00059 
00060 int walker( std::string & result, int& test_result)
00061 {
00062   DIR           *d;
00063   struct dirent *dir;
00064   d = opendir( "robots" );
00065   if( d == NULL )
00066   {
00067     return 1;
00068   }
00069   while( ( dir = readdir( d ) ) )
00070   {
00071     if( strcmp( dir->d_name, "." ) == 0 || 
00072         strcmp( dir->d_name, ".." ) == 0 )
00073     {
00074       continue;
00075     }
00076     if( dir->d_type != DT_DIR )
00077     {
00078       std::string dir_name = dir->d_name;
00079       if (dir_name.find(std::string(".urdf.xacro")) == dir_name.size()-11)
00080       {
00081         char pwd[MAXPATHLEN];
00082         getcwd( pwd, MAXPATHLEN );
00083         std::string name = std::string(pwd)+"/robots/"+dir_name;
00084         printf("\n\ntesting: %s\n",name.c_str());
00085         result += name;
00086         result += " ";
00087 
00088         runExternalProcess("python `rospack find xacro`/xacro.py", name+" > `rospack find pr2_description`/test/tmp.urdf" );
00089 
00090         std::string path = std::string(pwd)+"/test/tmp.urdf";
00091 
00092 
00093         std::string xml_string;
00094         std::fstream xml_file(path.c_str(), std::fstream::in);
00095         while ( xml_file.good() )
00096         {
00097             std::string line;
00098             std::getline( xml_file, line);
00099             xml_string += (line + "\n");
00100         }
00101         xml_file.close();
00102 
00103         boost::shared_ptr<urdf::ModelInterface> robot = urdf::parseURDF(xml_string);
00104         if (!robot) test_result = test_result || 1;
00105 
00106       }
00107     }
00108   }
00109   closedir( d );
00110   return test_result;
00111 }
00112 
00113 TEST(URDF, CorrectFormat)
00114 {
00115   int test_result = 0;
00116 
00117   std::string result;
00118   if( walker( result, test_result ) == 0 )
00119   {
00120     printf( "Found: %s\n", result.c_str() );
00121   }
00122   else
00123   {
00124     puts( "Not found" );
00125     test_result = -1;
00126   }
00127 
00128   EXPECT_TRUE(test_result == 0);
00129 }
00130 
00131 int main(int argc, char **argv)
00132 {
00133     testing::InitGoogleTest(&argc, argv);
00134     return RUN_ALL_TESTS();
00135 }


pr2_description
Author(s): John Hsu, Eric Berger, Austin Hendrix
autogenerated on Fri Oct 14 2016 04:22:06