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.

◆ ABS

#define ABS   "./"

Definition at line 48 of file FilesystemTest.cpp.

◆ REL

#define REL   "./relative"

Definition at line 49 of file FilesystemTest.cpp.

◆ A

#define A   "/"

Definition at line 50 of file FilesystemTest.cpp.

◆ D

#define D   ":"

Definition at line 51 of file FilesystemTest.cpp.

Function Documentation

◆ setSystemDrive()

string setSystemDrive ( const string &  path)

Definition at line 55 of file FilesystemTest.cpp.

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

References systemDrive.

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

◆ create_file()

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

Definition at line 116 of file FilesystemTest.cpp.

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

References SYSTEMCATEGORY.

Referenced by createTestPath().

◆ createTestPath()

void createTestPath ( )

Definition at line 125 of file FilesystemTest.cpp.

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 int testPathContentPairArraySize
#define ABS
string setSystemDrive(const string &path)
const char * testPathContentPairArray[]
void create_file(const bfs::path &ph, const string &contents)
#define REL
#define unit_assert(x)
Definition unit.hpp:85

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

Referenced by testExpandPathmask().

◆ deleteTestPath()

void deleteTestPath ( )

Definition at line 155 of file FilesystemTest.cpp.

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}

References setSystemDrive(), testPathContentPairArray, and testPathContentPairArraySize.

Referenced by main(), and testExpandPathmask().

◆ parsePathArray()

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

Definition at line 163 of file FilesystemTest.cpp.

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

References D, and setSystemDrive().

Referenced by testExpandPathmask().

◆ testExpandPathmask()

void testExpandPathmask ( )

Definition at line 175 of file FilesystemTest.cpp.

176{
177 char* systemDriveEnv = ::getenv("SystemDrive"); // usually "C:" on Windows
178 if (systemDriveEnv)
179 systemDrive = systemDriveEnv;
180
181 // create a filesystem tree for testing
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
229}
void createTestPath()
const int testPathmaskArraySize
set< bfs::path > parsePathArray(const string &pathArray)
const TestPathmask testPathmaskArray[]
void deleteTestPath()
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

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

Referenced by main().

◆ testAbbreviateByteSize()

void testAbbreviateByteSize ( )

Definition at line 232 of file FilesystemTest.cpp.

233{
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");
247 unit_assert(abbreviate_byte_size((1024 << 20)-1, ByteSizeAbbreviation_IEC) == "1023 MiB");
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}
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
@ ByteSizeAbbreviation_JEDEC
sizes are treated as multiples of 2; abbreviations are: GB (Gigabyte), MB (Megabyte),...
@ ByteSizeAbbreviation_IEC
sizes are treated as multiples of 2; abbreviations are: GiB (Gibibyte), MiB (Mebibyte),...

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

Referenced by main().

◆ main()

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

Definition at line 260 of file FilesystemTest.cpp.

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
280}
void testAbbreviateByteSize()
void testExpandPathmask()
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175

References deleteTestPath(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testAbbreviateByteSize(), and 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

Definition at line 60 of file FilesystemTest.cpp.

61{
62 // path content (empty indicates directory)
63 ABS "pwiz_foofoo_test", "root file",
64 ABS "pwiz_foo_test", "",
65 ABS "pwiz_foo_test" A "this file", "has content",
66 ABS "pwiz_foo_test" A "this dir has", "",
67 ABS "pwiz_foo_test" A "this dir has" A "a test file", "with content",
68 ABS "pwiz_bar_test", "",
69 ABS "pwiz_bar_test" A "some file", "12345",
70 ABS "pwiz_bar_test" A "some dir", "",
71
72 REL "pwiz_foofoo_test", "root file",
73 REL "pwiz_foo_test", "",
74 REL "pwiz_foo_test" A "this file", "has content",
75 REL "pwiz_foo_test" A "this dir has", "",
76 REL "pwiz_foo_test" A "this dir has" A "a test file", "with content",
77 REL "pwiz_bar_test", "",
78 REL "pwiz_bar_test" A "some file", "12345",
79 REL "pwiz_bar_test" A "some dir", ""
80};

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" }
}

Definition at line 93 of file FilesystemTest.cpp.

94{
95 { ABS "pwiz_f??f??_test", ABS "pwiz_foofoo_test" },
96 { ABS "pwiz_???_test", ABS "pwiz_foo_test" D ABS "pwiz_bar_test" },
97 { ABS "pwiz_f*o_test", ABS "pwiz_foo_test" D ABS "pwiz_foofoo_test" },
98 { ABS "pwiz_foobar_test", "" },
99 { ABS "pwiz_foo_test" A "no*hit", "" },
100 { ABS "pwiz_foo_test" A "*", ABS "pwiz_foo_test" A "this file" D ABS "pwiz_foo_test" A "this dir has" },
101 { ABS"pwiz_foo_test" A "this *", ABS "pwiz_foo_test" A "this file" D ABS "pwiz_foo_test" A "this dir has" },
102
103 { REL "pwiz_f??f??_test", REL "pwiz_foofoo_test" },
104 { REL "pwiz_???_test", REL "pwiz_foo_test" D REL "pwiz_bar_test" },
105 { REL "pwiz_f*o_test", REL "pwiz_foo_test" D REL "pwiz_foofoo_test" },
106 { REL "pwiz_foobar_test", "" },
107 { REL "pwiz_foo_test" A "no*hit", "" },
108 { REL "pwiz_foo_test" A "*", REL "pwiz_foo_test" A "this file" D REL "pwiz_foo_test" A "this dir has" },
109 { REL "pwiz_foo_test" A "this *", REL "pwiz_foo_test" A "this file" D REL "pwiz_foo_test" A "this dir has" }
110};

Referenced by testExpandPathmask().

◆ testPathmaskArraySize

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

Definition at line 113 of file FilesystemTest.cpp.

Referenced by testExpandPathmask().