SFML/test/Graphics/VertexBuffer.test.cpp

182 lines
6.5 KiB
C++
Raw Normal View History

#include <SFML/Graphics/VertexBuffer.hpp>
2023-08-06 02:55:44 +08:00
// Other 1st party headers
2023-02-13 01:51:35 +08:00
#include <SFML/Graphics/Vertex.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2023-02-13 01:51:35 +08:00
#include <GraphicsUtil.hpp>
#include <array>
#include <type_traits>
2023-01-18 12:51:08 +08:00
// Skip these tests with [.display] because they produce flakey failures in CI when using xvfb-run
TEST_CASE("[Graphics] sf::VertexBuffer", "[.display]")
2023-02-13 01:51:35 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
STATIC_CHECK(std::is_copy_constructible_v<sf::VertexBuffer>);
STATIC_CHECK(std::is_copy_assignable_v<sf::VertexBuffer>);
STATIC_CHECK(std::is_move_constructible_v<sf::VertexBuffer>);
STATIC_CHECK(!std::is_nothrow_move_constructible_v<sf::VertexBuffer>);
STATIC_CHECK(std::is_move_assignable_v<sf::VertexBuffer>);
STATIC_CHECK(!std::is_nothrow_move_assignable_v<sf::VertexBuffer>);
STATIC_CHECK(std::is_nothrow_swappable_v<sf::VertexBuffer>);
}
2023-02-13 01:51:35 +08:00
// Skip tests if vertex buffers aren't available
if (!sf::VertexBuffer::isAvailable())
return;
2023-01-18 12:51:08 +08:00
SECTION("Construction")
2023-02-13 01:51:35 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Default constructor")
2023-02-13 01:51:35 +08:00
{
const sf::VertexBuffer vertexBuffer;
CHECK(vertexBuffer.getVertexCount() == 0);
CHECK(vertexBuffer.getNativeHandle() == 0);
CHECK(vertexBuffer.getPrimitiveType() == sf::PrimitiveType::Points);
CHECK(vertexBuffer.getUsage() == sf::VertexBuffer::Usage::Stream);
2023-02-13 01:51:35 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Primitive type constructor")
2023-02-13 01:51:35 +08:00
{
const sf::VertexBuffer vertexBuffer(sf::PrimitiveType::Triangles);
CHECK(vertexBuffer.getVertexCount() == 0);
CHECK(vertexBuffer.getNativeHandle() == 0);
CHECK(vertexBuffer.getPrimitiveType() == sf::PrimitiveType::Triangles);
CHECK(vertexBuffer.getUsage() == sf::VertexBuffer::Usage::Stream);
2023-02-13 01:51:35 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Usage constructor")
2023-02-13 01:51:35 +08:00
{
const sf::VertexBuffer vertexBuffer(sf::VertexBuffer::Usage::Static);
2023-02-13 01:51:35 +08:00
CHECK(vertexBuffer.getVertexCount() == 0);
CHECK(vertexBuffer.getNativeHandle() == 0);
CHECK(vertexBuffer.getPrimitiveType() == sf::PrimitiveType::Points);
CHECK(vertexBuffer.getUsage() == sf::VertexBuffer::Usage::Static);
2023-02-13 01:51:35 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Primitive type and usage constructor")
2023-02-13 01:51:35 +08:00
{
const sf::VertexBuffer vertexBuffer(sf::PrimitiveType::LineStrip, sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
CHECK(vertexBuffer.getVertexCount() == 0);
CHECK(vertexBuffer.getNativeHandle() == 0);
CHECK(vertexBuffer.getPrimitiveType() == sf::PrimitiveType::LineStrip);
CHECK(vertexBuffer.getUsage() == sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("Copy semantics")
2023-02-13 01:51:35 +08:00
{
const sf::VertexBuffer vertexBuffer(sf::PrimitiveType::LineStrip, sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
2023-01-18 12:51:08 +08:00
SECTION("Construction")
2023-02-13 01:51:35 +08:00
{
2023-04-09 02:51:35 +08:00
const sf::VertexBuffer vertexBufferCopy(vertexBuffer); // NOLINT(performance-unnecessary-copy-initialization)
2023-02-13 01:51:35 +08:00
CHECK(vertexBufferCopy.getVertexCount() == 0);
CHECK(vertexBufferCopy.getNativeHandle() == 0);
CHECK(vertexBufferCopy.getPrimitiveType() == sf::PrimitiveType::LineStrip);
CHECK(vertexBufferCopy.getUsage() == sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Assignment")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer vertexBufferCopy;
vertexBufferCopy = vertexBuffer;
CHECK(vertexBufferCopy.getVertexCount() == 0);
CHECK(vertexBufferCopy.getNativeHandle() == 0);
CHECK(vertexBufferCopy.getPrimitiveType() == sf::PrimitiveType::LineStrip);
CHECK(vertexBufferCopy.getUsage() == sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
}
}
2023-01-18 12:51:08 +08:00
SECTION("create()")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer vertexBuffer;
CHECK(vertexBuffer.create(100));
CHECK(vertexBuffer.getVertexCount() == 100);
}
2023-01-18 12:51:08 +08:00
SECTION("update()")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer vertexBuffer;
std::array<sf::Vertex, 128> vertices{};
2023-01-18 12:51:08 +08:00
SECTION("Vertices")
2023-02-13 01:51:35 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Uninitialized buffer")
2023-02-13 01:51:35 +08:00
{
CHECK(!vertexBuffer.update(vertices.data()));
}
CHECK(vertexBuffer.create(128));
2023-01-18 12:51:08 +08:00
SECTION("Null vertices")
2023-02-13 01:51:35 +08:00
{
CHECK(!vertexBuffer.update(nullptr));
}
CHECK(vertexBuffer.update(vertices.data()));
CHECK(vertexBuffer.getVertexCount() == 128);
CHECK(vertexBuffer.getNativeHandle() != 0);
}
2023-01-18 12:51:08 +08:00
SECTION("Vertices, count, and offset")
2023-02-13 01:51:35 +08:00
{
CHECK(vertexBuffer.create(128));
2023-01-18 12:51:08 +08:00
SECTION("Count + offset too large")
2023-02-13 01:51:35 +08:00
{
CHECK(!vertexBuffer.update(vertices.data(), 100, 100));
}
CHECK(vertexBuffer.update(vertices.data(), 128, 0));
CHECK(vertexBuffer.getVertexCount() == 128);
}
2023-01-18 12:51:08 +08:00
SECTION("Another buffer")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer otherVertexBuffer;
CHECK(!vertexBuffer.update(otherVertexBuffer));
CHECK(otherVertexBuffer.create(42));
CHECK(!vertexBuffer.update(otherVertexBuffer));
}
}
2023-01-18 12:51:08 +08:00
SECTION("swap()")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer vertexBuffer1(sf::PrimitiveType::LineStrip, sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
CHECK(vertexBuffer1.create(50));
sf::VertexBuffer vertexBuffer2(sf::PrimitiveType::TriangleStrip, sf::VertexBuffer::Usage::Stream);
2023-02-13 01:51:35 +08:00
CHECK(vertexBuffer2.create(60));
2023-05-13 12:25:06 +08:00
sf::swap(vertexBuffer1, vertexBuffer2);
2023-02-13 01:51:35 +08:00
CHECK(vertexBuffer1.getVertexCount() == 60);
CHECK(vertexBuffer1.getNativeHandle() != 0);
CHECK(vertexBuffer1.getPrimitiveType() == sf::PrimitiveType::TriangleStrip);
CHECK(vertexBuffer1.getUsage() == sf::VertexBuffer::Usage::Stream);
2023-02-13 01:51:35 +08:00
CHECK(vertexBuffer2.getVertexCount() == 50);
CHECK(vertexBuffer2.getNativeHandle() != 0);
CHECK(vertexBuffer2.getPrimitiveType() == sf::PrimitiveType::LineStrip);
CHECK(vertexBuffer2.getUsage() == sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get primitive type")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer vertexBuffer;
vertexBuffer.setPrimitiveType(sf::PrimitiveType::TriangleFan);
CHECK(vertexBuffer.getPrimitiveType() == sf::PrimitiveType::TriangleFan);
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get usage")
2023-02-13 01:51:35 +08:00
{
sf::VertexBuffer vertexBuffer;
vertexBuffer.setUsage(sf::VertexBuffer::Usage::Dynamic);
CHECK(vertexBuffer.getUsage() == sf::VertexBuffer::Usage::Dynamic);
2023-02-13 01:51:35 +08:00
}
}