The Battle for Wesnoth  1.19.7+dev
test_sdl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2024
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 
20 #include <algorithm>
21 #include <array>
22 #include <boost/test/unit_test.hpp>
23 
24 constexpr uint32_t red = 0xFF'FF'00'00;
25 constexpr uint32_t green = 0xFF'00'FF'00;
26 constexpr uint32_t blue = 0xFF'00'00'FF;
27 constexpr uint32_t yellow = 0xFF'FF'FF'00;
28 constexpr uint32_t white = 0xFF'FF'FF'FF;
29 constexpr uint32_t black = 0xFF'00'00'00;
30 
31 constexpr std::array<uint32_t, 16> img_4x4 {
32  red, white, green, black,
36 };
37 
38 constexpr std::array<uint32_t, 4> img_4x4_to_2x2_result {
39  red, green,
40  blue, yellow,
41 };
42 
43 constexpr std::array<uint32_t, 6> img_4x4_to_3x2_result {
44  red, white, green,
45  blue, white, yellow
46 };
47 
48 template<size_t w, size_t h>
49 surface array_to_surface(const std::array<uint32_t, w * h>& arr)
50 {
51  surface surf{w, h};
52 
53  {
54  surface_lock surf_lock{surf};
55  uint32_t* const pixels = surf_lock.pixels();
56  for(size_t i = 0; i < w * h; ++i) {
57  pixels[i] = arr[i];
58  }
59  }
60 
61  return surf;
62 }
63 
64 static std::vector<uint32_t> surface_to_vec(const surface& surf)
65 {
67  const uint32_t* const pixels = lock.pixels();
68  std::vector<uint32_t> pixel_vec;
69  const int surf_size = surf->w * surf->h;
70  std::copy(pixels, pixels + surf_size, std::back_inserter(pixel_vec));
71  return pixel_vec;
72 }
73 
75 
76 BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
77 {
78  surface result = scale_surface_sharp(nullptr, 2, 2);
79  BOOST_CHECK_EQUAL(result, nullptr);
80 }
81 
82 BOOST_AUTO_TEST_CASE(test_scale_sharp_zero)
83 {
84  surface src = array_to_surface<4, 4>(img_4x4);
85  surface result = scale_surface_sharp(src, 0, 0);
86  BOOST_CHECK_EQUAL(result->w, 0);
87  BOOST_CHECK_EQUAL(result->h, 0);
88 }
89 
90 BOOST_AUTO_TEST_CASE(test_scale_sharp_round)
91 {
92  surface src = array_to_surface<4, 4>(img_4x4);
93  surface result = scale_surface_sharp(src, 2, 2);
94  std::vector<uint32_t> result_pixels = surface_to_vec(result);
95  BOOST_CHECK_EQUAL_COLLECTIONS(
96  result_pixels.begin(), result_pixels.end(), img_4x4_to_2x2_result.begin(), img_4x4_to_2x2_result.end());
97 }
98 
99 BOOST_AUTO_TEST_CASE(test_scale_sharp_fractional)
100 {
101  surface src = array_to_surface<4, 4>(img_4x4);
102  surface result = scale_surface_sharp(src, 3, 2);
103  std::vector<uint32_t> result_pixels = surface_to_vec(result);
104  BOOST_CHECK_EQUAL_COLLECTIONS(
105  result_pixels.begin(), result_pixels.end(), img_4x4_to_3x2_result.begin(), img_4x4_to_3x2_result.end());
106 }
107 
108 BOOST_AUTO_TEST_SUITE_END()
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:127
std::size_t i
Definition: function.cpp:1029
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:29
constexpr uint32_t red
Definition: test_sdl.cpp:24
constexpr std::array< uint32_t, 6 > img_4x4_to_3x2_result
Definition: test_sdl.cpp:43
constexpr std::array< uint32_t, 16 > img_4x4
Definition: test_sdl.cpp:31
constexpr uint32_t green
Definition: test_sdl.cpp:25
surface array_to_surface(const std::array< uint32_t, w *h > &arr)
Definition: test_sdl.cpp:49
static std::vector< uint32_t > surface_to_vec(const surface &surf)
Definition: test_sdl.cpp:64
constexpr std::array< uint32_t, 4 > img_4x4_to_2x2_result
Definition: test_sdl.cpp:38
constexpr uint32_t yellow
Definition: test_sdl.cpp:27
constexpr uint32_t white
Definition: test_sdl.cpp:28
constexpr uint32_t blue
Definition: test_sdl.cpp:26
BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
Definition: test_sdl.cpp:76
surface scale_surface_sharp(const surface &surf, int w, int h)
Scale a surface using modified nearest neighbour algorithm.
Definition: utils.cpp:354
#define h