From efcfaf06fb3058df24c615c6c55aa59c4ff16dc1 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 11 Apr 2024 10:29:03 -0600 Subject: [PATCH] Improve test coverage --- test/Graphics/Font.test.cpp | 13 ++-------- test/Graphics/RenderTarget.test.cpp | 23 +++++++++++------ test/Graphics/Shader.test.cpp | 25 ++++++++++++++++++- test/Graphics/Texture.test.cpp | 38 +++++++++++++++++++++++++++++ test/TestUtilities/GraphicsUtil.cpp | 15 ++++++++++++ test/TestUtilities/GraphicsUtil.hpp | 6 +++++ 6 files changed, 101 insertions(+), 19 deletions(-) diff --git a/test/Graphics/Font.test.cpp b/test/Graphics/Font.test.cpp index fbebfdb97..834c97b4a 100644 --- a/test/Graphics/Font.test.cpp +++ b/test/Graphics/Font.test.cpp @@ -81,17 +81,7 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) SECTION("Successful load") { - const auto memory = []() - { - std::ifstream file("Graphics/tuffy.ttf", std::ios::binary | std::ios::ate); - REQUIRE(file); - const auto size = file.tellg(); - file.seekg(0, std::ios::beg); - std::vector buffer(static_cast(size)); - REQUIRE(file.read(reinterpret_cast(buffer.data()), size)); - return buffer; - }(); - + const auto memory = loadIntoMemory("Graphics/tuffy.ttf"); REQUIRE(font.loadFromMemory(memory.data(), memory.size())); CHECK(font.getInfo().family == "Tuffy"); const auto& glyph = font.getGlyph(0x45, 16, false); @@ -158,6 +148,7 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) SECTION("Set/get smooth") { sf::Font font; + REQUIRE(font.loadFromFile("Graphics/tuffy.ttf")); font.setSmooth(false); CHECK(!font.isSmooth()); } diff --git a/test/Graphics/RenderTarget.test.cpp b/test/Graphics/RenderTarget.test.cpp index c2c3943cb..1076e3349 100644 --- a/test/Graphics/RenderTarget.test.cpp +++ b/test/Graphics/RenderTarget.test.cpp @@ -61,15 +61,15 @@ TEST_CASE("[Graphics] sf::RenderTarget") CHECK(renderTarget.setActive(true)); } + const auto makeView = [](const auto& viewport) + { + sf::View view; + view.setViewport(viewport); + return view; + }; + SECTION("getViewport(const View&)") { - const auto makeView = [](const auto& viewport) - { - sf::View view; - view.setViewport(viewport); - return view; - }; - const RenderTarget renderTarget; CHECK(renderTarget.getViewport(makeView(sf::FloatRect({0, 0}, {1, 1}))) == sf::IntRect({0, 0}, {640, 480})); CHECK(renderTarget.getViewport(makeView(sf::FloatRect({1, 1}, {.5f, .25f}))) == @@ -78,6 +78,15 @@ TEST_CASE("[Graphics] sf::RenderTarget") sf::IntRect({320, 240}, {160, 360})); } + SECTION("getScissor(const View&)") + { + const RenderTarget renderTarget; + CHECK(renderTarget.getScissor(makeView(sf::FloatRect({0, 0}, {1, 1}))) == sf::IntRect({0, 0}, {640, 480})); + CHECK(renderTarget.getScissor(makeView(sf::FloatRect({1, 1}, {.5f, .25f}))) == sf::IntRect({0, 0}, {640, 480})); + CHECK(renderTarget.getScissor(makeView(sf::FloatRect({.5f, .5f}, {.25f, .75f}))) == + sf::IntRect({0, 0}, {640, 480})); + } + SECTION("mapPixelToCoords(const Vector2i&)") { sf::View view; diff --git a/test/Graphics/Shader.test.cpp b/test/Graphics/Shader.test.cpp index 48d32944c..7fbbce449 100644 --- a/test/Graphics/Shader.test.cpp +++ b/test/Graphics/Shader.test.cpp @@ -172,6 +172,24 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) CHECK(shader.getNativeHandle() == 0); } + SECTION("Move semantics") + { + SECTION("Construction") + { + sf::Shader movedShader; + const sf::Shader shader = std::move(movedShader); + CHECK(shader.getNativeHandle() == 0); + } + + SECTION("Assignment") + { + sf::Shader movedShader; + sf::Shader shader; + shader = std::move(movedShader); + CHECK(shader.getNativeHandle() == 0); + } + } + SECTION("loadFromFile()") { sf::Shader shader; @@ -229,19 +247,24 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) sf::FileInputStream geometryShaderStream; REQUIRE(geometryShaderStream.open("Graphics/shader.geom")); + sf::FileInputStream emptyStream; + SECTION("One shader") { + REQUIRE(!shader.loadFromStream(emptyStream, sf::Shader::Type::Vertex)); REQUIRE(shader.loadFromStream(vertexShaderStream, sf::Shader::Type::Vertex) == sf::Shader::isAvailable()); + REQUIRE(shader.loadFromStream(fragmentShaderStream, sf::Shader::Type::Fragment) == sf::Shader::isAvailable()); } SECTION("Two shaders") { + REQUIRE(!shader.loadFromStream(emptyStream, fragmentShaderStream)); + REQUIRE(!shader.loadFromStream(vertexShaderStream, emptyStream)); REQUIRE(shader.loadFromStream(vertexShaderStream, fragmentShaderStream) == sf::Shader::isAvailable()); } SECTION("Three shaders") { - sf::FileInputStream emptyStream; REQUIRE(!shader.loadFromStream(emptyStream, geometryShaderStream, fragmentShaderStream)); REQUIRE(!shader.loadFromStream(vertexShaderStream, emptyStream, fragmentShaderStream)); REQUIRE(!shader.loadFromStream(vertexShaderStream, geometryShaderStream, emptyStream)); diff --git a/test/Graphics/Texture.test.cpp b/test/Graphics/Texture.test.cpp index 3d92b8c5d..3e99184f0 100644 --- a/test/Graphics/Texture.test.cpp +++ b/test/Graphics/Texture.test.cpp @@ -32,6 +32,32 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests()) CHECK(texture.getNativeHandle() == 0); } + SECTION("Move semantics") + { + SECTION("Construction") + { + sf::Texture movedTexture; + const sf::Texture texture = std::move(movedTexture); + CHECK(texture.getSize() == sf::Vector2u()); + CHECK(!texture.isSmooth()); + CHECK(!texture.isSrgb()); + CHECK(!texture.isRepeated()); + CHECK(texture.getNativeHandle() == 0); + } + + SECTION("Assignment") + { + sf::Texture movedTexture; + sf::Texture texture; + texture = std::move(movedTexture); + CHECK(texture.getSize() == sf::Vector2u()); + CHECK(!texture.isSmooth()); + CHECK(!texture.isSrgb()); + CHECK(!texture.isRepeated()); + CHECK(texture.getNativeHandle() == 0); + } + } + SECTION("create()") { sf::Texture texture; @@ -68,6 +94,18 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests()) CHECK(texture.getNativeHandle() != 0); } + SECTION("loadFromMemory()") + { + const auto memory = loadIntoMemory("Graphics/sfml-logo-big.png"); + sf::Texture texture; + REQUIRE(texture.loadFromMemory(memory.data(), memory.size())); + CHECK(texture.getSize() == sf::Vector2u(1001, 304)); + CHECK(!texture.isSmooth()); + CHECK(!texture.isSrgb()); + CHECK(!texture.isRepeated()); + CHECK(texture.getNativeHandle() != 0); + } + SECTION("loadFromStream()") { sf::Texture texture; diff --git a/test/TestUtilities/GraphicsUtil.cpp b/test/TestUtilities/GraphicsUtil.cpp index 05ed0688d..2530af6ae 100644 --- a/test/TestUtilities/GraphicsUtil.cpp +++ b/test/TestUtilities/GraphicsUtil.cpp @@ -6,9 +6,12 @@ #include #include +#include #include #include +#include + namespace sf { std::ostream& operator<<(std::ostream& os, const BlendMode& blendMode) @@ -113,3 +116,15 @@ bool operator==(const sf::Transform& lhs, const Approx& rhs) lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) && lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]); } + +std::vector loadIntoMemory(const std::filesystem::path& path) +{ + std::ifstream file(path, std::ios::binary | std::ios::ate); + assert(file); + const auto size = file.tellg(); + file.seekg(0, std::ios::beg); + std::vector buffer(static_cast(size)); + [[maybe_unused]] const auto& result = file.read(reinterpret_cast(buffer.data()), size); + assert(result); + return buffer; +} diff --git a/test/TestUtilities/GraphicsUtil.hpp b/test/TestUtilities/GraphicsUtil.hpp index 0ae41b31e..77bfff45d 100644 --- a/test/TestUtilities/GraphicsUtil.hpp +++ b/test/TestUtilities/GraphicsUtil.hpp @@ -6,7 +6,11 @@ #pragma once #include +#include #include +#include + +#include namespace sf { @@ -35,3 +39,5 @@ bool operator==(const sf::Rect& lhs, const Approx>& rhs) return lhs.left == Approx(rhs.value.left) && lhs.top == Approx(rhs.value.top) && lhs.width == Approx(rhs.value.width) && lhs.height == Approx(rhs.value.height); } + +[[nodiscard]] std::vector loadIntoMemory(const std::filesystem::path& path);