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 testExpandPathmask().

§ REL

#define REL   "./relative"

Definition at line 49 of file FilesystemTest.cpp.

§ A

#define A   "/"

§ D

#define D   ":"

Definition at line 51 of file FilesystemTest.cpp.

Referenced by parsePathArray().

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 create_file(), setSystemDrive(), testPathContentPairArray, testPathContentPairArraySize, and unit_assert.

Referenced by testExpandPathmask().

126 {
127  for (int i=0; i < testPathContentPairArraySize; i += 2)
128  {
129  // if content is empty, create a directory
130  if (strlen(testPathContentPairArray[i+1]) == 0)
131  bfs::create_directory(setSystemDrive(testPathContentPairArray[i]));
132  else
134 
135  // test that the directory/file was really created
137  }
138 }
const char * testPathContentPairArray[]
string setSystemDrive(const string &path)
const int testPathContentPairArraySize
void create_file(const bfs::path &ph, const string &contents)
#define unit_assert(x)
Definition: unit.hpp:85

§ deleteTestPath()

void deleteTestPath ( )

Definition at line 141 of file FilesystemTest.cpp.

References setSystemDrive(), testPathContentPairArray, and testPathContentPairArraySize.

Referenced by main(), and testExpandPathmask().

142 {
143  for (int i=0; i < testPathContentPairArraySize; i += 2)
144  if (bfs::exists(setSystemDrive(testPathContentPairArray[i])))
145  bfs::remove_all(setSystemDrive(testPathContentPairArray[i]));
146 }
const char * testPathContentPairArray[]
string setSystemDrive(const string &path)
const int testPathContentPairArraySize

§ parsePathArray()

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

Definition at line 149 of file FilesystemTest.cpp.

References D, and setSystemDrive().

Referenced by testExpandPathmask().

150 {
151  set<bfs::path> pathSet;
152  vector<string> tokens;
153  bal::split(tokens, pathArray, bal::is_any_of(D));
154  if (!tokens.empty() && !tokens[0].empty())
155  for (size_t i=0; i < tokens.size(); ++i)
156  pathSet.insert(setSystemDrive(tokens[i]));
157  return pathSet;
158 }
#define D
string setSystemDrive(const string &path)

§ testExpandPathmask()

void testExpandPathmask ( )

Definition at line 161 of file FilesystemTest.cpp.

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

Referenced by main().

162 {
163  char* systemDriveEnv = ::getenv("SystemDrive"); // usually "C:" on Windows
164  if (systemDriveEnv)
165  systemDrive = systemDriveEnv;
166 
167  // create a filesystem tree for testing
168  createTestPath();
169 
170  for (int i=0; i < testPathmaskArraySize; ++i)
171  {
172  try
173  {
174  vector<bfs::path> matchingPaths;
175  expand_pathmask(setSystemDrive(testPathmaskArray[i].pathmask), matchingPaths);
176 
177  set<bfs::path> targetPathSet = parsePathArray(testPathmaskArray[i].pathnameArray);
178  unit_assert(matchingPaths.size() == targetPathSet.size());
179 
180  set<bfs::path> matchingPathSet(matchingPaths.begin(), matchingPaths.end());
181  vector<bfs::path> xorSet;
182  std::set_symmetric_difference(targetPathSet.begin(), targetPathSet.end(),
183  matchingPathSet.begin(), matchingPathSet.end(),
184  xorSet.end());
185  unit_assert(xorSet.empty());
186  }
187  catch (exception& e)
188  {
189  cout << "Unit test on pathmask \"" << setSystemDrive(testPathmaskArray[i].pathmask) << "\" failed:\n"
190  << e.what() << endl;
191  }
192  }
193 
194  // special test of wildcard in the root (on Windows)
195  vector<bfs::path> matchingPaths;
196  expand_pathmask(setSystemDrive(ABS"*"), matchingPaths);
197  unit_assert(find(matchingPaths.begin(), matchingPaths.end(), setSystemDrive(ABS"pwiz_foofoo_test")) != matchingPaths.end());
198  unit_assert(find(matchingPaths.begin(), matchingPaths.end(), setSystemDrive(ABS"pwiz_foo_test")) != matchingPaths.end());
199  unit_assert(find(matchingPaths.begin(), matchingPaths.end(), setSystemDrive(ABS"pwiz_bar_test")) != matchingPaths.end());
200 
201  // cleanup test tree
202  deleteTestPath();
203 }
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...
void deleteTestPath()
const int testPathmaskArraySize
string systemDrive
string setSystemDrive(const string &path)
#define ABS
const TestPathmask testPathmaskArray[]
void createTestPath()
set< bfs::path > parsePathArray(const string &pathArray)
#define unit_assert(x)
Definition: unit.hpp:85

§ testAbbreviateByteSize()

void testAbbreviateByteSize ( )

Definition at line 206 of file FilesystemTest.cpp.

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

Referenced by main().

207 {
208  unit_assert(abbreviate_byte_size(1) == "1 B");
209  unit_assert(abbreviate_byte_size(999) == "999 B");
210  unit_assert(abbreviate_byte_size(1000) == "1 KB");
211  unit_assert(abbreviate_byte_size(999999) == "999 KB");
212  unit_assert(abbreviate_byte_size(1000000) == "1 MB");
213  unit_assert(abbreviate_byte_size(999999999) == "999 MB");
214  unit_assert(abbreviate_byte_size(1000000000) == "1 GB");
215 
219  unit_assert(abbreviate_byte_size((1024 << 10)-1, ByteSizeAbbreviation_IEC) == "1023 KiB");
220  unit_assert(abbreviate_byte_size((1024 << 10), ByteSizeAbbreviation_IEC) == "1 MiB");
221  unit_assert(abbreviate_byte_size((1024 << 20)-1, ByteSizeAbbreviation_IEC) == "1023 MiB");
222  unit_assert(abbreviate_byte_size((1024 << 20), ByteSizeAbbreviation_IEC) == "1 GiB");
223 
227  unit_assert(abbreviate_byte_size((1024 << 10)-1, ByteSizeAbbreviation_JEDEC) == "1023 KB");
229  unit_assert(abbreviate_byte_size((1024 << 20)-1, ByteSizeAbbreviation_JEDEC) == "1023 MB");
231 }
sizes are treated as multiples of 2; abbreviations are: GiB (Gibibyte), MiB (Mebibyte), KiB (Kibibyte), B (byte)
Definition: Filesystem.hpp:86
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:90
#define unit_assert(x)
Definition: unit.hpp:85

§ main()

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

Definition at line 234 of file FilesystemTest.cpp.

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

235 {
236  TEST_PROLOG(argc, argv)
237 
238  try
239  {
242  }
243  catch (exception& e)
244  {
245  TEST_FAILED(e.what())
246  }
247  catch (...)
248  {
249  TEST_FAILED("Caught unknown exception.")
250  }
251 
252  deleteTestPath();
254 }
#define TEST_EPILOG
Definition: unit.hpp:182
void testAbbreviateByteSize()
void deleteTestPath()
#define TEST_FAILED(x)
Definition: unit.hpp:176
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174
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().