GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/updatecache.cc Lines: 268 268 100.0 %
Date: 2021-03-17 Branches: 944 2520 37.5 %

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 "configmanager.h"
25
#include "dirs.h"
26
27
#include "resources/image/image.h"
28
29
#include "resources/map/maplayer.h"
30
31
#include "debug.h"
32
33
12
TEST_CASE("MapLayer updateCache", "")
34
{
35
10
    Dirs::initRootDir();
36
10
    Dirs::initHomeDir();
37
38
10
    ConfigManager::initConfiguration();
39
40
10
    Image *const img1 = new Image(32, 32);
41
10
    Image *const img2 = new Image(32, 32);
42
10
    Image *const img3 = new Image(32, 32);
43
10
    MapLayer *layer = nullptr;
44
45



70
    SECTION("simple 1")
46
    {
47
1
        layer = new MapLayer("test",
48
            0, 0,
49
            1, 1,
50
            false,
51
            0,
52

3
            0);
53
2
        layer->setTile(0, 0, img1);
54
1
        TileInfo *const tiles = layer->getTiles();
55
1
        layer->updateCache(1, 1);
56



4
        REQUIRE(tiles[0].isEnabled == true);
57



4
        REQUIRE(tiles[0].width == 32);
58



4
        REQUIRE(tiles[0].count == 0);
59



4
        REQUIRE(tiles[0].nextTile == 0);
60
    }
61
62



70
    SECTION("simple 2")
63
    {
64
1
        layer = new MapLayer("test",
65
            0, 0,
66
            2, 1,
67
            false,
68
            0,
69

3
            0);
70
2
        layer->setTile(0, 0, img1);
71
1
        TileInfo *const tiles = layer->getTiles();
72
1
        layer->updateCache(2, 1);
73



4
        REQUIRE(tiles[0].isEnabled == true);
74



4
        REQUIRE(tiles[0].width == 32);
75



4
        REQUIRE(tiles[0].count == 0);
76



4
        REQUIRE(tiles[0].nextTile == 1);
77



4
        REQUIRE(tiles[1].isEnabled == false);
78



4
        REQUIRE(tiles[1].width == 0);
79



4
        REQUIRE(tiles[1].count == 0);
80



4
        REQUIRE(tiles[1].nextTile == 0);
81
    }
82
83



70
    SECTION("simple 3")
84
    {
85
1
        layer = new MapLayer("test",
86
            0, 0,
87
            2, 1,
88
            false,
89
            0,
90

3
            0);
91
2
        layer->setTile(0, 0, img1);
92
2
        layer->setTile(1, 0, img2);
93
1
        TileInfo *const tiles = layer->getTiles();
94
1
        layer->updateCache(2, 1);
95



4
        REQUIRE(tiles[0].isEnabled == true);
96



4
        REQUIRE(tiles[0].width == 32);
97



4
        REQUIRE(tiles[0].count == 0);
98



4
        REQUIRE(tiles[0].nextTile == 0);
99



4
        REQUIRE(tiles[1].isEnabled == true);
100



4
        REQUIRE(tiles[1].width == 32);
101



4
        REQUIRE(tiles[1].count == 0);
102



4
        REQUIRE(tiles[1].nextTile == 0);
103
    }
104
105



70
    SECTION("simple 4")
106
    {
107
1
        layer = new MapLayer("test",
108
            0, 0,
109
            2, 1,
110
            false,
111
            0,
112

3
            0);
113
2
        layer->setTile(0, 0, img1);
114
2
        layer->setTile(1, 0, img1);
115
1
        TileInfo *const tiles = layer->getTiles();
116
1
        layer->updateCache(2, 1);
117



4
        REQUIRE(tiles[0].isEnabled == true);
118



4
        REQUIRE(tiles[0].width == 64);
119



4
        REQUIRE(tiles[0].count == 1);
120



4
        REQUIRE(tiles[0].nextTile == 1);
121



4
        REQUIRE(tiles[1].isEnabled == true);
122



4
        REQUIRE(tiles[1].width == 32);
123



4
        REQUIRE(tiles[1].count == 0);
124



4
        REQUIRE(tiles[1].nextTile == 0);
125
    }
126
127



70
    SECTION("simple 4.2")
128
    {
129
1
        layer = new MapLayer("test",
130
            0, 0,
131
            3, 1,
132
            false,
133
            0,
134

3
            0);
135
2
        layer->setTile(0, 0, img1);
136
2
        layer->setTile(2, 0, img1);
137
1
        TileInfo *const tiles = layer->getTiles();
138
1
        layer->updateCache(3, 1);
139



4
        REQUIRE(tiles[0].isEnabled == true);
140



4
        REQUIRE(tiles[0].width == 32);
141



4
        REQUIRE(tiles[0].count == 0);
142



4
        REQUIRE(tiles[0].nextTile == 1);
143



4
        REQUIRE(tiles[1].isEnabled == false);
144



4
        REQUIRE(tiles[1].width == 0);
145



4
        REQUIRE(tiles[1].count == 0);
146



4
        REQUIRE(tiles[1].nextTile == 0);
147



4
        REQUIRE(tiles[2].isEnabled == true);
148



4
        REQUIRE(tiles[2].width == 32);
149



4
        REQUIRE(tiles[2].count == 0);
150



4
        REQUIRE(tiles[2].nextTile == 0);
151
    }
152
153



70
    SECTION("simple 5")
154
    {
155
1
        layer = new MapLayer("test",
156
            0, 0,
157
            3, 1,
158
            false,
159
            0,
160

3
            0);
161
2
        layer->setTile(0, 0, img1);
162
2
        layer->setTile(1, 0, img1);
163
1
        TileInfo *const tiles = layer->getTiles();
164
1
        layer->updateCache(3, 1);
165



4
        REQUIRE(tiles[0].isEnabled == true);
166



4
        REQUIRE(tiles[0].width == 64);
167



4
        REQUIRE(tiles[0].count == 1);
168



4
        REQUIRE(tiles[0].nextTile == 2);
169



4
        REQUIRE(tiles[1].isEnabled == true);
170



4
        REQUIRE(tiles[1].width == 32);
171



4
        REQUIRE(tiles[1].count == 0);
172



4
        REQUIRE(tiles[1].nextTile == 1);
173



4
        REQUIRE(tiles[2].isEnabled == false);
174



4
        REQUIRE(tiles[2].width == 0);
175



4
        REQUIRE(tiles[2].count == 0);
176



4
        REQUIRE(tiles[2].nextTile == 0);
177
    }
178
179



70
    SECTION("simple 6")
180
    {
181
1
        layer = new MapLayer("test",
182
            0, 0,
183
            3, 1,
184
            false,
185
            0,
186

3
            0);
187
2
        layer->setTile(0, 0, img1);
188
2
        layer->setTile(1, 0, img1);
189
2
        layer->setTile(2, 0, img2);
190
1
        TileInfo *const tiles = layer->getTiles();
191
1
        layer->updateCache(3, 1);
192



4
        REQUIRE(tiles[0].isEnabled == true);
193



4
        REQUIRE(tiles[0].width == 64);
194



4
        REQUIRE(tiles[0].count == 1);
195



4
        REQUIRE(tiles[0].nextTile == 1);
196



4
        REQUIRE(tiles[1].isEnabled == true);
197



4
        REQUIRE(tiles[1].width == 32);
198



4
        REQUIRE(tiles[1].count == 0);
199



4
        REQUIRE(tiles[1].nextTile == 0);
200



4
        REQUIRE(tiles[2].isEnabled == true);
201



4
        REQUIRE(tiles[2].width == 32);
202



4
        REQUIRE(tiles[2].count == 0);
203



4
        REQUIRE(tiles[2].nextTile == 0);
204
    }
205
206



70
    SECTION("simple 7")
207
    {
208
1
        layer = new MapLayer("test",
209
            0, 0,
210
            3, 1,
211
            false,
212
            0,
213

3
            0);
214
2
        layer->setTile(0, 0, img1);
215
2
        layer->setTile(1, 0, img1);
216
2
        layer->setTile(2, 0, img2);
217
1
        TileInfo *const tiles = layer->getTiles();
218
1
        tiles[0].isEnabled = false;
219
1
        layer->updateCache(3, 1);
220



4
        REQUIRE(tiles[0].isEnabled == false);
221



4
        REQUIRE(tiles[0].width == 0);
222



4
        REQUIRE(tiles[0].count == 0);
223



4
        REQUIRE(tiles[0].nextTile == 0);
224



4
        REQUIRE(tiles[1].isEnabled == true);
225



4
        REQUIRE(tiles[1].width == 32);
226



4
        REQUIRE(tiles[1].count == 0);
227



4
        REQUIRE(tiles[1].nextTile == 0);
228



4
        REQUIRE(tiles[2].isEnabled == true);
229



4
        REQUIRE(tiles[2].width == 32);
230



4
        REQUIRE(tiles[2].count == 0);
231



4
        REQUIRE(tiles[2].nextTile == 0);
232
    }
233
234



70
    SECTION("normal 1")
235
    {
236
1
        layer = new MapLayer("test",
237
            0, 0,
238
            100, 100,
239
            false,
240
            0,
241

3
            0);
242
2
        layer->setTile(1, 10, img1);
243
2
        layer->setTile(2, 10, img1);
244
2
        layer->setTile(3, 10, img1);
245
2
        layer->setTile(4, 10, img2);
246
2
        layer->setTile(5, 10, nullptr);
247
2
        layer->setTile(6, 10, img2);
248
2
        layer->setTile(7, 10, nullptr);
249
2
        layer->setTile(8, 10, nullptr);
250
2
        layer->setTile(9, 10, img2);
251
2
        layer->setTile(10, 10, img2);
252
2
        layer->setTile(11, 10, img3);
253
2
        layer->setTile(12, 10, nullptr);
254
2
        layer->setTile(13, 10, nullptr);
255
2
        layer->setTile(14, 10, nullptr);
256
2
        layer->setTile(15, 10, img1);
257
2
        layer->setTile(16, 10, img1);
258
2
        layer->setTile(17, 10, img1);
259
1
        TileInfo *const tiles = layer->getTiles();
260
1
        layer->updateCache(100, 100);
261
262



4
        REQUIRE(tiles[0 * 100 + 0].isEnabled == false);
263



4
        REQUIRE(tiles[0 * 100 + 0].width == 0);
264



4
        REQUIRE(tiles[0 * 100 + 0].count == 99);
265



4
        REQUIRE(tiles[0 * 100 + 0].nextTile == 99);
266
267



4
        REQUIRE(tiles[0 * 100 + 1].isEnabled == false);
268



4
        REQUIRE(tiles[0 * 100 + 1].width == 0);
269



4
        REQUIRE(tiles[0 * 100 + 1].count == 98);
270



4
        REQUIRE(tiles[0 * 100 + 1].nextTile == 98);
271
272



4
        REQUIRE(tiles[10 * 100 + 0].isEnabled == false);
273



4
        REQUIRE(tiles[10 * 100 + 0].width == 0);
274



4
        REQUIRE(tiles[10 * 100 + 0].count == 0);
275



4
        REQUIRE(tiles[10 * 100 + 0].nextTile == 0);
276
277



4
        REQUIRE(tiles[10 * 100 + 1].isEnabled == true);
278



4
        REQUIRE(tiles[10 * 100 + 1].width == 96);
279



4
        REQUIRE(tiles[10 * 100 + 1].count == 2);
280



4
        REQUIRE(tiles[10 * 100 + 1].nextTile == 2);
281
282



4
        REQUIRE(tiles[10 * 100 + 2].isEnabled == true);
283



4
        REQUIRE(tiles[10 * 100 + 2].width == 64);
284



4
        REQUIRE(tiles[10 * 100 + 2].count == 1);
285



4
        REQUIRE(tiles[10 * 100 + 2].nextTile == 1);
286
287



4
        REQUIRE(tiles[10 * 100 + 3].isEnabled == true);
288



4
        REQUIRE(tiles[10 * 100 + 3].width == 32);
289



4
        REQUIRE(tiles[10 * 100 + 3].count == 0);
290



4
        REQUIRE(tiles[10 * 100 + 3].nextTile == 0);
291
292



4
        REQUIRE(tiles[10 * 100 + 4].isEnabled == true);
293



4
        REQUIRE(tiles[10 * 100 + 4].width == 32);
294



4
        REQUIRE(tiles[10 * 100 + 4].count == 0);
295



4
        REQUIRE(tiles[10 * 100 + 4].nextTile == 1);
296
297



4
        REQUIRE(tiles[10 * 100 + 5].isEnabled == false);
298



4
        REQUIRE(tiles[10 * 100 + 5].width == 0);
299



4
        REQUIRE(tiles[10 * 100 + 5].count == 0);
300



4
        REQUIRE(tiles[10 * 100 + 5].nextTile == 0);
301
302



4
        REQUIRE(tiles[10 * 100 + 6].isEnabled == true);
303



4
        REQUIRE(tiles[10 * 100 + 6].width == 32);
304



4
        REQUIRE(tiles[10 * 100 + 6].count == 0);
305



4
        REQUIRE(tiles[10 * 100 + 6].nextTile == 2);
306
307



4
        REQUIRE(tiles[10 * 100 + 7].isEnabled == false);
308



4
        REQUIRE(tiles[10 * 100 + 7].width == 0);
309



4
        REQUIRE(tiles[10 * 100 + 7].count == 1);
310



4
        REQUIRE(tiles[10 * 100 + 7].nextTile == 1);
311
312



4
        REQUIRE(tiles[10 * 100 + 8].isEnabled == false);
313



4
        REQUIRE(tiles[10 * 100 + 8].width == 0);
314



4
        REQUIRE(tiles[10 * 100 + 8].count == 0);
315



4
        REQUIRE(tiles[10 * 100 + 8].nextTile == 0);
316
317



4
        REQUIRE(tiles[10 * 100 + 9].isEnabled == true);
318



4
        REQUIRE(tiles[10 * 100 + 9].width == 64);
319



4
        REQUIRE(tiles[10 * 100 + 9].count == 1);
320



4
        REQUIRE(tiles[10 * 100 + 9].nextTile == 1);
321
322



4
        REQUIRE(tiles[10 * 100 + 10].isEnabled == true);
323



4
        REQUIRE(tiles[10 * 100 + 10].width == 32);
324



4
        REQUIRE(tiles[10 * 100 + 10].count == 0);
325



4
        REQUIRE(tiles[10 * 100 + 10].nextTile == 0);
326
327



4
        REQUIRE(tiles[10 * 100 + 11].isEnabled == true);
328



4
        REQUIRE(tiles[10 * 100 + 11].width == 32);
329



4
        REQUIRE(tiles[10 * 100 + 11].count == 0);
330



4
        REQUIRE(tiles[10 * 100 + 11].nextTile == 3);
331
332



4
        REQUIRE(tiles[10 * 100 + 12].isEnabled == false);
333



4
        REQUIRE(tiles[10 * 100 + 12].width == 0);
334



4
        REQUIRE(tiles[10 * 100 + 12].count == 2);
335



4
        REQUIRE(tiles[10 * 100 + 12].nextTile == 2);
336
337



4
        REQUIRE(tiles[10 * 100 + 13].isEnabled == false);
338



4
        REQUIRE(tiles[10 * 100 + 13].width == 0);
339



4
        REQUIRE(tiles[10 * 100 + 13].count == 1);
340



4
        REQUIRE(tiles[10 * 100 + 13].nextTile == 1);
341
342



4
        REQUIRE(tiles[10 * 100 + 14].isEnabled == false);
343



4
        REQUIRE(tiles[10 * 100 + 14].width == 0);
344



4
        REQUIRE(tiles[10 * 100 + 14].count == 0);
345



4
        REQUIRE(tiles[10 * 100 + 14].nextTile == 0);
346
347



4
        REQUIRE(tiles[10 * 100 + 15].isEnabled == true);
348



4
        REQUIRE(tiles[10 * 100 + 15].width == 96);
349



4
        REQUIRE(tiles[10 * 100 + 15].count == 2);
350



4
        REQUIRE(tiles[10 * 100 + 15].nextTile == 84);
351
352



4
        REQUIRE(tiles[10 * 100 + 16].isEnabled == true);
353



4
        REQUIRE(tiles[10 * 100 + 16].width == 64);
354



4
        REQUIRE(tiles[10 * 100 + 16].count == 1);
355



4
        REQUIRE(tiles[10 * 100 + 16].nextTile == 83);
356
357



4
        REQUIRE(tiles[10 * 100 + 17].isEnabled == true);
358



4
        REQUIRE(tiles[10 * 100 + 17].width == 32);
359



4
        REQUIRE(tiles[10 * 100 + 17].count == 0);
360



4
        REQUIRE(tiles[10 * 100 + 17].nextTile == 82);
361
    }
362
363



70
    SECTION("normal2")
364
    {
365
1
        const int maxX = 100;
366
1
        const int maxY = 100;
367
1
        layer = new MapLayer("test",
368
            0, 0,
369
            maxX, maxY,
370
            false,
371
            0,
372

3
            0);
373
1
        TileInfo *const tiles = layer->getTiles();
374
101
        for (int x = 0; x < maxX; x ++)
375
        {
376
20100
            for (int y = 0; y < maxY; y ++)
377
            {
378
20000
                layer->setTile(x, y, img1);
379
10000
                tiles[y * maxX + x].isEnabled = false;
380
            }
381
        }
382
1
        tiles[10 * maxX + 41].isEnabled = true;
383
1
        layer->updateCache(maxX, maxY);
384
385



4
        REQUIRE(tiles[10 * maxX + 0].isEnabled == false);
386



4
        REQUIRE(tiles[10 * maxX + 0].width == 0);
387



4
        REQUIRE(tiles[10 * maxX + 0].count == 40);
388



4
        REQUIRE(tiles[10 * maxX + 0].nextTile == 40);
389
390



4
        REQUIRE(tiles[10 * maxX + 1].isEnabled == false);
391



4
        REQUIRE(tiles[10 * maxX + 1].width == 0);
392



4
        REQUIRE(tiles[10 * maxX + 1].count == 39);
393



4
        REQUIRE(tiles[10 * maxX + 1].nextTile == 39);
394
    }
395
396
10
    delete layer;
397
10
    delete img1;
398
10
    delete img2;
399
10
    delete img3;
400

13
}