The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_unit_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Pauli Nieminen <[email protected]>
3  Part of thie Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-test"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "log.hpp"
20 #include "config.hpp"
21 #include "units/unit.hpp"
23 #include "units/map.hpp"
24 #include "units/id.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 
29 /*
30 ./test --report_level=detailed --log_level=all --run_test=interpolate_suite
31 
32  */
33 
34 BOOST_AUTO_TEST_SUITE( unit_map_suite )
35 
37  // FIXME: this test currently fails becasue unit id manager was moved to game_board.
38  return;
40 
41  config orc_config;
42  orc_config["id"]="Orcish Grunt";
43  orc_config["random_traits"]=false;
44  orc_config["animate"]=false;
45  unit_type orc_type(orc_config);
46 
48 
49  unit orc1_side0_real(orc_type, 0, false);
50  unit orc2_side0_fake(orc_type, 0, false);
51 
53 
54  typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
55  t_uresult uresult1 = unit_map.add(map_location(1,1), orc1_side0_real);
56 
57  BOOST_CHECK_MESSAGE(uresult1.second == true, "Good Add");
58  BOOST_CHECK_EQUAL(unit_map.size(), 1);
59 
60  unit_map::unit_iterator ui = unit_map.find(map_location(1,1));
61  BOOST_CHECK_MESSAGE(uresult1.first == ui, "Good Add");
62  BOOST_CHECK_MESSAGE(ui->underlying_id() == orc1_side0_real.underlying_id(), "Found Orc1");
63 
64  unit_map::unit_iterator ui2 = unit_map.find(map_location(1,2));
65  BOOST_CHECK_MESSAGE(ui2 == unit_map.end(), "Not Found Orc1");
66  ui2 = unit_map.find(orc1_side0_real.underlying_id()+1);
67  BOOST_CHECK_MESSAGE(ui2 == unit_map.end(), "Not Found Orc1");
68 
69  // unit * orc1p = new unit(orc1_side0_real);
70 
71  lg::set_log_domain_severity("engine", lg::err().get_severity() - 1); // Don't log anything
73  uresult1 = unit_map.add(map_location(1,1), orc1_side0_real);
76  BOOST_CHECK_EQUAL(unit_map.size(), 1);
77  BOOST_CHECK_MESSAGE(uresult1.second == false, "Didn't Add at occupied location.");
78  BOOST_CHECK_MESSAGE(uresult1.first == unit_map.end(), "Didn't Add at occupied location.");
79 
80  lg::set_log_domain_severity("engine", lg::err().get_severity() - 1); // Don't log anything
81  // If the location is invalid, the unit never needs to be cloned, so no warning is emitted in the unit domain
82  uresult1 = unit_map.add(map_location(-1,1), orc1_side0_real);
84  BOOST_CHECK_EQUAL(unit_map.size(), 1);
85  BOOST_CHECK_MESSAGE(uresult1.second == false, "Didn't Add at invalid location.");
86  BOOST_CHECK_MESSAGE(uresult1.first == unit_map.end(), "Didn't Add at invalid location.");
87 
88 
89  // std::cerr<<"ID real ="<<orc1_side0_real.underlying_id()<<"\n";
90  // std::cerr<<"ID fake ="<<orc2_side0_fake.underlying_id()<<"\n";
91 
92  lg::set_log_domain_severity("engine", lg::err().get_severity() - 1); // Don't log anything
94  uresult1 = unit_map.add(map_location(1,2), orc1_side0_real);
97  BOOST_CHECK_EQUAL(unit_map.size(), 2);
98  BOOST_CHECK_MESSAGE(uresult1.second == true, "Added in face of id collision.");
99  BOOST_CHECK_MESSAGE(uresult1.first != unit_map.end(), "Added in face of id collision.");
100  BOOST_CHECK_MESSAGE(uresult1.first->underlying_id() != orc1_side0_real.underlying_id(), "Found Orc1");
101 
102  BOOST_CHECK_MESSAGE(!unit_map.end().valid(), "Hmm, unit_map.end() is valid for dereference...");
103  //To check that the collisions will cut off change the cutoff in unit_map.cpp from 1e6 to less than the guard value below
104  // unit_map.add(map_location(1,3), orc2_side0_fake);
105  // unit_map.add(map_location(1,3), orc2_side0_fake);
106 
107  // unsigned long long guard =0;
108  // for(; guard< 2e2;++guard) {
109  // unit_map.add(map_location(2,guard), orc1_side0_real);
110  // };
111 
112  // std::cerr<<"BREAK\n;";
113  // unit_map.add(map_location(1,3), orc2_side0_fake);
114  // unit_map.add(map_location(1,4), orc2_side0_fake);
115  // try {
116  // unit_map.add(map_location(1,5), orc2_side0_fake);
117  // }catch (std::runtime_error e ){
118  // BOOST_CHECK_MESSAGE(std::string(e.what()) == std::string("One million collisions in unit_map")
119  // , "One million uid collision exception");
120  // }
121 
122 }
123 
124 BOOST_AUTO_TEST_CASE( track_real_unit_by_underlying_id ) {
125  // FIXME: this test currently fails becasue unit id manager was moved to game_board.
126  return;
128 
129  config orc_config;
130  orc_config["id"]="Orcish Grunt";
131  orc_config["random_traits"] = false;
132  orc_config["animate"]=false;
133  unit_type orc_type(orc_config);
134 
136 
137  unit orc1_side0_real(orc_type, 0, true);
138 
139  size_t underlying_id = orc1_side0_real.underlying_id();
140  map_location hex = map_location(1,1);
141 
143 
144  typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
145  t_uresult uresult1 = unit_map.add(hex, orc1_side0_real);
146 
147  BOOST_CHECK(uresult1.second == true);
148 
149  {
150  unit_map::unit_iterator ui = unit_map.find(underlying_id);
151  BOOST_CHECK(uresult1.first == ui);
152  BOOST_CHECK(ui->underlying_id() == orc1_side0_real.underlying_id());
153  }
154 
155  unit_ptr extracted_unit = unit_map.extract(hex);
156 
157  {
158  unit_map::unit_iterator ui = unit_map.find(underlying_id);
159  BOOST_CHECK(ui == unit_map.end());
160  }
161 
162  unit_map.insert(extracted_unit);
163  extracted_unit.reset();
164 
165  {
166  unit_map::unit_iterator ui = unit_map.find(underlying_id);
167  BOOST_CHECK(uresult1.first == ui);
168  BOOST_CHECK(ui->underlying_id() == orc1_side0_real.underlying_id());
169  }
170 }
171 
172 BOOST_AUTO_TEST_CASE( track_fake_unit_by_underlying_id ) {
173  // FIXME: this test currently fails becasue unit id manager was moved to game_board.
174  return;
176 
177  config orc_config;
178  orc_config["id"]="Orcish Grunt";
179  orc_config["random_traits"] = false;
180  orc_config["animate"]=false;
181  unit_type orc_type(orc_config);
182 
184 
185  unit orc1_side0_fake(orc_type, 0, false);
186 
187  size_t underlying_id = orc1_side0_fake.underlying_id();
188  map_location hex = map_location(1,1);
189 
191 
192  typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
193  t_uresult uresult1 = unit_map.add(hex, orc1_side0_fake);
194 
195  BOOST_CHECK(uresult1.second == true);
196 
197  {
198  unit_map::unit_iterator ui = unit_map.find(underlying_id);
199  BOOST_CHECK(uresult1.first == ui);
200  BOOST_CHECK(ui->underlying_id() == orc1_side0_fake.underlying_id());
201  }
202 
203  unit_ptr extracted_unit = unit_map.extract(hex);
204 
205  {
206  unit_map::unit_iterator ui = unit_map.find(underlying_id);
207  BOOST_CHECK(ui == unit_map.end());
208  }
209 
210  unit_map.insert(extracted_unit);
211  extracted_unit.reset();
212 
213  {
214  unit_map::unit_iterator ui = unit_map.find(underlying_id);
215  BOOST_CHECK(uresult1.first == ui);
216  BOOST_CHECK(ui->underlying_id() == orc1_side0_fake.underlying_id());
217  }
218 }
219 
220 BOOST_AUTO_TEST_CASE( track_real_unit_by_iterator ) {
221  // FIXME: this test currently fails becasue unit id manager was moved to game_board.
222  return;
223 
225 
226  config orc_config;
227  orc_config["id"]="Orcish Grunt";
228  orc_config["random_traits"] = false;
229  orc_config["animate"]=false;
230  unit_type orc_type(orc_config);
231 
233 
234  unit orc1_side0_real(orc_type, 0, true);
235 
236  map_location hex = map_location(1,1);
237 
239 
240  typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
241  t_uresult uresult1 = unit_map.add(hex, orc1_side0_real);
242 
243  unit_map::unit_iterator unit_iterator = uresult1.first;
244 
245  BOOST_CHECK(unit_iterator.valid());
246 
247  unit_ptr extracted_unit = unit_map.extract(hex);
248 
249  BOOST_CHECK_MESSAGE(unit_iterator.valid() == false, "Iterator should be invalid after extraction.");
250 
251  unit_map.insert(extracted_unit);
252 
253  BOOST_CHECK_MESSAGE(unit_iterator.valid(), "Iterator should be valid after extraction and reinsertion.");
254 
255  unit_map::unit_iterator unit_iterator2 = unit_map.find(hex);
256  BOOST_CHECK(unit_iterator2.valid());
257  BOOST_CHECK(unit_iterator == unit_iterator2);
258 }
259 
260 BOOST_AUTO_TEST_CASE( track_fake_unit_by_iterator ) {
261  // FIXME: this test currently fails becasue unit id manager was moved to game_board.
262  return;
264 
265  config orc_config;
266  orc_config["id"]="Orcish Grunt";
267  orc_config["random_traits"] = false;
268  orc_config["animate"]=false;
269  unit_type orc_type(orc_config);
270 
272 
273  unit orc1_side0_fake(orc_type, 0, false);
274 
275  map_location hex = map_location(1,1);
276 
278 
279  typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
280  t_uresult uresult1 = unit_map.add(hex, orc1_side0_fake);
281 
282  unit_map::unit_iterator unit_iterator = uresult1.first;
283 
284  BOOST_CHECK(unit_iterator.valid());
285 
286  unit_ptr extracted_unit = unit_map.extract(hex);
287 
288  BOOST_CHECK_MESSAGE(unit_iterator.valid() == false, "Iterator should be invalid after extraction.");
289 
290  unit_map.insert(extracted_unit);
291 
292  BOOST_CHECK_MESSAGE(unit_iterator.valid(), "Iterator should be valid after extraction and reinsertion.");
293 
294  unit_map::unit_iterator unit_iterator2 = unit_map.find(hex);
295  BOOST_CHECK(unit_iterator2.valid());
296  BOOST_CHECK(unit_iterator == unit_iterator2);
297 }
298 
299 /* vim: set ts=4 sw=4: */
300 BOOST_AUTO_TEST_SUITE_END()
301 
unit_iterator end()
Definition: map.hpp:311
size_t size() const
Definition: map.hpp:314
Definition: unit.hpp:95
size_t underlying_id() const
The unique internal ID of the unit.
Definition: unit.hpp:150
logger & info()
Definition: log.cpp:91
unit_type_data unit_types
Definition: types.cpp:1314
BOOST_AUTO_TEST_SUITE(test_map_location)
BOOST_AUTO_TEST_CASE(test_1)
Definitions for the interface to Wesnoth Markup Language (WML).
bool set_log_domain_severity(std::string const &name, int severity)
Definition: log.cpp:117
std::pair< unit_iterator, bool > insert(unit_ptr p)
Adds the unit to the map.
Definition: map.cpp:126
void build_unit_type(const unit_type &ut, unit_type::BUILD_STATUS status) const
Makes sure the provided unit_type is built to the specified level.
Definition: types.hpp:326
std::pair< unit_iterator, bool > add(const map_location &l, const unit &u)
Adds a copy of unit u at location l of the map.
Definition: map.cpp:70
Encapsulates the map of the game.
Definition: location.hpp:38
config get_test_config()
logger & err()
Definition: log.cpp:79
Game configuration data as global variables.
Definition: build_info.cpp:38
logger & warn()
Definition: log.cpp:85
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:249
Standard logging facilities (interface).
Container associating units to locations.
Definition: map.hpp:90
unit_iterator find(size_t id)
Definition: map.cpp:285
bool valid() const
Definition: map.hpp:229
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83