From 140378f47d4b4b6e5c1eb2dc0160772159b2530b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81oskot?= Date: Mon, 27 Jun 2022 23:29:27 +0200 Subject: [PATCH] test: Add test cases for image with empty dimensions Test fix #649 --- test/core/image/CMakeLists.txt | 3 +- test/core/image/Jamfile | 1 + test/core/image/empty_dimensions.cpp | 91 ++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 test/core/image/empty_dimensions.cpp diff --git a/test/core/image/CMakeLists.txt b/test/core/image/CMakeLists.txt index 6574cf2757..e121f3f9bf 100644 --- a/test/core/image/CMakeLists.txt +++ b/test/core/image/CMakeLists.txt @@ -7,7 +7,8 @@ # foreach(_name concepts - image) + image + empty_dimensions) set(_test t_core_image_${_name}) set(_target test_core_image_${_name}) diff --git a/test/core/image/Jamfile b/test/core/image/Jamfile index 07da63467d..e32d74da8b 100644 --- a/test/core/image/Jamfile +++ b/test/core/image/Jamfile @@ -11,3 +11,4 @@ import testing ; compile concepts.cpp ; run image.cpp ; +run empty_dimensions.cpp ; diff --git a/test/core/image/empty_dimensions.cpp b/test/core/image/empty_dimensions.cpp new file mode 100644 index 0000000000..9a665e490f --- /dev/null +++ b/test/core/image/empty_dimensions.cpp @@ -0,0 +1,91 @@ +// +// Copyright 2019-2020 Mateusz Loskot +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +#include + +#include + +#include "test_fixture.hpp" +#include "test_utility_output_stream.hpp" +#include "core/pixel/test_fixture.hpp" + +namespace gil = boost::gil; +namespace fixture = boost::gil::test::fixture; + +// Test cases of memory leak potential for image created with empty dimesions, +// see https://github.com/boostorg/gil/pull/649 +// The main goal of these test cases is to trigger any memory leak detectors. + +void test_default_constructor() +{ + boost::gil::rgb8_image_t image; + BOOST_TEST_EQ(image.width(), 0); + BOOST_TEST_EQ(image.height(), 0); +} + +void test_copy_constructor_with_empty_image() +{ + boost::gil::rgb8_image_t image1; + boost::gil::rgb8_image_t image2(image1); + BOOST_TEST_EQ(image2.width(), 0); + BOOST_TEST_EQ(image2.height(), 0); +} + +struct test_constructor_with_empty_dimensions +{ + template + void operator()(Image const &) + { + using image_t = Image; + image_t image(0, 0); + BOOST_TEST_EQ(image.width(), 0); + BOOST_TEST_EQ(image.height(), 0); + } + + static void run() + { + boost::mp11::mp_for_each(test_constructor_with_empty_dimensions{}); + } +}; + +struct test_constructor_with_empty_dimensions_with_pixel +{ + template + void operator()(Image const &) + { + using image_t = Image; + gil::point_t const dimensions{0, 0}; + using pixel_t = typename image_t::view_t::value_type; + pixel_t const rnd_pixel = fixture::pixel_generator::random(); + image_t image(dimensions, rnd_pixel); + BOOST_TEST_EQ(image.width(), dimensions.x); + BOOST_TEST_EQ(image.height(), dimensions.y); + + bool none_visited = true; + for (pixel_t const &p : gil::view(image)) + { + none_visited = false; + BOOST_TEST_EQ(p, rnd_pixel); + } + BOOST_TEST(none_visited); + } + + static void run() + { + boost::mp11::mp_for_each(test_constructor_with_empty_dimensions_with_pixel{}); + } +}; + +int main() +{ + test_default_constructor(); + test_copy_constructor_with_empty_image(); + test_constructor_with_empty_dimensions::run(); + test_constructor_with_empty_dimensions_with_pixel::run(); + + return ::boost::report_errors(); +}