195 lines
6.4 KiB
C++
195 lines
6.4 KiB
C++
/*********************************************************************
|
|
* \file File.cpp
|
|
*
|
|
* \author Romain BOULLARD
|
|
* \date December 2025
|
|
*********************************************************************/
|
|
#include <System/File/File.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <array>
|
|
#include <cstring>
|
|
#include <type_traits>
|
|
|
|
namespace Bigfoot
|
|
{
|
|
class FileFixture: public ::testing::Test
|
|
{
|
|
public:
|
|
File m_file {eastl::string_view {"Fixture/file"}};
|
|
File m_nonExistent {eastl::string_view {"Fixture/bigfoot"}};
|
|
File m_writeScratchFile {eastl::string_view {"Fixture/scratch"}};
|
|
|
|
protected:
|
|
void SetUp() override
|
|
{
|
|
m_writeScratchFile.Delete();
|
|
}
|
|
};
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, IsRelative_ShouldReturnTrueOnRelativeFile)
|
|
{
|
|
EXPECT_TRUE(m_file.IsRelative());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, IsRelative_ShouldReturnFalseOnAbsoluteFile)
|
|
{
|
|
EXPECT_FALSE(m_file.ToAbsolute().IsRelative());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, IsAbsolute_ShouldReturnTrueOnAbsoluteFile)
|
|
{
|
|
EXPECT_TRUE(m_file.ToAbsolute().IsAbsolute());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, IsAbsolute_ShouldReturnFalseOnRelativeFile)
|
|
{
|
|
EXPECT_FALSE(m_file.IsAbsolute());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Exists_ShouldReturnTrueOnExistingFile)
|
|
{
|
|
EXPECT_TRUE(m_file.Exists());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Exists_ShouldReturnFalseOnNonExistingFile)
|
|
{
|
|
EXPECT_FALSE(m_nonExistent.Exists());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, GetPath_ShouldReturnThePath)
|
|
{
|
|
EXPECT_STREQ(m_file.GetPath().data(), "Fixture/file");
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, ToAbsolute_ShouldReturnTheAbsolutePath)
|
|
{
|
|
EXPECT_STREQ(std::filesystem::absolute("Fixture/file").string().c_str(), m_file.ToAbsolute().GetPath().data());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, ToRelative_ShouldReturnTheRelativePath)
|
|
{
|
|
EXPECT_STREQ(std::filesystem::relative("Fixture/file").string().c_str(), m_file.ToRelative().GetPath().data());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, GetSize_ShouldReturnZeroForAnEmptyFile)
|
|
{
|
|
EXPECT_EQ(m_file.GetSize(), 0);
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, GetSize_ShouldReturnTheFileSize)
|
|
{
|
|
constexpr eastl::string_view content = "Hello, Sasquatch!";
|
|
m_writeScratchFile.Write({std::bit_cast<const std::byte*>(content.data()), content.size()}, 0);
|
|
|
|
EXPECT_EQ(m_writeScratchFile.GetSize(), content.size());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Read_ShouldReturnTheRequestedBytesAtTheGivenOffset)
|
|
{
|
|
constexpr eastl::string_view content = "Hello, Sasquatch!";
|
|
m_writeScratchFile.Write({std::bit_cast<const std::byte*>(content.data()), content.size()}, 0);
|
|
|
|
const eastl::vector<std::byte> data = m_writeScratchFile.Read(7, 10);
|
|
ASSERT_EQ(data.size(), 10);
|
|
EXPECT_EQ(std::memcmp(data.data(), "Sasquatch!", data.size()), 0);
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Write_ShouldCreateTheFileWithTheGivenContent)
|
|
{
|
|
constexpr eastl::string_view content = "Hello, Sasquatch!";
|
|
m_writeScratchFile.Write({std::bit_cast<const std::byte*>(content.data()), content.size()}, 0);
|
|
|
|
const eastl::vector<std::byte> data = m_writeScratchFile.Read(0, m_writeScratchFile.GetSize());
|
|
ASSERT_EQ(data.size(), content.size());
|
|
EXPECT_EQ(std::memcmp(data.data(), content.data(), data.size()), 0);
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Write_ShouldNotTouchContentOutsideTheWrittenRange)
|
|
{
|
|
constexpr eastl::string_view initial = "Hello, Sasquatch!";
|
|
m_writeScratchFile.Write({std::bit_cast<const std::byte*>(initial.data()), initial.size()}, 0);
|
|
|
|
constexpr eastl::string_view patch = "BIGFOOT";
|
|
m_writeScratchFile.Write({std::bit_cast<const std::byte*>(patch.data()), patch.size()}, 7);
|
|
|
|
const eastl::vector<std::byte> data = m_writeScratchFile.Read(0, m_writeScratchFile.GetSize());
|
|
ASSERT_EQ(data.size(), initial.size());
|
|
EXPECT_EQ(std::memcmp(data.data(), "Hello, ", 7), 0);
|
|
EXPECT_EQ(std::memcmp(data.data() + 7, "BIGFOOT", 7), 0);
|
|
EXPECT_EQ(std::memcmp(data.data() + 14, "ch!", 3), 0);
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Write_ShouldRoundTripArbitraryData)
|
|
{
|
|
struct Payload
|
|
{
|
|
int m_intValue;
|
|
double m_doubleValue;
|
|
std::array<char, 4> m_tag;
|
|
|
|
bool operator==(const Payload&) const = default;
|
|
};
|
|
|
|
constexpr Payload payload {.m_intValue = 42, .m_doubleValue = 3.14, .m_tag = {'B', 'I', 'G', 'F'}};
|
|
m_writeScratchFile.Write({std::bit_cast<const std::byte*>(&payload), sizeof(payload)}, 0);
|
|
|
|
const eastl::vector<std::byte> data = m_writeScratchFile.Read(0, m_writeScratchFile.GetSize());
|
|
ASSERT_EQ(data.size(), sizeof(Payload));
|
|
EXPECT_EQ(*std::bit_cast<const Payload*>(data.data()), payload);
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Delete_ShouldRemoveTheFile)
|
|
{
|
|
constexpr std::byte payload {1};
|
|
|
|
m_writeScratchFile.Write({&payload, 1}, 0);
|
|
ASSERT_TRUE(m_writeScratchFile.Exists());
|
|
|
|
m_writeScratchFile.Delete();
|
|
EXPECT_FALSE(m_writeScratchFile.Exists());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
TEST_F(FileFixture, Delete_ShouldDoNothingWhenTheFileDoesNotExist)
|
|
{
|
|
ASSERT_FALSE(m_writeScratchFile.Exists());
|
|
m_writeScratchFile.Delete();
|
|
EXPECT_FALSE(m_writeScratchFile.Exists());
|
|
}
|
|
} // namespace Bigfoot
|