1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <SFML/System/FileInputStream.hpp>
#include <catch2/catch_test_macros.hpp>
#include <array>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <cassert>
namespace
{
std::filesystem::path getTemporaryFilePath()
{
static int counter = 0;
std::ostringstream oss;
oss << "sfmltemp" << counter++ << ".tmp";
return std::filesystem::temp_directory_path() / oss.str();
}
class TemporaryFile
{
public:
// Create a temporary file with a randomly generated path, containing 'contents'.
explicit TemporaryFile(const std::string& contents) : m_path(getTemporaryFilePath())
{
std::ofstream ofs(m_path);
assert(ofs && "Stream encountered an error");
ofs << contents;
assert(ofs && "Stream encountered an error");
}
// 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");
}
// Prevent copies.
TemporaryFile(const TemporaryFile&) = delete;
TemporaryFile& operator=(const TemporaryFile&) = delete;
// Return the randomly generated path.
[[nodiscard]] const std::filesystem::path& getPath() const
{
return m_path;
}
private:
std::filesystem::path m_path;
};
} // namespace
TEST_CASE("[System] sf::FileInputStream")
{
using namespace std::string_view_literals;
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>);
}
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());
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
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);
CHECK(fileInputStream.read(buffer.data(), 6) == 6);
CHECK(fileInputStream.tell() == 6);
CHECK(fileInputStream.getSize() == 11);
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);
CHECK(fileInputStream.read(buffer.data(), 6) == 6);
CHECK(fileInputStream.tell() == 6);
CHECK(fileInputStream.getSize() == 11);
CHECK(std::string_view(buffer.data(), 6) == "Hello "sv);
}
}
SECTION("Temporary file stream open")
{
sf::FileInputStream fileInputStream;
REQUIRE(fileInputStream.open(temporaryFile.getPath()));
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
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());
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
CHECK(fileInputStream.seek(6) == 6);
CHECK(fileInputStream.tell() == 6);
}
}
|