From 501125ba4b4a31e92b7f893b00953caa928a0aa7 Mon Sep 17 00:00:00 2001 From: Tim Sylvester Date: Wed, 4 Oct 2023 09:05:22 -0700 Subject: [PATCH] Add checks for enum conversions and `reduceMemoryUse` to improve coverage --- test/CMakeLists.txt | 1 + test/gl/context.test.cpp | 5 ++ test/gl/enum.test.cpp | 160 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 test/gl/enum.test.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bad61b04c..a11b58587 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -129,6 +129,7 @@ if(MLN_WITH_OPENGL) PRIVATE ${PROJECT_SOURCE_DIR}/test/api/custom_layer.test.cpp ${PROJECT_SOURCE_DIR}/test/gl/bucket.test.cpp + ${PROJECT_SOURCE_DIR}/test/gl/enum.test.cpp ${PROJECT_SOURCE_DIR}/test/gl/context.test.cpp ${PROJECT_SOURCE_DIR}/test/gl/gl_functions.test.cpp ${PROJECT_SOURCE_DIR}/test/gl/object.test.cpp diff --git a/test/gl/context.test.cpp b/test/gl/context.test.cpp index c8bc88fd6..85311a9c8 100644 --- a/test/gl/context.test.cpp +++ b/test/gl/context.test.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -114,7 +115,11 @@ TEST(GLContextMode, Shared) { MBGL_CHECK_ERROR(glEnableVertexAttribArray(paintShader.a_pos)); MBGL_CHECK_ERROR(glVertexAttribPointer(paintShader.a_pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr)); MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, 3)); + + frontend.getRenderer()->reduceMemoryUse(); + ASSERT_TRUE(static_cast(frontend.getBackend()->getContext()).empty()); } test::checkImage("test/fixtures/shared_context", frontend.render(map).image, 0.5, 0.1); } + diff --git a/test/gl/enum.test.cpp b/test/gl/enum.test.cpp new file mode 100644 index 000000000..d4802232e --- /dev/null +++ b/test/gl/enum.test.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + +using namespace mbgl; +using namespace mbgl::gfx; +using namespace mbgl::gl; + +TEST(GL, DrawModeType) { + ASSERT_EQ(GL_POINTS, Enum::to(DrawModeType::Points)); + ASSERT_EQ(GL_LINES, Enum::to(DrawModeType::Lines)); + ASSERT_EQ(GL_LINE_LOOP, Enum::to(DrawModeType::LineLoop)); + ASSERT_EQ(GL_LINE_STRIP, Enum::to(DrawModeType::LineStrip)); + ASSERT_EQ(GL_TRIANGLES, Enum::to(DrawModeType::Triangles)); + ASSERT_EQ(GL_TRIANGLE_STRIP, Enum::to(DrawModeType::TriangleStrip)); + ASSERT_EQ(GL_TRIANGLE_FAN, Enum::to(DrawModeType::TriangleFan)); + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); +} + +namespace { +template +auto enumIdentity(T x) { return Enum::from(Enum::to(x)); } +} + +TEST(GL, ColorBlendEquationType) { + //const auto identity = [](auto x){ return Enum::from(Enum::to(x)); }; + ASSERT_EQ(ColorBlendEquationType::Add, enumIdentity(ColorBlendEquationType::Add)); + ASSERT_EQ(ColorBlendEquationType::Subtract, enumIdentity(ColorBlendEquationType::Subtract)); + ASSERT_EQ(ColorBlendEquationType::ReverseSubtract, enumIdentity(ColorBlendEquationType::ReverseSubtract)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(ColorBlendEquationType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, ColorBlendFactorType) { + ASSERT_EQ(ColorBlendFactorType::Zero, enumIdentity(ColorBlendFactorType::Zero)); + ASSERT_EQ(ColorBlendFactorType::One, enumIdentity(ColorBlendFactorType::One)); + ASSERT_EQ(ColorBlendFactorType::SrcColor, enumIdentity(ColorBlendFactorType::SrcColor)); + ASSERT_EQ(ColorBlendFactorType::OneMinusSrcColor, enumIdentity(ColorBlendFactorType::OneMinusSrcColor)); + ASSERT_EQ(ColorBlendFactorType::DstColor, enumIdentity(ColorBlendFactorType::DstColor)); + ASSERT_EQ(ColorBlendFactorType::OneMinusDstColor, enumIdentity(ColorBlendFactorType::OneMinusDstColor)); + ASSERT_EQ(ColorBlendFactorType::SrcAlpha, enumIdentity(ColorBlendFactorType::SrcAlpha)); + ASSERT_EQ(ColorBlendFactorType::OneMinusSrcAlpha, enumIdentity(ColorBlendFactorType::OneMinusSrcAlpha)); + ASSERT_EQ(ColorBlendFactorType::DstAlpha, enumIdentity(ColorBlendFactorType::DstAlpha)); + ASSERT_EQ(ColorBlendFactorType::OneMinusDstAlpha, enumIdentity(ColorBlendFactorType::OneMinusDstAlpha)); + ASSERT_EQ(ColorBlendFactorType::ConstantColor, enumIdentity(ColorBlendFactorType::ConstantColor)); + ASSERT_EQ(ColorBlendFactorType::OneMinusConstantColor, enumIdentity(ColorBlendFactorType::OneMinusConstantColor)); + ASSERT_EQ(ColorBlendFactorType::ConstantAlpha, enumIdentity(ColorBlendFactorType::ConstantAlpha)); + ASSERT_EQ(ColorBlendFactorType::OneMinusConstantAlpha, enumIdentity(ColorBlendFactorType::OneMinusConstantAlpha)); + ASSERT_EQ(ColorBlendFactorType::SrcAlphaSaturate, enumIdentity(ColorBlendFactorType::SrcAlphaSaturate)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(ColorBlendFactorType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, DepthFunctionType) { + ASSERT_EQ(DepthFunctionType::Never, enumIdentity(DepthFunctionType::Never)); + ASSERT_EQ(DepthFunctionType::Less, enumIdentity(DepthFunctionType::Less)); + ASSERT_EQ(DepthFunctionType::Equal, enumIdentity(DepthFunctionType::Equal)); + ASSERT_EQ(DepthFunctionType::LessEqual, enumIdentity(DepthFunctionType::LessEqual)); + ASSERT_EQ(DepthFunctionType::Greater, enumIdentity(DepthFunctionType::Greater)); + ASSERT_EQ(DepthFunctionType::NotEqual, enumIdentity(DepthFunctionType::NotEqual)); + ASSERT_EQ(DepthFunctionType::GreaterEqual, enumIdentity(DepthFunctionType::GreaterEqual)); + ASSERT_EQ(DepthFunctionType::Always, enumIdentity(DepthFunctionType::Always)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(DepthFunctionType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, DepthMaskType) { + ASSERT_EQ(DepthMaskType::ReadOnly, enumIdentity(DepthMaskType::ReadOnly)); + ASSERT_EQ(DepthMaskType::ReadWrite, enumIdentity(DepthMaskType::ReadWrite)); + + // anything non-zero is true, there's no way to represent invalid + ASSERT_EQ(GL_TRUE, Enum::to(static_cast(-1))); +} + +TEST(GL, StencilFunctionType) { + ASSERT_EQ(StencilFunctionType::Never, enumIdentity(StencilFunctionType::Never)); + ASSERT_EQ(StencilFunctionType::Less, enumIdentity(StencilFunctionType::Less)); + ASSERT_EQ(StencilFunctionType::Equal, enumIdentity(StencilFunctionType::Equal)); + ASSERT_EQ(StencilFunctionType::LessEqual, enumIdentity(StencilFunctionType::LessEqual)); + ASSERT_EQ(StencilFunctionType::Greater, enumIdentity(StencilFunctionType::Greater)); + ASSERT_EQ(StencilFunctionType::NotEqual, enumIdentity(StencilFunctionType::NotEqual)); + ASSERT_EQ(StencilFunctionType::GreaterEqual, enumIdentity(StencilFunctionType::GreaterEqual)); + ASSERT_EQ(StencilFunctionType::Always, enumIdentity(StencilFunctionType::Always)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(StencilFunctionType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, StencilOpType) { + ASSERT_EQ(StencilOpType::Keep, enumIdentity(StencilOpType::Keep)); + ASSERT_EQ(StencilOpType::Zero, enumIdentity(StencilOpType::Zero)); + ASSERT_EQ(StencilOpType::Replace, enumIdentity(StencilOpType::Replace)); + ASSERT_EQ(StencilOpType::Increment, enumIdentity(StencilOpType::Increment)); + ASSERT_EQ(StencilOpType::IncrementWrap, enumIdentity(StencilOpType::IncrementWrap)); + ASSERT_EQ(StencilOpType::Decrement, enumIdentity(StencilOpType::Decrement)); + ASSERT_EQ(StencilOpType::DecrementWrap, enumIdentity(StencilOpType::DecrementWrap)); + ASSERT_EQ(StencilOpType::Invert, enumIdentity(StencilOpType::Invert)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(StencilOpType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, CullFaceSideType) { + ASSERT_EQ(CullFaceSideType::Front, enumIdentity(CullFaceSideType::Front)); + ASSERT_EQ(CullFaceSideType::Back, enumIdentity(CullFaceSideType::Back)); + ASSERT_EQ(CullFaceSideType::FrontAndBack, enumIdentity(CullFaceSideType::FrontAndBack)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(CullFaceSideType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, CullFaceWindingType) { + ASSERT_EQ(CullFaceWindingType::Clockwise, enumIdentity(CullFaceWindingType::Clockwise)); + ASSERT_EQ(CullFaceWindingType::CounterClockwise, enumIdentity(CullFaceWindingType::CounterClockwise)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(CullFaceWindingType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, BufferUsageType) { + ASSERT_EQ(BufferUsageType::StreamDraw, enumIdentity(BufferUsageType::StreamDraw)); + ASSERT_EQ(BufferUsageType::StaticDraw, enumIdentity(BufferUsageType::StaticDraw)); + ASSERT_EQ(BufferUsageType::DynamicDraw, enumIdentity(BufferUsageType::DynamicDraw)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(BufferUsageType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, TexturePixelType) { + ASSERT_EQ(TexturePixelType::RGBA, enumIdentity(TexturePixelType::RGBA)); + ASSERT_EQ(TexturePixelType::Alpha, enumIdentity(TexturePixelType::Alpha)); + ASSERT_EQ(TexturePixelType::Stencil, enumIdentity(TexturePixelType::Stencil)); + ASSERT_EQ(TexturePixelType::Depth, enumIdentity(TexturePixelType::Depth)); + ASSERT_EQ(TexturePixelType::Luminance, enumIdentity(TexturePixelType::Luminance)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(TexturePixelType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, TextureChannelDataType) { + ASSERT_EQ(TextureChannelDataType::UnsignedByte, enumIdentity(TextureChannelDataType::UnsignedByte)); + ASSERT_EQ(TextureChannelDataType::HalfFloat, enumIdentity(TextureChannelDataType::HalfFloat)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(TextureChannelDataType{}, Enum::from(GL_RGBA8)); +} + +TEST(GL, RenderbufferPixelType) { + ASSERT_EQ(RenderbufferPixelType::RGBA, enumIdentity(RenderbufferPixelType::RGBA)); + ASSERT_EQ(RenderbufferPixelType::Depth, enumIdentity(RenderbufferPixelType::Depth)); + ASSERT_EQ(RenderbufferPixelType::DepthStencil, enumIdentity(RenderbufferPixelType::DepthStencil)); + + ASSERT_EQ(GL_INVALID_ENUM, Enum::to(static_cast(-1))); + ASSERT_EQ(RenderbufferPixelType{}, Enum::from(GL_RGBA8)); +}