2022-06-24 09:13:55 +08:00
|
|
|
#include <SFML/System/MemoryInputStream.hpp>
|
|
|
|
|
2023-01-18 12:51:08 +08:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2022-06-24 09:13:55 +08:00
|
|
|
|
2024-04-17 13:19:52 +08:00
|
|
|
#include <array>
|
2022-07-05 00:20:58 +08:00
|
|
|
#include <ostream>
|
|
|
|
#include <string_view>
|
|
|
|
|
2022-07-05 13:51:18 +08:00
|
|
|
TEST_CASE("[System] sf::MemoryInputStream")
|
2022-06-24 09:13:55 +08:00
|
|
|
{
|
2023-01-18 12:51:08 +08:00
|
|
|
SECTION("Type traits")
|
|
|
|
{
|
|
|
|
STATIC_CHECK(std::is_copy_constructible_v<sf::MemoryInputStream>);
|
|
|
|
STATIC_CHECK(std::is_copy_assignable_v<sf::MemoryInputStream>);
|
|
|
|
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::MemoryInputStream>);
|
|
|
|
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::MemoryInputStream>);
|
|
|
|
}
|
|
|
|
|
2024-04-29 11:53:11 +08:00
|
|
|
using namespace std::literals::string_view_literals;
|
|
|
|
|
|
|
|
SECTION("open()")
|
|
|
|
{
|
|
|
|
static constexpr auto input = "hello world"sv;
|
2024-05-03 01:06:56 +08:00
|
|
|
|
|
|
|
SECTION("Zero length")
|
|
|
|
{
|
|
|
|
sf::MemoryInputStream memoryInputStream(input.data(), 0);
|
|
|
|
CHECK(memoryInputStream.tell().value() == 0);
|
|
|
|
CHECK(memoryInputStream.getSize().value() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Full length")
|
|
|
|
{
|
|
|
|
sf::MemoryInputStream memoryInputStream(input.data(), input.size());
|
|
|
|
CHECK(memoryInputStream.tell().value() == 0);
|
|
|
|
CHECK(memoryInputStream.getSize().value() == input.size());
|
|
|
|
}
|
2024-04-29 11:53:11 +08:00
|
|
|
}
|
2022-06-24 09:13:55 +08:00
|
|
|
|
2024-04-29 11:53:11 +08:00
|
|
|
SECTION("read()")
|
|
|
|
{
|
|
|
|
static constexpr auto input = "hello world"sv;
|
2024-05-03 01:06:56 +08:00
|
|
|
sf::MemoryInputStream memoryInputStream(input.data(), input.size());
|
2024-04-29 11:53:11 +08:00
|
|
|
CHECK(memoryInputStream.tell().value() == 0);
|
|
|
|
CHECK(memoryInputStream.getSize().value() == input.size());
|
|
|
|
|
|
|
|
// Read within input
|
|
|
|
std::array<char, 32> output{};
|
|
|
|
CHECK(memoryInputStream.read(output.data(), 5).value() == 5);
|
|
|
|
CHECK(std::string_view(output.data(), 5) == "hello"sv);
|
|
|
|
CHECK(memoryInputStream.tell().value() == 5);
|
|
|
|
CHECK(memoryInputStream.getSize().value() == input.size());
|
|
|
|
|
|
|
|
// Read beyond input
|
|
|
|
CHECK(memoryInputStream.read(output.data(), 100).value() == 6);
|
|
|
|
CHECK(std::string_view(output.data(), 6) == " world"sv);
|
|
|
|
CHECK(memoryInputStream.tell().value() == 11);
|
|
|
|
CHECK(memoryInputStream.getSize().value() == input.size());
|
2022-06-24 09:13:55 +08:00
|
|
|
}
|
|
|
|
|
2024-04-29 11:53:11 +08:00
|
|
|
SECTION("seek()")
|
2022-06-24 09:13:55 +08:00
|
|
|
{
|
2024-04-29 11:53:11 +08:00
|
|
|
static constexpr auto input = "We Love SFML!"sv;
|
2024-05-03 01:06:56 +08:00
|
|
|
sf::MemoryInputStream memoryInputStream(input.data(), input.size());
|
2024-04-29 11:53:11 +08:00
|
|
|
CHECK(memoryInputStream.tell().value() == 0);
|
|
|
|
CHECK(memoryInputStream.getSize().value() == input.size());
|
|
|
|
|
|
|
|
SECTION("Seek within input")
|
|
|
|
{
|
|
|
|
CHECK(memoryInputStream.seek(0).value() == 0);
|
|
|
|
CHECK(memoryInputStream.tell().value() == 0);
|
|
|
|
|
|
|
|
CHECK(memoryInputStream.seek(5).value() == 5);
|
|
|
|
CHECK(memoryInputStream.tell().value() == 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Seek beyond input")
|
|
|
|
{
|
|
|
|
CHECK(memoryInputStream.seek(1'000).value() == input.size());
|
|
|
|
CHECK(memoryInputStream.tell().value() == input.size());
|
|
|
|
}
|
2022-06-24 09:13:55 +08:00
|
|
|
}
|
|
|
|
}
|