platformFileIoTests.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. // We don't want tests in a shipping version.
  23. #ifndef TORQUE_SHIPPING
  24. #ifndef _UNIT_TESTING_H_
  25. #include "testing/unitTesting.h"
  26. #endif
  27. #ifndef _PLATFORM_H_
  28. #include "platform/platform.h"
  29. #endif
  30. #ifndef _PLATFORM_FILEIO_H_
  31. #include "platform/platformFileIO.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. #define PLATFORM_UNITTEST_FILEIO_FILE "_unitTestFile_RemoveMe.txt"
  35. #define PLATFORM_UNITTEST_FILEIO_FILEMESSAGE "Write a line of text."
  36. //-----------------------------------------------------------------------------
  37. TEST( PlatformFileIOTests, FileWriteRead )
  38. {
  39. File testWriteFile;
  40. File::Status fileStatus;
  41. // Open the file for writing.
  42. fileStatus = testWriteFile.open( PLATFORM_UNITTEST_FILEIO_FILE, File::Write );
  43. // Check the file was opened.
  44. ASSERT_EQ( fileStatus, File::Ok ) << "Failed to open file for (over)write.";
  45. // Check we can write to the file.
  46. ASSERT_TRUE( testWriteFile.hasCapability( File::FileWrite ) ) << "File cannot be written to.";
  47. // Check test message length (we may not have executed any string tests yet).
  48. const S32 fileMessageLength = (S32)dStrlen(PLATFORM_UNITTEST_FILEIO_FILEMESSAGE);
  49. ASSERT_GT( fileMessageLength, 0 ) << "Test message must be a non-zero length.";
  50. // Write to the file.
  51. U32 bytesWritten;
  52. fileStatus = testWriteFile.write( fileMessageLength, PLATFORM_UNITTEST_FILEIO_FILEMESSAGE, &bytesWritten );
  53. // Check write operation.
  54. ASSERT_EQ( fileStatus, File::Ok ) << "Test message write operation failed.";
  55. ASSERT_EQ( bytesWritten, fileMessageLength ) << "Test message was not written fully.";
  56. // Check position and size.
  57. ASSERT_EQ( testWriteFile.getPosition(), bytesWritten ) << "File position is invalid.";
  58. ASSERT_EQ( testWriteFile.getSize(), bytesWritten ) << "File size is incorrect.";
  59. // Close the file.
  60. fileStatus = testWriteFile.close();
  61. // Check status.
  62. ASSERT_EQ( fileStatus, File::Closed ) << "Write file was not closed.";
  63. /// Test file reading...
  64. File testReadFile;
  65. // Open the file for reading.
  66. fileStatus = testReadFile.open( PLATFORM_UNITTEST_FILEIO_FILE, File::Read );
  67. // Check the file was opened.
  68. ASSERT_EQ( fileStatus, File::Ok ) << "Failed to open file for read.";
  69. // Check we can read from the file.
  70. ASSERT_TRUE( testReadFile.hasCapability( File::FileRead ) ) << "File cannot be read from.";
  71. // Create a read buffer.
  72. char* pReadBuffer = new char[fileMessageLength+1];
  73. pReadBuffer[fileMessageLength] = 0;
  74. U32 bytesRead;
  75. // Read from the file.
  76. fileStatus = testReadFile.read( fileMessageLength, pReadBuffer, &bytesRead );
  77. // Check read operation.
  78. ASSERT_EQ( fileStatus, File::Ok ) << "Test message read operation failed.";
  79. ASSERT_EQ( bytesRead, fileMessageLength ) << "Test message was not read fully.";
  80. // Check position and size.
  81. ASSERT_EQ( testReadFile.getPosition(), bytesRead ) << "File position is invalid.";
  82. ASSERT_EQ( testReadFile.getSize(), bytesRead ) << "File size is incorrect.";
  83. // Check contents.
  84. ASSERT_STREQ( PLATFORM_UNITTEST_FILEIO_FILEMESSAGE, pReadBuffer ) << "Test message read incorrectly.";
  85. // Destroy the read buffer.
  86. delete [] pReadBuffer;
  87. // Close the file.
  88. fileStatus = testReadFile.close();
  89. // Check status.
  90. ASSERT_EQ( fileStatus, File::Closed ) << "Read file was not closed.";
  91. // Check the file has been deleted.
  92. ASSERT_TRUE( Platform::fileDelete( PLATFORM_UNITTEST_FILEIO_FILE ) );
  93. }
  94. //-----------------------------------------------------------------------------
  95. #endif // TORQUE_SHIPPING