GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/updateconditiontiles.cc Lines: 107 107 100.0 %
Date: 2018-09-20 Branches: 178 352 50.6 %

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 "enums/resources/map/blockmask.h"
27
28
#include "resources/image/image.h"
29
30
#include "resources/map/map.h"
31
#include "resources/map/maplayer.h"
32
33
#include "debug.h"
34
35
7
TEST_CASE("MapLayer updateConditionTiles", "")
36
{
37
5
    Dirs::initRootDir();
38
5
    Dirs::initHomeDir();
39
40
5
    ConfigManager::initConfiguration();
41
42
5
    Image *const img1 = new Image(32, 32);
43
5
    Map *map = nullptr;
44
5
    MapLayer *layer = nullptr;
45
46



35
    SECTION("simple 1")
47
    {
48
1
        map = new Map("map",
49
            1, 1,
50

3
            32, 32);
51
1
        layer = new MapLayer("test",
52
            0, 0,
53
            1, 1,
54
            false,
55
            0,
56

3
            0);
57
1
        layer->setTile(0, 0, img1);
58
1
        map->addLayer(layer);
59
2
        layer->setTileCondition(BlockMask::WATER);
60
1
        TileInfo *const tiles = layer->getTiles();
61
62
1
        map->addBlockMask(0, 0, BlockType::NONE);
63
1
        layer->updateConditionTiles(map->getMetaTiles(),
64
1
            1, 1);
65



4
        REQUIRE(tiles[0].isEnabled == false);
66
67
1
        map->addBlockMask(0, 0, BlockType::WATER);
68
1
        layer->updateConditionTiles(map->getMetaTiles(),
69
1
            1, 1);
70



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



35
    SECTION("normal 1")
74
    {
75
1
        map = new Map("map",
76
            100, 200,
77

3
            32, 32);
78
1
        layer = new MapLayer("test",
79
            0, 0,
80
            100, 200,
81
            false,
82
            0,
83

3
            0);
84
1
        layer->setTile(10, 10, img1);
85
1
        layer->setTile(10, 20, img1);
86
1
        layer->setTile(10, 30, img1);
87
1
        map->addLayer(layer);
88
2
        layer->setTileCondition(BlockMask::WATER);
89
1
        TileInfo *const tiles = layer->getTiles();
90
91
1
        map->addBlockMask(10, 10, BlockType::NONE);
92
1
        map->addBlockMask(10, 20, BlockType::NONE);
93
1
        map->addBlockMask(20, 20, BlockType::NONE);
94
1
        layer->updateConditionTiles(map->getMetaTiles(),
95
1
            100, 200);
96
201
        for (int x = 0; x < 100; x ++)
97
        {
98
40100
            for (int y = 0; y < 200; y ++)
99
            {
100



80000
                REQUIRE(tiles[y * 100 + x].isEnabled == false);
101
            }
102
        }
103
    }
104
105



35
    SECTION("normal 2")
106
    {
107
1
        map = new Map("map",
108
            100, 200,
109

3
            32, 32);
110
1
        layer = new MapLayer("test",
111
            0, 0,
112
            100, 200,
113
            false,
114
            0,
115

3
            0);
116
1
        layer->setTile(10, 10, img1);
117
1
        layer->setTile(10, 20, img1);
118
1
        layer->setTile(10, 30, img1);
119
1
        map->addLayer(layer);
120
2
        layer->setTileCondition(BlockMask::WATER);
121
1
        TileInfo *const tiles = layer->getTiles();
122
123
1
        map->addBlockMask(10, 10, BlockType::WATER);
124
1
        map->addBlockMask(10, 20, BlockType::WATER);
125
1
        map->addBlockMask(20, 20, BlockType::WATER);
126
1
        layer->updateConditionTiles(map->getMetaTiles(),
127
1
            100, 200);
128
201
        for (int x = 0; x < 100; x ++)
129
        {
130
40100
            for (int y = 0; y < 200; y ++)
131
            {
132
20000
                if ((x == 10 && y == 10) || (x == 10 && y == 20))
133
                {
134



8
                    REQUIRE(tiles[y * 100 + x].isEnabled == true);
135
                }
136
                else
137
                {
138



79992
                    REQUIRE(tiles[y * 100 + x].isEnabled == false);
139
                }
140
            }
141
        }
142
    }
143
144



35
    SECTION("normal 3")
145
    {
146
1
        map = new Map("map",
147
            100, 200,
148

3
            32, 32);
149
1
        layer = new MapLayer("test",
150
            0, 0,
151
            100, 200,
152
            false,
153
            0,
154

3
            0);
155
101
        for (int x = 0; x < 100; x ++)
156
        {
157
40100
            for (int y = 0; y < 200; y ++)
158
            {
159
20000
                layer->setTile(x, y, img1);
160
            }
161
        }
162
1
        map->addLayer(layer);
163
2
        layer->setTileCondition(BlockMask::WATER);
164
1
        TileInfo *const tiles = layer->getTiles();
165
166
1
        map->addBlockMask(10, 10, BlockType::WATER);
167
1
        map->addBlockMask(10, 20, BlockType::WATER);
168
1
        layer->updateConditionTiles(map->getMetaTiles(),
169
1
            100, 200);
170
201
        for (int x = 0; x < 100; x ++)
171
        {
172
40100
            for (int y = 0; y < 200; y ++)
173
            {
174
20000
                if ((x == 10 && y == 10) || (x == 10 && y == 20))
175
                {
176



8
                    REQUIRE(tiles[y * 100 + x].isEnabled == true);
177
                }
178
                else
179
                {
180



79992
                    REQUIRE(tiles[y * 100 + x].isEnabled == false);
181
                }
182
            }
183
        }
184
    }
185
186



35
    SECTION("normal 4")
187
    {
188
1
        map = new Map("map",
189
            100, 200,
190

3
            32, 32);
191
1
        layer = new MapLayer("test",
192
            0, 0,
193
            100, 200,
194
            false,
195
            0,
196

3
            0);
197
1
        layer->setTile(10, 10, img1);
198
1
        layer->setTile(10, 20, img1);
199
1
        map->addLayer(layer);
200
2
        layer->setTileCondition(BlockMask::WATER);
201
1
        TileInfo *const tiles = layer->getTiles();
202
203
101
        for (int x = 0; x < 100; x ++)
204
        {
205
40100
            for (int y = 0; y < 200; y ++)
206
            {
207
20000
                map->addBlockMask(x, y, BlockType::WATER);
208
            }
209
        }
210
211
1
        layer->updateConditionTiles(map->getMetaTiles(),
212
1
            100, 200);
213
201
        for (int x = 0; x < 100; x ++)
214
        {
215
40100
            for (int y = 0; y < 200; y ++)
216
            {
217
20000
                if ((x == 10 && y == 10) || (x == 10 && y == 20))
218
                {
219



8
                    REQUIRE(tiles[y * 100 + x].isEnabled == true);
220
                }
221
                else
222
                {
223



79992
                    REQUIRE(tiles[y * 100 + x].isEnabled == false);
224
                }
225
            }
226
        }
227
    }
228
229
5
    delete map;
230
5
    delete img1;
231

8
}