Revision: 4893
Author:
Xawo...@gmail.com
Date: Wed Oct 23 22:08:30 2013 UTC
Log: Support for running the testsuite in command line programs for
continuous integration.
Here is a small example inspired from SDLmain.c:
g_launcher = new JGameLauncher();
InitGame();
MTGCollection()->loadFolder("sets/primitives/");
MTGCollection()->loadFolder("sets/", "_cards.dat");
options.reloadProfile();
TestSuite testSuite("test/_tests.txt");
int result = testSuite.run();
DestroyGame();
delete g_launcher;
return result;
http://code.google.com/p/wagic/source/detail?r=4893
Modified:
/trunk/projects/mtg/include/MTGDeck.h
/trunk/projects/mtg/include/TestSuiteAI.h
/trunk/projects/mtg/src/GameApp.cpp
/trunk/projects/mtg/src/GameStateDuel.cpp
/trunk/projects/mtg/src/MTGDeck.cpp
/trunk/projects/mtg/src/TestSuiteAI.cpp
=======================================
--- /trunk/projects/mtg/include/MTGDeck.h Wed Jan 2 16:15:52 2013 UTC
+++ /trunk/projects/mtg/include/MTGDeck.h Wed Oct 23 22:08:30 2013 UTC
@@ -125,6 +125,8 @@
#endif
MTGCard * getCardByName(string name);
+ void loadFolder(const string& folder, const string& filename="" );
+
int load(const char * config_file, const char * setName = NULL, int
autoload = 1);
int countByType(const char * _type);
int countByColor(int color);
@@ -181,9 +183,8 @@
return instance->subtypesList.find(value,false);
}
- static void loadInstance();
static void unloadAll();
- static inline MTGAllCards* getInstance() { return instance; };
+ static MTGAllCards* getInstance();
private:
boost::mutex mMutex;
=======================================
--- /trunk/projects/mtg/include/TestSuiteAI.h Mon Sep 23 19:40:12 2013 UTC
+++ /trunk/projects/mtg/include/TestSuiteAI.h Wed Oct 23 22:08:30 2013 UTC
@@ -85,26 +85,33 @@
void cleanup();
vector<boost::thread*> mWorkerThread;
Rules* mRules;
+
bool mProcessing;
+ int startTime, endTime;
+ static void ThreadProc(void* inParam);
+ string getNextFile() {
+ boost::mutex::scoped_lock lock(mMutex);
+ if (currentfile >= nbfiles) return "";
+ currentfile++;
+ return files[currentfile - 1];
+ };
+ void pregameTests();
public:
- int startTime, endTime;
+ int getElapsedTime() {return endTime-startTime;};
unsigned int seed;
int nbFailed, nbTests, nbAIFailed, nbAITests;
TestSuite(const char * filename);
~TestSuite();
void initGame(GameObserver* g);
- void pregameTests();
int loadNext();
- string getNextFile() {
- boost::mutex::scoped_lock lock(mMutex);
- if (currentfile >= nbfiles) return "";
- currentfile++;
- return files[currentfile - 1];
+ void setRules(Rules* rules) {
+ mRules = rules;
};
- static void ThreadProc(void* inParam);
- void setRules(Rules* rules) {mRules = rules;};
void handleResults(bool wasAI, int error);
+ // run the test suite in turbo mode without UI,
+ // returns the amount of failed tests (AI or not), so 0 if everything
went fine.
+ int run();
};
class TestSuiteAI:public AIPlayerBaka
=======================================
--- /trunk/projects/mtg/src/GameApp.cpp Sun Feb 3 21:41:31 2013 UTC
+++ /trunk/projects/mtg/src/GameApp.cpp Wed Oct 23 22:08:30 2013 UTC
@@ -164,7 +164,7 @@
HasMusic =
jfs->FileExists(WResourceManager::Instance()->musicFile("Track0.mp3")) &&
jfs->FileExists(WResourceManager::Instance()->musicFile("Track1.mp3"));
LOG("Init Collection");
- MTGAllCards::loadInstance();
+ MTGAllCards::getInstance();
LOG("Loading rules");
Rules::loadAllRules();
=======================================
--- /trunk/projects/mtg/src/GameStateDuel.cpp Tue Jun 18 01:41:34 2013 UTC
+++ /trunk/projects/mtg/src/GameStateDuel.cpp Wed Oct 23 22:08:30 2013 UTC
@@ -1097,7 +1097,7 @@
char buf[4096];
mFont->SetColor(ARGB(255,255,255,255));
- int elapsedTime = (testSuite->endTime -
testSuite->startTime);
+ int elapsedTime = testSuite->getElapsedTime();
sprintf(buf, "Time to run the tests: %is",
elapsedTime/1000);
mFont->DrawString(buf,0,SCREEN_HEIGHT/2 - 20);
=======================================
--- /trunk/projects/mtg/src/MTGDeck.cpp Sat Oct 19 14:29:17 2013 UTC
+++ /trunk/projects/mtg/src/MTGDeck.cpp Wed Oct 23 22:08:30 2013 UTC
@@ -335,6 +335,42 @@
total_cards = 0;
initCounters();
}
+
+void MTGAllCards::loadFolder(const string& folder, const string& filename )
+{
+ vector<string> files = JFileSystem::GetInstance()->scanfolder(folder);
+
+ if (!files.size())
+ {
+ DebugTrace("loadPrimitives:WARNING:Primitives folder is missing");
+ return;
+ }
+
+ for (size_t i = 0; i < files.size(); ++i)
+ {
+ string afile = folder;
+ afile.append(files[i]);
+
+ if(files[i] == "." || files[i] == "..")
+ continue;
+
+ if (!JFileSystem::GetInstance()->FileExists(afile))
+ continue;
+
+ if(JFileSystem::GetInstance()->DirExists(afile))
+ loadFolder(string(afile+"/").c_str(), filename);
+
+ if(filename.size())
+ {
+ if(filename == files[i])
+ {
+ load(afile.c_str(), folder.c_str());
+ }
+ } else {
+ load(afile.c_str());
+ }
+ }
+}
int MTGAllCards::load(const char * config_file, const char * set_name, int)
{
@@ -445,10 +481,12 @@
primitives.clear();
}
-void MTGAllCards::loadInstance()
+MTGAllCards* MTGAllCards::getInstance()
{
if(!instance)
instance = new MTGAllCards();
+
+ return instance;
}
void MTGAllCards::unloadAll()
=======================================
--- /trunk/projects/mtg/src/TestSuiteAI.cpp Tue Oct 1 14:49:31 2013 UTC
+++ /trunk/projects/mtg/src/TestSuiteAI.cpp Wed Oct 23 22:08:30 2013 UTC
@@ -531,7 +531,6 @@
int TestSuite::loadNext()
{
-
endTime = JGEGetTime();
summoningSickness = 0;
seed = 0;
@@ -581,6 +580,43 @@
cout << "Starting test : " << files[currentfile - 1] << endl;
return currentfile;
}
+
+void TestSuite::ThreadProc(void* inParam)
+{
+ LOG("Entering TestSuite::ThreadProc");
+ TestSuite* instance = reinterpret_cast<TestSuite*>(inParam);
+ if (instance)
+ {
+ string filename;
+ float counter = 1.0f;
+ while(instance->mProcessing && (filename =
instance->getNextFile()) != "")
+ {
+ TestSuiteGame theGame(instance, filename);
+ if(theGame.isOK)
+ {
+ theGame.observer->loadTestSuitePlayer(0, &theGame);
+ theGame.observer->loadTestSuitePlayer(1, &theGame);
+
+ theGame.observer->startGame(theGame.gameType,
/*instance->mRules*/Rules::getRulesByFilename("testsuite.txt"));
+ theGame.initGame();
+
+ while(!theGame.observer->didWin())
+ theGame.observer->Update(counter++);
+ }
+ }
+ }
+ LOG("Leaving TestSuite::ThreadProc");
+}
+
+int TestSuite::run()
+{
+ mProcessing = false;
+ loadNext();
+ ThreadProc(this);
+
+ return nbFailed + nbAIFailed;
+}
+
void TestSuiteActions::cleanup()
{
@@ -753,41 +789,6 @@
Log(result);
if (!sb.unitTest()) nbFailed++;
}
-}
-
-void TestSuite::ThreadProc(void* inParam)
-{
- LOG("Entering TestSuite::ThreadProc");
- TestSuite* instance = reinterpret_cast<TestSuite*>(inParam);
- if (instance)
- {
- string filename;
- while(instance->mProcessing && (filename =
instance->getNextFile()) != "")
- {
- TestSuiteGame theGame(instance, filename);
- if(theGame.isOK)
- {
- theGame.observer->loadTestSuitePlayer(0, &theGame);
- theGame.observer->loadTestSuitePlayer(1, &theGame);
-
- theGame.observer->startGame(theGame.gameType,
instance->mRules);
- theGame.initGame();
-
- while(!theGame.observer->didWin())
- theGame.observer->Update(1);
-/*
- if(theGame.observer->gameType() != GAME_TYPE_MOMIR)
- {
- stringstream stream;
- stream << *(theGame.observer);
- theGame.observer->load(stream.str(), false, &theGame);
- theGame.assertGame();
- }
-*/
- }
- }
- }
- LOG("Leaving TestSuite::ThreadProc");
}
boost::mutex TestSuiteGame::mMutex;