GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/updateconditiontiles.cc Lines: 101 101 100.0 %
Date: 2017-11-29 Branches: 177 350 50.6 %

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 "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
14
TEST_CASE("MapLayer updateConditionTiles", "")
36
{
37
10
    Dirs::initRootDir();
38
10
    Dirs::initHomeDir();
39
40
10
    ConfigManager::initConfiguration();
41
42
10
    Image *const img1 = new Image(32, 32);
43
10
    Map *map = nullptr;
44
10
    MapLayer *layer = nullptr;
45
46



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

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

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



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



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



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

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

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



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



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

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

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



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



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



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

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

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



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



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



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

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

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



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



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

16
}