The Battle for Wesnoth  1.19.7+dev
test_whiteboard_side_actions.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
3  by Étienne Simon <etienne.jl.simon@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-test"
17 
18 #include "whiteboard/action.hpp"
20 #include "whiteboard/typedefs.hpp"
21 #include "whiteboard/visitor.hpp"
22 
23 #include <boost/test/unit_test.hpp>
24 
25 using namespace wb;
26 
28  dummy_action(std::size_t team_index, bool hidden, int id): action(team_index, hidden), id_(id) {}
29  int id_;
30 
31  // un-abstraction
32  std::ostream& print(std::ostream& s) const { s<<id_; return s; }
33  void accept(visitor&){}
34  std::shared_ptr<dummy_action> shared_from_this() { return std::static_pointer_cast<dummy_action>(action::shared_from_this()); }
35  void execute(bool& success, bool& complete){ success=true; complete=true; }
38  void draw_hex(const map_location&){}
40  unit_ptr get_unit() const { return unit_ptr(); }
42  error check_validity() const { return OK; }
43 };
44 
45 BOOST_AUTO_TEST_SUITE( whiteboard_side_actions_container )
46 
47 BOOST_AUTO_TEST_CASE( test_insertion )
48 {
50  std::shared_ptr<dummy_action> dact;
51 
52  // Basic insertions
53  auto act1 = std::make_shared<dummy_action>(0, false, 1);
54  auto act2 = std::make_shared<dummy_action>(0, false, 2);
55  auto act3 = std::make_shared<dummy_action>(0, false, 3);
56 
57  sac.queue(0, act2);
58  sac.queue(0, act3);
59  sac.insert(sac.begin(), act1);
60 
61  BOOST_REQUIRE(sac.num_turns() == 1);
62 
63  int tmp=0;
64  for(action_ptr act : sac) {
65  ++tmp;
66  BOOST_REQUIRE(dact = std::dynamic_pointer_cast<dummy_action>(act));
67  BOOST_REQUIRE(dact->id_ == tmp);
68  }
69 
70  // Multi-turn insertions
71  auto act4 = std::make_shared<dummy_action>(0, false, 4);
72  auto act5 = std::make_shared<dummy_action>(0, false, 5);
73  auto act6 = std::make_shared<dummy_action>(0, false, 6);
74  auto act7 = std::make_shared<dummy_action>(0, false, 7);
75  auto act8 = std::make_shared<dummy_action>(0, false, 8);
76  sac.queue(1, act5);
77  sac.queue(2, act8);
78  sac.queue(1, act7);
79  sac.queue(0, act4);
80  sac.insert(sac.turn_begin(1)+1, act6);
81 
82  BOOST_REQUIRE(sac.num_turns() == 3);
83 
84  tmp=0;
85  for(action_ptr act : sac) {
86  ++tmp;
87  BOOST_REQUIRE(dact = std::dynamic_pointer_cast<dummy_action>(act));
88  BOOST_REQUIRE(dact->id_ == tmp);
89  }
90 
91  BOOST_REQUIRE(dact = std::dynamic_pointer_cast<dummy_action>(*sac.turn_begin(1)));
92  BOOST_REQUIRE(dact->id_ == 5);
93 
94  BOOST_REQUIRE(dact = std::dynamic_pointer_cast<dummy_action>(*(1+sac.turn_begin(1))));
95  BOOST_REQUIRE(dact->id_ == 6);
96 
97  BOOST_REQUIRE(sac.turn_size(1) == 3);
98  BOOST_REQUIRE(3+sac.turn_begin(1) == sac.turn_end(1));
99 }
100 
101 BOOST_AUTO_TEST_CASE( test_removal )
102 {
104  std::shared_ptr<dummy_action> dact;
105 
106  auto act1 = std::make_shared<dummy_action>(0, false, 1);
107  auto act2 = std::make_shared<dummy_action>(0, false, 2);
108  auto act3 = std::make_shared<dummy_action>(0, false, 3);
109  auto act4 = std::make_shared<dummy_action>(0, false, 4);
110  auto act5 = std::make_shared<dummy_action>(0, false, 5);
111  auto act6 = std::make_shared<dummy_action>(0, false, 6);
112 
113  sac.queue(0, act1);
114  side_actions::iterator ite2 = sac.queue(0, act2);
115  sac.queue(0, act3);
116  side_actions::iterator ite4 = sac.queue(1, act4);
117  sac.queue(1, act5);
118  side_actions::iterator ite6 = sac.queue(2, act6);
119 
120  sac.erase(ite2);
121  sac.erase(ite4);
122  sac.erase(ite6);
123 
124  BOOST_REQUIRE(sac.num_turns() == 2);
125  side_actions::iterator it = sac.begin();
126  for(int i=1; i<6; i+=2, ++it){
127  BOOST_REQUIRE(dact = std::dynamic_pointer_cast<dummy_action>(*it));
128  BOOST_REQUIRE(dact->id_ == i);
129  }
130 }
131 
132 BOOST_AUTO_TEST_SUITE_END()
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
Container associating units to locations.
Definition: map.hpp:98
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
error
Possible errors.
Definition: action.hpp:107
Datastructure holding the actions of a side on multiple turns.
iterator turn_end(std::size_t turn_num)
iterator queue(std::size_t turn_num, const action_ptr &action)
Queues an action to be executed last.
iterator turn_begin(std::size_t turn_num)
Returns the iterator for the first (executed earlier) action of a given turn within the actions queue...
iterator erase(iterator position)
Deletes the action at the specified position.
iterator begin()
Returns the iterator for the first (executed earlier) action within the actions queue.
std::size_t turn_size(std::size_t turn_num) const
Returns the number of actions planned for turn turn_num.
iterator insert(iterator position, const action_ptr &action)
Inserts an action at the specified position.
std::size_t num_turns() const
Returns the number of turns that have plans.
container::iterator iterator
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses.
Definition: visitor.hpp:33
std::size_t i
Definition: function.cpp:1029
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:198
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
Definition: display.hpp:45
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
dummy_action(std::size_t team_index, bool hidden, int id)
unit_ptr get_unit() const
Return the unit targeted by this action.
error check_validity() const
Check the validity of the action.
void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
map_location get_numbering_hex() const
void draw_hex(const map_location &)
Gets called by display when drawing a hex, to allow actions to draw to the screen.
std::shared_ptr< dummy_action > shared_from_this()
void apply_temp_modifier(unit_map &)
Applies temporarily the result of this action to the specified unit map.
std::ostream & print(std::ostream &s) const
void remove_temp_modifier(unit_map &)
Removes the result of this action from the specified unit map.
Encapsulates the map of the game.
Definition: location.hpp:45
BOOST_AUTO_TEST_SUITE(filesystem)
static map_location::direction s
BOOST_AUTO_TEST_CASE(test_insertion)
Contains typedefs for the whiteboard.
visitor is an abstract interface : action.accept(visitor) calls visitor.visit(action)