From 304a7c1d69e440f419f569a4a3c83690f9b33e22 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 9 Jun 2024 22:02:54 -0500 Subject: [PATCH] Remove default empty state of `sf::FileInputStream` --- examples/vulkan/Vulkan.cpp | 18 +++-- include/SFML/System/FileInputStream.hpp | 43 +++++++----- src/SFML/Audio/InputSoundFile.cpp | 11 +-- src/SFML/Audio/SoundFileFactory.cpp | 8 +-- src/SFML/System/FileInputStream.cpp | 59 +++++++++------- test/Audio/InputSoundFile.test.cpp | 90 +++++++++++-------------- test/Audio/Music.test.cpp | 33 +++------ test/Audio/SoundBuffer.test.cpp | 24 ++----- test/Audio/SoundFileFactory.test.cpp | 43 +++++------- test/Graphics/Font.test.cpp | 56 +++++++-------- test/Graphics/Image.test.cpp | 22 ++---- test/Graphics/Shader.test.cpp | 13 ++-- test/Graphics/Texture.test.cpp | 3 +- test/Graphics/invalid_shader.vert | 0 test/System/FileInputStream.test.cpp | 28 +++----- test/install/Install.cpp | 7 +- 16 files changed, 198 insertions(+), 260 deletions(-) create mode 100644 test/Graphics/invalid_shader.vert diff --git a/examples/vulkan/Vulkan.cpp b/examples/vulkan/Vulkan.cpp index 07d0830df..10407928d 100644 --- a/examples/vulkan/Vulkan.cpp +++ b/examples/vulkan/Vulkan.cpp @@ -923,18 +923,17 @@ public: // Use the vertex shader SPIR-V code to create a vertex shader module { - sf::FileInputStream file; - - if (!file.open("resources/shader.vert.spv")) + auto file = sf::FileInputStream::open("resources/shader.vert.spv"); + if (!file) { vulkanAvailable = false; return; } - const auto fileSize = file.getSize().value(); + const auto fileSize = file->getSize().value(); std::vector buffer(fileSize / sizeof(std::uint32_t)); - if (file.read(buffer.data(), fileSize) != file.getSize()) + if (file->read(buffer.data(), fileSize) != file->getSize()) { vulkanAvailable = false; return; @@ -952,18 +951,17 @@ public: // Use the fragment shader SPIR-V code to create a fragment shader module { - sf::FileInputStream file; - - if (!file.open("resources/shader.frag.spv")) + auto file = sf::FileInputStream::open("resources/shader.frag.spv"); + if (!file) { vulkanAvailable = false; return; } - const auto fileSize = file.getSize().value(); + const auto fileSize = file->getSize().value(); std::vector buffer(fileSize / sizeof(std::uint32_t)); - if (file.read(buffer.data(), fileSize) != file.getSize()) + if (file->read(buffer.data(), fileSize) != file->getSize()) { vulkanAvailable = false; return; diff --git a/include/SFML/System/FileInputStream.hpp b/include/SFML/System/FileInputStream.hpp index add0c70f6..2246eeef9 100644 --- a/include/SFML/System/FileInputStream.hpp +++ b/include/SFML/System/FileInputStream.hpp @@ -56,12 +56,6 @@ namespace sf class SFML_SYSTEM_API FileInputStream : public InputStream { public: - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - //////////////////////////////////////////////////////////// - FileInputStream(); - //////////////////////////////////////////////////////////// /// \brief Default destructor /// @@ -97,10 +91,10 @@ public: /// /// \param filename Name of the file to open /// - /// \return True on success, false on error + /// \return File input stream on success, `std::nullopt` on error /// //////////////////////////////////////////////////////////// - [[nodiscard]] bool open(const std::filesystem::path& filename); + [[nodiscard]] static std::optional open(const std::filesystem::path& filename); //////////////////////////////////////////////////////////// /// \brief Read data from the stream @@ -143,12 +137,6 @@ public: std::optional getSize() override; private: - //////////////////////////////////////////////////////////// - // Member data - //////////////////////////////////////////////////////////// -#ifdef SFML_SYSTEM_ANDROID - std::unique_ptr m_androidFile; -#endif //////////////////////////////////////////////////////////// /// \brief Deleter for stdio file stream that closes the file stream /// @@ -158,6 +146,27 @@ private: void operator()(std::FILE* file); }; + //////////////////////////////////////////////////////////// + /// \brief Construct from file + /// + //////////////////////////////////////////////////////////// + explicit FileInputStream(std::unique_ptr&& file); + +#ifdef SFML_SYSTEM_ANDROID + //////////////////////////////////////////////////////////// + /// \brief Construct from resource stream + /// + //////////////////////////////////////////////////////////// + explicit FileInputStream(std::unique_ptr&& androidFile); +#endif + + //////////////////////////////////////////////////////////// + // Member data + //////////////////////////////////////////////////////////// +#ifdef SFML_SYSTEM_ANDROID + std::unique_ptr m_androidFile; +#endif + std::unique_ptr m_file; //!< stdio file stream }; @@ -188,9 +197,9 @@ private: /// \code /// void process(InputStream& stream); /// -/// FileInputStream stream; -/// if (stream.open("some_file.dat")) -/// process(stream); +/// std::optional stream = sf::FileInputStream::open("some_file.dat"); +/// if (stream) +/// process(*stream); /// \endcode /// /// InputStream, MemoryInputStream diff --git a/src/SFML/Audio/InputSoundFile.cpp b/src/SFML/Audio/InputSoundFile.cpp index 47fd61643..2e9ec885c 100644 --- a/src/SFML/Audio/InputSoundFile.cpp +++ b/src/SFML/Audio/InputSoundFile.cpp @@ -77,11 +77,9 @@ std::optional InputSoundFile::openFromFile(const std::filesystem return std::nullopt; } - // Wrap the file into a stream - auto file = std::make_unique(); - - // Open it - if (!file->open(filename)) + // Open the file + auto fileInputStream = FileInputStream::open(filename); + if (!fileInputStream) { err() << "Failed to open input sound file from file (couldn't open file input stream)\n" << formatDebugPathInfo(filename) << std::endl; @@ -89,6 +87,9 @@ std::optional InputSoundFile::openFromFile(const std::filesystem return std::nullopt; } + // Wrap the file into a stream + auto file = std::make_unique(std::move(*fileInputStream)); + // Pass the stream to the reader auto info = reader->open(*file); if (!info) diff --git a/src/SFML/Audio/SoundFileFactory.cpp b/src/SFML/Audio/SoundFileFactory.cpp index f74d95241..f1108e3cc 100644 --- a/src/SFML/Audio/SoundFileFactory.cpp +++ b/src/SFML/Audio/SoundFileFactory.cpp @@ -48,8 +48,8 @@ namespace sf std::unique_ptr SoundFileFactory::createReaderFromFilename(const std::filesystem::path& filename) { // Wrap the input file into a file stream - FileInputStream stream; - if (!stream.open(filename)) + auto stream = FileInputStream::open(filename); + if (!stream) { err() << "Failed to open sound file (couldn't open stream)\n" << formatDebugPathInfo(filename) << std::endl; return nullptr; @@ -58,13 +58,13 @@ std::unique_ptr SoundFileFactory::createReaderFromFilename(cons // Test the filename in all the registered factories for (const auto& [fpCreate, fpCheck] : getReaderFactoryMap()) { - if (!stream.seek(0).has_value()) + if (!stream->seek(0).has_value()) { err() << "Failed to seek sound stream" << std::endl; return nullptr; } - if (fpCheck(stream)) + if (fpCheck(*stream)) return fpCreate(); } diff --git a/src/SFML/System/FileInputStream.cpp b/src/SFML/System/FileInputStream.cpp index 72f7eecf7..2f37914c7 100644 --- a/src/SFML/System/FileInputStream.cpp +++ b/src/SFML/System/FileInputStream.cpp @@ -32,6 +32,7 @@ #endif #include +#include #include namespace sf @@ -43,10 +44,6 @@ void FileInputStream::FileCloser::operator()(std::FILE* file) } -//////////////////////////////////////////////////////////// -FileInputStream::FileInputStream() = default; - - //////////////////////////////////////////////////////////// FileInputStream::~FileInputStream() = default; @@ -60,21 +57,24 @@ FileInputStream& FileInputStream::operator=(FileInputStream&&) noexcept = defaul //////////////////////////////////////////////////////////// -bool FileInputStream::open(const std::filesystem::path& filename) +std::optional FileInputStream::open(const std::filesystem::path& filename) { #ifdef SFML_SYSTEM_ANDROID if (priv::getActivityStatesPtr() != nullptr) { - m_androidFile = std::make_unique(filename); - return m_androidFile->tell().has_value(); + auto androidFile = std::make_unique(filename); + if (androidFile->tell().has_value()) + return FileInputStream(std::move(androidFile)); + return std::nullopt; } #endif #ifdef SFML_SYSTEM_WINDOWS - m_file.reset(_wfopen(filename.c_str(), L"rb")); + if (auto file = std::unique_ptr(_wfopen(filename.c_str(), L"rb"))) #else - m_file.reset(std::fopen(filename.c_str(), "rb")); + if (auto file = std::unique_ptr(std::fopen(filename.c_str(), "rb"))) #endif - return m_file != nullptr; + return FileInputStream(std::move(file)); + return std::nullopt; } @@ -84,13 +84,11 @@ std::optional FileInputStream::read(void* data, std::size_t size) #ifdef SFML_SYSTEM_ANDROID if (priv::getActivityStatesPtr() != nullptr) { - if (!m_androidFile) - return std::nullopt; + assert(m_androidFile); return m_androidFile->read(data, size); } #endif - if (!m_file) - return std::nullopt; + assert(m_file); return std::fread(data, 1, size, m_file.get()); } @@ -101,13 +99,11 @@ std::optional FileInputStream::seek(std::size_t position) #ifdef SFML_SYSTEM_ANDROID if (priv::getActivityStatesPtr() != nullptr) { - if (!m_androidFile) - return std::nullopt; + assert(m_androidFile); return m_androidFile->seek(position); } #endif - if (!m_file) - return std::nullopt; + assert(m_file); if (std::fseek(m_file.get(), static_cast(position), SEEK_SET)) return std::nullopt; @@ -121,13 +117,11 @@ std::optional FileInputStream::tell() #ifdef SFML_SYSTEM_ANDROID if (priv::getActivityStatesPtr() != nullptr) { - if (!m_androidFile) - return std::nullopt; + assert(m_androidFile); return m_androidFile->tell(); } #endif - if (!m_file) - return std::nullopt; + assert(m_file); const auto position = std::ftell(m_file.get()); return position < 0 ? std::nullopt : std::optional(position); } @@ -139,13 +133,11 @@ std::optional FileInputStream::getSize() #ifdef SFML_SYSTEM_ANDROID if (priv::getActivityStatesPtr() != nullptr) { - if (!m_androidFile) - return std::nullopt; + assert(m_androidFile); return m_androidFile->getSize(); } #endif - if (!m_file) - return std::nullopt; + assert(m_file); const auto position = tell().value(); std::fseek(m_file.get(), 0, SEEK_END); const std::optional size = tell(); @@ -156,4 +148,19 @@ std::optional FileInputStream::getSize() return size; } + +//////////////////////////////////////////////////////////// +FileInputStream::FileInputStream(std::unique_ptr&& file) : m_file(std::move(file)) +{ +} + + +//////////////////////////////////////////////////////////// +#ifdef SFML_SYSTEM_ANDROID +FileInputStream::FileInputStream(std::unique_ptr&& androidFile) : +m_androidFile(std::move(androidFile)) +{ +} +#endif + } // namespace sf diff --git a/test/Audio/InputSoundFile.test.cpp b/test/Audio/InputSoundFile.test.cpp index 5ecc90b99..4d2e97b70 100644 --- a/test/Audio/InputSoundFile.test.cpp +++ b/test/Audio/InputSoundFile.test.cpp @@ -91,62 +91,52 @@ TEST_CASE("[Audio] sf::InputSoundFile") SECTION("openFromStream()") { - sf::FileInputStream stream; - - SECTION("Invalid stream") + SECTION("flac") { - CHECK(!sf::InputSoundFile::openFromStream(stream)); + auto stream = sf::FileInputStream::open("Audio/ding.flac").value(); + const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); + CHECK(inputSoundFile.getSampleCount() == 87'798); + CHECK(inputSoundFile.getChannelCount() == 1); + CHECK(inputSoundFile.getSampleRate() == 44'100); + CHECK(inputSoundFile.getDuration() == sf::microseconds(1'990'884)); + CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); + CHECK(inputSoundFile.getSampleOffset() == 0); } - SECTION("Valid stream") + SECTION("mp3") { - SECTION("flac") - { - REQUIRE(stream.open("Audio/ding.flac")); - const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); - CHECK(inputSoundFile.getSampleCount() == 87'798); - CHECK(inputSoundFile.getChannelCount() == 1); - CHECK(inputSoundFile.getSampleRate() == 44'100); - CHECK(inputSoundFile.getDuration() == sf::microseconds(1'990'884)); - CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); - CHECK(inputSoundFile.getSampleOffset() == 0); - } + auto stream = sf::FileInputStream::open("Audio/ding.mp3").value(); + const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); + CHECK(inputSoundFile.getSampleCount() == 87'798); + CHECK(inputSoundFile.getChannelCount() == 1); + CHECK(inputSoundFile.getSampleRate() == 44'100); + CHECK(inputSoundFile.getDuration() == sf::microseconds(1'990'884)); + CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); + CHECK(inputSoundFile.getSampleOffset() == 0); + } - SECTION("mp3") - { - REQUIRE(stream.open("Audio/ding.mp3")); - const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); - CHECK(inputSoundFile.getSampleCount() == 87'798); - CHECK(inputSoundFile.getChannelCount() == 1); - CHECK(inputSoundFile.getSampleRate() == 44'100); - CHECK(inputSoundFile.getDuration() == sf::microseconds(1'990'884)); - CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); - CHECK(inputSoundFile.getSampleOffset() == 0); - } + SECTION("ogg") + { + auto stream = sf::FileInputStream::open("Audio/doodle_pop.ogg").value(); + const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); + CHECK(inputSoundFile.getSampleCount() == 2'116'992); + CHECK(inputSoundFile.getChannelCount() == 2); + CHECK(inputSoundFile.getSampleRate() == 44'100); + CHECK(inputSoundFile.getDuration() == sf::microseconds(24'002'176)); + CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); + CHECK(inputSoundFile.getSampleOffset() == 0); + } - SECTION("ogg") - { - REQUIRE(stream.open("Audio/doodle_pop.ogg")); - const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); - CHECK(inputSoundFile.getSampleCount() == 2'116'992); - CHECK(inputSoundFile.getChannelCount() == 2); - CHECK(inputSoundFile.getSampleRate() == 44'100); - CHECK(inputSoundFile.getDuration() == sf::microseconds(24'002'176)); - CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); - CHECK(inputSoundFile.getSampleOffset() == 0); - } - - SECTION("wav") - { - REQUIRE(stream.open("Audio/killdeer.wav")); - const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); - CHECK(inputSoundFile.getSampleCount() == 112'941); - CHECK(inputSoundFile.getChannelCount() == 1); - CHECK(inputSoundFile.getSampleRate() == 22'050); - CHECK(inputSoundFile.getDuration() == sf::microseconds(5'122'040)); - CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); - CHECK(inputSoundFile.getSampleOffset() == 0); - } + SECTION("wav") + { + auto stream = sf::FileInputStream::open("Audio/killdeer.wav").value(); + const auto inputSoundFile = sf::InputSoundFile::openFromStream(stream).value(); + CHECK(inputSoundFile.getSampleCount() == 112'941); + CHECK(inputSoundFile.getChannelCount() == 1); + CHECK(inputSoundFile.getSampleRate() == 22'050); + CHECK(inputSoundFile.getDuration() == sf::microseconds(5'122'040)); + CHECK(inputSoundFile.getTimeOffset() == sf::Time::Zero); + CHECK(inputSoundFile.getSampleOffset() == 0); } } diff --git a/test/Audio/Music.test.cpp b/test/Audio/Music.test.cpp index 369378365..c40a42c00 100644 --- a/test/Audio/Music.test.cpp +++ b/test/Audio/Music.test.cpp @@ -82,28 +82,17 @@ TEST_CASE("[Audio] sf::Music", runAudioDeviceTests()) SECTION("openFromStream()") { - sf::FileInputStream stream; - - SECTION("Invalid stream") - { - CHECK(!sf::Music::openFromStream(stream)); - } - - SECTION("Valid stream") - { - REQUIRE(stream.open("Audio/doodle_pop.ogg")); - - const auto music = sf::Music::openFromStream(stream).value(); - CHECK(music.getDuration() == sf::microseconds(24002176)); - const auto [offset, length] = music.getLoopPoints(); - CHECK(offset == sf::Time::Zero); - CHECK(length == sf::microseconds(24002176)); - CHECK(music.getChannelCount() == 2); - CHECK(music.getSampleRate() == 44100); - CHECK(music.getStatus() == sf::Music::Status::Stopped); - CHECK(music.getPlayingOffset() == sf::Time::Zero); - CHECK(!music.getLoop()); - } + auto stream = sf::FileInputStream::open("Audio/doodle_pop.ogg").value(); + const auto music = sf::Music::openFromStream(stream).value(); + CHECK(music.getDuration() == sf::microseconds(24002176)); + const auto [offset, length] = music.getLoopPoints(); + CHECK(offset == sf::Time::Zero); + CHECK(length == sf::microseconds(24002176)); + CHECK(music.getChannelCount() == 2); + CHECK(music.getSampleRate() == 44100); + CHECK(music.getStatus() == sf::Music::Status::Stopped); + CHECK(music.getPlayingOffset() == sf::Time::Zero); + CHECK(!music.getLoop()); } SECTION("play/pause/stop") diff --git a/test/Audio/SoundBuffer.test.cpp b/test/Audio/SoundBuffer.test.cpp index 2831f9f8f..5b1cc8334 100644 --- a/test/Audio/SoundBuffer.test.cpp +++ b/test/Audio/SoundBuffer.test.cpp @@ -89,23 +89,13 @@ TEST_CASE("[Audio] sf::SoundBuffer", runAudioDeviceTests()) SECTION("loadFromStream()") { - sf::FileInputStream stream; - - SECTION("Invalid stream") - { - CHECK(!sf::SoundBuffer::loadFromStream(stream)); - } - - SECTION("Valid stream") - { - REQUIRE(stream.open("Audio/ding.flac")); - const auto soundBuffer = sf::SoundBuffer::loadFromStream(stream).value(); - CHECK(soundBuffer.getSamples() != nullptr); - CHECK(soundBuffer.getSampleCount() == 87798); - CHECK(soundBuffer.getSampleRate() == 44100); - CHECK(soundBuffer.getChannelCount() == 1); - CHECK(soundBuffer.getDuration() == sf::microseconds(1990884)); - } + auto stream = sf::FileInputStream::open("Audio/ding.flac").value(); + const auto soundBuffer = sf::SoundBuffer::loadFromStream(stream).value(); + CHECK(soundBuffer.getSamples() != nullptr); + CHECK(soundBuffer.getSampleCount() == 87798); + CHECK(soundBuffer.getSampleRate() == 44100); + CHECK(soundBuffer.getChannelCount() == 1); + CHECK(soundBuffer.getDuration() == sf::microseconds(1990884)); } SECTION("saveToFile()") diff --git a/test/Audio/SoundFileFactory.test.cpp b/test/Audio/SoundFileFactory.test.cpp index 882ea960d..9bbed5669 100644 --- a/test/Audio/SoundFileFactory.test.cpp +++ b/test/Audio/SoundFileFactory.test.cpp @@ -110,37 +110,30 @@ TEST_CASE("[Audio] sf::SoundFileFactory") SECTION("createReaderFromStream()") { - sf::FileInputStream stream; + std::optional stream; - SECTION("Invalid stream") + SECTION("flac") { - CHECK(!sf::SoundFileFactory::createReaderFromStream(stream)); + stream = sf::FileInputStream::open("Audio/ding.flac"); } - SECTION("Valid file") + SECTION("mp3") { - SECTION("flac") - { - REQUIRE(stream.open("Audio/ding.flac")); - } - - SECTION("mp3") - { - REQUIRE(stream.open("Audio/ding.mp3")); - } - - SECTION("ogg") - { - REQUIRE(stream.open("Audio/doodle_pop.ogg")); - } - - SECTION("wav") - { - REQUIRE(stream.open("Audio/killdeer.wav")); - } - - CHECK(sf::SoundFileFactory::createReaderFromStream(stream)); + stream = sf::FileInputStream::open("Audio/ding.mp3"); } + + SECTION("ogg") + { + stream = sf::FileInputStream::open("Audio/doodle_pop.ogg"); + } + + SECTION("wav") + { + stream = sf::FileInputStream::open("Audio/killdeer.wav"); + } + + REQUIRE(stream); + CHECK(sf::SoundFileFactory::createReaderFromStream(*stream)); } SECTION("createWriterFromFilename()") diff --git a/test/Graphics/Font.test.cpp b/test/Graphics/Font.test.cpp index 1591193d2..76d36ac2d 100644 --- a/test/Graphics/Font.test.cpp +++ b/test/Graphics/Font.test.cpp @@ -95,39 +95,29 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) SECTION("loadFromStream()") { - sf::FileInputStream stream; - - SECTION("Invalid stream") - { - CHECK(!sf::Font::loadFromStream(stream)); - } - - SECTION("Successful load") - { - REQUIRE(stream.open("Graphics/tuffy.ttf")); - const auto font = sf::Font::loadFromStream(stream).value(); - CHECK(font.getInfo().family == "Tuffy"); - const auto& glyph = font.getGlyph(0x45, 16, false); - CHECK(glyph.advance == 9); - CHECK(glyph.lsbDelta == 9); - CHECK(glyph.rsbDelta == 16); - CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12})); - CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12})); - CHECK(font.hasGlyph(0x41)); - CHECK(font.hasGlyph(0xC0)); - CHECK(font.getKerning(0x41, 0x42, 12) == -1); - CHECK(font.getKerning(0x43, 0x44, 24, true) == 0); - CHECK(font.getLineSpacing(24) == 30); - CHECK(font.getUnderlinePosition(36) == Approx(2.20312f)); - CHECK(font.getUnderlineThickness(48) == Approx(1.17188f)); - const auto& texture = font.getTexture(10); - CHECK(texture.getSize() == sf::Vector2u(128, 128)); - CHECK(texture.isSmooth()); - CHECK(!texture.isSrgb()); - CHECK(!texture.isRepeated()); - CHECK(texture.getNativeHandle() != 0); - CHECK(font.isSmooth()); - } + auto stream = sf::FileInputStream::open("Graphics/tuffy.ttf").value(); + const auto font = sf::Font::loadFromStream(stream).value(); + CHECK(font.getInfo().family == "Tuffy"); + const auto& glyph = font.getGlyph(0x45, 16, false); + CHECK(glyph.advance == 9); + CHECK(glyph.lsbDelta == 9); + CHECK(glyph.rsbDelta == 16); + CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12})); + CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12})); + CHECK(font.hasGlyph(0x41)); + CHECK(font.hasGlyph(0xC0)); + CHECK(font.getKerning(0x41, 0x42, 12) == -1); + CHECK(font.getKerning(0x43, 0x44, 24, true) == 0); + CHECK(font.getLineSpacing(24) == 30); + CHECK(font.getUnderlinePosition(36) == Approx(2.20312f)); + CHECK(font.getUnderlineThickness(48) == Approx(1.17188f)); + const auto& texture = font.getTexture(10); + CHECK(texture.getSize() == sf::Vector2u(128, 128)); + CHECK(texture.isSmooth()); + CHECK(!texture.isSrgb()); + CHECK(!texture.isRepeated()); + CHECK(texture.getNativeHandle() != 0); + CHECK(font.isSmooth()); } SECTION("Set/get smooth") diff --git a/test/Graphics/Image.test.cpp b/test/Graphics/Image.test.cpp index f63ce6c5a..d0e44e1cc 100644 --- a/test/Graphics/Image.test.cpp +++ b/test/Graphics/Image.test.cpp @@ -178,22 +178,12 @@ TEST_CASE("[Graphics] sf::Image") SECTION("loadFromStream()") { - sf::FileInputStream stream; - - SECTION("Invalid stream") - { - CHECK(!sf::Image::loadFromStream(stream)); - } - - SECTION("Successful load") - { - CHECK(stream.open("Graphics/sfml-logo-big.png")); - const auto image = sf::Image::loadFromStream(stream).value(); - CHECK(image.getSize() == sf::Vector2u(1001, 304)); - CHECK(image.getPixelsPtr() != nullptr); - CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0)); - CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62)); - } + auto stream = sf::FileInputStream::open("Graphics/sfml-logo-big.png").value(); + const auto image = sf::Image::loadFromStream(stream).value(); + CHECK(image.getSize() == sf::Vector2u(1001, 304)); + CHECK(image.getPixelsPtr() != nullptr); + CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0)); + CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62)); } SECTION("saveToFile()") diff --git a/test/Graphics/Shader.test.cpp b/test/Graphics/Shader.test.cpp index 7557f0143..e7d0a3728 100644 --- a/test/Graphics/Shader.test.cpp +++ b/test/Graphics/Shader.test.cpp @@ -243,16 +243,11 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) SECTION("loadFromStream()") { - sf::FileInputStream vertexShaderStream; - REQUIRE(vertexShaderStream.open("Graphics/shader.vert")); + auto vertexShaderStream = sf::FileInputStream::open("Graphics/shader.vert").value(); + auto fragmentShaderStream = sf::FileInputStream::open("Graphics/shader.frag").value(); + auto geometryShaderStream = sf::FileInputStream::open("Graphics/shader.geom").value(); - sf::FileInputStream fragmentShaderStream; - REQUIRE(fragmentShaderStream.open("Graphics/shader.frag")); - - sf::FileInputStream geometryShaderStream; - REQUIRE(geometryShaderStream.open("Graphics/shader.geom")); - - sf::FileInputStream emptyStream; + auto emptyStream = sf::FileInputStream::open("Graphics/invalid_shader.vert").value(); SECTION("One shader") { diff --git a/test/Graphics/Texture.test.cpp b/test/Graphics/Texture.test.cpp index c22003cc2..6244f59a2 100644 --- a/test/Graphics/Texture.test.cpp +++ b/test/Graphics/Texture.test.cpp @@ -95,8 +95,7 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests()) SECTION("loadFromStream()") { - sf::FileInputStream stream; - REQUIRE(stream.open("Graphics/sfml-logo-big.png")); + auto stream = sf::FileInputStream::open("Graphics/sfml-logo-big.png").value(); const auto texture = sf::Texture::loadFromStream(stream).value(); CHECK(texture.getSize() == sf::Vector2u(1001, 304)); CHECK(!texture.isSmooth()); diff --git a/test/Graphics/invalid_shader.vert b/test/Graphics/invalid_shader.vert new file mode 100644 index 000000000..e69de29bb diff --git a/test/System/FileInputStream.test.cpp b/test/System/FileInputStream.test.cpp index ae5386c52..1fb1c896a 100644 --- a/test/System/FileInputStream.test.cpp +++ b/test/System/FileInputStream.test.cpp @@ -66,21 +66,13 @@ TEST_CASE("[System] sf::FileInputStream") SECTION("Type traits") { + STATIC_CHECK(!std::is_default_constructible_v); STATIC_CHECK(!std::is_copy_constructible_v); STATIC_CHECK(!std::is_copy_assignable_v); STATIC_CHECK(std::is_nothrow_move_constructible_v); STATIC_CHECK(std::is_nothrow_move_assignable_v); } - 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); - } - const TemporaryFile temporaryFile("Hello world"); char buffer[32]; @@ -88,10 +80,8 @@ TEST_CASE("[System] sf::FileInputStream") { SECTION("Move constructor") { - sf::FileInputStream movedFileInputStream; - REQUIRE(movedFileInputStream.open(temporaryFile.getPath())); - - sf::FileInputStream fileInputStream = std::move(movedFileInputStream); + auto movedFileInputStream = sf::FileInputStream::open(temporaryFile.getPath()).value(); + sf::FileInputStream fileInputStream = std::move(movedFileInputStream); CHECK(fileInputStream.read(buffer, 6) == 6); CHECK(fileInputStream.tell() == 6); CHECK(fileInputStream.getSize() == 11); @@ -100,11 +90,10 @@ TEST_CASE("[System] sf::FileInputStream") SECTION("Move assignment") { - sf::FileInputStream movedFileInputStream; - REQUIRE(movedFileInputStream.open(temporaryFile.getPath())); - - sf::FileInputStream fileInputStream; - fileInputStream = std::move(movedFileInputStream); + auto movedFileInputStream = sf::FileInputStream::open(temporaryFile.getPath()).value(); + const TemporaryFile temporaryFile2("Hello world the sequel"); + auto fileInputStream = sf::FileInputStream::open(temporaryFile2.getPath()).value(); + fileInputStream = std::move(movedFileInputStream); CHECK(fileInputStream.read(buffer, 6) == 6); CHECK(fileInputStream.tell() == 6); CHECK(fileInputStream.getSize() == 11); @@ -114,8 +103,7 @@ TEST_CASE("[System] sf::FileInputStream") SECTION("Temporary file stream") { - sf::FileInputStream fileInputStream; - REQUIRE(fileInputStream.open(temporaryFile.getPath())); + auto fileInputStream = sf::FileInputStream::open(temporaryFile.getPath()).value(); CHECK(fileInputStream.read(buffer, 5) == 5); CHECK(fileInputStream.tell() == 5); CHECK(fileInputStream.getSize() == 11); diff --git a/test/install/Install.cpp b/test/install/Install.cpp index ad78970ad..bab2b5a0d 100644 --- a/test/install/Install.cpp +++ b/test/install/Install.cpp @@ -24,10 +24,9 @@ int main() [[maybe_unused]] const sf::UdpSocket udpSocket; // System - [[maybe_unused]] const sf::Angle angle; - [[maybe_unused]] const sf::FileInputStream fileInputStream; - [[maybe_unused]] const sf::String string; - [[maybe_unused]] const sf::Time time; + [[maybe_unused]] const sf::Angle angle; + [[maybe_unused]] const sf::String string; + [[maybe_unused]] const sf::Time time; // Window [[maybe_unused]] const sf::Context context;