platformFileIOTest.cpp 4.6 KB

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