2021-12-25 00:16:06 +08:00
|
|
|
#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-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>
|
2021-12-25 00:16:06 +08:00
|
|
|
#include <string_view>
|
2022-10-15 14:56:45 +08:00
|
|
|
#include <type_traits>
|
2021-12-25 00:16:06 +08:00
|
|
|
#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;
|
2023-09-19 09:28:01 +08:00
|
|
|
oss << "sfmltemp" << counter++ << ".tmp";
|
2022-06-26 01:54:57 +08:00
|
|
|
|
2023-09-19 09:28:01 +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'.
|
2024-09-27 04:18:59 +08:00
|
|
|
explicit TemporaryFile(const std::string& contents) : m_path(getTemporaryFilePath())
|
2022-06-26 01:54:57 +08:00
|
|
|
{
|
|
|
|
std::ofstream ofs(m_path);
|
2023-07-03 00:08:58 +08:00
|
|
|
assert(ofs && "Stream encountered an error");
|
2022-06-26 01:54:57 +08:00
|
|
|
|
|
|
|
ofs << contents;
|
2023-07-03 00:08:58 +08:00
|
|
|
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);
|
2023-07-03 00:08:58 +08:00
|
|
|
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-02-18 03:32:28 +08:00
|
|
|
|
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;
|
|
|
|
}
|
2023-09-19 09:28:01 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::filesystem::path m_path;
|
2022-06-26 01:54:57 +08:00
|
|
|
};
|
2023-03-24 04:13:52 +08:00
|
|
|
} // namespace
|
2021-12-29 13:03:15 +08:00
|
|
|
|
2022-07-05 13:51:18 +08:00
|
|
|
TEST_CASE("[System] sf::FileInputStream")
|
2021-12-25 00:16:06 +08:00
|
|
|
{
|
2023-09-19 09:28:01 +08:00
|
|
|
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>);
|
|
|
|
}
|
|
|
|
|
2023-09-19 09:28:01 +08:00
|
|
|
const TemporaryFile temporaryFile("Hello world");
|
|
|
|
char buffer[32];
|
2021-12-25 00:16:06 +08:00
|
|
|
|
2024-07-06 10:24:00 +08:00
|
|
|
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());
|
|
|
|
CHECK(fileInputStream.read(buffer, 5) == 5);
|
|
|
|
CHECK(fileInputStream.tell() == 5);
|
|
|
|
CHECK(fileInputStream.getSize() == 11);
|
|
|
|
CHECK(std::string_view(buffer, 5) == "Hello"sv);
|
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 09:28:01 +08:00
|
|
|
SECTION("Move semantics")
|
|
|
|
{
|
|
|
|
SECTION("Move constructor")
|
2021-12-25 00:16:06 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::FileInputStream movedFileInputStream(temporaryFile.getPath());
|
|
|
|
sf::FileInputStream fileInputStream = std::move(movedFileInputStream);
|
2023-09-19 09:28:01 +08:00
|
|
|
CHECK(fileInputStream.read(buffer, 6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
CHECK(fileInputStream.getSize() == 11);
|
|
|
|
CHECK(std::string_view(buffer, 6) == "Hello "sv);
|
|
|
|
}
|
2021-12-25 00:16:06 +08:00
|
|
|
|
2023-09-19 09:28:01 +08:00
|
|
|
SECTION("Move assignment")
|
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::FileInputStream movedFileInputStream(temporaryFile.getPath());
|
2024-06-10 11:02:54 +08:00
|
|
|
const TemporaryFile temporaryFile2("Hello world the sequel");
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::FileInputStream fileInputStream(temporaryFile2.getPath());
|
|
|
|
fileInputStream = std::move(movedFileInputStream);
|
2023-09-19 09:28:01 +08:00
|
|
|
CHECK(fileInputStream.read(buffer, 6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
CHECK(fileInputStream.getSize() == 11);
|
|
|
|
CHECK(std::string_view(buffer, 6) == "Hello "sv);
|
2021-12-25 00:16:06 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 09:28:01 +08:00
|
|
|
|
2024-07-06 09:38:32 +08:00
|
|
|
SECTION("Temporary file stream open")
|
|
|
|
{
|
|
|
|
sf::FileInputStream fileInputStream;
|
|
|
|
REQUIRE(fileInputStream.open(temporaryFile.getPath()));
|
|
|
|
CHECK(fileInputStream.read(buffer, 5) == 5);
|
|
|
|
CHECK(fileInputStream.tell() == 5);
|
|
|
|
CHECK(fileInputStream.getSize() == 11);
|
|
|
|
CHECK(std::string_view(buffer, 5) == "Hello"sv);
|
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Temporary file stream create")
|
2023-09-19 09:28:01 +08:00
|
|
|
{
|
2024-07-06 10:24:00 +08:00
|
|
|
sf::FileInputStream fileInputStream(temporaryFile.getPath());
|
2023-09-19 09:28:01 +08:00
|
|
|
CHECK(fileInputStream.read(buffer, 5) == 5);
|
|
|
|
CHECK(fileInputStream.tell() == 5);
|
|
|
|
CHECK(fileInputStream.getSize() == 11);
|
|
|
|
CHECK(std::string_view(buffer, 5) == "Hello"sv);
|
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
2021-12-25 00:16:06 +08:00
|
|
|
}
|