SystemFileStreamTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/IO/GenericStreams.h>
  9. #include <AzCore/IO/Path/Path.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzTest/Utils.h>
  12. namespace UnitTest
  13. {
  14. class SystemFileStreamTest
  15. : public LeakDetectionFixture
  16. {
  17. protected:
  18. AZ::Test::ScopedAutoTempDirectory m_tempDirectory;
  19. };
  20. TEST_F(SystemFileStreamTest, SystemFileStream_CanConstructFromSystemFile_Succeeds)
  21. {
  22. AZ::IO::Path testFile = m_tempDirectory.Resolve("TestFile.txt");
  23. {
  24. constexpr AZ::IO::OpenMode writeOpenMode = AZ::IO::OpenMode::ModeWrite;
  25. const int systemFileMode = AZ::IO::TranslateOpenModeToSystemFileMode(testFile.c_str(), writeOpenMode);
  26. AZ::IO::SystemFile systemFile(testFile.c_str(), systemFileMode);
  27. EXPECT_TRUE(systemFile.IsOpen());
  28. EXPECT_EQ(testFile, systemFile.Name());
  29. {
  30. // Write test output to the test file using SystemFile variable
  31. constexpr AZStd::string_view testOutput = "hello";
  32. systemFile.Write(testOutput.data(), testOutput.size());
  33. }
  34. // The SystemFile should be moved into the SystemFileStream
  35. AZ::IO::SystemFileStream systemFileStream(AZStd::move(systemFile));
  36. // The SystemFile instance should NOT refer to a file
  37. EXPECT_FALSE(systemFile.IsOpen());
  38. EXPECT_NE(testFile, systemFile.Name());
  39. // The SystemFileStream instance should refer to an open file
  40. EXPECT_TRUE(systemFileStream.IsOpen());
  41. EXPECT_EQ(testFile, systemFileStream.GetFilename());
  42. {
  43. // Write test output to the test file using SystemFileStream variable,
  44. // this should write to the same underlying file
  45. constexpr AZStd::string_view testOutput = " world";
  46. systemFileStream.Write(testOutput.size(), testOutput.data());
  47. }
  48. // Move the SystemFile instance in the stream back to the local systemFileVariable
  49. systemFile = AZStd::move(systemFileStream).MoveSystemFile();
  50. // Now the SystemFileStream instance should no longer refer to a file
  51. EXPECT_FALSE(systemFileStream.IsOpen());
  52. EXPECT_NE(testFile, systemFileStream.GetFilename());
  53. // Once again the SystmeFile instance should refer to the open file
  54. EXPECT_TRUE(systemFile.IsOpen());
  55. EXPECT_EQ(testFile, systemFile.Name());
  56. // Close the file for write. It will not be written to again in this test
  57. systemFile.Close();
  58. }
  59. // Finally validate that writing to the test file through both the SystemFile and SystemFileStream
  60. // outputs to the same underlying file
  61. // This will be done by reading the contents of the file
  62. {
  63. constexpr AZStd::string_view expectedOutput = "hello world";
  64. // Test the constructor which accepts a c-string path and a mode
  65. constexpr AZ::IO::OpenMode readOpenMode = AZ::IO::OpenMode::ModeRead;
  66. AZ::IO::SystemFileStream systemFileStream(testFile.c_str(), readOpenMode);
  67. ASSERT_TRUE(systemFileStream.IsOpen());
  68. ASSERT_EQ(expectedOutput.size(), systemFileStream.GetLength());
  69. // Resize the readString to fit the contents of the test file
  70. AZStd::string readString;
  71. auto readFile = [&systemFileStream](char* buffer, size_t size)
  72. {
  73. EXPECT_EQ(size, systemFileStream.Read(size, buffer));
  74. return size;
  75. };
  76. readString.resize_and_overwrite(systemFileStream.GetLength(), readFile);
  77. EXPECT_EQ(expectedOutput, readString);
  78. }
  79. }
  80. TEST_F(SystemFileStreamTest, SystemFileStream_CanReopenWhenSystemFileAndOpenModeConstructor_HasBeenUsed)
  81. {
  82. AZ::IO::Path testFile = m_tempDirectory.Resolve("EmptyFile.txt");
  83. // Create an file so that it exist on disk. This will be used to test the ReOpen functionality
  84. constexpr AZ::IO::OpenMode writeOpenMode = AZ::IO::OpenMode::ModeWrite;
  85. const int systemFileMode = AZ::IO::TranslateOpenModeToSystemFileMode(testFile.c_str(), writeOpenMode);
  86. [[maybe_unused]] AZ::IO::SystemFile systemFile(testFile.c_str(), systemFileMode);
  87. // Close the systemFileStream and re-open it to verify that ReOpen works when a mode is explicitly
  88. // provided to SystemFileStream constructor
  89. AZ::IO::SystemFileStream systemFileStream(AZStd::move(systemFile), writeOpenMode);
  90. EXPECT_TRUE(systemFileStream.IsOpen());
  91. systemFileStream.Close();
  92. EXPECT_FALSE(systemFileStream.IsOpen());
  93. EXPECT_TRUE(systemFileStream.ReOpen());
  94. }
  95. }