Skip to content

Commit

Permalink
test: Add virtual_2d_locator fixture; is_2d_traversable test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mloskot committed Jun 29, 2022
1 parent 7b7c786 commit 86ee473
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 1 deletion.
3 changes: 2 additions & 1 deletion test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ add_subdirectory(color_base)
add_subdirectory(pixel)
add_subdirectory(iterator)
add_subdirectory(locator)
add_subdirectory(virtual_2d_locator)
add_subdirectory(image)
add_subdirectory(image_view)
add_subdirectory(algorithm)
add_subdirectory(image_processing)
add_subdirectory(histogram)
add_subdirectory(histogram)
26 changes: 26 additions & 0 deletions test/core/virtual_2d_locator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright (c) 2022 Mateusz Loskot <mateusz at loskot dot net>
#
# 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)
#
foreach(_name
is_2d_traversable)
set(_test t_core_virtual_2d_locator_${_name})
set(_target test_core_virtual_2d_locator_${_name})

add_executable(${_target} "")
target_sources(${_target} PRIVATE ${_name}.cpp)
target_link_libraries(${_target}
PRIVATE
gil_compile_options
gil_include_directories
gil_dependencies)
target_compile_definitions(${_target} PRIVATE BOOST_GIL_USE_CONCEPT_CHECK)
add_test(NAME ${_test} COMMAND ${_target})

unset(_name)
unset(_target)
unset(_test)
endforeach()
11 changes: 11 additions & 0 deletions test/core/virtual_2d_locator/Jamfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Boost.GIL (Generic Image Library) - tests
#
# Copyright (c) 2022 Mateusz Loskot <[email protected]>
#
# 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)

import testing ;

run is_2d_traversable.cpp ;
41 changes: 41 additions & 0 deletions test/core/virtual_2d_locator/is_2d_traversable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Copyright 2022 Mateusz Loskot <mateusz at loskot dot net>
//
// 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 <boost/gil.hpp>

#include <boost/core/lightweight_test.hpp>

#include "test_fixture.hpp"
#include "core/pixel/test_fixture.hpp"

namespace gil = boost::gil;
namespace fixture = boost::gil::test::fixture;

struct test_pixel_types
{
template <typename Pixel>
void operator()(Pixel const&)
{
using deref_t = fixture::noop_dereference_fn<Pixel>;
using locator_t = gil::virtual_2d_locator<deref_t, false>;
using view_t = gil::image_view<locator_t>;

view_t view;
BOOST_TEST(!view.is_1d_traversable());
}
static void run()
{
boost::mp11::mp_for_each<fixture::pixel_types>(test_pixel_types{});
}
};

int main()
{
test_pixel_types::run();

return boost::report_errors();
}
76 changes: 76 additions & 0 deletions test/core/virtual_2d_locator/test_fixture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Copyright 2022 Mateusz Loskot <mateusz at loskot dot net>
//
// 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
//
#ifndef BOOST_GIL_TEST_CORE_VIRTUAL_2D_LOCATOR_TEST_FIXTURE_HPP
#define BOOST_GIL_TEST_CORE_VIRTUAL_2D_LOCATOR_TEST_FIXTURE_HPP

#include <boost/gil.hpp>
#include <boost/assert.hpp>

#include <stdexcept>

#include "core/pixel/test_fixture.hpp"

namespace boost { namespace gil { namespace test { namespace fixture {

template <typename Pixel>
struct noop_dereference_fn
{
static constexpr bool is_mutable =false;

using const_t = noop_dereference_fn;
using value_type = Pixel;
using reference = value_type;
using const_reference = value_type;
using argument_type = gil::point_t;
using result_type = reference;

static_assert(std::is_same<value_type, reference>::value, "value_type != reference");
static_assert(std::is_same<value_type, result_type>::value, "value_type != result_type");

result_type operator()(gil::point_t const&) const
{
return result_type{};
}
};

template <typename Pixel>
struct locator_dereference_fn
{
static constexpr bool is_mutable =false;

using const_t = locator_dereference_fn;
using value_type = Pixel;
using reference = value_type;
using const_reference = value_type;
using argument_type = gil::point_t;
using result_type = reference;

static_assert(std::is_same<value_type, reference>::value, "value_type != reference");
static_assert(std::is_same<value_type, result_type>::value, "value_type != result_type");

locator_dereference_fn() = default;
locator_dereference_fn(point_t const& dims) : _dimensions(dims) {}

result_type operator()(gil::point_t const& position) const
{
if (position.x >= _dimensions.x)
throw std::out_of_range("position x out of range");
if (position.y >= _dimensions.y)
throw std::out_of_range("position y out of range");

auto pixel = pixel_generator<value_type>::random();
return pixel;
}

private:
gil::point_t _dimensions;
};

}}}} // namespace boost::gil::test::fixture

#endif

0 comments on commit 86ee473

Please sign in to comment.