SFML/test/System/FileInputStream.test.cpp

115 lines
3.5 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-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'.
2022-07-05 00:20:58 +08:00
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.
2023-07-03 00:11:42 +08:00
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_default_constructible_v<sf::FileInputStream>);
2023-01-18 12:51:08 +08:00
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>);
}
const TemporaryFile temporaryFile("Hello world");
char buffer[32];
SECTION("Move semantics")
{
SECTION("Move constructor")
{
auto movedFileInputStream = sf::FileInputStream::open(temporaryFile.getPath()).value();
sf::FileInputStream fileInputStream = std::move(movedFileInputStream);
CHECK(fileInputStream.read(buffer, 6) == 6);
CHECK(fileInputStream.tell() == 6);
CHECK(fileInputStream.getSize() == 11);
CHECK(std::string_view(buffer, 6) == "Hello "sv);
}
SECTION("Move assignment")
{
auto movedFileInputStream = sf::FileInputStream::open(temporaryFile.getPath()).value();
const TemporaryFile temporaryFile2("Hello world the sequel");
auto fileInputStream = sf::FileInputStream::open(temporaryFile2.getPath()).value();
fileInputStream = std::move(movedFileInputStream);
CHECK(fileInputStream.read(buffer, 6) == 6);
CHECK(fileInputStream.tell() == 6);
CHECK(fileInputStream.getSize() == 11);
CHECK(std::string_view(buffer, 6) == "Hello "sv);
}
}
SECTION("Temporary file stream")
{
auto fileInputStream = sf::FileInputStream::open(temporaryFile.getPath()).value();
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);
}
}