ProteoWizard
FilesystemTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: FilesystemTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2008 Spielberg Family Center for Applied Proteomics
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 // Copyright 2008 Vanderbilt University - Nashville, TN 37232
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // Unless required by applicable law or agreed to in writing, software
18 // distributed under the License is distributed on an "AS IS" BASIS,
19 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 // See the License for the specific language governing permissions and
21 // limitations under the License.
22 //
23 
24 #include "Std.hpp"
25 #include "Filesystem.hpp"
26 #include "unit.hpp"
27 
28 
29 using namespace pwiz::util;
30 using boost::system::error_code;
31 using boost::system::system_category;
32 using boost::system::system_error;
33 
34 #if (BOOST_VERSION/100) >= 1044
35 # define SYSTEMCATEGORY system_category()
36 #else // exported library symbol used to be a global variable, now its a function. Okeedokee, then.
37 # define SYSTEMCATEGORY system_category
38 #endif
39 
40 
41 // platform-specific path elements
42 #ifdef WIN32
43 # define ABS "%SD%\\" // test at %SystemDrive% because FindFile behaves a little tricky there
44 # define REL ".\\relative"
45 # define A "\\" // both slash types should work
46 # define D ";" // path separator
47 #else
48 # define ABS "./" // POSIX filesystems don't have the same problem,
49 # define REL "./relative" // so all tests are relative (avoids permission issues)
50 # define A "/"
51 # define D ":"
52 #endif
53 
54 string systemDrive;
55 string setSystemDrive(const string& path)
56 {
57  return bal::replace_all_copy(path, "%SD%", systemDrive);
58 }
59 
60 const char* testPathContentPairArray[] =
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 };
81 
82 
83 const int testPathContentPairArraySize = sizeof(testPathContentPairArray) / sizeof(const char*);
84 
85 
87 {
88  const char* pathmask; // mask to be passed to expand_pathmask()
89  const char* pathnameArray; // paths that should match with expand_pathmask()
90 };
91 
92 
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 };
111 
112 
114 
115 
116 void create_file(const bfs::path& ph, const string& contents)
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 }
123 
124 
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 }
139 
140 
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 }
147 
148 
149 set<bfs::path> parsePathArray(const string& pathArray)
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 }
159 
160 
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 }
204 
205 
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 }
232 
233 
234 int main(int argc, const char* argv[])
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 }