Advanced Strategic Command
repairtest.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * This program is free software; you can redistribute it and/or modify *
4  * it under the terms of the GNU General Public License as published by *
5  * the Free Software Foundation; either version 2 of the License, or *
6  * (at your option) any later version. *
7  * *
8  ***************************************************************************/
9 
10 #include <iostream>
11 
12 #include "../actions/moveunitcommand.h"
13 #include "../actions/repairunitcommand.h"
14 #include "../loaders.h"
15 #include "../itemrepository.h"
16 #include "unittestutil.h"
17 #include "repairtest.h"
18 
19 /* NextTurnStrategy_Abort will delete the map if the game cannot be continued.
20  * To avoid double deallocation, we must intercept the event and release the auto_ptr
21  */
22 class MapHolder : public sigc::trackable {
23  auto_ptr<GameMap> game;
24  public:
25  MapHolder( GameMap* gamemap ) : game( gamemap ){
26  GameMap::sigMapDeletion.connect( sigc::mem_fun( *this, &MapHolder::reset ));
27  }
28 
29  GameMap* get() {
30  return game.get();
31  }
32 
34  {
35  return game.operator->();
36  }
37 
38  private:
39  void reset( GameMap& gamemap ) {
40  if ( &gamemap == game.get() )
41  game.release();
42  }
43 };
44 
46 {
47  MapHolder game ( startMap("unittest-repair.map"));
48 
49  Vehicle* veh = game->getField(3,6)->vehicle;
50  assertOrThrow( veh != NULL );
51 
52  assertOrThrow ( veh->damage == 40 );
53 
54  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
55 
56  assertOrThrow ( veh->damage == 35 );
57 
58  for ( int i = 0; i < 10; ++i) {
59  // std::cout << "experience in turn " << i << " is " << veh->getExperience_offensive_raw() << "\n";
60  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
61  }
62 
63  assertOrThrow ( veh->damage == 10 );
64 
65 }
66 
67 
69 {
70  MapHolder game ( startMap("unittest-repair.map"));
71 
72  Vehicle* carrier = game->getField(1,9)->vehicle;
73  assertOrThrow( carrier != NULL );
74 
75  Vehicle* aircraft = carrier->getCargo(0);
76  assertOrThrow( aircraft != NULL );
77  assertOrThrow ( aircraft->getExperience_offensive() == 10 );
78  assertOrThrow ( aircraft->getExperience_defensive() == 10 );
79  assertOrThrow ( aircraft->damage == 50 );
80 
82  auto_ptr<RepairUnitCommand> ruc ( new RepairUnitCommand( carrier ));
83 
84  assertOrThrow( ruc->getInternalTargets().size() == 1 );
85  assertOrThrow( ruc->getInternalTargets()[0] == aircraft );
86 
87  ruc->setTarget(aircraft);
88 
89  ActionResult res = ruc->execute( createTestingContext( game.get() ));
90  if ( res.successful() )
91  ruc.release();
92  else
93  throw ActionResult(res);
94 
95  assertOrThrow ( aircraft->damage == 0 );
96  assertOrThrow ( aircraft->getExperience_offensive() == 9 );
97  assertOrThrow ( aircraft->getExperience_defensive() == 9 );
98 
99 }
100 
102  MapHolder game ( startMap("unittest-selfdamage.map"));
103  Vehicle* u1 = game->getField(4,7)->vehicle;
104  Vehicle* u2 = game->getField(5,6)->vehicle;
105 
106  assertOrThrow( u1->damage == 0 );
107  assertOrThrow ( u2->damage == 79 );
108 
109  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
110 
111  assertOrThrow( u1->damage == 10 );
112  assertOrThrow ( u2->damage == 89 );
113 
114  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
115  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
116 
117  u2 = game->getField(5,6)->vehicle;
118  assertOrThrow( u2 == NULL );
119 
120 
121 }
122 
124  testAutoRepair();
126  testSelfDamage();
127 }
int getExperience_defensive() const
Definition: vehicle.cpp:258
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
Vehicle * vehicle
Definition: mapfield.h:89
#define assertOrThrow(expr)
Definition: errors.h:69
Context createTestingContext(GameMap *gamemap)
void testUnitRepair()
Definition: repairtest.cpp:123
GameMap * get()
Definition: repairtest.cpp:29
void next_turn(GameMap *gamemap, const NextTurnStrategy &nextTurnStrategy, MapDisplayInterface *display, int playerView)
static sigc::signal< void, GameMap & > sigMapDeletion
Definition: gamemap.h:501
static bool avail(const ContainerBase *servicer)
GameMap * startMap(const ASCString &filename)
void testSelfDamage()
Definition: repairtest.cpp:101
void testManualRepair()
Definition: repairtest.cpp:68
bool successful() const
GameMap * operator->() const
Definition: repairtest.cpp:33
MapHolder(GameMap *gamemap)
Definition: repairtest.cpp:25
void testAutoRepair()
Definition: repairtest.cpp:45
const Cargo & getCargo() const
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465
int getExperience_offensive() const
Definition: vehicle.cpp:253