GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/speciallayer.cc Lines: 175 175 100.0 %
Date: 2017-11-29 Branches: 443 1054 42.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "unittests/unittests.h"
22
23
#include "client.h"
24
#include "configmanager.h"
25
#include "configuration.h"
26
#include "dirs.h"
27
28
#include "graphicsmanager.h"
29
30
#include "being/actorsprite.h"
31
32
#include "gui/userpalette.h"
33
#include "gui/theme.h"
34
35
#include "enums/resources/map/mapitemtype.h"
36
37
#include "fs/virtfs/fs.h"
38
39
#include "utils/delete2.h"
40
#include "utils/env.h"
41
42
#include "resources/sdlimagehelper.h"
43
44
#include "resources/map/speciallayer.h"
45
46
#include "resources/resourcemanager/resourcemanager.h"
47
48
#include "debug.h"
49
50
28
TEST_CASE("SpecialLayer updateCache", "")
51
{
52
24
    setEnv("SDL_VIDEODRIVER", "dummy");
53
54
24
    client = new Client;
55

96
    VirtFs::mountDirSilent("data", Append_false);
56

96
    VirtFs::mountDirSilent("../data", Append_false);
57
58
48
    imageHelper = new SDLImageHelper;
59
60
24
    Dirs::initRootDir();
61
24
    Dirs::initHomeDir();
62
63
24
    setBrandingDefaults(branding);
64
24
    ConfigManager::initConfiguration();
65
66
#ifdef USE_SDL2
67
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
68
        graphicsManager.createWindow(640, 480, 0,
69
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
70
#else  // USE_SDL2
71
72
24
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
73
#endif  // USE_SDL2
74
75
24
    userPalette = new UserPalette;
76
77
24
    theme = new Theme;
78
24
    Theme::selectSkin();
79
80
24
    ActorSprite::load();
81
82
24
    SpecialLayer *layer = nullptr;
83
84



168
    SECTION("simple 1")
85
    {
86
10
        layer = new SpecialLayer("test",
87

2
            1, 1);
88
2
        const int *const cache = layer->getCache();
89



8
        REQUIRE(cache[0] == 10000);
90
2
        layer->setTile(0, 0, MapItemType::ARROW_UP);
91
2
        layer->updateCache();
92



8
        REQUIRE(cache[0] == 10000);
93
    }
94
95



168
    SECTION("simple 2")
96
    {
97
10
        layer = new SpecialLayer("test",
98

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



8
        REQUIRE(cache[0] == 10000);
101



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



8
        REQUIRE(cache[0] == 10000);
105



8
        REQUIRE(cache[1] == 10000);
106
    }
107
108



168
    SECTION("simple 3")
109
    {
110
10
        layer = new SpecialLayer("test",
111

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



8
        REQUIRE(cache[0] == 0);
117



8
        REQUIRE(cache[1] == 10000);
118
    }
119
120



168
    SECTION("simple 4")
121
    {
122
10
        layer = new SpecialLayer("test",
123

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



8
        REQUIRE(cache[0] == 0);
129



8
        REQUIRE(cache[1] == 10000);
130
    }
131
132



168
    SECTION("simple 4.2")
133
    {
134
10
        layer = new SpecialLayer("test",
135

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



8
        REQUIRE(cache[0] == 1);
141



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



8
        REQUIRE(cache[2] == 10000);
143
    }
144
145



168
    SECTION("simple 5")
146
    {
147
10
        layer = new SpecialLayer("test",
148

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



8
        REQUIRE(cache[0] == 0);
154



8
        REQUIRE(cache[1] == 10000);
155



8
        REQUIRE(cache[2] == 10000);
156
    }
157
158



168
    SECTION("simple 6")
159
    {
160
10
        layer = new SpecialLayer("test",
161

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



8
        REQUIRE(cache[0] == 0);
168



8
        REQUIRE(cache[1] == 0);
169



8
        REQUIRE(cache[2] == 10000);
170
    }
171
172



168
    SECTION("simple 7")
173
    {
174
10
        layer = new SpecialLayer("test",
175

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



8
        REQUIRE(cache[0] == 10000);
179



8
        REQUIRE(cache[1] == 10000);
180



8
        REQUIRE(cache[2] == 10000);
181
    }
182
183



168
    SECTION("simple 8")
184
    {
185
10
        layer = new SpecialLayer("test",
186

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



8
        REQUIRE(cache[0] == 10000);
193



8
        REQUIRE(cache[1] == 10000);
194



8
        REQUIRE(cache[2] == 10000);
195
    }
196
197



168
    SECTION("simple 9")
198
    {
199
10
        layer = new SpecialLayer("test",
200

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



8
        REQUIRE(cache[0] == 10000);
203



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



8
        REQUIRE(cache[0] == 0);
207



8
        REQUIRE(cache[1] == 10000);
208
    }
209
210



168
    SECTION("normal 1")
211
    {
212
10
        layer = new SpecialLayer("test",
213

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



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



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



8
        REQUIRE(cache[10 * 100 + 2] == 0);
237



8
        REQUIRE(cache[10 * 100 + 3] == 0);
238



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



8
        REQUIRE(cache[10 * 100 + 5] == 0);
240



8
        REQUIRE(cache[10 * 100 + 6] == 2);
241



8
        REQUIRE(cache[10 * 100 + 7] == 1);
242



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



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



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



8
        REQUIRE(cache[10 * 100 + 11] == 3);
246



8
        REQUIRE(cache[10 * 100 + 12] == 2);
247



8
        REQUIRE(cache[10 * 100 + 13] == 1);
248



8
        REQUIRE(cache[10 * 100 + 14] == 0);
249



8
        REQUIRE(cache[10 * 100 + 15] == 0);
250



8
        REQUIRE(cache[10 * 100 + 16] == 0);
251



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



168
    SECTION("normal2")
255
    {
256
2
        const int maxX = 100;
257
2
        const int maxY = 100;
258
10
        layer = new SpecialLayer("test",
259

2
            maxX, maxY);
260
2
        const int *const cache = layer->getCache();
261
202
        for (int x = 0; x < maxX; x ++)
262
        {
263
40200
            for (int y = 0; y < maxY; y ++)
264
            {
265
20000
                layer->setTile(x, y, MapItemType::ARROW_UP);
266



80000
                REQUIRE(layer->getTiles()[x + y * maxX] != nullptr);
267
            }
268
        }
269
2
        layer->updateCache();
270
271
402
        for (int y = 0; y < maxY; y ++)
272
        {
273
39800
            for (int x = 0; x < maxX - 1; x ++)
274
            {
275



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



800
            REQUIRE(cache[y * maxX + maxX - 1] == 10000);
278
        }
279
    }
280
281
24
    delete layer;
282
24
    ResourceManager::cleanOrphans();
283
24
    delete2(userPalette);
284
24
    delete2(theme);
285
24
    delete2(client);
286

96
    VirtFs::unmountDirSilent("data");
287

96
    VirtFs::unmountDirSilent("../data");
288

30
}