GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/updatecache.cc Lines: 268 268 100.0 %
Date: 2018-09-20 Branches: 978 2584 37.8 %

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



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

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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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


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



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



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



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



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



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



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



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

13
}