GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/updatecache.cc Lines: 268 268 100.0 %
Date: 2017-11-29 Branches: 975 2582 37.8 %

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 "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
24
TEST_CASE("MapLayer updateCache", "")
33
{
34
20
    Dirs::initRootDir();
35
20
    Dirs::initHomeDir();
36
37
20
    ConfigManager::initConfiguration();
38
39
20
    Image *const img1 = new Image(32, 32);
40
20
    Image *const img2 = new Image(32, 32);
41
20
    Image *const img3 = new Image(32, 32);
42
20
    MapLayer *layer = nullptr;
43
44



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

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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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

26
}