GeneralTests.cpp
Go to the documentation of this file.
1 #include <cppunit/extensions/HelperMacros.h>
2 #include <NovaDAQUtilities/LocationUtils.h>
3 #include <NovaDAQUtilities/ProcessUtils.h>
4 #include <NovaDAQUtilities/TimeUtils.h>
5 #include <boost/filesystem.hpp>
6 #include <vector>
7 #include <sys/types.h>
8 #include <unistd.h>
9 
10 namespace BFS = boost::filesystem;
11 
12 using namespace novadaq;
13 
14 class GeneralTests : public CppUnit::TestFixture
15 {
16  CPPUNIT_TEST_SUITE(GeneralTests);
17  CPPUNIT_TEST(testProcessUtils);
18  CPPUNIT_TEST(testTimeUtils);
19  CPPUNIT_TEST(testFindFile);
20  CPPUNIT_TEST(testEnvVarReplacement);
21  CPPUNIT_TEST_SUITE_END();
22 
23 public:
24  void setUp();
25  void tearDown();
26 
27  void testProcessUtils();
28  void testTimeUtils();
29  void testFindFile();
30  void testEnvVarReplacement();
31 
32 private:
33 
34 };
35 
36 void
38 {
39  // flush stdout to get progress markers to display in a timely way
40  std::cout.flush();
41 }
42 
43 void
45 {
46  // flush stdout to get progress markers to display in a timely way
47  std::cout.flush();
48 }
49 
50 void
52 {
53  pid_t thisPid = getpid();
54  pid_t parentPid = getppid();
55  std::vector<int> childPidList;
56  int result =
57  ProcessUtils::getDescendantProcessIds(parentPid, childPidList);
58  CPPUNIT_ASSERT(result == 0);
59  if (result == 0) {
60  bool found = false;
61  for (std::vector<int>::size_type jdx = 0;
62  jdx < childPidList.size(); ++jdx) {
63  if (childPidList[jdx] == thisPid) {
64  found = true;
65  break;
66  }
67  }
68  CPPUNIT_ASSERT(found);
69  }
70 }
71 
72 void
74 {
75  // test the conversion of a timeval struct to a double
76  struct timeval unixTime;
77  gettimeofday(&unixTime, 0);
78  double doubleTime = convertTimevalToDouble(unixTime);
79  CPPUNIT_ASSERT( ((int64_t) (doubleTime * 1000000)) ==
80  ((((int64_t) unixTime.tv_sec) * 1000000) +
81  ((int64_t) unixTime.tv_usec)));
82 }
83 
84 void
86 {
87  bool foundFile;
88  std::string locatedFileURL;
89 
90  // test a couple of expected full paths
91  foundFile = LocationUtils::findFile("/etc/passwd", locatedFileURL);
92  CPPUNIT_ASSERT(foundFile);
93  CPPUNIT_ASSERT(locatedFileURL == "/etc/passwd");
94 
95  foundFile = LocationUtils::findFile("/nova/novadaq", locatedFileURL);
96  CPPUNIT_ASSERT(foundFile);
97  CPPUNIT_ASSERT(locatedFileURL == "/nova/novadaq");
98 
99  // test tilde expansion
100  std::string pathString1, pathString2;
101  foundFile = LocationUtils::findFile("~", pathString1);
102  CPPUNIT_ASSERT(foundFile);
103  foundFile = LocationUtils::findFile("$HOME", pathString2);
104  CPPUNIT_ASSERT(foundFile);
105  CPPUNIT_ASSERT(pathString2 == pathString1);
106 
107  // test that a file in the current working directory is found
108  std::string localFile;
109  BFS::path currentPath = BFS::current_path();
110  BFS::directory_iterator endDirIter;
111  for (BFS::directory_iterator dirIter(currentPath.file_string());
112  dirIter != endDirIter; ++dirIter) {
113  if (BFS::is_regular_file(dirIter->status())) {
114  std::string filename(dirIter->path().filename());
115  localFile = filename;
116  break;
117  }
118  }
119  if (localFile.length() == 0) {
120  for (BFS::directory_iterator dirIter(currentPath.file_string());
121  dirIter != endDirIter; ++dirIter) {
122  std::string filename(dirIter->path().filename());
123  localFile = filename;
124  break;
125  }
126  }
127  if (localFile.length() > 0) {
128  foundFile = LocationUtils::findFile(localFile, locatedFileURL);
129  CPPUNIT_ASSERT(foundFile);
130  std::string tmpString(currentPath.file_string());
131  tmpString.append("/" + localFile);
132  CPPUNIT_ASSERT(locatedFileURL == tmpString);
133  }
134 
135  // test that an expected file in our SRT directories is found
136  foundFile = LocationUtils::findFile("NovaDAQUtilities/GNUmakefile",
137  locatedFileURL);
138  CPPUNIT_ASSERT(foundFile);
139 
140  // test that a garbage file is not found
141  foundFile = LocationUtils::findFile("a/file/which/should/not/be/found",
142  locatedFileURL);
143  CPPUNIT_ASSERT(! foundFile);
144 
145  // try prefixes
146  std::vector<std::string> prefixes;
147  prefixes.push_back("NovaRunControl/cmd/");
148  foundFile = LocationUtils::findFile("SimMode1.cmd",
149  locatedFileURL,
150  prefixes);
151  CPPUNIT_ASSERT(foundFile);
152 }
153 
154 void
156 {
157  {
158  std::string userString = LocationUtils::replaceEnvVars("${USER}");
159  char* envPtr = getenv("USER");
160  CPPUNIT_ASSERT(envPtr != 0);
161  std::string tmpString(envPtr);
162  CPPUNIT_ASSERT(tmpString == userString);
163  }
164 
165  {
166  std::string pathString =
167  LocationUtils::replaceEnvVars("${HOME}/${HOSTNAME}");
168  char* envPtr = getenv("HOME");
169  CPPUNIT_ASSERT(envPtr != 0);
170  std::string tmpString(envPtr);
171  tmpString.append("/");
172  envPtr = getenv("HOSTNAME");
173  if (envPtr != 0) {
174  tmpString.append(envPtr);
175  CPPUNIT_ASSERT(tmpString == pathString);
176  }
177  }
178 
179  {
180  std::string userString = LocationUtils::wordExpansion("${USER}");
181  char* envPtr = getenv("USER");
182  CPPUNIT_ASSERT(envPtr != 0);
183  std::string tmpString(envPtr);
184  CPPUNIT_ASSERT(tmpString == userString);
185  }
186 
187  {
188  std::string pathString1 = LocationUtils::wordExpansion("${HOME}");
189  std::string pathString2 = LocationUtils::wordExpansion("~");
190  CPPUNIT_ASSERT(pathString2 == pathString1);
191  }
192 }
193 
195 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQUtilities");
void testFindFile()
void testTimeUtils()
static std::string wordExpansion(const std::string &sourceString)
static bool findFile(const std::string &filePath, std::string &locationOfFoundInstance)
string filename
Definition: shutoffs.py:106
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQUtilities")
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:11
static int getDescendantProcessIds(const int &parentPid, std::vector< int > &descendantPidList)
std::string getenv(std::string const &name)
double convertTimevalToDouble(struct timeval const &inputUnixTime)
Definition: TimeUtils.cpp:10
OStream cout
Definition: OStream.cxx:6
const std::string path
Definition: plot_BEN.C:43
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(GeneralTests,"NovaDAQUtilities")
static std::string replaceEnvVars(const std::string &sourceString)
void testEnvVarReplacement()
void testProcessUtils()
enum BeamMode string