00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <boost/test/unit_test.hpp>
00017
00018 #include "log.hpp"
00019 #include "config.hpp"
00020 #include "unit.hpp"
00021 #include "tests/utils/game_config_manager.hpp"
00022 #include "unit_map.hpp"
00023 #include "unit_id.hpp"
00024
00025 #include <boost/bind.hpp>
00026
00027
00028
00029
00030
00031
00032
00033 BOOST_AUTO_TEST_SUITE( unit_map_suite )
00034
00035 BOOST_AUTO_TEST_CASE( test_1 ) {
00036
00037 config game_config(test_utils::get_test_config());
00038
00039 config orc_config;
00040 orc_config["id"]="Orcish Grunt";
00041 orc_config["random_traits"]=false;
00042 unit_type orc_type(orc_config);
00043
00044 unit orc1_side0_real(&orc_type, 0, true);
00045 unit orc2_side0_fake(&orc_type, 0, false);
00046
00047 unit_map unit_map;
00048
00049 typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
00050 t_uresult uresult1 = unit_map.add(map_location(1,1), orc1_side0_real);
00051
00052 BOOST_CHECK_MESSAGE(uresult1.second == true, "Good Add");
00053
00054 unit_map::unit_iterator ui = unit_map.find(map_location(1,1));
00055 BOOST_CHECK_MESSAGE(uresult1.first == ui, "Good Add");
00056 BOOST_CHECK_MESSAGE(ui->underlying_id() == orc1_side0_real.underlying_id(), "Found Orc1");
00057
00058 unit_map::unit_iterator ui2 = unit_map.find(map_location(1,2));
00059 BOOST_CHECK_MESSAGE(ui2 == unit_map.end(), "Not Found Orc1");
00060 ui2 = unit_map.find(orc1_side0_real.underlying_id()+1);
00061 BOOST_CHECK_MESSAGE(ui2 == unit_map.end(), "Not Found Orc1");
00062
00063
00064
00065 uresult1 = unit_map.add(map_location(1,1), orc1_side0_real);
00066 BOOST_CHECK_MESSAGE(uresult1.second == false, "Didn't Add at occupied location.");
00067 BOOST_CHECK_MESSAGE(uresult1.first == unit_map.end(), "Didn't Add at occupied location.");
00068
00069 uresult1 = unit_map.add(map_location(-1,1), orc1_side0_real);
00070 BOOST_CHECK_MESSAGE(uresult1.second == false, "Didn't Add at invalid location.");
00071 BOOST_CHECK_MESSAGE(uresult1.first == unit_map.end(), "Didn't Add at invalid location.");
00072
00073
00074
00075
00076
00077 uresult1 = unit_map.add(map_location(1,2), orc1_side0_real);
00078 BOOST_CHECK_MESSAGE(uresult1.second == true, "Added in face of id collision.");
00079 BOOST_CHECK_MESSAGE(uresult1.first != unit_map.end(), "Added in face of id collision.");
00080 BOOST_CHECK_MESSAGE(uresult1.first->underlying_id() != orc1_side0_real.underlying_id(), "Found Orc1");
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 }
00105
00106 BOOST_AUTO_TEST_CASE( track_real_unit_by_underlying_id ) {
00107
00108 config game_config(test_utils::get_test_config());
00109
00110 config orc_config;
00111 orc_config["id"]="Orcish Grunt";
00112 orc_config["random_traits"] = false;
00113 unit_type orc_type(orc_config);
00114
00115 unit orc1_side0_real(&orc_type, 0, true);
00116
00117 size_t underlying_id = orc1_side0_real.underlying_id();
00118 map_location hex = map_location(1,1);
00119
00120 unit_map unit_map;
00121
00122 typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
00123 t_uresult uresult1 = unit_map.add(hex, orc1_side0_real);
00124
00125 BOOST_CHECK(uresult1.second == true);
00126
00127 {
00128 unit_map::unit_iterator ui = unit_map.find(underlying_id);
00129 BOOST_CHECK(uresult1.first == ui);
00130 BOOST_CHECK(ui->underlying_id() == orc1_side0_real.underlying_id());
00131 }
00132
00133 unit* extracted_unit = unit_map.extract(hex);
00134
00135 {
00136 unit_map::unit_iterator ui = unit_map.find(underlying_id);
00137 BOOST_CHECK(ui == unit_map.end());
00138 }
00139
00140 unit_map.insert(extracted_unit);
00141 extracted_unit = NULL;
00142
00143 {
00144 unit_map::unit_iterator ui = unit_map.find(underlying_id);
00145 BOOST_CHECK(uresult1.first == ui);
00146 BOOST_CHECK(ui->underlying_id() == orc1_side0_real.underlying_id());
00147 }
00148 }
00149
00150 BOOST_AUTO_TEST_CASE( track_fake_unit_by_underlying_id ) {
00151
00152 config game_config(test_utils::get_test_config());
00153
00154 config orc_config;
00155 orc_config["id"]="Orcish Grunt";
00156 orc_config["random_traits"] = false;
00157 unit_type orc_type(orc_config);
00158
00159 unit orc1_side0_fake(&orc_type, 0, false);
00160
00161 size_t underlying_id = orc1_side0_fake.underlying_id();
00162 map_location hex = map_location(1,1);
00163
00164 unit_map unit_map;
00165
00166 typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
00167 t_uresult uresult1 = unit_map.add(hex, orc1_side0_fake);
00168
00169 BOOST_CHECK(uresult1.second == true);
00170
00171 {
00172 unit_map::unit_iterator ui = unit_map.find(underlying_id);
00173 BOOST_CHECK(uresult1.first == ui);
00174 BOOST_CHECK(ui->underlying_id() == orc1_side0_fake.underlying_id());
00175 }
00176
00177 unit* extracted_unit = unit_map.extract(hex);
00178
00179 {
00180 unit_map::unit_iterator ui = unit_map.find(underlying_id);
00181 BOOST_CHECK(ui == unit_map.end());
00182 }
00183
00184 unit_map.insert(extracted_unit);
00185 extracted_unit = NULL;
00186
00187 {
00188 unit_map::unit_iterator ui = unit_map.find(underlying_id);
00189 BOOST_CHECK(uresult1.first == ui);
00190 BOOST_CHECK(ui->underlying_id() == orc1_side0_fake.underlying_id());
00191 }
00192 }
00193
00194 BOOST_AUTO_TEST_CASE( track_real_unit_by_iterator ) {
00195
00196 config game_config(test_utils::get_test_config());
00197
00198 config orc_config;
00199 orc_config["id"]="Orcish Grunt";
00200 orc_config["random_traits"] = false;
00201 unit_type orc_type(orc_config);
00202
00203 unit orc1_side0_real(&orc_type, 0, true);
00204
00205 map_location hex = map_location(1,1);
00206
00207 unit_map unit_map;
00208
00209 typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
00210 t_uresult uresult1 = unit_map.add(hex, orc1_side0_real);
00211
00212 unit_map::unit_iterator unit_iterator = uresult1.first;
00213
00214 BOOST_CHECK(unit_iterator.valid());
00215
00216 unit* extracted_unit = unit_map.extract(hex);
00217
00218 BOOST_CHECK_MESSAGE(unit_iterator.valid() == false, "Iterator should be invalid after extraction.");
00219
00220 unit_map.insert(extracted_unit);
00221
00222 BOOST_CHECK_MESSAGE(unit_iterator.valid() == false, "Iterator should be invalid after extraction and reinsertion.");
00223
00224 unit_iterator = unit_map.find(hex);
00225 BOOST_CHECK(unit_iterator.valid());
00226 }
00227
00228 BOOST_AUTO_TEST_CASE( track_fake_unit_by_iterator ) {
00229 config game_config(test_utils::get_test_config());
00230
00231 config orc_config;
00232 orc_config["id"]="Orcish Grunt";
00233 orc_config["random_traits"] = false;
00234 unit_type orc_type(orc_config);
00235
00236 unit orc1_side0_fake(&orc_type, 0, false);
00237
00238 map_location hex = map_location(1,1);
00239
00240 unit_map unit_map;
00241
00242 typedef std::pair<unit_map::unit_iterator, bool> t_uresult;
00243 t_uresult uresult1 = unit_map.add(hex, orc1_side0_fake);
00244
00245 unit_map::unit_iterator unit_iterator = uresult1.first;
00246
00247 BOOST_CHECK(unit_iterator.valid());
00248
00249 unit* extracted_unit = unit_map.extract(hex);
00250
00251 BOOST_CHECK_MESSAGE(unit_iterator.valid() == false, "Iterator should be invalid after extraction.");
00252
00253 unit_map.insert(extracted_unit);
00254
00255 BOOST_CHECK_MESSAGE(unit_iterator.valid() == false, "Iterator should be invalid after extraction and reinsertion.");
00256
00257 unit_iterator = unit_map.find(hex);
00258 BOOST_CHECK(unit_iterator.valid());
00259 }
00260
00261
00262 BOOST_AUTO_TEST_SUITE_END()
00263