GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/speciallayer.cc Lines: 175 175 100.0 %
Date: 2021-03-17 Branches: 439 1046 42.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "client.h"
25
#include "configmanager.h"
26
#include "configuration.h"
27
#include "dirs.h"
28
29
#include "graphicsmanager.h"
30
31
#include "being/actorsprite.h"
32
33
#include "gui/userpalette.h"
34
#include "gui/theme.h"
35
36
#include "enums/resources/map/mapitemtype.h"
37
38
#include "fs/virtfs/fs.h"
39
40
#include "utils/delete2.h"
41
#include "utils/env.h"
42
43
#include "resources/sdlimagehelper.h"
44
45
#include "resources/map/speciallayer.h"
46
47
#include "resources/resourcemanager/resourcemanager.h"
48
49
#include "debug.h"
50
51
14
TEST_CASE("SpecialLayer updateCache", "")
52
{
53
12
    setEnv("SDL_VIDEODRIVER", "dummy");
54
55
12
    client = new Client;
56
36
    VirtFs::mountDirSilent("data", Append_false);
57
36
    VirtFs::mountDirSilent("../data", Append_false);
58
59
24
    imageHelper = new SDLImageHelper;
60
61
12
    Dirs::initRootDir();
62
12
    Dirs::initHomeDir();
63
64
12
    setBrandingDefaults(branding);
65
12
    ConfigManager::initConfiguration();
66
67
#ifdef USE_SDL2
68
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
69
        GraphicsManager::createWindow(640, 480, 0,
70
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
71
#else  // USE_SDL2
72
73
12
    GraphicsManager::createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
74
#endif  // USE_SDL2
75
76
12
    userPalette = new UserPalette;
77
78
12
    theme = new Theme;
79
12
    Theme::selectSkin();
80
81
12
    ActorSprite::load();
82
83
12
    SpecialLayer *layer = nullptr;
84
85



84
    SECTION("simple 1")
86
    {
87
1
        layer = new SpecialLayer("test",
88

3
            1, 1);
89
1
        const int *const cache = layer->getCache();
90



4
        REQUIRE(cache[0] == 10000);
91
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
92
1
        layer->updateCache();
93



4
        REQUIRE(cache[0] == 10000);
94
    }
95
96



84
    SECTION("simple 2")
97
    {
98
1
        layer = new SpecialLayer("test",
99

3
            2, 1);
100
1
        const int *const cache = layer->getCache();
101



4
        REQUIRE(cache[0] == 10000);
102



4
        REQUIRE(cache[1] == 10000);
103
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
104
1
        layer->updateCache();
105



4
        REQUIRE(cache[0] == 10000);
106



4
        REQUIRE(cache[1] == 10000);
107
    }
108
109



84
    SECTION("simple 3")
110
    {
111
1
        layer = new SpecialLayer("test",
112

3
            2, 1);
113
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
114
1
        layer->setTile(1, 0, MapItemType::ARROW_DOWN);
115
1
        const int *const cache = layer->getCache();
116
1
        layer->updateCache();
117



4
        REQUIRE(cache[0] == 0);
118



4
        REQUIRE(cache[1] == 10000);
119
    }
120
121



84
    SECTION("simple 4")
122
    {
123
1
        layer = new SpecialLayer("test",
124

3
            2, 1);
125
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
126
1
        layer->setTile(1, 0, MapItemType::ARROW_UP);
127
1
        const int *const cache = layer->getCache();
128
1
        layer->updateCache();
129



4
        REQUIRE(cache[0] == 0);
130



4
        REQUIRE(cache[1] == 10000);
131
    }
132
133



84
    SECTION("simple 4.2")
134
    {
135
1
        layer = new SpecialLayer("test",
136

3
            3, 1);
137
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
138
1
        layer->setTile(2, 0, MapItemType::ARROW_UP);
139
1
        const int *const cache = layer->getCache();
140
1
        layer->updateCache();
141



4
        REQUIRE(cache[0] == 1);
142



4
        REQUIRE(cache[1] == 0);
143



4
        REQUIRE(cache[2] == 10000);
144
    }
145
146



84
    SECTION("simple 5")
147
    {
148
1
        layer = new SpecialLayer("test",
149

3
            3, 1);
150
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
151
1
        layer->setTile(1, 0, MapItemType::ARROW_UP);
152
1
        const int *const cache = layer->getCache();
153
1
        layer->updateCache();
154



4
        REQUIRE(cache[0] == 0);
155



4
        REQUIRE(cache[1] == 10000);
156



4
        REQUIRE(cache[2] == 10000);
157
    }
158
159



84
    SECTION("simple 6")
160
    {
161
1
        layer = new SpecialLayer("test",
162

3
            3, 1);
163
1
        layer->setTile(0, 0, MapItemType::ARROW_UP);
164
1
        layer->setTile(1, 0, MapItemType::ARROW_UP);
165
1
        layer->setTile(2, 0, MapItemType::ARROW_DOWN);
166
1
        const int *const cache = layer->getCache();
167
1
        layer->updateCache();
168



4
        REQUIRE(cache[0] == 0);
169



4
        REQUIRE(cache[1] == 0);
170



4
        REQUIRE(cache[2] == 10000);
171
    }
172
173



84
    SECTION("simple 7")
174
    {
175
1
        layer = new SpecialLayer("test",
176

3
            3, 1);
177
1
        const int *const cache = layer->getCache();
178
1
        layer->updateCache();
179



4
        REQUIRE(cache[0] == 10000);
180



4
        REQUIRE(cache[1] == 10000);
181



4
        REQUIRE(cache[2] == 10000);
182
    }
183
184



84
    SECTION("simple 8")
185
    {
186
1
        layer = new SpecialLayer("test",
187

3
            3, 1);
188
1
        layer->setTile(0, 0, MapItemType::EMPTY);
189
1
        layer->setTile(1, 0, MapItemType::EMPTY);
190
1
        layer->setTile(2, 0, MapItemType::EMPTY);
191
1
        const int *const cache = layer->getCache();
192
1
        layer->updateCache();
193



4
        REQUIRE(cache[0] == 10000);
194



4
        REQUIRE(cache[1] == 10000);
195



4
        REQUIRE(cache[2] == 10000);
196
    }
197
198



84
    SECTION("simple 9")
199
    {
200
1
        layer = new SpecialLayer("test",
201

3
            2, 1);
202
1
        const int *const cache = layer->getCache();
203



4
        REQUIRE(cache[0] == 10000);
204



4
        REQUIRE(cache[1] == 10000);
205
1
        layer->setTile(1, 0, MapItemType::ARROW_UP);
206
1
        layer->updateCache();
207



4
        REQUIRE(cache[0] == 0);
208



4
        REQUIRE(cache[1] == 10000);
209
    }
210
211



84
    SECTION("normal 1")
212
    {
213
1
        layer = new SpecialLayer("test",
214

3
            100, 100);
215
1
        layer->setTile(1, 10, MapItemType::ARROW_UP);
216
1
        layer->setTile(2, 10, MapItemType::ARROW_UP);
217
1
        layer->setTile(3, 10, MapItemType::ARROW_UP);
218
1
        layer->setTile(4, 10, MapItemType::ARROW_DOWN);
219
1
        layer->setTile(5, 10, MapItemType::EMPTY);
220
1
        layer->setTile(6, 10, MapItemType::ARROW_DOWN);
221
1
        layer->setTile(7, 10, MapItemType::EMPTY);
222
1
        layer->setTile(8, 10, MapItemType::EMPTY);
223
1
        layer->setTile(9, 10, MapItemType::ARROW_DOWN);
224
1
        layer->setTile(10, 10, MapItemType::ARROW_DOWN);
225
1
        layer->setTile(11, 10, MapItemType::ARROW_LEFT);
226
1
        layer->setTile(12, 10, MapItemType::EMPTY);
227
1
        layer->setTile(13, 10, MapItemType::EMPTY);
228
1
        layer->setTile(14, 10, MapItemType::EMPTY);
229
1
        layer->setTile(15, 10, MapItemType::ARROW_UP);
230
1
        layer->setTile(16, 10, MapItemType::ARROW_UP);
231
1
        layer->setTile(17, 10, MapItemType::ARROW_UP);
232
1
        const int *const cache = layer->getCache();
233
1
        layer->updateCache();
234
235



4
        REQUIRE(cache[10 * 100 + 0] == 0);
236



4
        REQUIRE(cache[10 * 100 + 1] == 0);
237



4
        REQUIRE(cache[10 * 100 + 2] == 0);
238



4
        REQUIRE(cache[10 * 100 + 3] == 0);
239



4
        REQUIRE(cache[10 * 100 + 4] == 1);
240



4
        REQUIRE(cache[10 * 100 + 5] == 0);
241



4
        REQUIRE(cache[10 * 100 + 6] == 2);
242



4
        REQUIRE(cache[10 * 100 + 7] == 1);
243



4
        REQUIRE(cache[10 * 100 + 8] == 0);
244



4
        REQUIRE(cache[10 * 100 + 9] == 0);
245



4
        REQUIRE(cache[10 * 100 + 10] == 0);
246



4
        REQUIRE(cache[10 * 100 + 11] == 3);
247



4
        REQUIRE(cache[10 * 100 + 12] == 2);
248



4
        REQUIRE(cache[10 * 100 + 13] == 1);
249



4
        REQUIRE(cache[10 * 100 + 14] == 0);
250



4
        REQUIRE(cache[10 * 100 + 15] == 0);
251



4
        REQUIRE(cache[10 * 100 + 16] == 0);
252



4
        REQUIRE(cache[10 * 100 + 17] == 10000);
253
    }
254
255



84
    SECTION("normal2")
256
    {
257
1
        const int maxX = 100;
258
1
        const int maxY = 100;
259
1
        layer = new SpecialLayer("test",
260

3
            maxX, maxY);
261
1
        const int *const cache = layer->getCache();
262
101
        for (int x = 0; x < maxX; x ++)
263
        {
264
20100
            for (int y = 0; y < maxY; y ++)
265
            {
266
10000
                layer->setTile(x, y, MapItemType::ARROW_UP);
267



40000
                REQUIRE(layer->getTiles()[x + y * maxX] != nullptr);
268
            }
269
        }
270
1
        layer->updateCache();
271
272
201
        for (int y = 0; y < maxY; y ++)
273
        {
274
19900
            for (int x = 0; x < maxX - 1; x ++)
275
            {
276



39600
                REQUIRE(cache[y * maxX + x] == 0);
277
            }
278



400
            REQUIRE(cache[y * maxX + maxX - 1] == 10000);
279
        }
280
    }
281
282
12
    delete layer;
283
12
    ResourceManager::cleanOrphans(false);
284
12
    delete2(userPalette)
285
12
    delete2(theme)
286
12
    delete2(client)
287
36
    VirtFs::unmountDirSilent("data");
288
36
    VirtFs::unmountDirSilent("../data");
289

15
}