Advanced Strategic Command
researchtest.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 "../gamemap.h"
11 #include "../loaders.h"
12 #include "diplomacytest.h"
13 #include "unittestutil.h"
14 #include "spfst.h"
15 #include "../actions/directresearchcommand.h"
16 #include "itemrepository.h"
17 #include "../actions/buildproductionlinecommand.h"
18 
19 bool vectorContains( vector<const Technology*>& v, const Technology* t )
20 {
21  return find( v.begin(), v.end(), t) != v.end();
22 }
23 
25 {
26  auto_ptr<GameMap> game ( startMap("unittest-research.map"));
27 
28  Player& p0 = game->getPlayer(0);
29  Research& r = p0.research;
30 
31  assertOrThrow( r.progress == 0 );
33 
34  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
35  assertOrThrow( game->actplayer == 0);
36 
37  assertOrThrow( r.progress == 40 );
39 
40  Technology* t1 = technologyRepository.getObject_byID( 1000000002 );
41  assertOrThrow( t1 != NULL );
42 
43  Technology* t2 = technologyRepository.getObject_byID( 1000000003 );
44  assertOrThrow( t2 != NULL );
45 
46  Technology* tjam = technologyRepository.getObject_byID( 1000000051 );
47  assertOrThrow( tjam != NULL );
48 
50  vector<const Technology*> avTechs = drc->getAvailableTechnologies( true );
51 
52  assertOrThrow( vectorContains(avTechs, t1 ));
53  assertOrThrow( vectorContains(avTechs, t2 ));
54  assertOrThrow( vectorContains(avTechs, tjam ));
55 
56  drc->setTechnology( t1 );
57  ActionResult res = drc->execute( createTestingContext( game.get() ));
58 
59  assertOrThrow( res.successful());
60 
61 
62  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
63  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
64  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
65 
66  assertOrThrow( r.activetechnology == NULL );
67  assertOrThrow( r.progress == 20 );
68 
69  drc = new DirectResearchCommand( p0 );
70  avTechs = drc->getAvailableTechnologies( true );
71 
72  assertOrThrow( !vectorContains(avTechs, t1 ));
73  assertOrThrow( vectorContains(avTechs, t2 ));
74  assertOrThrow( vectorContains(avTechs, tjam ));
75 
76  drc->setTechnology( t2 );
77  res = drc->execute( createTestingContext( game.get() ));
78  assertOrThrow( res.successful());
79 
80  Building* bld = game->getField( MapCoordinate( 7,8 ))->building;
81  assertOrThrow( bld != NULL );
82 
83  VehicleType* jam = vehicleTypeRepository.getObject_byID( 1000000051 );
84  assertOrThrow( jam != NULL );
85 
87 
88  vector<const VehicleType*> prods = bplc->productionLinesBuyable();
89  assertOrThrow( find( prods.begin(), prods.end(), jam ) == prods.end() );
90 
91  drc = new DirectResearchCommand( p0 );
92  drc->setTechnology( tjam );
93  res = drc->execute( createTestingContext( game.get() ));
94 
95  assertOrThrow( r.progress == 5 );
96 
97  prods = bplc->productionLinesBuyable();
98  assertOrThrow( find( prods.begin(), prods.end(), jam ) != prods.end() );
99 
100  bplc->setProduction( jam );
101  res = bplc->execute( createTestingContext( game.get() ));
102  assertOrThrow( res.successful() );
103 
104  res = game->actions.undo( createTestingContext( game.get() ) ); // build production line
105  assertOrThrow( res.successful() );
106 
107  res = game->actions.undo( createTestingContext( game.get() ) ); // research jammer
108  assertOrThrow( res.successful() );
109 
110  res = game->actions.undo( createTestingContext( game.get() ) ); // research T level 2
111  assertOrThrow( res.successful() );
112 
113  assertOrThrow( r.activetechnology == NULL );
114  assertOrThrow( r.progress == 20 );
115 
116 
117  BuildProductionLineCommand bplc2( bld );
118  prods = bplc2.productionLinesBuyable();
119  assertOrThrow( find( prods.begin(), prods.end(), jam ) == prods.end() );
120 
121 }
122 
124 {
125  auto_ptr<GameMap> game ( startMap("unittest-research.map"));
126 
127  Player& p0 = game->getPlayer(0);
128  Research& r = p0.research;
129 
130  assertOrThrow( r.progress == 0 );
132 
133  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
134  assertOrThrow( game->actplayer == 0);
135 
136  assertOrThrow( r.progress == 40 );
138 
139  Technology* t1 = technologyRepository.getObject_byID( 1000000002 );
140  assertOrThrow( t1 != NULL );
141 
142  Technology* t2 = technologyRepository.getObject_byID( 1000000003 );
143  assertOrThrow( t2 != NULL );
144 
145  Technology* tjam = technologyRepository.getObject_byID( 1000000051 );
146  assertOrThrow( tjam != NULL );
147 
149  vector<const Technology*> avTechs = drc->getAvailableTechnologies( true );
150 
151  assertOrThrow( vectorContains(avTechs, t1 ));
152  assertOrThrow( vectorContains(avTechs, t2 ));
153  assertOrThrow( vectorContains(avTechs, tjam ));
154 
155  drc->setTechnology( tjam );
156  ActionResult res = drc->execute( createTestingContext( game.get() ));
157 
158  assertOrThrow( res.successful());
159 
160 
161  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
162  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
163  next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
164 
165  assertOrThrow( r.activetechnology == NULL );
166  assertOrThrow( r.progress == 15 );
167 
168  drc = new DirectResearchCommand( p0 );
169  avTechs = drc->getAvailableTechnologies( true );
170 
171  assertOrThrow( !vectorContains(avTechs, t1 ));
172  assertOrThrow( vectorContains(avTechs, t2 ));
173  assertOrThrow( !vectorContains(avTechs, tjam ));
174 
175  drc->setTechnology( t2 );
176  res = drc->execute( createTestingContext( game.get() ));
177  assertOrThrow( res.successful());
178 
179  Building* bld = game->getField( MapCoordinate( 7,8 ))->building;
180  assertOrThrow( bld != NULL );
181 
182  VehicleType* jam = vehicleTypeRepository.getObject_byID( 1000000051 );
183  assertOrThrow( jam != NULL );
184 
185  BuildProductionLineCommand bplc ( bld );
186 
187  vector<const VehicleType*> prods = bplc.productionLinesBuyable();
188  assertOrThrow( find( prods.begin(), prods.end(), jam ) != prods.end() );
189 
190 }
191 
192 
194 {
195  testresearch1();
196  testresearch2();
197 }
vector< const Technology * > getAvailableTechnologies(bool longTerm)
returns the technologies that can be researched by the player
map accessing and usage routines used by ASC and the mapeditor
#define assertOrThrow(expr)
Definition: errors.h:69
Context createTestingContext(GameMap *gamemap)
ItemRepositoryLoader< Technology > technologyRepository("technology")
void testResearch()
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
void next_turn(GameMap *gamemap, const NextTurnStrategy &nextTurnStrategy, MapDisplayInterface *display, int playerView)
ActionResult execute(const Context &context)
Definition: action.cpp:41
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
const Technology * activetechnology
the technology that is currently being researched
Definition: research.h:191
Coordinate on the twodimensional map.
Definition: typen.h:202
MapField * getField(const BuildingType::LocalCoordinate &localCoordinates) const
returns the pointer to the field which the given part of the building is standing on ...
Definition: buildings.cpp:399
cancels the research on the current technology, losing all research points
GameMap * startMap(const ASCString &filename)
void testresearch2()
bool successful() const
void testresearch1()
Research research
the status of the scientific research
Definition: player.h:142
void setTechnology(const Technology *tech)
static bool available(const Player &player)
Building * building
Definition: mapfield.h:102
An actual building on the map, which references a BuildingType Buildings have an owner,.
Definition: buildings.h:38
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
int progress
Definition: research.h:189
vector< const VehicleType * > productionLinesBuyable()
bool vectorContains(vector< const Technology * > &v, const Technology *t)
void setProduction(const VehicleType *vehicleType)