/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/UnitTests/FoundationTest/IO/OSFileTest.cpp
354 строки
11 KB
Jan Krassnigg
Fixed file system iterator, particle orientation and added reverse string hash lookup (#1336)
21 июн 2024, 08:57
Не верифицирован
21 июн 2024, 08:57
fb0e917
Код
Авторство
О чём код?
#include <FoundationTest/FoundationTestPCH.h> #include <Foundation/IO/FileSystem/FileSystem.h> #include <Foundation/IO/OSFile.h> EZ_CREATE_SIMPLE_TEST(IO, OSFile) { ezStringBuilder sFileContent = "Lyrics to Taste The Cake:\n\ Turret: Who's there?\n\ Turret: Is anyone there?\n\ Turret: I see you.\n\ \n\ Chell rises from a stasis inside of a glass box\n\ She isn't greeted by faces,\n\ Only concrete and clocks.\n\ ..."; const ezUInt32 uiTextLen = sFileContent.GetElementCount(); ezStringBuilder sOutputFile = ezTestFramework::GetInstance()->GetAbsOutputPath(); sOutputFile.MakeCleanPath(); sOutputFile.AppendPath("IO", "SubFolder"); sOutputFile.AppendPath("OSFile_TestFile.txt"); ezStringBuilder sOutputFile2 = ezTestFramework::GetInstance()->GetAbsOutputPath(); sOutputFile2.MakeCleanPath(); sOutputFile2.AppendPath("IO", "SubFolder2"); sOutputFile2.AppendPath("OSFile_TestFileCopy.txt"); ezStringBuilder sOutputFile3 = ezTestFramework::GetInstance()->GetAbsOutputPath(); sOutputFile3.MakeCleanPath(); sOutputFile3.AppendPath("IO", "SubFolder2", "SubSubFolder"); sOutputFile3.AppendPath("RandomFile.txt"); EZ_TEST_BLOCK(ezTestBlock::Enabled, "Write File") { ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile.GetData(), ezFileOpenMode::Write) == EZ_SUCCESS); EZ_TEST_BOOL(f.IsOpen()); EZ_TEST_INT(f.GetFilePosition(), 0); EZ_TEST_INT(f.GetFileSize(), 0); for (ezUInt32 i = 0; i < uiTextLen; ++i) { EZ_TEST_BOOL(f.Write(&sFileContent.GetData()[i], 1) == EZ_SUCCESS); EZ_TEST_INT(f.GetFilePosition(), i + 1); EZ_TEST_INT(f.GetFileSize(), i + 1); } EZ_TEST_INT(f.GetFilePosition(), uiTextLen); f.SetFilePosition(5, ezFileSeekMode::FromStart); EZ_TEST_INT(f.GetFileSize(), uiTextLen); EZ_TEST_INT(f.GetFilePosition(), 5); // f.Close(); // The file should be closed automatically } EZ_TEST_BLOCK(ezTestBlock::Enabled, "Append File") { ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile.GetData(), ezFileOpenMode::Append) == EZ_SUCCESS); EZ_TEST_BOOL(f.IsOpen()); EZ_TEST_INT(f.GetFilePosition(), uiTextLen); EZ_TEST_BOOL(f.Write(sFileContent.GetData(), uiTextLen) == EZ_SUCCESS); EZ_TEST_INT(f.GetFilePosition(), uiTextLen * 2); f.Close(); EZ_TEST_BOOL(!f.IsOpen()); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "Read File") { const ezUInt32 FS_MAX_PATH = 1024; char szTemp[FS_MAX_PATH]; ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile.GetData(), ezFileOpenMode::Read) == EZ_SUCCESS); EZ_TEST_BOOL(f.IsOpen()); EZ_TEST_INT(f.GetFilePosition(), 0); EZ_TEST_INT(f.Read(szTemp, FS_MAX_PATH), uiTextLen * 2); EZ_TEST_INT(f.GetFilePosition(), uiTextLen * 2); EZ_TEST_BOOL(ezMemoryUtils::IsEqual(szTemp, sFileContent.GetData(), uiTextLen)); EZ_TEST_BOOL(ezMemoryUtils::IsEqual(&szTemp[uiTextLen], sFileContent.GetData(), uiTextLen)); f.Close(); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "Copy File") { ezOSFile::CopyFile(sOutputFile.GetData(), sOutputFile2.GetData()).IgnoreResult(); ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile2.GetData(), ezFileOpenMode::Read) == EZ_SUCCESS); const ezUInt32 FS_MAX_PATH = 1024; char szTemp[FS_MAX_PATH]; EZ_TEST_INT(f.Read(szTemp, FS_MAX_PATH), uiTextLen * 2); EZ_TEST_BOOL(ezMemoryUtils::IsEqual(szTemp, sFileContent.GetData(), uiTextLen)); EZ_TEST_BOOL(ezMemoryUtils::IsEqual(&szTemp[uiTextLen], sFileContent.GetData(), uiTextLen)); f.Close(); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "ReadAll") { ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile, ezFileOpenMode::Read) == EZ_SUCCESS); ezDynamicArray<ezUInt8> fileContent; const ezUInt64 bytes = f.ReadAll(fileContent); EZ_TEST_INT(bytes, uiTextLen * 2); EZ_TEST_BOOL(ezMemoryUtils::IsEqual(fileContent.GetData(), (const ezUInt8*)sFileContent.GetData(), uiTextLen)); f.Close(); } #if EZ_ENABLED(EZ_SUPPORTS_FILE_STATS) EZ_TEST_BLOCK(ezTestBlock::Enabled, "File Stats") { ezFileStats s; ezStringBuilder dir = sOutputFile2.GetFileDirectory(); EZ_TEST_BOOL(ezOSFile::GetFileStats(sOutputFile2.GetData(), s) == EZ_SUCCESS); // printf("%s Name: '%s' (%lli Bytes), Modified Time: %lli\n", s.m_bIsDirectory ? "Directory" : "File", s.m_sFileName.GetData(), // s.m_uiFileSize, s.m_LastModificationTime.GetInt64(ezSIUnitOfTime::Microsecond)); EZ_TEST_BOOL(ezOSFile::GetFileStats(dir.GetData(), s) == EZ_SUCCESS); // printf("%s Name: '%s' (%lli Bytes), Modified Time: %lli\n", s.m_bIsDirectory ? "Directory" : "File", s.m_sFileName.GetData(), // s.m_uiFileSize, s.m_LastModificationTime.GetInt64(ezSIUnitOfTime::Microsecond)); } # if (EZ_ENABLED(EZ_SUPPORTS_CASE_INSENSITIVE_PATHS) && EZ_ENABLED(EZ_SUPPORTS_UNRESTRICTED_FILE_ACCESS)) EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetFileCasing") { ezStringBuilder dir = sOutputFile2; dir.ToLower(); # if EZ_ENABLED(EZ_PLATFORM_WINDOWS) // On Windows the drive letter will always be turned upper case by ezOSFile::GetFileCasing() // ensure that our input data ('ground truth') also uses an upper case drive letter auto driveLetterIterator = sOutputFile2.GetIteratorFront(); const ezUInt32 uiDriveLetter = ezStringUtils::ToUpperChar(driveLetterIterator.GetCharacter()); sOutputFile2.ChangeCharacter(driveLetterIterator, uiDriveLetter); # endif ezStringBuilder sCorrected; EZ_TEST_BOOL(ezOSFile::GetFileCasing(dir.GetData(), sCorrected) == EZ_SUCCESS); // On Windows the drive letter will always be made to upper case EZ_TEST_STRING(sCorrected.GetData(), sOutputFile2.GetData()); } # endif // EZ_SUPPORTS_CASE_INSENSITIVE_PATHS && EZ_SUPPORTS_UNRESTRICTED_FILE_ACCESS #endif // EZ_SUPPORTS_FILE_STATS #if EZ_ENABLED(EZ_SUPPORTS_FILE_ITERATORS) EZ_TEST_BLOCK(ezTestBlock::Enabled, "File Iterator") { ezStringBuilder sOutputFolder = ezFileSystem::GetSdkRootDirectory(); sOutputFolder.AppendPath("Data/Base/*"); ezStringBuilder sFullPath; ezUInt32 uiFolders = 0; ezUInt32 uiFiles = 0; bool bSkipFolder = true; ezFileSystemIterator it; for (it.StartSearch(sOutputFolder.GetData(), ezFileSystemIteratorFlags::ReportFilesAndFoldersRecursive); it.IsValid();) { sFullPath = it.GetCurrentPath(); sFullPath.AppendPath(it.GetStats().m_sName.GetData()); it.GetStats(); it.GetCurrentPath(); if (it.GetStats().m_bIsDirectory) { ++uiFolders; bSkipFolder = !bSkipFolder; if (bSkipFolder) { it.SkipFolder(); // replaces the 'Next' call continue; } } else { ++uiFiles; } it.Next(); } // The binary folder will only have subdirectories on windows desktop # if EZ_ENABLED(EZ_PLATFORM_WINDOWS_DESKTOP) EZ_TEST_BOOL(uiFolders > 0); # endif EZ_TEST_BOOL(uiFiles > 0); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "File Iterator (non recursive)") { ezStringBuilder sOutputFolder = ezFileSystem::GetSdkRootDirectory(); sOutputFolder.AppendPath("Data/Base"); ezStringBuilder sFullPath; ezUInt32 uiFolders = 0; ezUInt32 uiFiles = 0; ezFileSystemIterator it; for (it.StartSearch(sOutputFolder.GetData(), ezFileSystemIteratorFlags::ReportFiles); it.IsValid();) { sFullPath = it.GetCurrentPath(); sFullPath.AppendPath(it.GetStats().m_sName.GetData()); it.GetStats(); it.GetCurrentPath(); if (it.GetStats().m_bIsDirectory) { ++uiFolders; } else { ++uiFiles; } EZ_TEST_BOOL(sFullPath.StartsWith(sOutputFolder)); sFullPath.MakeRelativeTo(sOutputFolder).AssertSuccess(); EZ_TEST_BOOL(!sFullPath.FindSubString("/")); // no sub path it.Next(); } EZ_TEST_BOOL(uiFolders == 0); EZ_TEST_BOOL(uiFiles > 0); } #endif EZ_TEST_BLOCK(ezTestBlock::Enabled, "Delete File") { EZ_TEST_BOOL(ezOSFile::DeleteFile(sOutputFile.GetData()) == EZ_SUCCESS); EZ_TEST_BOOL(ezOSFile::DeleteFile(sOutputFile.GetData()) == EZ_SUCCESS); // second time should still 'succeed' EZ_TEST_BOOL(ezOSFile::DeleteFile(sOutputFile2.GetData()) == EZ_SUCCESS); EZ_TEST_BOOL(ezOSFile::DeleteFile(sOutputFile2.GetData()) == EZ_SUCCESS); // second time should still 'succeed' ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile.GetData(), ezFileOpenMode::Read) == EZ_FAILURE); // file should not exist anymore EZ_TEST_BOOL(f.Open(sOutputFile2.GetData(), ezFileOpenMode::Read) == EZ_FAILURE); // file should not exist anymore } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetCurrentWorkingDirectory") { ezStringBuilder cwd = ezOSFile::GetCurrentWorkingDirectory(); EZ_TEST_BOOL(!cwd.IsEmpty()); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "MakePathAbsoluteWithCWD") { ezStringBuilder cwd = ezOSFile::GetCurrentWorkingDirectory(); ezStringBuilder path = ezOSFile::MakePathAbsoluteWithCWD("sub/folder"); EZ_TEST_BOOL(path.StartsWith(cwd)); EZ_TEST_BOOL(path.EndsWith("/sub/folder")); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "ExistsFile") { EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile.GetData()) == false); EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile2.GetData()) == false); { ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile.GetData(), ezFileOpenMode::Write) == EZ_SUCCESS); } EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile.GetData()) == true); EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile2.GetData()) == false); { ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile2.GetData(), ezFileOpenMode::Write) == EZ_SUCCESS); } EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile.GetData()) == true); EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile2.GetData()) == true); EZ_TEST_BOOL(ezOSFile::DeleteFile(sOutputFile.GetData()) == EZ_SUCCESS); EZ_TEST_BOOL(ezOSFile::DeleteFile(sOutputFile2.GetData()) == EZ_SUCCESS); EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile.GetData()) == false); EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFile2.GetData()) == false); ezStringBuilder sOutputFolder = ezTestFramework::GetInstance()->GetAbsOutputPath(); // We should not report folders as files EZ_TEST_BOOL(ezOSFile::ExistsFile(sOutputFolder) == false); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "ExistsDirectory") { // files are not folders EZ_TEST_BOOL(ezOSFile::ExistsDirectory(sOutputFile.GetData()) == false); EZ_TEST_BOOL(ezOSFile::ExistsDirectory(sOutputFile2.GetData()) == false); ezStringBuilder sOutputFolder = ezTestFramework::GetInstance()->GetAbsOutputPath(); EZ_TEST_BOOL(ezOSFile::ExistsDirectory(sOutputFolder) == true); sOutputFile.AppendPath("IO"); EZ_TEST_BOOL(ezOSFile::ExistsDirectory(sOutputFolder) == true); sOutputFile.AppendPath("SubFolder"); EZ_TEST_BOOL(ezOSFile::ExistsDirectory(sOutputFolder) == true); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetApplicationDirectory") { ezStringView sAppDir = ezOSFile::GetApplicationDirectory(); EZ_TEST_BOOL(!sAppDir.IsEmpty()); } #if (EZ_ENABLED(EZ_SUPPORTS_FILE_ITERATORS) && EZ_ENABLED(EZ_SUPPORTS_FILE_STATS)) EZ_TEST_BLOCK(ezTestBlock::Enabled, "DeleteFolder") { { ezOSFile f; EZ_TEST_BOOL(f.Open(sOutputFile3.GetData(), ezFileOpenMode::Write) == EZ_SUCCESS); } ezStringBuilder SubFolder2 = ezTestFramework::GetInstance()->GetAbsOutputPath(); SubFolder2.MakeCleanPath(); SubFolder2.AppendPath("IO", "SubFolder2"); EZ_TEST_BOOL(ezOSFile::DeleteFolder(SubFolder2).Succeeded()); EZ_TEST_BOOL(!ezOSFile::ExistsDirectory(SubFolder2)); } #endif }