Add tests for various loadFromStream() functions

This commit is contained in:
Chris Thrasher 2023-11-16 10:03:29 -07:00 committed by GitHub
parent 0da4382d6c
commit daa1efcf05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 200 additions and 25 deletions

View File

@ -1,8 +1,12 @@
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
// Other 1st party headers
#include <SFML/System/FileInputStream.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <GraphicsUtil.hpp> #include <GraphicsUtil.hpp>
#include <fstream>
#include <type_traits> #include <type_traits>
TEST_CASE("[Graphics] sf::Font", runDisplayTests()) TEST_CASE("[Graphics] sf::Font", runDisplayTests())
@ -29,28 +33,124 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests())
SECTION("loadFromFile()") SECTION("loadFromFile()")
{ {
sf::Font font; sf::Font font;
REQUIRE(font.loadFromFile("Graphics/tuffy.ttf"));
CHECK(font.getInfo().family == "Tuffy"); SECTION("Invalid filename")
const auto& glyph = font.getGlyph(0x45, 16, false); {
CHECK(glyph.advance == 9); CHECK(!font.loadFromFile("does/not/exist.ttf"));
CHECK(glyph.lsbDelta == 9); }
CHECK(glyph.rsbDelta == 16);
CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12})); SECTION("Successful load")
CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12})); {
CHECK(font.hasGlyph(0x41)); REQUIRE(font.loadFromFile("Graphics/tuffy.ttf"));
CHECK(font.hasGlyph(0xC0)); CHECK(font.getInfo().family == "Tuffy");
CHECK(font.getKerning(0x41, 0x42, 12) == -1); const auto& glyph = font.getGlyph(0x45, 16, false);
CHECK(font.getKerning(0x43, 0x44, 24, true) == 0); CHECK(glyph.advance == 9);
CHECK(font.getLineSpacing(24) == 30); CHECK(glyph.lsbDelta == 9);
CHECK(font.getUnderlinePosition(36) == Approx(2.20312f)); CHECK(glyph.rsbDelta == 16);
CHECK(font.getUnderlineThickness(48) == Approx(1.17188f)); CHECK(glyph.bounds == sf::FloatRect({0, -12}, {8, 12}));
const auto& texture = font.getTexture(10); CHECK(glyph.textureRect == sf::IntRect({2, 5}, {8, 12}));
CHECK(texture.getSize() == sf::Vector2u(128, 128)); CHECK(font.hasGlyph(0x41));
CHECK(texture.isSmooth()); CHECK(font.hasGlyph(0xC0));
CHECK(!texture.isSrgb()); CHECK(font.getKerning(0x41, 0x42, 12) == -1);
CHECK(!texture.isRepeated()); CHECK(font.getKerning(0x43, 0x44, 24, true) == 0);
CHECK(texture.getNativeHandle() != 0); CHECK(font.getLineSpacing(24) == 30);
CHECK(font.isSmooth()); 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<std::byte> buffer(static_cast<std::size_t>(size));
REQUIRE(file.read(reinterpret_cast<char*>(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") SECTION("Set/get smooth")

View File

@ -1,5 +1,8 @@
#include <SFML/Graphics/Image.hpp> #include <SFML/Graphics/Image.hpp>
// Other 1st party headers
#include <SFML/System/FileInputStream.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <GraphicsUtil.hpp> #include <GraphicsUtil.hpp>
@ -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()") SECTION("saveToFile()")
{ {
sf::Image image; sf::Image image;

View File

@ -1,5 +1,8 @@
#include <SFML/Graphics/Shader.hpp> #include <SFML/Graphics/Shader.hpp>
// Other 1st party headers
#include <SFML/System/FileInputStream.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <type_traits> #include <type_traits>
@ -171,9 +174,10 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests())
SECTION("loadFromFile()") SECTION("loadFromFile()")
{ {
sf::Shader shader;
SECTION("One shader") SECTION("One shader")
{ {
sf::Shader shader;
CHECK(!shader.loadFromFile("does-not-exist.vert", sf::Shader::Type::Vertex)); CHECK(!shader.loadFromFile("does-not-exist.vert", sf::Shader::Type::Vertex));
CHECK(shader.loadFromFile("Graphics/shader.vert", sf::Shader::Type::Vertex) == sf::Shader::isAvailable()); 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") SECTION("Two shaders")
{ {
sf::Shader shader;
CHECK(!shader.loadFromFile("does-not-exist.vert", "Graphics/shader.frag")); 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", "does-not-exist.frag"));
CHECK(shader.loadFromFile("Graphics/shader.vert", "Graphics/shader.frag") == sf::Shader::isAvailable()); 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") SECTION("Three shaders")
{ {
sf::Shader shader;
CHECK(!shader.loadFromFile("does-not-exist.vert", "Graphics/shader.geom", "Graphics/shader.frag")); 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", "does-not-exist.geom", "Graphics/shader.frag"));
CHECK(!shader.loadFromFile("Graphics/shader.vert", "Graphics/shader.geom", "does-not-exist.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(shader.loadFromMemory(vertexSource, geometrySource, fragmentSource) == sf::Shader::isGeometryAvailable());
CHECK(static_cast<bool>(shader.getNativeHandle()) == sf::Shader::isAvailable()); CHECK(static_cast<bool>(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<bool>(shader.getNativeHandle()) == sf::Shader::isGeometryAvailable());
}
}
} }

View File

@ -3,6 +3,8 @@
// Other 1st party headers // Other 1st party headers
#include <SFML/Graphics/Image.hpp> #include <SFML/Graphics/Image.hpp>
#include <SFML/System/FileInputStream.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <GraphicsUtil.hpp> #include <GraphicsUtil.hpp>
@ -65,6 +67,19 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests())
CHECK(texture.getNativeHandle() != 0); 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") SECTION("Copy semantics")
{ {
constexpr std::uint8_t red[] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF}; constexpr std::uint8_t red[] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF};