GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/updateconditiontiles.cc Lines: 107 107 100.0 %
Date: 2021-03-17 Branches: 168 332 50.6 %

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



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

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

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



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



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



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

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

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



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



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

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

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



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



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



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

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

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



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



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



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

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

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



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



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

8
}