ProteoWizard
Classes | Macros | Functions | Variables
FilesystemTest.cpp File Reference
#include "Std.hpp"
#include "Filesystem.hpp"
#include "unit.hpp"

Go to the source code of this file.

Classes

struct  TestPathmask
 

Macros

#define SYSTEMCATEGORY   system_category
 
#define ABS   "./"
 
#define REL   "./relative"
 
#define A   "/"
 
#define D   ":"
 

Functions

string setSystemDrive (const string &path)
 
void create_file (const bfs::path &ph, const string &contents)
 
void createTestPath ()
 
void deleteTestPath ()
 
set< bfs::path > parsePathArray (const string &pathArray)
 
void testExpandPathmask ()
 
void testAbbreviateByteSize ()
 
int main (int argc, char *argv[])
 

Variables

string systemDrive
 
const char * testPathContentPairArray []
 
const int testPathContentPairArraySize = sizeof(testPathContentPairArray) / sizeof(const char*)
 
const TestPathmask testPathmaskArray []
 
const int testPathmaskArraySize = sizeof(testPathmaskArray) / sizeof(TestPathmask)
 

Macro Definition Documentation

◆ SYSTEMCATEGORY

#define SYSTEMCATEGORY   system_category

Definition at line 37 of file FilesystemTest.cpp.

Referenced by create_file().

◆ ABS

#define ABS   "./"

Definition at line 48 of file FilesystemTest.cpp.

Referenced by createTestPath(), and testExpandPathmask().

◆ REL

#define REL   "./relative"

Definition at line 49 of file FilesystemTest.cpp.

Referenced by createTestPath(), and testExpandPathmask().

◆ A

#define A   "/"

◆ D

#define D   ":"

Definition at line 51 of file FilesystemTest.cpp.

Referenced by parsePathArray(), and DemuxDebugRWTest::SetUp().

Function Documentation

◆ setSystemDrive()

string setSystemDrive ( const string &  path)

Definition at line 55 of file FilesystemTest.cpp.

References systemDrive.

Referenced by createTestPath(), deleteTestPath(), parsePathArray(), and testExpandPathmask().

56 {
57  return bal::replace_all_copy(path, "%SD%", systemDrive);
58 }
string systemDrive

◆ create_file()

void create_file ( const bfs::path &  ph,
const string &  contents 
)

Definition at line 116 of file FilesystemTest.cpp.

References SYSTEMCATEGORY.

Referenced by createTestPath().

117 {
118  ofstream f(ph.string().c_str());
119  if (!f)
120  throw bfs::filesystem_error("create_file", ph, error_code(errno, SYSTEMCATEGORY));
121  if (!contents.empty()) f << contents;
122 }
#define SYSTEMCATEGORY

◆ createTestPath()

void createTestPath ( )

Definition at line 125 of file FilesystemTest.cpp.

References ABS, create_file(), REL, setSystemDrive(), testPathContentPairArray, testPathContentPairArraySize, and unit_assert.

Referenced by testExpandPathmask().

126 {
127  for (int i=0; i < testPathContentPairArraySize; i += 2)
128  {
129  auto testPath = setSystemDrive(testPathContentPairArray[i]);
130 
131  try
132  {
133  // if content is empty, create a directory
134  if (strlen(testPathContentPairArray[i + 1]) == 0)
135  bfs::create_directory(testPath);
136  else
137  create_file(testPath, testPathContentPairArray[i + 1]);
138  }
139  catch (exception&)
140  {
141  // the absolute path tests on Windows will fail if not run with administartor permissions; don't count these as test failures
142  if (string(ABS) != REL && bal::starts_with(testPath, setSystemDrive(ABS)))
143  {
144  cerr << "Test on \"" << testPath << "\" skipped; requires administrator permissions." << endl;
145  continue;
146  }
147  }
148 
149  // test that the directory/file was really created
150  unit_assert(bfs::exists(testPath));
151  }
152 }
const char * testPathContentPairArray[]
string setSystemDrive(const string &path)
#define ABS
const int testPathContentPairArraySize
void create_file(const bfs::path &ph, const string &contents)
#define REL
#define unit_assert(x)
Definition: unit.hpp:85

◆ deleteTestPath()

void deleteTestPath ( )

Definition at line 155 of file FilesystemTest.cpp.

References setSystemDrive(), testPathContentPairArray, and testPathContentPairArraySize.

Referenced by main(), and testExpandPathmask().

156 {
157  for (int i=0; i < testPathContentPairArraySize; i += 2)
158  if (bfs::exists(setSystemDrive(testPathContentPairArray[i])))
159  bfs::remove_all(setSystemDrive(testPathContentPairArray[i]));
160 }
const char * testPathContentPairArray[]
string setSystemDrive(const string &path)
const int testPathContentPairArraySize

◆ parsePathArray()

set<bfs::path> parsePathArray ( const string &  pathArray)

Definition at line 163 of file FilesystemTest.cpp.

References D, and setSystemDrive().

Referenced by testExpandPathmask().

164 {
165  set<bfs::path> pathSet;
166  vector<string> tokens;
167  bal::split(tokens, pathArray, bal::is_any_of(D));
168  if (!tokens.empty() && !tokens[0].empty())
169  for (size_t i=0; i < tokens.size(); ++i)
170  pathSet.insert(setSystemDrive(tokens[i]));
171  return pathSet;
172 }
#define D
string setSystemDrive(const string &path)

◆ testExpandPathmask()

void testExpandPathmask ( )

Definition at line 175 of file FilesystemTest.cpp.

References ABS, createTestPath(), deleteTestPath(), pwiz::util::expand_pathmask(), parsePathArray(), REL, setSystemDrive(), systemDrive, testPathmaskArraySize, unit_assert, and unit_assert_operator_equal.

Referenced by main().

176 {
177  char* systemDriveEnv = ::getenv("SystemDrive"); // usually "C:" on Windows
178  if (systemDriveEnv)
179  systemDrive = systemDriveEnv;
180 
181  // create a filesystem tree for testing
182  createTestPath();
183 
184  int failedTests = 0;
185 
186  for (int i=0; i < testPathmaskArraySize; ++i)
187  {
188  try
189  {
190  vector<bfs::path> matchingPaths;
191  expand_pathmask(setSystemDrive(testPathmaskArray[i].pathmask), matchingPaths);
192 
193  set<bfs::path> targetPathSet = parsePathArray(testPathmaskArray[i].pathnameArray);
194  unit_assert(matchingPaths.size() == targetPathSet.size());
195 
196  set<bfs::path> matchingPathSet(matchingPaths.begin(), matchingPaths.end());
197  vector<bfs::path> xorSet;
198  std::set_symmetric_difference(targetPathSet.begin(), targetPathSet.end(),
199  matchingPathSet.begin(), matchingPathSet.end(),
200  xorSet.end());
201  unit_assert(xorSet.empty());
202  }
203  catch (exception& e)
204  {
205  // the absolute path tests on Windows will fail if not run with administartor permissions; don't count these as test failures
206  if (string(ABS) != REL && bal::starts_with(testPathmaskArray[i].pathmask, ABS))
207  continue;
208 
209  cout << "Unit test on pathmask \"" << setSystemDrive(testPathmaskArray[i].pathmask) << "\" failed:\n"
210  << e.what() << endl;
211  ++failedTests;
212  }
213  }
214 
215  unit_assert_operator_equal(0, failedTests);
216 
217  // special test of wildcard in the root (on Windows, if run with administrator permissions)
218  if (bfs::exists(setSystemDrive(ABS"pwiz_foofoo_test")))
219  {
220  vector<bfs::path> matchingPaths;
221  expand_pathmask(setSystemDrive(ABS"*"), matchingPaths);
222  unit_assert(find(matchingPaths.begin(), matchingPaths.end(), setSystemDrive(ABS"pwiz_foofoo_test")) != matchingPaths.end());
223  unit_assert(find(matchingPaths.begin(), matchingPaths.end(), setSystemDrive(ABS"pwiz_foo_test")) != matchingPaths.end());
224  unit_assert(find(matchingPaths.begin(), matchingPaths.end(), setSystemDrive(ABS"pwiz_bar_test")) != matchingPaths.end());
225  }
226 
227  // cleanup test tree
228  deleteTestPath();
229 }
PWIZ_API_DECL int expand_pathmask(const bfs::path &pathmask, vector< bfs::path > &matchingPaths)
expands (aka globs) a pathmask to zero or more matching paths and returns the number of matching path...
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
void deleteTestPath()
const int testPathmaskArraySize
string systemDrive
string setSystemDrive(const string &path)
#define ABS
const TestPathmask testPathmaskArray[]
void createTestPath()
#define REL
set< bfs::path > parsePathArray(const string &pathArray)
#define unit_assert(x)
Definition: unit.hpp:85

◆ testAbbreviateByteSize()

void testAbbreviateByteSize ( )

Definition at line 232 of file FilesystemTest.cpp.

References pwiz::util::abbreviate_byte_size(), pwiz::util::ByteSizeAbbreviation_IEC, pwiz::util::ByteSizeAbbreviation_JEDEC, and unit_assert.

Referenced by main().

233 {
234  unit_assert(abbreviate_byte_size(1) == "1 B");
235  unit_assert(abbreviate_byte_size(999) == "999 B");
236  unit_assert(abbreviate_byte_size(1000) == "1 KB");
237  unit_assert(abbreviate_byte_size(999999) == "999 KB");
238  unit_assert(abbreviate_byte_size(1000000) == "1 MB");
239  unit_assert(abbreviate_byte_size(999999999) == "999 MB");
240  unit_assert(abbreviate_byte_size(1000000000) == "1 GB");
241 
245  unit_assert(abbreviate_byte_size((1024 << 10)-1, ByteSizeAbbreviation_IEC) == "1023 KiB");
246  unit_assert(abbreviate_byte_size((1024 << 10), ByteSizeAbbreviation_IEC) == "1 MiB");
247  unit_assert(abbreviate_byte_size((1024 << 20)-1, ByteSizeAbbreviation_IEC) == "1023 MiB");
248  unit_assert(abbreviate_byte_size((1024 << 20), ByteSizeAbbreviation_IEC) == "1 GiB");
249 
253  unit_assert(abbreviate_byte_size((1024 << 10)-1, ByteSizeAbbreviation_JEDEC) == "1023 KB");
255  unit_assert(abbreviate_byte_size((1024 << 20)-1, ByteSizeAbbreviation_JEDEC) == "1023 MB");
257 }
sizes are treated as multiples of 2; abbreviations are: GiB (Gibibyte), MiB (Mebibyte), KiB (Kibibyte), B (byte)
Definition: Filesystem.hpp:94
PWIZ_API_DECL std::string abbreviate_byte_size(boost::uintmax_t byteSize, ByteSizeAbbreviation abbreviationType=ByteSizeAbbreviation_SI)
abbreviates a byte size (file or RAM) as a readable string, using the specified notation ...
sizes are treated as multiples of 2; abbreviations are: GB (Gigabyte), MB (Megabyte), KB (Kilobyte), B (byte)
Definition: Filesystem.hpp:98
#define unit_assert(x)
Definition: unit.hpp:85

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 260 of file FilesystemTest.cpp.

References deleteTestPath(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testAbbreviateByteSize(), and testExpandPathmask().

261 {
262  TEST_PROLOG(argc, argv)
263 
264  try
265  {
268  }
269  catch (exception& e)
270  {
271  TEST_FAILED(e.what())
272  }
273  catch (...)
274  {
275  TEST_FAILED("Caught unknown exception.")
276  }
277 
278  deleteTestPath();
280 }
#define TEST_EPILOG
Definition: unit.hpp:183
void testAbbreviateByteSize()
void deleteTestPath()
#define TEST_FAILED(x)
Definition: unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
void testExpandPathmask()

Variable Documentation

◆ systemDrive

string systemDrive

Definition at line 54 of file FilesystemTest.cpp.

Referenced by setSystemDrive(), and testExpandPathmask().

◆ testPathContentPairArray

const char* testPathContentPairArray[]
Initial value:
=
{
ABS "pwiz_foofoo_test", "root file",
ABS "pwiz_foo_test", "",
ABS "pwiz_foo_test" A "this file", "has content",
ABS "pwiz_foo_test" A "this dir has", "",
ABS "pwiz_foo_test" A "this dir has" A "a test file", "with content",
ABS "pwiz_bar_test", "",
ABS "pwiz_bar_test" A "some file", "12345",
ABS "pwiz_bar_test" A "some dir", "",
REL "pwiz_foofoo_test", "root file",
REL "pwiz_foo_test", "",
REL "pwiz_foo_test" A "this file", "has content",
REL "pwiz_foo_test" A "this dir has", "",
REL "pwiz_foo_test" A "this dir has" A "a test file", "with content",
REL "pwiz_bar_test", "",
REL "pwiz_bar_test" A "some file", "12345",
REL "pwiz_bar_test" A "some dir", ""
}
#define A
#define ABS
#define REL

Definition at line 60 of file FilesystemTest.cpp.

Referenced by createTestPath(), and deleteTestPath().

◆ testPathContentPairArraySize

const int testPathContentPairArraySize = sizeof(testPathContentPairArray) / sizeof(const char*)

Definition at line 83 of file FilesystemTest.cpp.

Referenced by createTestPath(), and deleteTestPath().

◆ testPathmaskArray

const TestPathmask testPathmaskArray[]
Initial value:
=
{
{ ABS "pwiz_f??f??_test", ABS "pwiz_foofoo_test" },
{ ABS "pwiz_???_test", ABS "pwiz_foo_test" D ABS "pwiz_bar_test" },
{ ABS "pwiz_f*o_test", ABS "pwiz_foo_test" D ABS "pwiz_foofoo_test" },
{ ABS "pwiz_foobar_test", "" },
{ ABS "pwiz_foo_test" A "no*hit", "" },
{ ABS "pwiz_foo_test" A "*", ABS "pwiz_foo_test" A "this file" D ABS "pwiz_foo_test" A "this dir has" },
{ ABS"pwiz_foo_test" A "this *", ABS "pwiz_foo_test" A "this file" D ABS "pwiz_foo_test" A "this dir has" },
{ REL "pwiz_f??f??_test", REL "pwiz_foofoo_test" },
{ REL "pwiz_???_test", REL "pwiz_foo_test" D REL "pwiz_bar_test" },
{ REL "pwiz_f*o_test", REL "pwiz_foo_test" D REL "pwiz_foofoo_test" },
{ REL "pwiz_foobar_test", "" },
{ REL "pwiz_foo_test" A "no*hit", "" },
{ REL "pwiz_foo_test" A "*", REL "pwiz_foo_test" A "this file" D REL "pwiz_foo_test" A "this dir has" },
{ REL "pwiz_foo_test" A "this *", REL "pwiz_foo_test" A "this file" D REL "pwiz_foo_test" A "this dir has" }
}
#define D
#define A
#define ABS
#define REL

Definition at line 93 of file FilesystemTest.cpp.

◆ testPathmaskArraySize

const int testPathmaskArraySize = sizeof(testPathmaskArray) / sizeof(TestPathmask)

Definition at line 113 of file FilesystemTest.cpp.

Referenced by testExpandPathmask().