PageRenderTime 93ms CodeModel.GetById 52ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llvfs/tests/lldir_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 423 lines | 270 code | 85 blank | 68 comment | 15 complexity | b2f56e7c1ba043f2dc2f65c76016c3bb MD5 | raw file
  1/**
  2 * @file lldir_test.cpp
  3 * @date 2008-05
  4 * @brief LLDir test cases.
  5 *
  6 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include "linden_common.h"
 29
 30#include "../lldir.h"
 31#include "../lldiriterator.h"
 32
 33#include "../test/lltut.h"
 34
 35
 36namespace tut
 37{
 38	struct LLDirTest
 39        {
 40        };
 41        typedef test_group<LLDirTest> LLDirTest_t;
 42        typedef LLDirTest_t::object LLDirTest_object_t;
 43        tut::LLDirTest_t tut_LLDirTest("LLDir");
 44
 45	template<> template<>
 46	void LLDirTest_object_t::test<1>()
 47		// getDirDelimiter
 48	{
 49		ensure("getDirDelimiter", !gDirUtilp->getDirDelimiter().empty());
 50	}
 51
 52	template<> template<>
 53	void LLDirTest_object_t::test<2>()
 54		// getBaseFileName
 55	{
 56		std::string delim = gDirUtilp->getDirDelimiter();
 57		std::string rawFile = "foo";
 58		std::string rawFileExt = "foo.bAr";
 59		std::string rawFileNullExt = "foo.";
 60		std::string rawExt = ".bAr";
 61		std::string rawDot = ".";
 62		std::string pathNoExt = "aa" + delim + "bb" + delim + "cc" + delim + "dd" + delim + "ee";
 63		std::string pathExt = pathNoExt + ".eXt";
 64		std::string dottedPathNoExt = "aa" + delim + "bb" + delim + "cc.dd" + delim + "ee";
 65		std::string dottedPathExt = dottedPathNoExt + ".eXt";
 66
 67		// foo[.bAr]
 68
 69		ensure_equals("getBaseFileName/r-no-ext/no-strip-exten",
 70			      gDirUtilp->getBaseFileName(rawFile, false),
 71			      "foo");
 72
 73		ensure_equals("getBaseFileName/r-no-ext/strip-exten",
 74			      gDirUtilp->getBaseFileName(rawFile, true),
 75			      "foo");
 76
 77		ensure_equals("getBaseFileName/r-ext/no-strip-exten",
 78			      gDirUtilp->getBaseFileName(rawFileExt, false),
 79			      "foo.bAr");
 80
 81		ensure_equals("getBaseFileName/r-ext/strip-exten",
 82			      gDirUtilp->getBaseFileName(rawFileExt, true),
 83			      "foo");
 84
 85		// foo.
 86
 87		ensure_equals("getBaseFileName/rn-no-ext/no-strip-exten",
 88			      gDirUtilp->getBaseFileName(rawFileNullExt, false),
 89			      "foo.");
 90
 91		ensure_equals("getBaseFileName/rn-no-ext/strip-exten",
 92			      gDirUtilp->getBaseFileName(rawFileNullExt, true),
 93			      "foo");
 94
 95		// .bAr
 96		// interesting case - with no basename, this IS the basename, not the extension.
 97
 98		ensure_equals("getBaseFileName/e-ext/no-strip-exten",
 99			      gDirUtilp->getBaseFileName(rawExt, false),
100			      ".bAr");
101
102		ensure_equals("getBaseFileName/e-ext/strip-exten",
103			      gDirUtilp->getBaseFileName(rawExt, true),
104			      ".bAr");
105
106		// .
107
108		ensure_equals("getBaseFileName/d/no-strip-exten",
109			      gDirUtilp->getBaseFileName(rawDot, false),
110			      ".");
111
112		ensure_equals("getBaseFileName/d/strip-exten",
113			      gDirUtilp->getBaseFileName(rawDot, true),
114			      ".");
115
116		// aa/bb/cc/dd/ee[.eXt]
117
118		ensure_equals("getBaseFileName/no-ext/no-strip-exten",
119			      gDirUtilp->getBaseFileName(pathNoExt, false),
120			      "ee");
121
122		ensure_equals("getBaseFileName/no-ext/strip-exten",
123			      gDirUtilp->getBaseFileName(pathNoExt, true),
124			      "ee");
125
126		ensure_equals("getBaseFileName/ext/no-strip-exten",
127			      gDirUtilp->getBaseFileName(pathExt, false),
128			      "ee.eXt");
129
130		ensure_equals("getBaseFileName/ext/strip-exten",
131			      gDirUtilp->getBaseFileName(pathExt, true),
132			      "ee");
133
134		// aa/bb/cc.dd/ee[.eXt]
135
136		ensure_equals("getBaseFileName/d-no-ext/no-strip-exten",
137			      gDirUtilp->getBaseFileName(dottedPathNoExt, false),
138			      "ee");
139
140		ensure_equals("getBaseFileName/d-no-ext/strip-exten",
141			      gDirUtilp->getBaseFileName(dottedPathNoExt, true),
142			      "ee");
143
144		ensure_equals("getBaseFileName/d-ext/no-strip-exten",
145			      gDirUtilp->getBaseFileName(dottedPathExt, false),
146			      "ee.eXt");
147
148		ensure_equals("getBaseFileName/d-ext/strip-exten",
149			      gDirUtilp->getBaseFileName(dottedPathExt, true),
150			      "ee");
151	}
152
153	template<> template<>
154	void LLDirTest_object_t::test<3>()
155		// getDirName
156	{
157		std::string delim = gDirUtilp->getDirDelimiter();
158		std::string rawFile = "foo";
159		std::string rawFileExt = "foo.bAr";
160		std::string pathNoExt = "aa" + delim + "bb" + delim + "cc" + delim + "dd" + delim + "ee";
161		std::string pathExt = pathNoExt + ".eXt";
162		std::string dottedPathNoExt = "aa" + delim + "bb" + delim + "cc.dd" + delim + "ee";
163		std::string dottedPathExt = dottedPathNoExt + ".eXt";
164
165		// foo[.bAr]
166
167		ensure_equals("getDirName/r-no-ext",
168			      gDirUtilp->getDirName(rawFile),
169			      "");
170
171		ensure_equals("getDirName/r-ext",
172			      gDirUtilp->getDirName(rawFileExt),
173			      "");
174
175		// aa/bb/cc/dd/ee[.eXt]
176
177		ensure_equals("getDirName/no-ext",
178			      gDirUtilp->getDirName(pathNoExt),
179			      "aa" + delim + "bb" + delim + "cc" + delim + "dd");
180
181		ensure_equals("getDirName/ext",
182			      gDirUtilp->getDirName(pathExt),
183			      "aa" + delim + "bb" + delim + "cc" + delim + "dd");
184
185		// aa/bb/cc.dd/ee[.eXt]
186
187		ensure_equals("getDirName/d-no-ext",
188			      gDirUtilp->getDirName(dottedPathNoExt),
189			      "aa" + delim + "bb" + delim + "cc.dd");
190
191		ensure_equals("getDirName/d-ext",
192			      gDirUtilp->getDirName(dottedPathExt),
193			      "aa" + delim + "bb" + delim + "cc.dd");
194	}
195
196	template<> template<>
197	void LLDirTest_object_t::test<4>()
198		// getExtension
199	{
200		std::string delim = gDirUtilp->getDirDelimiter();
201		std::string rawFile = "foo";
202		std::string rawFileExt = "foo.bAr";
203		std::string rawFileNullExt = "foo.";
204		std::string rawExt = ".bAr";
205		std::string rawDot = ".";
206		std::string pathNoExt = "aa" + delim + "bb" + delim + "cc" + delim + "dd" + delim + "ee";
207		std::string pathExt = pathNoExt + ".eXt";
208		std::string dottedPathNoExt = "aa" + delim + "bb" + delim + "cc.dd" + delim + "ee";
209		std::string dottedPathExt = dottedPathNoExt + ".eXt";
210
211		// foo[.bAr]
212
213		ensure_equals("getExtension/r-no-ext",
214			      gDirUtilp->getExtension(rawFile),
215			      "");
216
217		ensure_equals("getExtension/r-ext",
218			      gDirUtilp->getExtension(rawFileExt),
219			      "bar");
220
221		// foo.
222
223		ensure_equals("getExtension/rn-no-ext",
224			      gDirUtilp->getExtension(rawFileNullExt),
225			      "");
226
227		// .bAr
228		// interesting case - with no basename, this IS the basename, not the extension.
229
230		ensure_equals("getExtension/e-ext",
231			      gDirUtilp->getExtension(rawExt),
232			      "");
233
234		// .
235
236		ensure_equals("getExtension/d",
237			      gDirUtilp->getExtension(rawDot),
238			      "");
239
240		// aa/bb/cc/dd/ee[.eXt]
241
242		ensure_equals("getExtension/no-ext",
243			      gDirUtilp->getExtension(pathNoExt),
244			      "");
245
246		ensure_equals("getExtension/ext",
247			      gDirUtilp->getExtension(pathExt),
248			      "ext");
249
250		// aa/bb/cc.dd/ee[.eXt]
251
252		ensure_equals("getExtension/d-no-ext",
253			      gDirUtilp->getExtension(dottedPathNoExt),
254			      "");
255
256		ensure_equals("getExtension/d-ext",
257			      gDirUtilp->getExtension(dottedPathExt),
258			      "ext");
259	}
260
261   std::string makeTestFile( const std::string& dir, const std::string& file )
262   {
263      std::string path = dir + file;
264      LLFILE* handle = LLFile::fopen( path, "w" );
265      ensure("failed to open test file '"+path+"'", handle != NULL );
266      // Harbison & Steele, 4th ed., p. 366: "If an error occurs, fputs
267      // returns EOF; otherwise, it returns some other, nonnegative value."
268      ensure("failed to write to test file '"+path+"'", EOF != fputs("test file", handle) );
269      fclose(handle);
270      return path;
271   }
272
273   std::string makeTestDir( const std::string& dirbase )
274   {
275      int counter;
276      std::string uniqueDir;
277      bool foundUnused;
278      std::string delim = gDirUtilp->getDirDelimiter();
279      
280      for (counter=0, foundUnused=false; !foundUnused; counter++ )
281      {
282         char counterStr[3];
283         sprintf(counterStr, "%02d", counter);
284         uniqueDir = dirbase + counterStr;
285         foundUnused = ! ( LLFile::isdir(uniqueDir) || LLFile::isfile(uniqueDir) );
286      }
287      ensure("test directory '" + uniqueDir + "' creation failed", !LLFile::mkdir(uniqueDir));
288      
289      return uniqueDir + delim; // HACK - apparently, the trailing delimiter is needed...
290   }
291
292   static const char* DirScanFilename[5] = { "file1.abc", "file2.abc", "file1.xyz", "file2.xyz", "file1.mno" };
293
294   void scanTest(const std::string& directory, const std::string& pattern, bool correctResult[5])
295   {
296
297      // Scan directory and see if any file1.* files are found
298      std::string scanResult;
299      int   found = 0;
300      bool  filesFound[5] = { false, false, false, false, false };
301      //std::cerr << "searching '"+directory+"' for '"+pattern+"'\n";
302
303      LLDirIterator iter(directory, pattern);
304      while ( found <= 5 && iter.next(scanResult) )
305      {
306         found++;
307         //std::cerr << "  found '"+scanResult+"'\n";
308         int check;
309         for (check=0; check < 5 && ! ( scanResult == DirScanFilename[check] ); check++)
310         {
311         }
312         // check is now either 5 (not found) or the index of the matching name
313         if (check < 5)
314         {
315            ensure( "found file '"+(std::string)DirScanFilename[check]+"' twice", ! filesFound[check] );
316            filesFound[check] = true;
317         }
318         else // check is 5 - should not happen
319         {
320            fail( "found unknown file '"+scanResult+"'");
321         }
322      }
323      for (int i=0; i<5; i++)
324      {
325         if (correctResult[i])
326         {
327            ensure("scan of '"+directory+"' using '"+pattern+"' did not return '"+DirScanFilename[i]+"'", filesFound[i]);
328         }
329         else
330         {
331            ensure("scan of '"+directory+"' using '"+pattern+"' incorrectly returned '"+DirScanFilename[i]+"'", !filesFound[i]);
332         }
333      }
334   }
335   
336   template<> template<>
337   void LLDirTest_object_t::test<5>()
338      // LLDirIterator::next
339   {
340      std::string delim = gDirUtilp->getDirDelimiter();
341      std::string dirTemp = LLFile::tmpdir();
342
343      // Create the same 5 file names of the two directories
344
345      std::string dir1 = makeTestDir(dirTemp + "LLDirIterator");
346      std::string dir2 = makeTestDir(dirTemp + "LLDirIterator");
347      std::string dir1files[5];
348      std::string dir2files[5];
349      for (int i=0; i<5; i++)
350      {
351         dir1files[i] = makeTestFile(dir1, DirScanFilename[i]);
352         dir2files[i] = makeTestFile(dir2, DirScanFilename[i]);
353      }
354
355      // Scan dir1 and see if each of the 5 files is found exactly once
356      bool expected1[5] = { true, true, true, true, true };
357      scanTest(dir1, "*", expected1);
358
359      // Scan dir2 and see if only the 2 *.xyz files are found
360      bool  expected2[5] = { false, false, true, true, false };
361      scanTest(dir1, "*.xyz", expected2);
362
363      // Scan dir2 and see if only the 1 *.mno file is found
364      bool  expected3[5] = { false, false, false, false, true };
365      scanTest(dir2, "*.mno", expected3);
366
367      // Scan dir1 and see if any *.foo files are found
368      bool  expected4[5] = { false, false, false, false, false };
369      scanTest(dir1, "*.foo", expected4);
370
371      // Scan dir1 and see if any file1.* files are found
372      bool  expected5[5] = { true, false, true, false, true };
373      scanTest(dir1, "file1.*", expected5);
374
375      // Scan dir1 and see if any file1.* files are found
376      bool  expected6[5] = { true, true, false, false, false };
377      scanTest(dir1, "file?.abc", expected6);
378
379      // Scan dir2 and see if any file?.x?z files are found
380      bool  expected7[5] = { false, false, true, true, false };
381      scanTest(dir2, "file?.x?z", expected7);
382
383      // Scan dir2 and see if any file?.??c files are found
384      bool  expected8[5] = { true, true, false, false, false };
385      scanTest(dir2, "file?.??c", expected8);
386      scanTest(dir2, "*.??c", expected8);
387
388      // Scan dir1 and see if any *.?n? files are found
389      bool  expected9[5] = { false, false, false, false, true };
390      scanTest(dir1, "*.?n?", expected9);
391
392      // Scan dir1 and see if any *.???? files are found
393      bool  expected10[5] = { false, false, false, false, false };
394      scanTest(dir1, "*.????", expected10);
395
396      // Scan dir1 and see if any ?????.* files are found
397      bool  expected11[5] = { true, true, true, true, true };
398      scanTest(dir1, "?????.*", expected11);
399
400      // Scan dir1 and see if any ??l??.xyz files are found
401      bool  expected12[5] = { false, false, true, true, false };
402      scanTest(dir1, "??l??.xyz", expected12);
403
404      bool expected13[5] = { true, false, true, false, false };
405      scanTest(dir1, "file1.{abc,xyz}", expected13);
406
407      bool expected14[5] = { true, true, false, false, false };
408      scanTest(dir1, "file[0-9].abc", expected14);
409
410      bool expected15[5] = { true, true, false, false, false };
411      scanTest(dir1, "file[!a-z].abc", expected15);
412
413      // clean up all test files and directories
414      for (int i=0; i<5; i++)
415      {
416         LLFile::remove(dir1files[i]);
417         LLFile::remove(dir2files[i]);
418      }
419      LLFile::rmdir(dir1);
420      LLFile::rmdir(dir2);
421   }
422}
423