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

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2018  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
14
TEST_CASE("SpecialLayer updateCache", "")
51
{
52
12
    setEnv("SDL_VIDEODRIVER", "dummy");
53
54
12
    client = new Client;
55
36
    VirtFs::mountDirSilent("data", Append_false);
56
36
    VirtFs::mountDirSilent("../data", Append_false);
57
58
24
    imageHelper = new SDLImageHelper;
59
60
12
    Dirs::initRootDir();
61
12
    Dirs::initHomeDir();
62
63
12
    setBrandingDefaults(branding);
64
12
    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
12
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
73
#endif  // USE_SDL2
74
75
12
    userPalette = new UserPalette;
76
77
12
    theme = new Theme;
78
12
    Theme::selectSkin();
79
80
12
    ActorSprite::load();
81
82
12
    SpecialLayer *layer = nullptr;
83
84



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

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



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



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



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

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



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



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



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



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



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

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



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



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



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

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



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



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



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

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



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



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



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



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

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



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



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



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



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

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



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



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



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



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

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



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



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



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



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

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



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



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



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



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

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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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

15
}