The Battle for Wesnoth  1.19.10+dev
test_sdl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-test"
16 
17 #include "sdl/surface.hpp"
18 #include "sdl/utils.hpp"
19 #include "utils/span.hpp"
20 
21 #include <algorithm>
22 #include <array>
23 #include <boost/test/unit_test.hpp>
24 
25 constexpr uint32_t red = 0xFF'FF'00'00;
26 constexpr uint32_t green = 0xFF'00'FF'00;
27 constexpr uint32_t blue = 0xFF'00'00'FF;
28 constexpr uint32_t yellow = 0xFF'FF'FF'00;
29 constexpr uint32_t white = 0xFF'FF'FF'FF;
30 constexpr uint32_t black = 0xFF'00'00'00;
31 
32 constexpr std::array<uint32_t, 16> img_4x4 {
33  red, white, green, black,
37 };
38 
39 constexpr std::array<uint32_t, 4> img_4x4_to_2x2_result {
40  red, green,
41  blue, yellow,
42 };
43 
44 constexpr std::array<uint32_t, 6> img_4x4_to_3x2_result {
45  red, white, green,
46  blue, white, yellow
47 };
48 
49 template<size_t w, size_t h>
50 surface array_to_surface(const std::array<uint32_t, w * h>& arr)
51 {
52  surface surf{w, h};
53 
54  {
55  surface_lock surf_lock{surf};
56  uint32_t* const pixels = surf_lock.pixels();
57  for(size_t i = 0; i < w * h; ++i) {
58  pixels[i] = arr[i];
59  }
60  }
61 
62  return surf;
63 }
64 
66 
67 BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
68 {
69  surface result = scale_surface_sharp(nullptr, 2, 2);
70  BOOST_CHECK_EQUAL(result, nullptr);
71 }
72 
73 BOOST_AUTO_TEST_CASE(test_scale_sharp_zero)
74 {
75  surface src = array_to_surface<4, 4>(img_4x4);
76  surface result = scale_surface_sharp(src, 0, 0);
77  BOOST_CHECK_EQUAL(result->w, 0);
78  BOOST_CHECK_EQUAL(result->h, 0);
79 }
80 
81 BOOST_AUTO_TEST_CASE(test_scale_sharp_round)
82 {
83  surface src = array_to_surface<4, 4>(img_4x4);
84  surface result = scale_surface_sharp(src, 2, 2);
85  const_surface_lock lock{result};
86  auto result_pixels = utils::span(lock.pixels(), result.area());
87  BOOST_CHECK_EQUAL_COLLECTIONS(
88  result_pixels.begin(), result_pixels.end(), img_4x4_to_2x2_result.begin(), img_4x4_to_2x2_result.end());
89 }
90 
91 BOOST_AUTO_TEST_CASE(test_scale_sharp_fractional)
92 {
93  surface src = array_to_surface<4, 4>(img_4x4);
94  surface result = scale_surface_sharp(src, 3, 2);
95  const_surface_lock lock{result};
96  auto result_pixels = utils::span(lock.pixels(), result.area());
97  BOOST_CHECK_EQUAL_COLLECTIONS(
98  result_pixels.begin(), result_pixels.end(), img_4x4_to_3x2_result.begin(), img_4x4_to_3x2_result.end());
99 }
100 
101 BOOST_AUTO_TEST_SUITE_END()
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:71
std::size_t area() const
Total area of the surface in square pixels.
Definition: surface.cpp:103
std::size_t i
Definition: function.cpp:1030
int w
surface surf
Image.
rect src
Non-transparent portion of the surface to compose.
BOOST_AUTO_TEST_SUITE(filesystem)
constexpr uint32_t black
Definition: test_sdl.cpp:30
constexpr uint32_t red
Definition: test_sdl.cpp:25
constexpr std::array< uint32_t, 6 > img_4x4_to_3x2_result
Definition: test_sdl.cpp:44
constexpr std::array< uint32_t, 16 > img_4x4
Definition: test_sdl.cpp:32
constexpr uint32_t green
Definition: test_sdl.cpp:26
surface array_to_surface(const std::array< uint32_t, w *h > &arr)
Definition: test_sdl.cpp:50
constexpr std::array< uint32_t, 4 > img_4x4_to_2x2_result
Definition: test_sdl.cpp:39
constexpr uint32_t yellow
Definition: test_sdl.cpp:28
constexpr uint32_t white
Definition: test_sdl.cpp:29
constexpr uint32_t blue
Definition: test_sdl.cpp:27
BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
Definition: test_sdl.cpp:67
surface scale_surface_sharp(const surface &surf, int w, int h)
Scale a surface using modified nearest neighbour algorithm.
Definition: utils.cpp:358
#define h