The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_whiteboard_side_actions.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Étienne Simon <[email protected]>
3  Part of the 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 "whiteboard/action.hpp"
19 #include "whiteboard/typedefs.hpp"
20 #include "whiteboard/visitor.hpp"
21 
22 #include <boost/test/unit_test.hpp>
23 
24 using namespace wb;
25 
27  dummy_action(size_t team_index, bool hidden, int id): action(team_index, hidden), id_(id) {}
28  int id_;
29 
30  // un-abstraction
31  std::ostream& print(std::ostream& s) const { s<<id_; return s; }
32  void accept(visitor&){}
33  boost::shared_ptr<dummy_action> shared_from_this() { return boost::static_pointer_cast<dummy_action>(action::shared_from_this()); }
34  void execute(bool& success, bool& complete){ success=true; complete=true; }
37  void draw_hex(const map_location&){}
39  unit_ptr get_unit() const { return unit_ptr(); }
41  error check_validity() const { return OK; }
42 };
43 
44 BOOST_AUTO_TEST_SUITE( whiteboard_side_actions_container )
45 
46 BOOST_AUTO_TEST_CASE( test_insertion )
47 {
50 
51  // Basic insertions
52  boost::shared_ptr<dummy_action> act1(new dummy_action(0, false, 1));
53  boost::shared_ptr<dummy_action> act2(new dummy_action(0, false, 2));
54  boost::shared_ptr<dummy_action> act3(new dummy_action(0, false, 3));
55 
56  sac.queue(0, act2);
57  sac.queue(0, act3);
58  sac.insert(sac.begin(), act1);
59 
60  BOOST_REQUIRE(sac.num_turns() == 1);
61 
62  int tmp=0;
63  for(action_ptr act : sac) {
64  ++tmp;
65  BOOST_REQUIRE(dact = boost::dynamic_pointer_cast<dummy_action>(act));
66  BOOST_REQUIRE(dact->id_ == tmp);
67  }
68 
69  // Multi-turn insertions
70  boost::shared_ptr<dummy_action> act4(new dummy_action(0, false, 4));
71  boost::shared_ptr<dummy_action> act5(new dummy_action(0, false, 5));
72  boost::shared_ptr<dummy_action> act6(new dummy_action(0, false, 6));
73  boost::shared_ptr<dummy_action> act7(new dummy_action(0, false, 7));
74  boost::shared_ptr<dummy_action> act8(new dummy_action(0, false, 8));
75  sac.queue(1, act5);
76  sac.queue(2, act8);
77  sac.queue(1, act7);
78  sac.queue(0, act4);
79  sac.insert(sac.turn_begin(1)+1, act6);
80 
81  BOOST_REQUIRE(sac.num_turns() == 3);
82 
83  tmp=0;
84  for(action_ptr act : sac) {
85  ++tmp;
86  BOOST_REQUIRE(dact = boost::dynamic_pointer_cast<dummy_action>(act));
87  BOOST_REQUIRE(dact->id_ == tmp);
88  }
89 
90  BOOST_REQUIRE(dact = boost::dynamic_pointer_cast<dummy_action>(*sac.turn_begin(1)));
91  BOOST_REQUIRE(dact->id_ == 5);
92 
93  BOOST_REQUIRE(dact = boost::dynamic_pointer_cast<dummy_action>(*(1+sac.turn_begin(1))));
94  BOOST_REQUIRE(dact->id_ == 6);
95 
96  BOOST_REQUIRE(sac.turn_size(1) == 3);
97  BOOST_REQUIRE(3+sac.turn_begin(1) == sac.turn_end(1));
98 }
99 
100 BOOST_AUTO_TEST_CASE( test_removal )
101 {
104 
105  boost::shared_ptr<dummy_action> act1(new dummy_action(0, false, 1));
106  boost::shared_ptr<dummy_action> act2(new dummy_action(0, false, 2));
107  boost::shared_ptr<dummy_action> act3(new dummy_action(0, false, 3));
108  boost::shared_ptr<dummy_action> act4(new dummy_action(0, false, 4));
109  boost::shared_ptr<dummy_action> act5(new dummy_action(0, false, 5));
110  boost::shared_ptr<dummy_action> act6(new dummy_action(0, false, 6));
111 
112  sac.queue(0, act1);
113  side_actions::iterator ite2 = sac.queue(0, act2);
114  sac.queue(0, act3);
115  side_actions::iterator ite4 = sac.queue(1, act4);
116  sac.queue(1, act5);
117  side_actions::iterator ite6 = sac.queue(2, act6);
118 
119  sac.erase(ite2);
120  sac.erase(ite4);
121  sac.erase(ite6);
122 
123  BOOST_REQUIRE(sac.num_turns() == 2);
124  side_actions::iterator it = sac.begin();
125  for(int i=1; i<6; i+=2, ++it){
126  BOOST_REQUIRE(dact = boost::dynamic_pointer_cast<dummy_action>(*it));
127  BOOST_REQUIRE(dact->id_ == i);
128  }
129 }
130 
131 BOOST_AUTO_TEST_SUITE_END()
container::iterator iterator
std::string id_
Definition: formula.cpp:636
size_t num_turns() const
Returns the number of turns that have plans.
BOOST_AUTO_TEST_SUITE(test_map_location)
boost::shared_ptr< dummy_action > shared_from_this()
iterator erase(iterator position)
Deletes the action at the specified position.
void draw_hex(const map_location &)
Gets called by display when drawing a hex, to allow actions to draw to the screen.
error
Possible errors.
Definition: action.hpp:104
Contains typedefs for the whiteboard.
map_location get_numbering_hex() const
BOOST_AUTO_TEST_CASE(test_insertion)
unit_ptr get_unit() const
Return the unit targeted by this action.
Encapsulates the map of the game.
Definition: location.hpp:38
error check_validity() const
Check the validity of the action.
dummy_action(size_t team_index, bool hidden, int id)
void apply_temp_modifier(unit_map &)
Applies temporarily the result of this action to the specified unit map.
Datastructure holding the actions of a side on multiple turns.
size_t i
Definition: function.cpp:1057
iterator begin()
Returns the iterator for the first (executed earlier) action within the actions queue.
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
std::ostream & print(std::ostream &s) const
iterator insert(iterator position, action_ptr action)
Inserts an action at the specified position.
Container associating units to locations.
Definition: map.hpp:90
visitor is an abstract interface : action.accept(visitor) calls visitor.visit(action) ...
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
GLdouble s
Definition: glew.h:1358
iterator queue(size_t turn_num, action_ptr action)
Queues an action to be executed last.
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
Definition: display.hpp:47
void remove_temp_modifier(unit_map &)
Removes the result of this action from the specified unit map.
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses...
Definition: visitor.hpp:32