Improve sf::FileInputStream test coverage

This commit is contained in:
Chris Thrasher 2023-09-18 19:28:01 -06:00
parent 46ba176463
commit b856d806be

View File

@ -17,21 +17,13 @@ std::filesystem::path getTemporaryFilePath()
static int counter = 0; static int counter = 0;
std::ostringstream oss; std::ostringstream oss;
oss << "sfmltemp" << counter << ".tmp"; oss << "sfmltemp" << counter++ << ".tmp";
++counter;
std::filesystem::path result; return std::filesystem::temp_directory_path() / oss.str();
result /= std::filesystem::temp_directory_path();
result /= oss.str();
return result;
} }
class TemporaryFile class TemporaryFile
{ {
private:
std::filesystem::path m_path;
public: public:
// Create a temporary file with a randomly generated path, containing 'contents'. // Create a temporary file with a randomly generated path, containing 'contents'.
TemporaryFile(const std::string& contents) : m_path(getTemporaryFilePath()) TemporaryFile(const std::string& contents) : m_path(getTemporaryFilePath())
@ -60,11 +52,16 @@ public:
{ {
return m_path; return m_path;
} }
private:
std::filesystem::path m_path;
}; };
} // namespace } // namespace
TEST_CASE("[System] sf::FileInputStream") TEST_CASE("[System] sf::FileInputStream")
{ {
using namespace std::string_view_literals;
SECTION("Type traits") SECTION("Type traits")
{ {
STATIC_CHECK(!std::is_copy_constructible_v<sf::FileInputStream>); STATIC_CHECK(!std::is_copy_constructible_v<sf::FileInputStream>);
@ -73,35 +70,55 @@ TEST_CASE("[System] sf::FileInputStream")
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::FileInputStream>); STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::FileInputStream>);
} }
SECTION("Empty stream") SECTION("Default constructor")
{ {
sf::FileInputStream fis; sf::FileInputStream fileInputStream;
CHECK(fileInputStream.read(nullptr, 0) == -1);
CHECK(fileInputStream.seek(0) == -1);
CHECK(fileInputStream.tell() == -1);
CHECK(fileInputStream.getSize() == -1);
}
CHECK(fis.read(nullptr, 0) == -1); const TemporaryFile temporaryFile("Hello world");
CHECK(fis.seek(0) == -1); char buffer[32];
CHECK(fis.tell() == -1);
SECTION("Move semantics")
{
SECTION("Move constructor")
{
sf::FileInputStream movedFileInputStream;
REQUIRE(movedFileInputStream.open(temporaryFile.getPath()));
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")
{
sf::FileInputStream movedFileInputStream;
REQUIRE(movedFileInputStream.open(temporaryFile.getPath()));
sf::FileInputStream 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("Temporary file stream") SECTION("Temporary file stream")
{ {
const std::string fileContents = "hello world"; sf::FileInputStream fileInputStream;
REQUIRE(fileInputStream.open(temporaryFile.getPath()));
const TemporaryFile tmpFile(fileContents); CHECK(fileInputStream.read(buffer, 5) == 5);
sf::FileInputStream fis; CHECK(fileInputStream.tell() == 5);
CHECK(fileInputStream.getSize() == 11);
REQUIRE(fis.open(tmpFile.getPath())); CHECK(std::string_view(buffer, 5) == "Hello"sv);
CHECK(fileInputStream.seek(6) == 6);
char buffer[32]; CHECK(fileInputStream.tell() == 6);
CHECK(fis.read(buffer, 5) == 5);
CHECK(std::string_view(buffer, 5) == std::string_view(fileContents.c_str(), 5));
SECTION("Move semantics")
{
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));
}
} }
} }