Add MLX42 V2
This commit is contained in:
40
MLX42/tests/CMakeLists.txt
Normal file
40
MLX42/tests/CMakeLists.txt
Normal file
@ -0,0 +1,40 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
# Codam Coding College, Amsterdam @ 2022-2023 by Jelle van Kraaij.
|
||||
# See README in the root project for more information.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
set(TEST_EXECUTABLE_NAME unit_tests)
|
||||
|
||||
# Download GoogleTest
|
||||
# -----------------------------------------------------------------------------
|
||||
include(GoogleTest)
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
googletest
|
||||
DOWNLOAD_EXTRACT_TIMESTAMP
|
||||
GIT_REPOSITORY https://github.com/google/googletest
|
||||
GIT_TAG v1.13.0
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
add_executable(
|
||||
${TEST_EXECUTABLE_NAME}
|
||||
tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
${TEST_EXECUTABLE_NAME}
|
||||
GTest::gtest_main
|
||||
mlx42
|
||||
)
|
||||
|
||||
set_property(TARGET ${TEST_EXECUTABLE_NAME} PROPERTY CXX_STANDARD 14)
|
||||
|
||||
# Add tests to CTest
|
||||
# Set working directory to the the testing folder so that the test can find their test files
|
||||
# -----------------------------------------------------------------------------
|
||||
gtest_discover_tests(${TEST_EXECUTABLE_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} TEST_PREFIX "MLX42.")
|
||||
enable_testing()
|
37
MLX42/tests/WindowFixture.hpp
Normal file
37
MLX42/tests/WindowFixture.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// Codam Coding College, Amsterdam @ 2022-2023 by Jelle van Kraaij.
|
||||
// See README in the root project for more information.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
# pragma once
|
||||
|
||||
# include <gtest/gtest.h>
|
||||
# include <MLX42/MLX42.h>
|
||||
|
||||
class Window : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
mlx_t* mlx = nullptr;
|
||||
|
||||
static constexpr const char* name = "MLX42";
|
||||
static const int32_t height = 400;
|
||||
static const int32_t width = 400;
|
||||
|
||||
inline void SetUp() override
|
||||
{
|
||||
// reset error code as it is shared between tests
|
||||
mlx_errno = MLX_SUCCESS;
|
||||
mlx_set_setting(MLX_HEADLESS, true);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
mlx = mlx_init(width, height, name, false);
|
||||
ASSERT_NE(mlx, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
||||
|
||||
inline void TearDown() override
|
||||
{
|
||||
ASSERT_NE(mlx, nullptr);
|
||||
mlx_terminate(mlx);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
||||
};
|
139
MLX42/tests/tests.cpp
Normal file
139
MLX42/tests/tests.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// Codam Coding College, Amsterdam @ 2022-2023 by Jelle van Kraaij.
|
||||
// See README in the root project for more information.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// If your new to gtest follow the following documentation:
|
||||
// http://google.github.io/googletest/primer.html
|
||||
|
||||
#include "WindowFixture.hpp"
|
||||
|
||||
// --------------------------------------------
|
||||
// Fixture for window tests
|
||||
// For every TEST_F(window, ...) the SetUp() and TearDown() functions are called
|
||||
// MLX can be accessed via the mlx variable in each test
|
||||
// For the implementation of the fixture see tests/windowFixture.hpp
|
||||
// --------------------------------------------
|
||||
|
||||
TEST_F(Window, Basic)
|
||||
{
|
||||
// Basic window is already tested in the fixture
|
||||
}
|
||||
|
||||
|
||||
// NOTE: This test cannot be runned with a fixture because the settings need to be set before the window is created
|
||||
TEST(MWindow, Settings)
|
||||
{
|
||||
mlx_errno = MLX_SUCCESS;
|
||||
mlx_set_setting(MLX_STRETCH_IMAGE, true);
|
||||
mlx_set_setting(MLX_MAXIMIZED, true);
|
||||
mlx_set_setting(MLX_DECORATED, true);
|
||||
mlx_set_setting(MLX_FULLSCREEN, true);
|
||||
|
||||
mlx_set_setting(MLX_HEADLESS, true);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
mlx_t *mlx = mlx_init(400, 400, "MLX42", false);
|
||||
ASSERT_NE(mlx, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_terminate(mlx);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
// Set all settings to default
|
||||
mlx_set_setting(MLX_STRETCH_IMAGE, false);
|
||||
mlx_set_setting(MLX_FULLSCREEN, false);
|
||||
mlx_set_setting(MLX_MAXIMIZED, false);
|
||||
mlx_set_setting(MLX_DECORATED, true);
|
||||
}
|
||||
|
||||
TEST_F(Window, SingleImage)
|
||||
{
|
||||
mlx_image_t* img = mlx_new_image(mlx, Window::width / 2, Window::height / 2);
|
||||
ASSERT_NE(img, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
int32_t val = mlx_image_to_window(mlx, img, Window::width / 4 , Window::height / 4);
|
||||
EXPECT_GE(val, 0);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_delete_image(mlx, img);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
||||
|
||||
TEST_F(Window, MultipleImages)
|
||||
{
|
||||
mlx_image_t* img1 = mlx_new_image(mlx, Window::width / 2, Window::height / 2);
|
||||
ASSERT_NE(img1, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_image_t* img2 = mlx_new_image(mlx, Window::width, Window::height);
|
||||
ASSERT_NE(img2, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
int32_t val1 = mlx_image_to_window(mlx, img1, Window::width / 4, Window::height / 4);
|
||||
EXPECT_GE(val1, 0);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
int32_t val2 = mlx_image_to_window(mlx, img2, 0, 0);
|
||||
EXPECT_GE(val2, 0);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_delete_image(mlx, img1);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_delete_image(mlx, img2);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
static void ft_draw(void* param)
|
||||
{
|
||||
static char buf[256];
|
||||
static int32_t count = 0;
|
||||
static mlx_image_t* img = nullptr;
|
||||
mlx_t* mlx = (mlx_t*)param;
|
||||
|
||||
if (img == nullptr)
|
||||
{
|
||||
mlx_delete_image(mlx, img);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
||||
|
||||
// Cheap itoa lol
|
||||
memset(buf, '\0', sizeof(buf));
|
||||
snprintf(buf, sizeof(buf), "%d", count);
|
||||
|
||||
img = mlx_put_string(mlx, buf, 0, 0);
|
||||
ASSERT_NE(img, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
if (count >= 420)
|
||||
{
|
||||
mlx_close_window(mlx);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
TEST_F(Window, stringTortureTest)
|
||||
{
|
||||
mlx_image_t *img = mlx_new_image(mlx, Window::width / 2, Window::height / 2);
|
||||
ASSERT_NE(img, nullptr);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
memset(img->pixels, 255, sizeof(int32_t) * img->width * img->height);
|
||||
|
||||
int32_t val_window = mlx_image_to_window(mlx, img, Window::width / 4 , Window::height / 4);
|
||||
EXPECT_GE(val_window, 0);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
bool val_hook = mlx_loop_hook(mlx, ft_draw, mlx);
|
||||
EXPECT_EQ(val_hook, true);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_loop(mlx);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
|
||||
mlx_delete_image(mlx, img);
|
||||
ASSERT_EQ(mlx_errno, MLX_SUCCESS);
|
||||
}
|
Reference in New Issue
Block a user