tests/test_unit_map.cpp

Go to the documentation of this file.
00001 /* $Id: test_unit_map.cpp 52627 2012-01-17 01:36:54Z gabba $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Pauli Nieminen <paniemin@cc.hut.fi>
00004    Part of thie Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
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 ./test --report_level=detailed --log_level=all --run_test=interpolate_suite
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     //  unit * orc1p = new unit(orc1_side0_real);
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     // std::cerr<<"ID real ="<<orc1_side0_real.underlying_id()<<"\n";
00075     // std::cerr<<"ID fake ="<<orc2_side0_fake.underlying_id()<<"\n";
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     //To check that the collisions will cut off change the cutoff in unit_map.cpp from 1e6 to less than the guard value below
00085     // unit_map.add(map_location(1,3), orc2_side0_fake);
00086     // unit_map.add(map_location(1,3), orc2_side0_fake);
00087 
00088     // unsigned long long guard =0;
00089     // for(; guard< 2e2;++guard) {
00090     //  unit_map.add(map_location(2,guard), orc1_side0_real);
00091     // };
00092 
00093     // n_unit::id_manager::instance().clear();
00094     // std::cerr<<"BREAK\n;";
00095     // unit_map.add(map_location(1,3), orc2_side0_fake);
00096     // unit_map.add(map_location(1,4), orc2_side0_fake);
00097     // try {
00098     //  unit_map.add(map_location(1,5), orc2_side0_fake);
00099     // }catch (std::runtime_error e ){
00100     //  BOOST_CHECK_MESSAGE(std::string(e.what()) == std::string("One million collisions in unit_map")
00101     //                      , "One million uid collision exception");
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 /* vim: set ts=4 sw=4: */
00262 BOOST_AUTO_TEST_SUITE_END()
00263 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:12 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs