2021-12-24 17:16:06 +01:00
|
|
|
#include <SFML/System/FileInputStream.hpp>
|
2022-07-04 11:20:58 -05:00
|
|
|
|
2023-01-17 21:51:08 -07:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2022-07-04 11:20:58 -05:00
|
|
|
|
2024-10-16 13:27:49 -06:00
|
|
|
#include <array>
|
2021-12-24 17:16:06 +01:00
|
|
|
#include <string_view>
|
2022-10-15 00:56:45 -06:00
|
|
|
#include <type_traits>
|
2021-12-24 17:16:06 +01:00
|
|
|
|
2021-12-28 22:03:15 -07:00
|
|
|
|
2022-07-04 23:51:18 -06:00
|
|
|
TEST_CASE("[System] sf::FileInputStream")
|
2021-12-24 17:16:06 +01:00
|
|
|
{
|
2023-09-18 19:28:01 -06:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
|
2023-01-17 21:51:08 -07: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>);
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:27:49 -06:00
|
|
|
std::array<char, 32> buffer{};
|
2021-12-24 17:16:06 +01:00
|
|
|
|
2024-07-06 04:24:00 +02: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")
|
|
|
|
{
|
2025-01-31 13:18:53 -07:00
|
|
|
sf::FileInputStream fileInputStream("System/test.txt");
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
|
2024-07-06 04:24:00 +02:00
|
|
|
CHECK(fileInputStream.tell() == 5);
|
2025-01-31 13:18:53 -07:00
|
|
|
CHECK(fileInputStream.getSize() == 12);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
|
2024-07-06 04:24:00 +02:00
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:28:01 -06:00
|
|
|
SECTION("Move semantics")
|
|
|
|
{
|
|
|
|
SECTION("Move constructor")
|
2021-12-24 17:16:06 +01:00
|
|
|
{
|
2025-01-31 13:18:53 -07:00
|
|
|
sf::FileInputStream movedFileInputStream("System/test.txt");
|
2024-07-06 04:24:00 +02:00
|
|
|
sf::FileInputStream fileInputStream = std::move(movedFileInputStream);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(fileInputStream.read(buffer.data(), 6) == 6);
|
2023-09-18 19:28:01 -06:00
|
|
|
CHECK(fileInputStream.tell() == 6);
|
2025-01-31 13:18:53 -07:00
|
|
|
CHECK(fileInputStream.getSize() == 12);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(std::string_view(buffer.data(), 6) == "Hello "sv);
|
2023-09-18 19:28:01 -06:00
|
|
|
}
|
2021-12-24 17:16:06 +01:00
|
|
|
|
2023-09-18 19:28:01 -06:00
|
|
|
SECTION("Move assignment")
|
|
|
|
{
|
2025-01-31 13:18:53 -07:00
|
|
|
sf::FileInputStream movedFileInputStream("System/test.txt");
|
|
|
|
sf::FileInputStream fileInputStream("System/test2.txt");
|
2024-07-06 04:24:00 +02:00
|
|
|
fileInputStream = std::move(movedFileInputStream);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(fileInputStream.read(buffer.data(), 6) == 6);
|
2023-09-18 19:28:01 -06:00
|
|
|
CHECK(fileInputStream.tell() == 6);
|
2025-01-31 13:18:53 -07:00
|
|
|
CHECK(fileInputStream.getSize() == 12);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(std::string_view(buffer.data(), 6) == "Hello "sv);
|
2021-12-24 17:16:06 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-18 19:28:01 -06:00
|
|
|
|
2024-07-06 03:38:32 +02:00
|
|
|
SECTION("Temporary file stream open")
|
|
|
|
{
|
|
|
|
sf::FileInputStream fileInputStream;
|
2025-01-31 13:18:53 -07:00
|
|
|
REQUIRE(fileInputStream.open("System/test.txt"));
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
|
2024-07-06 03:38:32 +02:00
|
|
|
CHECK(fileInputStream.tell() == 5);
|
2025-01-31 13:18:53 -07:00
|
|
|
CHECK(fileInputStream.getSize() == 12);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
|
2024-07-06 03:38:32 +02:00
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Temporary file stream create")
|
2023-09-18 19:28:01 -06:00
|
|
|
{
|
2025-01-31 13:18:53 -07:00
|
|
|
sf::FileInputStream fileInputStream("System/test.txt");
|
|
|
|
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
|
|
|
|
CHECK(fileInputStream.tell() == 5);
|
|
|
|
CHECK(fileInputStream.getSize() == 12);
|
|
|
|
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
|
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("open()")
|
|
|
|
{
|
|
|
|
sf::FileInputStream fileInputStream;
|
|
|
|
|
|
|
|
SECTION("From ASCII filename")
|
|
|
|
{
|
|
|
|
REQUIRE(fileInputStream.open("System/test.txt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("From Polish filename")
|
|
|
|
{
|
|
|
|
REQUIRE(fileInputStream.open(U"System/test-\u0144.txt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("From emoji filename")
|
|
|
|
{
|
|
|
|
REQUIRE(fileInputStream.open(U"System/test-\U0001F40C.txt"));
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(fileInputStream.read(buffer.data(), 5) == 5);
|
2023-09-18 19:28:01 -06:00
|
|
|
CHECK(fileInputStream.tell() == 5);
|
2025-01-31 13:18:53 -07:00
|
|
|
CHECK(fileInputStream.getSize() == 12);
|
2024-10-16 13:27:49 -06:00
|
|
|
CHECK(std::string_view(buffer.data(), 5) == "Hello"sv);
|
2023-09-18 19:28:01 -06:00
|
|
|
CHECK(fileInputStream.seek(6) == 6);
|
|
|
|
CHECK(fileInputStream.tell() == 6);
|
|
|
|
}
|
2021-12-24 17:16:06 +01:00
|
|
|
}
|