PageRenderTime 41ms CodeModel.GetById 23ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  28. #include "../lldir.h"
  29. #include "../lldiriterator.h"
  30. #include "../test/lltut.h"
  31. namespace tut
  32. {
  33. struct LLDirTest
  34. {
  35. };
  36. typedef test_group<LLDirTest> LLDirTest_t;
  37. typedef LLDirTest_t::object LLDirTest_object_t;
  38. tut::LLDirTest_t tut_LLDirTest("LLDir");
  39. template<> template<>
  40. void LLDirTest_object_t::test<1>()
  41. // getDirDelimiter
  42. {
  43. ensure("getDirDelimiter", !gDirUtilp->getDirDelimiter().empty());
  44. }
  45. template<> template<>
  46. void LLDirTest_object_t::test<2>()
  47. // getBaseFileName
  48. {
  49. std::string delim = gDirUtilp->getDirDelimiter();
  50. std::string rawFile = "foo";
  51. std::string rawFileExt = "foo.bAr";
  52. std::string rawFileNullExt = "foo.";
  53. std::string rawExt = ".bAr";
  54. std::string rawDot = ".";
  55. std::string pathNoExt = "aa" + delim + "bb" + delim + "cc" + delim + "dd" + delim + "ee";
  56. std::string pathExt = pathNoExt + ".eXt";
  57. std::string dottedPathNoExt = "aa" + delim + "bb" + delim + "cc.dd" + delim + "ee";
  58. std::string dottedPathExt = dottedPathNoExt + ".eXt";
  59. // foo[.bAr]
  60. ensure_equals("getBaseFileName/r-no-ext/no-strip-exten",
  61. gDirUtilp->getBaseFileName(rawFile, false),
  62. "foo");
  63. ensure_equals("getBaseFileName/r-no-ext/strip-exten",
  64. gDirUtilp->getBaseFileName(rawFile, true),
  65. "foo");
  66. ensure_equals("getBaseFileName/r-ext/no-strip-exten",
  67. gDirUtilp->getBaseFileName(rawFileExt, false),
  68. "foo.bAr");
  69. ensure_equals("getBaseFileName/r-ext/strip-exten",
  70. gDirUtilp->getBaseFileName(rawFileExt, true),
  71. "foo");
  72. // foo.
  73. ensure_equals("getBaseFileName/rn-no-ext/no-strip-exten",
  74. gDirUtilp->getBaseFileName(rawFileNullExt, false),
  75. "foo.");
  76. ensure_equals("getBaseFileName/rn-no-ext/strip-exten",
  77. gDirUtilp->getBaseFileName(rawFileNullExt, true),
  78. "foo");
  79. // .bAr
  80. // interesting case - with no basename, this IS the basename, not the extension.
  81. ensure_equals("getBaseFileName/e-ext/no-strip-exten",
  82. gDirUtilp->getBaseFileName(rawExt, false),
  83. ".bAr");
  84. ensure_equals("getBaseFileName/e-ext/strip-exten",
  85. gDirUtilp->getBaseFileName(rawExt, true),
  86. ".bAr");
  87. // .
  88. ensure_equals("getBaseFileName/d/no-strip-exten",
  89. gDirUtilp->getBaseFileName(rawDot, false),
  90. ".");
  91. ensure_equals("getBaseFileName/d/strip-exten",
  92. gDirUtilp->getBaseFileName(rawDot, true),
  93. ".");
  94. // aa/bb/cc/dd/ee[.eXt]
  95. ensure_equals("getBaseFileName/no-ext/no-strip-exten",
  96. gDirUtilp->getBaseFileName(pathNoExt, false),
  97. "ee");
  98. ensure_equals("getBaseFileName/no-ext/strip-exten",
  99. gDirUtilp->getBaseFileName(pathNoExt, true),
  100. "ee");
  101. ensure_equals("getBaseFileName/ext/no-strip-exten",
  102. gDirUtilp->getBaseFileName(pathExt, false),
  103. "ee.eXt");
  104. ensure_equals("getBaseFileName/ext/strip-exten",
  105. gDirUtilp->getBaseFileName(pathExt, true),
  106. "ee");
  107. // aa/bb/cc.dd/ee[.eXt]
  108. ensure_equals("getBaseFileName/d-no-ext/no-strip-exten",
  109. gDirUtilp->getBaseFileName(dottedPathNoExt, false),
  110. "ee");
  111. ensure_equals("getBaseFileName/d-no-ext/strip-exten",
  112. gDirUtilp->getBaseFileName(dottedPathNoExt, true),
  113. "ee");
  114. ensure_equals("getBaseFileName/d-ext/no-strip-exten",
  115. gDirUtilp->getBaseFileName(dottedPathExt, false),
  116. "ee.eXt");
  117. ensure_equals("getBaseFileName/d-ext/strip-exten",
  118. gDirUtilp->getBaseFileName(dottedPathExt, true),
  119. "ee");
  120. }
  121. template<> template<>
  122. void LLDirTest_object_t::test<3>()
  123. // getDirName
  124. {
  125. std::string delim = gDirUtilp->getDirDelimiter();
  126. std::string rawFile = "foo";
  127. std::string rawFileExt = "foo.bAr";
  128. std::string pathNoExt = "aa" + delim + "bb" + delim + "cc" + delim + "dd" + delim + "ee";
  129. std::string pathExt = pathNoExt + ".eXt";
  130. std::string dottedPathNoExt = "aa" + delim + "bb" + delim + "cc.dd" + delim + "ee";
  131. std::string dottedPathExt = dottedPathNoExt + ".eXt";
  132. // foo[.bAr]
  133. ensure_equals("getDirName/r-no-ext",
  134. gDirUtilp->getDirName(rawFile),
  135. "");
  136. ensure_equals("getDirName/r-ext",
  137. gDirUtilp->getDirName(rawFileExt),
  138. "");
  139. // aa/bb/cc/dd/ee[.eXt]
  140. ensure_equals("getDirName/no-ext",
  141. gDirUtilp->getDirName(pathNoExt),
  142. "aa" + delim + "bb" + delim + "cc" + delim + "dd");
  143. ensure_equals("getDirName/ext",
  144. gDirUtilp->getDirName(pathExt),
  145. "aa" + delim + "bb" + delim + "cc" + delim + "dd");
  146. // aa/bb/cc.dd/ee[.eXt]
  147. ensure_equals("getDirName/d-no-ext",
  148. gDirUtilp->getDirName(dottedPathNoExt),
  149. "aa" + delim + "bb" + delim + "cc.dd");
  150. ensure_equals("getDirName/d-ext",
  151. gDirUtilp->getDirName(dottedPathExt),
  152. "aa" + delim + "bb" + delim + "cc.dd");
  153. }
  154. template<> template<>
  155. void LLDirTest_object_t::test<4>()
  156. // getExtension
  157. {
  158. std::string delim = gDirUtilp->getDirDelimiter();
  159. std::string rawFile = "foo";
  160. std::string rawFileExt = "foo.bAr";
  161. std::string rawFileNullExt = "foo.";
  162. std::string rawExt = ".bAr";
  163. std::string rawDot = ".";
  164. std::string pathNoExt = "aa" + delim + "bb" + delim + "cc" + delim + "dd" + delim + "ee";
  165. std::string pathExt = pathNoExt + ".eXt";
  166. std::string dottedPathNoExt = "aa" + delim + "bb" + delim + "cc.dd" + delim + "ee";
  167. std::string dottedPathExt = dottedPathNoExt + ".eXt";
  168. // foo[.bAr]
  169. ensure_equals("getExtension/r-no-ext",
  170. gDirUtilp->getExtension(rawFile),
  171. "");
  172. ensure_equals("getExtension/r-ext",
  173. gDirUtilp->getExtension(rawFileExt),
  174. "bar");
  175. // foo.
  176. ensure_equals("getExtension/rn-no-ext",
  177. gDirUtilp->getExtension(rawFileNullExt),
  178. "");
  179. // .bAr
  180. // interesting case - with no basename, this IS the basename, not the extension.
  181. ensure_equals("getExtension/e-ext",
  182. gDirUtilp->getExtension(rawExt),
  183. "");
  184. // .
  185. ensure_equals("getExtension/d",
  186. gDirUtilp->getExtension(rawDot),
  187. "");
  188. // aa/bb/cc/dd/ee[.eXt]
  189. ensure_equals("getExtension/no-ext",
  190. gDirUtilp->getExtension(pathNoExt),
  191. "");
  192. ensure_equals("getExtension/ext",
  193. gDirUtilp->getExtension(pathExt),
  194. "ext");
  195. // aa/bb/cc.dd/ee[.eXt]
  196. ensure_equals("getExtension/d-no-ext",
  197. gDirUtilp->getExtension(dottedPathNoExt),
  198. "");
  199. ensure_equals("getExtension/d-ext",
  200. gDirUtilp->getExtension(dottedPathExt),
  201. "ext");
  202. }
  203. std::string makeTestFile( const std::string& dir, const std::string& file )
  204. {
  205. std::string path = dir + file;
  206. LLFILE* handle = LLFile::fopen( path, "w" );
  207. ensure("failed to open test file '"+path+"'", handle != NULL );
  208. // Harbison & Steele, 4th ed., p. 366: "If an error occurs, fputs
  209. // returns EOF; otherwise, it returns some other, nonnegative value."
  210. ensure("failed to write to test file '"+path+"'", EOF != fputs("test file", handle) );
  211. fclose(handle);
  212. return path;
  213. }
  214. std::string makeTestDir( const std::string& dirbase )
  215. {
  216. int counter;
  217. std::string uniqueDir;
  218. bool foundUnused;
  219. std::string delim = gDirUtilp->getDirDelimiter();
  220. for (counter=0, foundUnused=false; !foundUnused; counter++ )
  221. {
  222. char counterStr[3];
  223. sprintf(counterStr, "%02d", counter);
  224. uniqueDir = dirbase + counterStr;
  225. foundUnused = ! ( LLFile::isdir(uniqueDir) || LLFile::isfile(uniqueDir) );
  226. }
  227. ensure("test directory '" + uniqueDir + "' creation failed", !LLFile::mkdir(uniqueDir));
  228. return uniqueDir + delim; // HACK - apparently, the trailing delimiter is needed...
  229. }
  230. static const char* DirScanFilename[5] = { "file1.abc", "file2.abc", "file1.xyz", "file2.xyz", "file1.mno" };
  231. void scanTest(const std::string& directory, const std::string& pattern, bool correctResult[5])
  232. {
  233. // Scan directory and see if any file1.* files are found
  234. std::string scanResult;
  235. int found = 0;
  236. bool filesFound[5] = { false, false, false, false, false };
  237. //std::cerr << "searching '"+directory+"' for '"+pattern+"'\n";
  238. LLDirIterator iter(directory, pattern);
  239. while ( found <= 5 && iter.next(scanResult) )
  240. {
  241. found++;
  242. //std::cerr << " found '"+scanResult+"'\n";
  243. int check;
  244. for (check=0; check < 5 && ! ( scanResult == DirScanFilename[check] ); check++)
  245. {
  246. }
  247. // check is now either 5 (not found) or the index of the matching name
  248. if (check < 5)
  249. {
  250. ensure( "found file '"+(std::string)DirScanFilename[check]+"' twice", ! filesFound[check] );
  251. filesFound[check] = true;
  252. }
  253. else // check is 5 - should not happen
  254. {
  255. fail( "found unknown file '"+scanResult+"'");
  256. }
  257. }
  258. for (int i=0; i<5; i++)
  259. {
  260. if (correctResult[i])
  261. {
  262. ensure("scan of '"+directory+"' using '"+pattern+"' did not return '"+DirScanFilename[i]+"'", filesFound[i]);
  263. }
  264. else
  265. {
  266. ensure("scan of '"+directory+"' using '"+pattern+"' incorrectly returned '"+DirScanFilename[i]+"'", !filesFound[i]);
  267. }
  268. }
  269. }
  270. template<> template<>
  271. void LLDirTest_object_t::test<5>()
  272. // LLDirIterator::next
  273. {
  274. std::string delim = gDirUtilp->getDirDelimiter();
  275. std::string dirTemp = LLFile::tmpdir();
  276. // Create the same 5 file names of the two directories
  277. std::string dir1 = makeTestDir(dirTemp + "LLDirIterator");
  278. std::string dir2 = makeTestDir(dirTemp + "LLDirIterator");
  279. std::string dir1files[5];
  280. std::string dir2files[5];
  281. for (int i=0; i<5; i++)
  282. {
  283. dir1files[i] = makeTestFile(dir1, DirScanFilename[i]);
  284. dir2files[i] = makeTestFile(dir2, DirScanFilename[i]);
  285. }
  286. // Scan dir1 and see if each of the 5 files is found exactly once
  287. bool expected1[5] = { true, true, true, true, true };
  288. scanTest(dir1, "*", expected1);
  289. // Scan dir2 and see if only the 2 *.xyz files are found
  290. bool expected2[5] = { false, false, true, true, false };
  291. scanTest(dir1, "*.xyz", expected2);
  292. // Scan dir2 and see if only the 1 *.mno file is found
  293. bool expected3[5] = { false, false, false, false, true };
  294. scanTest(dir2, "*.mno", expected3);
  295. // Scan dir1 and see if any *.foo files are found
  296. bool expected4[5] = { false, false, false, false, false };
  297. scanTest(dir1, "*.foo", expected4);
  298. // Scan dir1 and see if any file1.* files are found
  299. bool expected5[5] = { true, false, true, false, true };
  300. scanTest(dir1, "file1.*", expected5);
  301. // Scan dir1 and see if any file1.* files are found
  302. bool expected6[5] = { true, true, false, false, false };
  303. scanTest(dir1, "file?.abc", expected6);
  304. // Scan dir2 and see if any file?.x?z files are found
  305. bool expected7[5] = { false, false, true, true, false };
  306. scanTest(dir2, "file?.x?z", expected7);
  307. // Scan dir2 and see if any file?.??c files are found
  308. bool expected8[5] = { true, true, false, false, false };
  309. scanTest(dir2, "file?.??c", expected8);
  310. scanTest(dir2, "*.??c", expected8);
  311. // Scan dir1 and see if any *.?n? files are found
  312. bool expected9[5] = { false, false, false, false, true };
  313. scanTest(dir1, "*.?n?", expected9);
  314. // Scan dir1 and see if any *.???? files are found
  315. bool expected10[5] = { false, false, false, false, false };
  316. scanTest(dir1, "*.????", expected10);
  317. // Scan dir1 and see if any ?????.* files are found
  318. bool expected11[5] = { true, true, true, true, true };
  319. scanTest(dir1, "?????.*", expected11);
  320. // Scan dir1 and see if any ??l??.xyz files are found
  321. bool expected12[5] = { false, false, true, true, false };
  322. scanTest(dir1, "??l??.xyz", expected12);
  323. bool expected13[5] = { true, false, true, false, false };
  324. scanTest(dir1, "file1.{abc,xyz}", expected13);
  325. bool expected14[5] = { true, true, false, false, false };
  326. scanTest(dir1, "file[0-9].abc", expected14);
  327. bool expected15[5] = { true, true, false, false, false };
  328. scanTest(dir1, "file[!a-z].abc", expected15);
  329. // clean up all test files and directories
  330. for (int i=0; i<5; i++)
  331. {
  332. LLFile::remove(dir1files[i]);
  333. LLFile::remove(dir2files[i]);
  334. }
  335. LLFile::rmdir(dir1);
  336. LLFile::rmdir(dir2);
  337. }
  338. }