From daa1efcf05df7d31da0acfbb476c6958dc3c4fa1 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 16 Nov 2023 10:03:29 -0700 Subject: [PATCH] Add tests for various `loadFromStream()` functions --- test/Graphics/Font.test.cpp | 144 ++++++++++++++++++++++++++++----- test/Graphics/Image.test.cpp | 24 ++++++ test/Graphics/Shader.test.cpp | 42 +++++++++- test/Graphics/Texture.test.cpp | 15 ++++ 4 files changed, 200 insertions(+), 25 deletions(-) diff --git a/test/Graphics/Font.test.cpp b/test/Graphics/Font.test.cpp index 5f41e7824..b6a4380d4 100644 --- a/test/Graphics/Font.test.cpp +++ b/test/Graphics/Font.test.cpp @@ -1,8 +1,12 @@ #include +// Other 1st party headers +#include + #include #include +#include #include TEST_CASE("[Graphics] sf::Font", runDisplayTests()) @@ -29,28 +33,124 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests()) SECTION("loadFromFile()") { sf::Font font; - REQUIRE(font.loadFromFile("Graphics/tuffy.ttf")); - 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("Invalid filename") + { + CHECK(!font.loadFromFile("does/not/exist.ttf")); + } + + SECTION("Successful load") + { + REQUIRE(font.loadFromFile("Graphics/tuffy.ttf")); + 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("loadFromMemory()") + { + sf::Font font; + + SECTION("Invalid data and size") + { + CHECK(!font.loadFromMemory(nullptr, 1)); + const std::byte byte{0xCD}; + CHECK(!font.loadFromMemory(&byte, 0)); + } + + 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; + }(); + + REQUIRE(font.loadFromMemory(memory.data(), memory.size())); + 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("loadFromStream()") + { + sf::Font font; + sf::FileInputStream stream; + + SECTION("Invalid stream") + { + CHECK(!font.loadFromStream(stream)); + } + + SECTION("Successful load") + { + REQUIRE(stream.open("Graphics/tuffy.ttf")); + REQUIRE(font.loadFromStream(stream)); + 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 0a6c40b69..cde8db13e 100644 --- a/test/Graphics/Image.test.cpp +++ b/test/Graphics/Image.test.cpp @@ -1,5 +1,8 @@ #include +// Other 1st party headers +#include + #include #include @@ -190,6 +193,27 @@ TEST_CASE("[Graphics] sf::Image") } } + SECTION("loadFromStream()") + { + sf::Image image; + sf::FileInputStream stream; + + SECTION("Invalid stream") + { + CHECK(!image.loadFromStream(stream)); + } + + SECTION("Successful load") + { + CHECK(stream.open("Graphics/sfml-logo-big.png")); + REQUIRE(image.loadFromStream(stream)); + 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()") { sf::Image image; diff --git a/test/Graphics/Shader.test.cpp b/test/Graphics/Shader.test.cpp index f6f70d9d4..48d32944c 100644 --- a/test/Graphics/Shader.test.cpp +++ b/test/Graphics/Shader.test.cpp @@ -1,5 +1,8 @@ #include +// Other 1st party headers +#include + #include #include @@ -171,9 +174,10 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) SECTION("loadFromFile()") { + sf::Shader shader; + SECTION("One shader") { - sf::Shader shader; CHECK(!shader.loadFromFile("does-not-exist.vert", sf::Shader::Type::Vertex)); CHECK(shader.loadFromFile("Graphics/shader.vert", sf::Shader::Type::Vertex) == sf::Shader::isAvailable()); @@ -185,7 +189,6 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) SECTION("Two shaders") { - sf::Shader shader; CHECK(!shader.loadFromFile("does-not-exist.vert", "Graphics/shader.frag")); CHECK(!shader.loadFromFile("Graphics/shader.vert", "does-not-exist.frag")); CHECK(shader.loadFromFile("Graphics/shader.vert", "Graphics/shader.frag") == sf::Shader::isAvailable()); @@ -194,7 +197,6 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) SECTION("Three shaders") { - sf::Shader shader; CHECK(!shader.loadFromFile("does-not-exist.vert", "Graphics/shader.geom", "Graphics/shader.frag")); CHECK(!shader.loadFromFile("Graphics/shader.vert", "does-not-exist.geom", "Graphics/shader.frag")); CHECK(!shader.loadFromFile("Graphics/shader.vert", "Graphics/shader.geom", "does-not-exist.frag")); @@ -214,4 +216,38 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests()) CHECK(shader.loadFromMemory(vertexSource, geometrySource, fragmentSource) == sf::Shader::isGeometryAvailable()); CHECK(static_cast(shader.getNativeHandle()) == sf::Shader::isAvailable()); } + + SECTION("loadFromStream()") + { + sf::Shader shader; + sf::FileInputStream vertexShaderStream; + REQUIRE(vertexShaderStream.open("Graphics/shader.vert")); + + sf::FileInputStream fragmentShaderStream; + REQUIRE(fragmentShaderStream.open("Graphics/shader.frag")); + + sf::FileInputStream geometryShaderStream; + REQUIRE(geometryShaderStream.open("Graphics/shader.geom")); + + SECTION("One shader") + { + REQUIRE(shader.loadFromStream(vertexShaderStream, sf::Shader::Type::Vertex) == sf::Shader::isAvailable()); + } + + SECTION("Two shaders") + { + 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)); + REQUIRE(shader.loadFromStream(vertexShaderStream, geometryShaderStream, fragmentShaderStream) == + sf::Shader::isGeometryAvailable()); + CHECK(static_cast(shader.getNativeHandle()) == sf::Shader::isGeometryAvailable()); + } + } } diff --git a/test/Graphics/Texture.test.cpp b/test/Graphics/Texture.test.cpp index 0659b4ece..6cbfe26d2 100644 --- a/test/Graphics/Texture.test.cpp +++ b/test/Graphics/Texture.test.cpp @@ -3,6 +3,8 @@ // Other 1st party headers #include +#include + #include #include @@ -65,6 +67,19 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests()) CHECK(texture.getNativeHandle() != 0); } + SECTION("loadFromStream()") + { + sf::Texture texture; + sf::FileInputStream stream; + REQUIRE(stream.open("Graphics/sfml-logo-big.png")); + REQUIRE(texture.loadFromStream(stream)); + CHECK(texture.getSize() == sf::Vector2u(1001, 304)); + CHECK(!texture.isSmooth()); + CHECK(!texture.isSrgb()); + CHECK(!texture.isRepeated()); + CHECK(texture.getNativeHandle() != 0); + } + SECTION("Copy semantics") { constexpr std::uint8_t red[] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF};