FileSpecFind_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #ifndef WIN32
40 #include <unistd.h>
41 #endif
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <stdlib.h>
45 #include <iostream>
46 #include <string>
47 #include <regex>
48 #include "TestUtil.hpp"
49 #include "YDSTime.hpp"
50 #include "FileSpecFind.hpp"
51 
57 using namespace std;
58 
60 {
61 public:
63  : fs(gnsstk::getFileSep())
64  {}
65 
67  unsigned findTestsAbs();
69  unsigned findTestsRel();
71  unsigned findTestsRelDot();
73  unsigned findTestsRelDotDot();
75  unsigned findSimpleFileName();
76 
77 private:
79  unsigned findTests(const std::string& tld, const std::string& testName);
81  bool openable(const list<string>& files);
82 
84  std::string fs;
85 };
86 
87 
89 openable(const list<string>& files)
90 {
91  for (const auto& i : files)
92  {
93  ifstream f(i.c_str());
94  if (!f)
95  {
96  cerr << "Can't open \"" << i << "\"" << endl;
97  return false;
98  }
99  }
100  return true;
101 }
102 
103 
104 unsigned FileSpecFind_T ::
106 {
107  string tld = gnsstk::getPathData() + fs + "FileSpecFind";
108  return findTests(tld, "Abs");
109 }
110 
111 
112 unsigned FileSpecFind_T ::
114 {
115  string tld = "FileSpecFind";
116  return findTests(tld, "Rel");
117 }
118 
119 
120 unsigned FileSpecFind_T ::
122 {
123  string tld = "." + fs + "FileSpecFind";
124  return findTests(tld, "RelDot");
125 }
126 
127 
128 unsigned FileSpecFind_T ::
130 {
131  string tld = ".." + fs + "data" + fs + "FileSpecFind";
132  return findTests(tld, "RelDotDot");
133 }
134 
135 
136 unsigned FileSpecFind_T ::
137 findTests(const std::string& tld, const std::string& testName)
138 {
139  TUDEF("FileSpecFind", "find("+testName+")");
140 
141  string searchSpec = tld + fs + "%04Y" + fs + "%05n" + fs + "%03j" + fs +
142  "nsh-%x-%5n-%1r-%04Y-%03j-%02H%02M%02S.xml";
143 
144  string winTest = gnsstk::FileSpecFind::transToken(searchSpec);
146  fsts[gnsstk::FileSpec::text] = "FOO";
147  list<string> files;
148  using ListSize = list<string>::size_type;
149 
150  // Expected counts in test below are based on the current
151  // contents of the directories as of the time of this writing.
152  // The directories should be messed with, but if they are it
153  // could break this test.
154 
155  TUCSM("find(" + testName + ") no results");
156  try
157  {
159  searchSpec,
160  gnsstk::YDSTime(2016,211,0),
161  gnsstk::YDSTime(2016,212,0),
162  fsts);
163  }
164  catch (gnsstk::Exception &exc)
165  {
166  cerr << exc;
167  TUFAIL("Unexpected exception");
168  }
169  catch (std::exception& exc)
170  {
171  TUFAIL("Unexpected exception " + std::string(exc.what()));
172  }
173  catch (...)
174  {
175  TUFAIL("Unexpected exception");
176  }
177  TUASSERTE(ListSize, 0, files.size());
178 
179  TUCSM("find(" + testName + ") single epoch");
180  try
181  {
183  searchSpec,
184  gnsstk::YDSTime(2018,211,67500),
185  gnsstk::YDSTime(2018,211,67500),
186  fsts);
187  }
188  catch (gnsstk::Exception &exc)
189  {
190  cerr << exc;
191  TUFAIL("Unexpected exception");
192  }
193  catch (std::exception& exc)
194  {
195  TUFAIL("Unexpected exception " + std::string(exc.what()));
196  }
197  catch (...)
198  {
199  TUFAIL("Unexpected exception");
200  }
201  TUASSERTE(ListSize, 20, files.size());
202  // make sure we got all valid files
203  TUASSERT(openable(files));
204 
205  TUCSM("find(" + testName + ") single epoch via span");
206  try
207  {
209  searchSpec,
210  gnsstk::YDSTime(2018,211,67500),
211  gnsstk::YDSTime(2018,211,67800),
212  fsts);
213  }
214  catch (gnsstk::Exception &exc)
215  {
216  cerr << exc;
217  TUFAIL("Unexpected exception");
218  }
219  catch (std::exception& exc)
220  {
221  TUFAIL("Unexpected exception " + std::string(exc.what()));
222  }
223  catch (...)
224  {
225  TUFAIL("Unexpected exception");
226  }
227  TUASSERTE(ListSize, 20, files.size());
228  // make sure we got all valid files
229  TUASSERT(openable(files));
230 
231  TUCSM("find(" + testName + ") multiple epochs");
232  try
233  {
235  searchSpec,
236  gnsstk::YDSTime(2018,211,67500),
237  gnsstk::YDSTime(2018,211,67801),
238  fsts);
239  }
240  catch (gnsstk::Exception &exc)
241  {
242  cerr << exc;
243  TUFAIL("Unexpected exception");
244  }
245  catch (std::exception& exc)
246  {
247  TUFAIL("Unexpected exception " + std::string(exc.what()));
248  }
249  catch (...)
250  {
251  TUFAIL("Unexpected exception");
252  }
253  TUASSERTE(ListSize, 40, files.size());
254  // make sure we got all valid files
255  TUASSERT(openable(files));
256 
257  // change the search spec for filter to not have variable length tokens.
258  searchSpec = tld + fs + "%04Y" + fs + "%05n" + fs + "%03j" + fs +
259  "nsh-%3x-%5n-%1r-%04Y-%03j-%02H%02M%02S.xml";
260  TUCSM("find(" + testName + ") multiple epochs with filter");
261  try
262  {
264  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
265  gnsstk::FileSpec::station, "10000"));
267  searchSpec,
268  gnsstk::YDSTime(2018,211,67500),
269  gnsstk::YDSTime(2018,211,67801),
270  filter);
271  }
272  catch (gnsstk::Exception &exc)
273  {
274  cerr << exc;
275  TUFAIL("Unexpected exception");
276  }
277  catch (std::exception& exc)
278  {
279  TUFAIL("Unexpected exception " + std::string(exc.what()));
280  }
281  catch (...)
282  {
283  TUFAIL("Unexpected exception");
284  }
285  TUASSERTE(ListSize, 5, files.size());
286  // make sure we got all valid files
287  TUASSERT(openable(files));
288 
289  try
290  {
292  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
293  gnsstk::FileSpec::station, "10000"));
294  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
295  gnsstk::FileSpec::text, "FOO"));
297  searchSpec,
298  gnsstk::YDSTime(2018,211,67500),
299  gnsstk::YDSTime(2018,211,67801),
300  filter);
301  }
302  catch (gnsstk::Exception &exc)
303  {
304  cerr << exc;
305  TUFAIL("Unexpected exception");
306  }
307  catch (std::exception& exc)
308  {
309  TUFAIL("Unexpected exception " + std::string(exc.what()));
310  }
311  catch (...)
312  {
313  TUFAIL("Unexpected exception");
314  }
315  TUASSERTE(ListSize, 4, files.size());
316  // make sure we got all valid files
317  TUASSERT(openable(files));
318 
319  try
320  {
322  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
323  gnsstk::FileSpec::station, "10000"));
324  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
325  gnsstk::FileSpec::text, "BAR"));
327  searchSpec,
328  gnsstk::YDSTime(2018,211,67500),
329  gnsstk::YDSTime(2018,211,67801),
330  filter);
331  }
332  catch (gnsstk::Exception &exc)
333  {
334  cerr << exc;
335  TUFAIL("Unexpected exception");
336  }
337  catch (std::exception& exc)
338  {
339  TUFAIL("Unexpected exception " + std::string(exc.what()));
340  }
341  catch (...)
342  {
343  TUFAIL("Unexpected exception");
344  }
345  TUASSERTE(ListSize, 1, files.size());
346  // make sure we got all valid files
347  TUASSERT(openable(files));
348 
349  try
350  {
352  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
353  gnsstk::FileSpec::station, "10000"));
354  filter.insert(gnsstk::FileSpecFind::Filter::value_type(
357  searchSpec,
358  gnsstk::YDSTime(2018,211,67500),
359  gnsstk::YDSTime(2018,211,67801),
360  filter);
361  }
362  catch (gnsstk::Exception &exc)
363  {
364  cerr << exc;
365  TUFAIL("Unexpected exception");
366  }
367  catch (std::exception& exc)
368  {
369  TUFAIL("Unexpected exception " + std::string(exc.what()));
370  }
371  catch (...)
372  {
373  TUFAIL("Unexpected exception");
374  }
375  TUASSERTE(ListSize, 2, files.size());
376  // make sure we got all valid files
377  TUASSERT(openable(files));
378 
379  searchSpec = tld + fs + "2018" + fs + "10000" + fs + "211" + fs +
380  "nsh-FOO-10000-1-2018-211-184500.xml";
381  TUCSM("find(" + testName + ") exact file name");
382  try
383  {
386  searchSpec,
389  filter);
390  }
391  catch (gnsstk::Exception &exc)
392  {
393  cerr << exc;
394  TUFAIL("Unexpected exception");
395  }
396  catch (std::exception& exc)
397  {
398  TUFAIL("Unexpected exception " + std::string(exc.what()));
399  }
400  catch (...)
401  {
402  TUFAIL("Unexpected exception");
403  }
404  TUASSERTE(ListSize, 1, files.size());
405  // make sure we got all valid files
406  TUASSERT(openable(files));
407 
408  TURETURN();
409 }
410 
411 
412 unsigned FileSpecFind_T ::
414 {
415  TUDEF("FileSpecFind", "find(simple file name)");
416  list<string> files;
417  using ListSize = list<string>::size_type;
418  try
419  {
420  // The spec doesn't really matter too much as long as it
421  // refers to a file that exists in the data directory. This
422  // also assumes the test is run in the data directory, but
423  // the other tests do as well.
425  "TropModel_Zero.exp",
426  gnsstk::YDSTime(2016,211,0),
427  gnsstk::YDSTime(2016,212,0));
428  }
429  catch (gnsstk::Exception &exc)
430  {
431  cerr << exc;
432  TUFAIL("Unexpected exception");
433  }
434  catch (std::exception& exc)
435  {
436  TUFAIL("Unexpected exception " + std::string(exc.what()));
437  }
438  catch (...)
439  {
440  TUFAIL("Unexpected exception");
441  }
442  TUASSERTE(ListSize, 1, files.size());
443  // make sure we got all valid files
444  TUASSERT(openable(files));
445 
446  TURETURN();
447 }
448 
449 
450 int main(int argc, char *argv[])
451 {
452  unsigned errorTotal = 0;
453  FileSpecFind_T testClass;
454  errorTotal += testClass.findTestsAbs();
455  errorTotal += testClass.findTestsRel();
456  errorTotal += testClass.findTestsRelDot();
457  errorTotal += testClass.findTestsRelDotDot();
458  errorTotal += testClass.findSimpleFileName();
459  cout << "Total Failures for " << __FILE__ << ": " << errorTotal << endl;
460  return errorTotal;
461 }
gnsstk::FileSpec::FSTStringMap
std::map< FileSpecType, std::string > FSTStringMap
Definition: FileSpec.hpp:136
FileSpecFind_T::openable
bool openable(const list< string > &files)
Return true if all paths in files can be opened for read.
Definition: FileSpecFind_T.cpp:89
TUCSM
#define TUCSM(METHOD)
Definition: TestUtil.hpp:59
YDSTime.hpp
gnsstk::FileSpecFind::Filter
std::multimap< FileSpec::FileSpecType, std::string > Filter
Data type for storing desired FileSpec values.
Definition: FileSpecFind.hpp:87
gnsstk::FileSpecFind::transToken
static std::string transToken(const std::string &token)
Definition: FileSpecFind.cpp:308
gnsstk::YDSTime
Definition: YDSTime.hpp:58
FileSpecFind_T
Definition: FileSpecFind_T.cpp:59
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
TUFAIL
#define TUFAIL(MSG)
Definition: TestUtil.hpp:228
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::FileSpecFind::find
static std::list< std::string > find(const std::string &fileSpec, const CommonTime &start, const CommonTime &end, const FileSpec::FSTStringMap &fsts=FileSpec::FSTStringMap())
Definition: FileSpecFind.cpp:225
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::CommonTime::END_OF_TIME
static const GNSSTK_EXPORT CommonTime END_OF_TIME
latest representable CommonTime
Definition: CommonTime.hpp:104
FileSpecFind_T::fs
std::string fs
File separator, but short.
Definition: FileSpecFind_T.cpp:84
TUASSERT
#define TUASSERT(EXPR)
Definition: TestUtil.hpp:63
FileSpecFind_T::findTestsRelDotDot
unsigned findTestsRelDotDot()
test find with relative paths that include ..
Definition: FileSpecFind_T.cpp:129
TestUtil.hpp
FileSpecFind_T::findTestsRel
unsigned findTestsRel()
test find with relative paths, no . or ..
Definition: FileSpecFind_T.cpp:113
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
FileSpecFind_T::findSimpleFileName
unsigned findSimpleFileName()
test find with a simple file name with no wildcards and no path
Definition: FileSpecFind_T.cpp:413
files
page HOWTO subpage DoxygenGuide Documenting Your Code page DoxygenGuide Documenting Your Code todo Flesh out this document section doctips Tips for Documenting When defining make sure that the prototype is identical between the cpp and hpp including both the namespaces and the parameter names for you have std::string as the return type in the hpp file and string as the return type in the cpp Doxygen may get confused and autolink to the cpp version with no documentation If you don t use the same parameter names between the cpp and hpp files
Definition: DOCUMENTING.dox:9
gnsstk::FileSpec::receiver
@ receiver
'r' A field for the receiver number
Definition: FileSpec.hpp:97
FileSpecFind_T::findTestsAbs
unsigned findTestsAbs()
test find with absolute paths
Definition: FileSpecFind_T.cpp:105
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
FileSpecFind_T::FileSpecFind_T
FileSpecFind_T()
Definition: FileSpecFind_T.cpp:62
gnsstk::FileSpec::station
@ station
'n' A field for station numbers
Definition: FileSpec.hpp:96
gnsstk::FileSpec::text
@ text
Definition: FileSpec.hpp:105
std
Definition: Angle.hpp:142
FileSpecFind_T::findTestsRelDot
unsigned findTestsRelDot()
test find with relative paths that include .
Definition: FileSpecFind_T.cpp:121
main
int main(int argc, char *argv[])
Definition: FileSpecFind_T.cpp:450
FileSpecFind.hpp
FileSpecFind_T::findTests
unsigned findTests(const std::string &tld, const std::string &testName)
generic version of above tests
Definition: FileSpecFind_T.cpp:137


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:39