researchtest.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  ***************************************************************************/
00009 
00010 #include "../gamemap.h"
00011 #include "../loaders.h"
00012 #include "diplomacytest.h"
00013 #include "unittestutil.h"
00014 #include "spfst.h"
00015 #include "../actions/directresearchcommand.h"
00016 #include "itemrepository.h"
00017 #include "../actions/buildproductionlinecommand.h"
00018 
00019 bool vectorContains( vector<const Technology*>& v, const Technology* t )
00020 {
00021    return find( v.begin(), v.end(), t) != v.end();  
00022 }
00023 
00024 void testresearch1() 
00025 {
00026    auto_ptr<GameMap> game ( startMap("unittest-research.map"));
00027    
00028    Player& p0 = game->getPlayer(0);
00029    Research& r = p0.research;
00030    
00031    assertOrThrow( r.progress == 0 );
00032    assertOrThrow( !DirectResearchCommand::available(p0 ));
00033    
00034    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00035    assertOrThrow( game->actplayer == 0);
00036    
00037    assertOrThrow( r.progress == 40 );
00038    assertOrThrow( DirectResearchCommand::available(p0 ));
00039    
00040    Technology* t1 = technologyRepository.getObject_byID( 1000000002  );
00041    assertOrThrow( t1 != NULL );
00042    
00043    Technology* t2 = technologyRepository.getObject_byID( 1000000003  );
00044    assertOrThrow( t2 != NULL );
00045    
00046    Technology* tjam = technologyRepository.getObject_byID( 1000000051  );
00047    assertOrThrow( tjam != NULL );
00048    
00049    DirectResearchCommand* drc = new DirectResearchCommand( p0 );
00050    vector<const Technology*> avTechs = drc->getAvailableTechnologies( true );
00051    
00052    assertOrThrow( vectorContains(avTechs, t1 ));
00053    assertOrThrow( vectorContains(avTechs, t2 ));
00054    assertOrThrow( vectorContains(avTechs, tjam ));
00055    
00056    drc->setTechnology( t1 );
00057    ActionResult res = drc->execute( createTestingContext( game.get() ));
00058    
00059    assertOrThrow( res.successful());
00060    
00061    
00062    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00063    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00064    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00065    
00066    assertOrThrow( r.activetechnology == NULL );
00067    assertOrThrow( r.progress == 20 );
00068    
00069    drc = new DirectResearchCommand( p0 );
00070    avTechs = drc->getAvailableTechnologies( true );
00071    
00072    assertOrThrow( !vectorContains(avTechs, t1 ));
00073    assertOrThrow( vectorContains(avTechs, t2 ));
00074    assertOrThrow( vectorContains(avTechs, tjam ));
00075    
00076    drc->setTechnology( t2 );
00077    res = drc->execute( createTestingContext( game.get() ));
00078    assertOrThrow( res.successful());
00079    
00080    Building* bld = game->getField( MapCoordinate( 7,8 ))->building;
00081    assertOrThrow( bld != NULL );
00082    
00083    VehicleType* jam = vehicleTypeRepository.getObject_byID( 1000000051 );
00084    assertOrThrow( jam != NULL );
00085    
00086    BuildProductionLineCommand* bplc = new BuildProductionLineCommand( bld );
00087    
00088    vector<const VehicleType*> prods = bplc->productionLinesBuyable();
00089    assertOrThrow( find( prods.begin(), prods.end(), jam ) == prods.end() );
00090    
00091    drc = new DirectResearchCommand( p0 );
00092    drc->setTechnology( tjam );
00093    res = drc->execute( createTestingContext( game.get() ));
00094    
00095    assertOrThrow( r.progress == 5 );
00096    
00097    prods = bplc->productionLinesBuyable();
00098    assertOrThrow( find( prods.begin(), prods.end(), jam ) != prods.end() );
00099    
00100    bplc->setProduction( jam );
00101    res = bplc->execute( createTestingContext( game.get() ));
00102    assertOrThrow( res.successful() );
00103    
00104    res = game->actions.undo( createTestingContext( game.get() ) );  // build production line
00105    assertOrThrow( res.successful() );
00106    
00107    res = game->actions.undo( createTestingContext( game.get() ) );  // research jammer 
00108    assertOrThrow( res.successful() );
00109    
00110    res = game->actions.undo( createTestingContext( game.get() ) );  // research T level 2
00111    assertOrThrow( res.successful() );
00112    
00113    assertOrThrow( r.activetechnology == NULL );
00114    assertOrThrow( r.progress == 20 );
00115    
00116    
00117    BuildProductionLineCommand bplc2( bld );
00118    prods = bplc2.productionLinesBuyable();
00119    assertOrThrow( find( prods.begin(), prods.end(), jam ) == prods.end() );
00120    
00121 }
00122 
00123 void testresearch2() 
00124 {
00125    auto_ptr<GameMap> game ( startMap("unittest-research.map"));
00126    
00127    Player& p0 = game->getPlayer(0);
00128    Research& r = p0.research;
00129    
00130    assertOrThrow( r.progress == 0 );
00131    assertOrThrow( !DirectResearchCommand::available(p0 ));
00132    
00133    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00134    assertOrThrow( game->actplayer == 0);
00135    
00136    assertOrThrow( r.progress == 40 );
00137    assertOrThrow( DirectResearchCommand::available(p0 ));
00138    
00139    Technology* t1 = technologyRepository.getObject_byID( 1000000002  );
00140    assertOrThrow( t1 != NULL );
00141    
00142    Technology* t2 = technologyRepository.getObject_byID( 1000000003  );
00143    assertOrThrow( t2 != NULL );
00144    
00145    Technology* tjam = technologyRepository.getObject_byID( 1000000051  );
00146    assertOrThrow( tjam != NULL );
00147    
00148    DirectResearchCommand* drc = new DirectResearchCommand( p0 );
00149    vector<const Technology*> avTechs = drc->getAvailableTechnologies( true );
00150    
00151    assertOrThrow( vectorContains(avTechs, t1 ));
00152    assertOrThrow( vectorContains(avTechs, t2 ));
00153    assertOrThrow( vectorContains(avTechs, tjam ));
00154    
00155    drc->setTechnology( tjam );
00156    ActionResult res = drc->execute( createTestingContext( game.get() ));
00157    
00158    assertOrThrow( res.successful());
00159    
00160    
00161    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00162    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00163    next_turn( game.get(), NextTurnStrategy_Abort(), NULL, -1 );
00164    
00165    assertOrThrow( r.activetechnology == NULL );
00166    assertOrThrow( r.progress == 15 );
00167    
00168    drc = new DirectResearchCommand( p0 );
00169    avTechs = drc->getAvailableTechnologies( true );
00170    
00171    assertOrThrow( !vectorContains(avTechs, t1 ));
00172    assertOrThrow( vectorContains(avTechs, t2 ));
00173    assertOrThrow( !vectorContains(avTechs, tjam ));
00174    
00175    drc->setTechnology( t2 );
00176    res = drc->execute( createTestingContext( game.get() ));
00177    assertOrThrow( res.successful());
00178    
00179    Building* bld = game->getField( MapCoordinate( 7,8 ))->building;
00180    assertOrThrow( bld != NULL );
00181    
00182    VehicleType* jam = vehicleTypeRepository.getObject_byID( 1000000051 );
00183    assertOrThrow( jam != NULL );
00184    
00185    BuildProductionLineCommand  bplc ( bld );
00186    
00187    vector<const VehicleType*> prods = bplc.productionLinesBuyable();
00188    assertOrThrow( find( prods.begin(), prods.end(), jam ) != prods.end() );
00189    
00190 }
00191 
00192 
00193 void testResearch() 
00194 {
00195    testresearch1();
00196    testresearch2();
00197 }

Generated on Mon May 21 01:26:36 2012 for Advanced Strategic Command by  doxygen 1.5.1