platformFileIOTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/platform.h"
  25. #include "core/fileio.h"
  26. #include "core/util/tVector.h"
  27. #include "console/console.h"
  28. TEST(Platform, ExcludedDirectories)
  29. {
  30. // Just dump everything under the current directory. We should
  31. // find at least one file.
  32. // Exclude .svn and CVS
  33. Platform::clearExcludedDirectories();
  34. Platform::addExcludedDirectory(".svn");
  35. Platform::addExcludedDirectory("CVS");
  36. EXPECT_TRUE(Platform::isExcludedDirectory(".svn"))
  37. << "On list, should be excluded.";
  38. EXPECT_TRUE(Platform::isExcludedDirectory("CVS"))
  39. << "On list, should be excluded.";
  40. EXPECT_FALSE(Platform::isExcludedDirectory("foo"))
  41. << "Doesn't match list, shouldn't be excluded.";
  42. EXPECT_FALSE(Platform::isExcludedDirectory(".svnCVS"))
  43. << "Looks like a duck, but it shouldn't be excluded cuz it's distinct from all entries on the exclusion list.";
  44. // Ok, now our exclusion list is setup, so let's dump some paths.
  45. Vector<Platform::FileInfo> pathInfo;
  46. Platform::dumpPath(Platform::getCurrentDirectory(), pathInfo, 2);
  47. EXPECT_GT(pathInfo.size(), 0)
  48. << "Should find at least SOMETHING in the current directory!";
  49. // This'll nuke info if we run it in a live situation... so don't run unit
  50. // tests in a live situation. ;)
  51. Platform::clearExcludedDirectories();
  52. };
  53. TEST(File, TouchAndTime)
  54. {
  55. FileTime create[2], modify[2];
  56. // Create a file and sleep for a second.
  57. File f;
  58. f.open("testTouch.file", File::WriteAppend);
  59. f.close();
  60. // Touch a file and note its last-modified.
  61. dFileTouch("testTouch.file");
  62. EXPECT_TRUE(Platform::isFile("testTouch.file"))
  63. << "We just touched this file - it should exist.";
  64. EXPECT_TRUE(Platform::getFileTimes("testTouch.file", &create[0], &modify[0]))
  65. << "Failed to get filetimes for a file we just created.";
  66. // Sleep for a tick
  67. Platform::sleep(10);
  68. // Touch it again, and compare the last-modifieds.
  69. EXPECT_TRUE(Platform::isFile("testTouch.file"))
  70. << "We just touched this file - it should exist.";
  71. dFileTouch("testTouch.file");
  72. EXPECT_TRUE(Platform::isFile("testTouch.file"))
  73. << "We just touched this file - it should exist.";
  74. EXPECT_TRUE(Platform::getFileTimes("testTouch.file", &create[1], &modify[1]))
  75. << "Failed to get filetimes for a file we just created.";
  76. // Now compare the times...
  77. EXPECT_LT(Platform::compareFileTimes(modify[0], modify[1]), 0)
  78. << "Timestamps are wrong - modify[0] should be before modify[1]!";
  79. EXPECT_EQ(Platform::compareFileTimes(create[0], create[1]), 0)
  80. << "Create timestamps should match - we didn't delete the file during this test.";
  81. // Clean up..
  82. dFileDelete("testTouch.file");
  83. EXPECT_FALSE(Platform::isFile("testTouch.file"))
  84. << "Somehow failed to delete our test file.";
  85. };
  86. // Mac/Linux have no implementations for these functions, so we 'def it out for now.
  87. #ifdef WIN32
  88. TEST(Platform, Volumes)
  89. {
  90. Vector<const char*> names;
  91. Platform::getVolumeNamesList(names);
  92. EXPECT_GT(names.size(), 0)
  93. << "We should have at least one volume...";
  94. Vector<Platform::VolumeInformation> info;
  95. Platform::getVolumeInformationList(info);
  96. EXPECT_EQ(names.size(), info.size())
  97. << "Got inconsistent number of volumes back from info vs. name list functions!";
  98. };
  99. #endif
  100. #endif