utest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Brian Gerkey */
31 
32 #include <string>
33 #include <vector>
34 #include <stdlib.h>
35 #ifndef _WIN32
36 #include <unistd.h>
37 #endif
38 #include <boost/filesystem.hpp>
39 #include <boost/thread.hpp>
40 #include <gtest/gtest.h>
41 #include "ros/package.h"
42 
43 #ifdef _WIN32
44 int setenv(const char *name, const char *value, int overwrite)
45 {
46  if(!overwrite)
47  {
48  size_t envsize = 0;
49  errno_t errcode = getenv_s(&envsize, NULL, 0, name);
50  if(errcode || envsize)
51  return errcode;
52  }
53  return _putenv_s(name, value);
54 }
55 #endif
56 
57 void string_split(const std::string &s, std::vector<std::string> &t, const std::string &d)
58 { t.clear();
59  size_t start = 0, end;
60  while ((end = s.find_first_of(d, start)) != std::string::npos)
61  {
62  if((end-start) > 0)
63  t.push_back(s.substr(start, end-start));
64  start = end + 1;
65  }
66  if(start < s.size())
67  t.push_back(s.substr(start));
68 }
69 
70 std::string g_rr_path;
71 void set_env_vars(void)
72 {
73  // Point ROS_PACKAGE_PATH at the roslib directory, and point
74  // ROS_ROOT into an empty directory.
75  char rr_buf[1024];
76  getcwd(rr_buf, sizeof(rr_buf));
77  setenv("ROS_PACKAGE_PATH", rr_buf, 1);
78 
79  boost::filesystem::path temp_path = boost::filesystem::unique_path();
80  boost::filesystem::create_directories(temp_path);
81  g_rr_path = temp_path.string();
82  setenv("ROS_ROOT", g_rr_path.c_str(), 1);
83 }
84 void cleanup_env_vars(void)
85 {
86  // Remove the empty directory that we created in set_env_vars().
87  rmdir(g_rr_path.c_str());
88  g_rr_path.clear();
89 }
90 
91 TEST(roslib, commandListNames)
92 {
93  set_env_vars();
94 
95  std::string output = ros::package::command("list-names");
96  std::vector<std::string> output_list;
97  string_split(output, output_list, "\n");
98  ASSERT_EQ((int)output_list.size(), 1);
99  ASSERT_STREQ(output_list[0].c_str(), "roslib");
100 
102 }
103 
104 TEST(roslib, commandList)
105 {
106  set_env_vars();
107 
108  std::string output = ros::package::command("list");
109  std::vector<std::string> output_list;
110  std::vector<std::string> name_path;
111  string_split(output, output_list, "\n");
112  ASSERT_EQ((int)output_list.size(), 1);
113  string_split(output_list[0], name_path, " ");
114  ASSERT_EQ((int)name_path.size(), 2);
115  ASSERT_STREQ(name_path[0].c_str(), "roslib");
116 
118 }
119 
120 TEST(roslib, getAll)
121 {
122  set_env_vars();
123 
124  std::vector<std::string> output_list;
125  ASSERT_TRUE(ros::package::getAll(output_list));
126  ASSERT_EQ((int)output_list.size(), 1);
127  ASSERT_STREQ(output_list[0].c_str(), "roslib");
128 
130 }
131 
132 void
134 {
135  struct timespec ts = {0, 100000};
136  std::string output;
137  for(int i=0;i<100;i++)
138  {
139  output = ros::package::command("plugins --attrib=foo roslib");
140 #ifndef _WIN32
141  nanosleep(&ts, NULL);
142 #else
143  boost::this_thread::sleep(boost::posix_time::milliseconds(1));
144 #endif
145  }
146 }
147 
148 TEST(roslib, concurrent_access)
149 {
150  set_env_vars();
151  const int size = 10;
152  boost::thread t[size];
153  for(int i=0;i<size; i++)
154  t[i] = boost::thread(boost::bind(roslib_caller));
155  for(int i=0;i<size; i++)
156  t[i].join();
158 }
159 
160 int main(int argc, char **argv)
161 {
162  testing::InitGoogleTest(&argc, argv);
163  return RUN_ALL_TESTS();
164 }
ROSLIB_DECL bool getAll(V_string &packages)
Gets a list of all packages. Returns false if it could not run the command.
void roslib_caller()
Definition: utest.cpp:133
void set_env_vars(void)
Definition: utest.cpp:71
std::string g_rr_path
Definition: utest.cpp:70
void cleanup_env_vars(void)
Definition: utest.cpp:84
ROSLIB_DECL std::string command(const std::string &cmd)
Runs a rospack command of the form &#39;rospack <cmd>&#39;, returning the output as a single string...
Definition: src/package.cpp:47
int main(int argc, char **argv)
Definition: utest.cpp:160
TEST(roslib, commandListNames)
Definition: utest.cpp:91
void string_split(const std::string &s, std::vector< std::string > &t, const std::string &d)
Definition: utest.cpp:57


roslib
Author(s): Ken Conley , Morgan Quigley , Josh Faust
autogenerated on Thu Apr 30 2020 06:30:15