SFML/test/System/FileInputStream.test.cpp

150 lines
4.7 KiB
C++
Raw Permalink Normal View History

#include <SFML/System/FileInputStream.hpp>
2022-07-05 00:20:58 +08:00
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-07-05 00:20:58 +08:00
2024-10-17 03:27:49 +08:00
#include <array>
2024-02-08 21:28:57 +08:00
#include <filesystem>
2022-06-26 01:54:57 +08:00
#include <fstream>
#include <sstream>
2024-02-08 21:28:57 +08:00
#include <string>
#include <string_view>
2022-10-15 14:56:45 +08:00
#include <type_traits>
#include <utility>
2023-04-24 20:13:52 +08:00
#include <cassert>
2023-03-24 04:13:52 +08:00
namespace
{
2023-07-03 00:11:42 +08:00
std::filesystem::path getTemporaryFilePath()
2022-06-26 01:54:57 +08:00
{
static int counter = 0;
std::ostringstream oss;
oss << "sfmltemp" << counter++ << ".tmp";
2022-06-26 01:54:57 +08:00
return std::filesystem::temp_directory_path() / oss.str();
2022-06-26 01:54:57 +08:00
}
class TemporaryFile
{
public:
// Create a temporary file with a randomly generated path, containing 'contents'.
explicit TemporaryFile(const std::string& contents) : m_path(getTemporaryFilePath())
2022-06-26 01:54:57 +08:00
{
std::ofstream ofs(m_path);
assert(ofs && "Stream encountered an error");
2022-06-26 01:54:57 +08:00
ofs << contents;
assert(ofs && "Stream encountered an error");
2022-06-26 01:54:57 +08:00
}
// Close and delete the generated file.
~TemporaryFile()
{
[[maybe_unused]] const bool removed = std::filesystem::remove(m_path);
assert(removed && "m_path failed to be removed from filesystem");
2022-06-26 01:54:57 +08:00
}
// Prevent copies.
TemporaryFile(const TemporaryFile&) = delete;
2022-06-26 01:54:57 +08:00
TemporaryFile& operator=(const TemporaryFile&) = delete;
// Return the randomly generated path.
2024-06-27 23:12:23 +08:00
[[nodiscard]] const std::filesystem::path& getPath() const
2022-06-26 01:54:57 +08:00
{
return m_path;
}
private:
std::filesystem::path m_path;
2022-06-26 01:54:57 +08:00
};
2023-03-24 04:13:52 +08:00
} // namespace
TEST_CASE("[System] sf::FileInputStream")
{
using namespace std::string_view_literals;
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
STATIC_CHECK(!std::is_copy_constructible_v<sf::FileInputStream>);
STATIC_CHECK(!std::is_copy_assignable_v<sf::FileInputStream>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::FileInputStream>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::FileInputStream>);
}
2024-10-17 03:27:49 +08:00
const TemporaryFile temporaryFile("Hello world");
std::array<char, 32> buffer{};
SECTION("Construction")
{
SECTION("Default constructor")
{
sf::FileInputStream fileInputStream;
CHECK(fileInputStream.read(nullptr, 0) == std::nullopt);
CHECK(fileInputStream.seek(0) == std::nullopt);
CHECK(fileInputStream.tell() == std::nullopt);
CHECK(fileInputStream.getSize() == std::nullopt);
}
SECTION("File path constructor")
{
sf::FileInputStream fileInputStream(temporaryFile.getPath());
2024-10-17 03:27:49 +08:00
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
2024-10-17 03:27:49 +08:00
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
CHECK(fileInputStream.seek(6) == 6);
CHECK(fileInputStream.tell() == 6);
}
}
SECTION("Move semantics")
{
SECTION("Move constructor")
{
sf::FileInputStream movedFileInputStream(temporaryFile.getPath());
sf::FileInputStream fileInputStream = std::move(movedFileInputStream);
2024-10-17 03:27:49 +08:00
CHECK(fileInputStream.read(buffer.data(), 6) == 6);
CHECK(fileInputStream.tell() == 6);
CHECK(fileInputStream.getSize() == 11);
2024-10-17 03:27:49 +08:00
CHECK(std::string_view(buffer.data(), 6) == "Hello "sv);
}
SECTION("Move assignment")
{
sf::FileInputStream movedFileInputStream(temporaryFile.getPath());
const TemporaryFile temporaryFile2("Hello world the sequel");
sf::FileInputStream fileInputStream(temporaryFile2.getPath());
fileInputStream = std::move(movedFileInputStream);
2024-10-17 03:27:49 +08:00
CHECK(fileInputStream.read(buffer.data(), 6) == 6);
CHECK(fileInputStream.tell() == 6);
CHECK(fileInputStream.getSize() == 11);
2024-10-17 03:27:49 +08:00
CHECK(std::string_view(buffer.data(), 6) == "Hello "sv);
}
}
SECTION("Temporary file stream open")
{
sf::FileInputStream fileInputStream;
REQUIRE(fileInputStream.open(temporaryFile.getPath()));
2024-10-17 03:27:49 +08:00
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
2024-10-17 03:27:49 +08:00
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
CHECK(fileInputStream.seek(6) == 6);
CHECK(fileInputStream.tell() == 6);
}
SECTION("Temporary file stream create")
{
sf::FileInputStream fileInputStream(temporaryFile.getPath());
2024-10-17 03:27:49 +08:00
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
2024-10-17 03:27:49 +08:00
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
CHECK(fileInputStream.seek(6) == 6);
CHECK(fileInputStream.tell() == 6);
}
}