File: util_image_file_test.cpp

package info (click to toggle)
vkmark 2025.01-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,500 kB
  • sloc: cpp: 28,949; ansic: 39; python: 20; makefile: 3
file content (172 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright © 2017 Collabora Ltd.
 *
 * This file is part of vkmark.
 *
 * vkmark is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * vkmark is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with vkmark. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Alexandros Frantzis <alexandros.frantzis@collabora.com>
 */

#include "src/util.h"

#include "noise_rgb.c"
#include "noise_rgba.c"

#include "catch.hpp"

#include <cstring>

namespace
{

struct TemporarySetDataDir
{
    TemporarySetDataDir(std::string const& dir) { Util::set_data_dir(dir); }
    ~TemporarySetDataDir() { Util::set_data_dir({}); }
};

template <typename ImageStruct>
class ImageEquals : public Catch::MatcherBase<Util::Image>
{
public:
    ImageEquals(ImageStruct& image_struct) : image_struct{image_struct} {}

    virtual bool match(Util::Image const& image) const override
    {
        return image.width == image_struct.width &&
               image.height == image_struct.height &&
               image.size == (sizeof(image_struct.pixel_data) - 1) &&
               !std::memcmp(image.data, image_struct.pixel_data, image.size);
    }

    virtual std::string describe() const override
    {
        std::ostringstream ss;
        ss << "matches image with width:" << image_struct.width
           << " height:" << image_struct.height
           << " size:" << sizeof(image_struct.pixel_data) - 1;
        return ss.str();
    }

    ImageStruct& image_struct;
};

template<typename ImageStruct>
ImageEquals<ImageStruct> Equals(ImageStruct& image_struct)
{
    return ImageEquals<ImageStruct>(image_struct);
}

}

SCENARIO("util image file reads", "")
{
    TemporarySetDataDir set_data_dir{VKMARK_TEST_DATA_DIR};

    GIVEN("An non-existent image file")
    {
        std::string const non_existent_file{"non_existent.txt"};

        WHEN("reading the file")
        {
            THEN("an exception is thrown")
            {
                REQUIRE_THROWS(Util::read_image_file(non_existent_file));
            }
        }
    }

    GIVEN("An image file without alpha")
    {
        std::string const rgb_file{"images/noise-rgb.png"};

        WHEN("reading the file")
        {
            auto const image = Util::read_image_file(rgb_file);

            THEN("the image contents are returned with fixed alpha 0xff")
            {
                REQUIRE_THAT(image, Equals(noise_rgb_image));
            }
        }
    }

    GIVEN("An image file with alpha")
    {
        std::string const rgba_file{"images/noise-rgba.png"};

        WHEN("reading the file")
        {
            auto const image = Util::read_image_file(rgba_file);

            THEN("the image contents are returned with alpha")
            {
                REQUIRE_THAT(image, Equals(noise_rgba_image));
            }
        }
    }
}

SCENARIO("util image object", "")
{
    TemporarySetDataDir set_data_dir{VKMARK_TEST_DATA_DIR};

    GIVEN("An image object")
    {
        auto image = Util::read_image_file("images/noise-rgb.png");
        auto const image_data = image.data;
        auto const image_size = image.size;
        auto const image_width = image.width;
        auto const image_height = image.width;

        WHEN("move constructing a new image object")
        {
            Util::Image new_image{std::move(image)};

            THEN("the old image object is emptied")
            {
                REQUIRE(image.data == nullptr);
            }

            THEN("the new image object contains valid data")
            {
                REQUIRE(new_image.data == image_data);
                REQUIRE(new_image.size == image_size);
                REQUIRE(new_image.width == image_width);
                REQUIRE(new_image.height == image_height);
            }
        }

        WHEN("move assigning to an image object")
        {
            Util::Image new_image;
            new_image = std::move(image);

            THEN("the old image object is emptied")
            {
                REQUIRE(image.data == nullptr);
            }

            THEN("the new image object contains valid data")
            {
                REQUIRE(new_image.data == image_data);
                REQUIRE(new_image.size == image_size);
                REQUIRE(new_image.width == image_width);
                REQUIRE(new_image.height == image_height);
            }
        }
    }
}