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
|
|
|
|
2022-06-26 01:54:57 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
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
|
|
|
|
{
|
|
|
|
std::string getTemporaryFilePath()
|
2022-06-26 01:54:57 +08:00
|
|
|
{
|
|
|
|
static int counter = 0;
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "sfmltemp" << counter << ".tmp";
|
|
|
|
++counter;
|
|
|
|
|
|
|
|
std::filesystem::path result;
|
|
|
|
result /= std::filesystem::temp_directory_path();
|
|
|
|
result /= oss.str();
|
|
|
|
|
|
|
|
return result.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
class TemporaryFile
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string m_path;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
ofs << contents;
|
|
|
|
assert(ofs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close and delete the generated file.
|
|
|
|
~TemporaryFile()
|
|
|
|
{
|
|
|
|
[[maybe_unused]] const bool removed = std::filesystem::remove(m_path);
|
|
|
|
assert(removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
const std::string& getPath() const
|
|
|
|
{
|
|
|
|
return m_path;
|
|
|
|
}
|
|
|
|
};
|
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-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>);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Empty stream")
|
2021-12-25 00:16:06 +08:00
|
|
|
{
|
|
|
|
sf::FileInputStream fis;
|
|
|
|
|
|
|
|
CHECK(fis.read(nullptr, 0) == -1);
|
|
|
|
CHECK(fis.seek(0) == -1);
|
|
|
|
CHECK(fis.tell() == -1);
|
|
|
|
}
|
|
|
|
|
2023-01-18 12:51:08 +08:00
|
|
|
SECTION("Temporary file stream")
|
2021-12-25 00:16:06 +08:00
|
|
|
{
|
|
|
|
const std::string fileContents = "hello world";
|
|
|
|
|
2023-02-15 11:42:12 +08:00
|
|
|
const TemporaryFile tmpFile(fileContents);
|
2021-12-25 00:16:06 +08:00
|
|
|
sf::FileInputStream fis;
|
|
|
|
|
|
|
|
REQUIRE(fis.open(tmpFile.getPath()));
|
|
|
|
|
|
|
|
char buffer[32];
|
|
|
|
|
|
|
|
CHECK(fis.read(buffer, 5) == 5);
|
|
|
|
CHECK(std::string_view(buffer, 5) == std::string_view(fileContents.c_str(), 5));
|
|
|
|
|
2023-01-18 12:51:08 +08:00
|
|
|
SECTION("Move semantics")
|
2021-12-25 00:16:06 +08:00
|
|
|
{
|
|
|
|
sf::FileInputStream fis2 = std::move(fis);
|
|
|
|
|
|
|
|
CHECK(fis2.read(buffer, 6) == 6);
|
|
|
|
CHECK(std::string_view(buffer, 6) == std::string_view(fileContents.c_str() + 5, 6));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|