testFile.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/fileio.h"
  24. #include "unit/test.h"
  25. #include "core/util/tVector.h"
  26. #include "console/console.h"
  27. using namespace UnitTesting;
  28. CreateUnitTest(CheckFileListingAndExclusion, "File/ListDirectoryAndExclusions")
  29. {
  30. void run()
  31. {
  32. // Just dump everything under the current directory. We should
  33. // find at least one file.
  34. // Exclude .svn and CVS
  35. Platform::clearExcludedDirectories();
  36. Platform::addExcludedDirectory(".svn");
  37. Platform::addExcludedDirectory("CVS");
  38. test(Platform::isExcludedDirectory("foo") == false, "Doesn't match list, shouldn't be excluded.");
  39. test(Platform::isExcludedDirectory(".svn") == true, "On list, should be excluded.");
  40. test(Platform::isExcludedDirectory("CVS") == true, "On list, should be excluded.");
  41. test(Platform::isExcludedDirectory(".svnCVS") == false, "Looks like a duck, but it shouldn't be excluded cuz it's distinct from all entries on the exclusion list.");
  42. // Ok, now our exclusion list is setup, so let's dump some paths.
  43. Vector < Platform::FileInfo > pathInfo;
  44. Platform::dumpPath (Platform::getCurrentDirectory(), pathInfo, 2);
  45. Con::printf("Dump of files in '%s', up to 2 levels deep...", Platform::getCurrentDirectory());
  46. for(S32 i=0; i<pathInfo.size(); i++)
  47. {
  48. Platform::FileInfo &file = pathInfo[i];
  49. Con::printf(" %s (%s) %d bytes", file.pFullPath, file.pFileName, file.fileSize);
  50. }
  51. test(pathInfo.size() > 0, "Should find at least SOMETHING in the current directory!");
  52. // This'll nuke info if we run it in a live situation... so don't run unit
  53. // tests in a live situation. ;)
  54. Platform::clearExcludedDirectories();
  55. }
  56. };
  57. CreateUnitTest(CheckFileTouchAndTime, "File/TouchAndTime")
  58. {
  59. void run()
  60. {
  61. FileTime create[2], modify[2];
  62. // Create a file and sleep for a second.
  63. File f;
  64. f.open("testTouch.file", File::WriteAppend);
  65. f.close();
  66. Platform::sleep(2000);
  67. // Touch a file and note its last-modified.
  68. dFileTouch("testTouch.file");
  69. test(Platform::isFile("testTouch.file"), "We just touched this file - it should exist.");
  70. test(Platform::getFileTimes("testTouch.file", &create[0], &modify[0]), "Failed to get filetimes for a file we just created.");
  71. // Sleep for a few seconds...
  72. Platform::sleep(5000);
  73. // Touch it again, and compare the last-modifieds.
  74. test(Platform::isFile("testTouch.file"), "We just touched this file - it should exist.");
  75. dFileTouch("testTouch.file");
  76. test(Platform::isFile("testTouch.file"), "We just touched this file - it should exist.");
  77. test(Platform::getFileTimes("testTouch.file", &create[1], &modify[1]), "Failed to get filetimes for a file we just created.");
  78. // Now compare the times...
  79. test(Platform::compareFileTimes(modify[0], modify[1]) < 0, "Timestamps are wrong - modify[0] should be before modify[1]!");
  80. // This seems to fail even on a valid case...
  81. // test(Platform::compareFileTimes(create[0], create[1]) == 0, "Create timestamps should match - we didn't delete the file during this test.");
  82. // Clean up..
  83. dFileDelete("testTouch.file");
  84. test(!Platform::isFile("testTouch.file"), "Somehow failed to delete our test file.");
  85. }
  86. };
  87. // Mac has no implementations for these functions, so we 'def it out for now.
  88. #if 0
  89. CreateUnitTest(CheckVolumes, "File/Volumes")
  90. {
  91. void run()
  92. {
  93. Con::printf("Dumping volumes by name:");
  94. Vector<const char*> names;
  95. Platform::getVolumeNamesList(names);
  96. test(names.size() > 0, "We should have at least one volume...");
  97. for(S32 i=0; i<names.size(); i++)
  98. Con::printf(" %s", names[i]);
  99. Con::printf("Dumping volume info:");
  100. Vector<Platform::VolumeInformation> info;
  101. Platform::getVolumeInformationList(info);
  102. test(names.size() == info.size(), "Got inconsistent number of volumes back from info vs. name list functions!");
  103. for(S32 i=0; i<info.size(); i++)
  104. Con::printf(" %s rootPath = %s filesystem = %s ser. num. = %d type = %d readonly = %s",
  105. info[i].Name,
  106. info[i].RootPath,
  107. info[i].FileSystem,
  108. info[i].SerialNumber,
  109. info[i].Type,
  110. info[i].ReadOnly ? "true" : "false");
  111. }
  112. };
  113. #endif
  114. CreateUnitTest(CheckFileWriteAndRead, "File/ReadAndWrite")
  115. {
  116. void run()
  117. {
  118. // Open a file, write some junk to it, close it,
  119. // check size is correct, and open it again.
  120. }
  121. };