From 209c6412ae149cc7c503fd7da384f344a830423c Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 3 Feb 2013 20:03:36 +0100 Subject: Refactoring in tests infrastructure * all tests are now in /test/ subdirectory * unit tests concatenated to one executable (TODO: ui, common) * preparation for test environments (OpenGL and others) * removed old TestCBot --- test/cbot/CBot_console/CBotConsole.cpp | 175 +++++++++++++++++++++++++++++++++ test/cbot/CBot_console/CBotConsole.h | 44 +++++++++ test/cbot/CBot_console/CBotDoc.cpp | 122 +++++++++++++++++++++++ test/cbot/CBot_console/CBotDoc.h | 39 ++++++++ test/cbot/CBot_console/CClass.cpp | 97 ++++++++++++++++++ test/cbot/CBot_console/CClass.h | 18 ++++ test/cbot/CBot_console/CMakeLists.txt | 16 +++ test/cbot/CBot_console/main.cpp | 45 +++++++++ test/cbot/CBot_console/routines.cpp | 141 ++++++++++++++++++++++++++ 9 files changed, 697 insertions(+) create mode 100644 test/cbot/CBot_console/CBotConsole.cpp create mode 100644 test/cbot/CBot_console/CBotConsole.h create mode 100644 test/cbot/CBot_console/CBotDoc.cpp create mode 100644 test/cbot/CBot_console/CBotDoc.h create mode 100644 test/cbot/CBot_console/CClass.cpp create mode 100644 test/cbot/CBot_console/CClass.h create mode 100644 test/cbot/CBot_console/CMakeLists.txt create mode 100644 test/cbot/CBot_console/main.cpp create mode 100644 test/cbot/CBot_console/routines.cpp (limited to 'test/cbot/CBot_console') diff --git a/test/cbot/CBot_console/CBotConsole.cpp b/test/cbot/CBot_console/CBotConsole.cpp new file mode 100644 index 0000000..e9209d3 --- /dev/null +++ b/test/cbot/CBot_console/CBotConsole.cpp @@ -0,0 +1,175 @@ +/* + * CBotConsole.cpp + * + * Created on: 08-08-2012 + * Author: michal + */ + +#include "CBotConsole.h" +#include "CClass.h" +#include +#include + +CBotConsole::CBotConsole() { + // TODO Auto-generated constructor stub + m_pProg = NULL; + m_threadinfo.m_bRun = false; + m_code = 0; + +} + +CBotConsole::~CBotConsole() { + // TODO Auto-generated destructor stub +} + +uint ThreadProc(ThreadInfo *info) +{ + time_t t0,t1; + time(&t0); + + int Cpt = 0; + + info->m_pProg->Start("LaCommande"); + while ( !info->m_bStop && !info->m_pProg->Run() ) + { +#if 0 + const char* FunctionName; + const char* FN; + int start, end; + + info->m_pProg->GetRunPos(FunctionName, start, end); + + if ( FunctionName != NULL ) + { + info->m_pEditx->SetSel(start, end); + + char buffer[200]; + sprintf( buffer, "step %s, %d, %d",FunctionName, start, end); + AfxMessageBox( buffer ); + + int level = 0; + do + { + CBotVar* t = info->m_pProg->GetStackVars(FN, level--); + if ( FN != FunctionName ) break; + if ( t != NULL ) + { + CString s ; + while ( t != NULL ) + { + if (s.IsEmpty()) s+= "Stack -> "; + else s+= " , "; + s += t->GetValString(); + t = t->GetNext(); + } + AfxMessageBox(s); + } + } while (TRUE); + } +#endif + Cpt++; + if ( Cpt%50 == 0 ) std::cout << "."; + } + + if ( info->m_bStop ) + { + std::cout << "\nInterrupt\n"; + } + else if (info->m_pProg->GetError() == 0) + { + time(&t1); + double prog_time = difftime(t0,t1); + + char buffer[200]; + sprintf( buffer, "\nExecution terminated in %f seconds.\nInterrupted %d time(s).\n", + prog_time, Cpt); + + std::cout << buffer; + } + +// info->m_pWndMessage->SendMessage(WM_ENDPROG, 0, 0) ; + return 0 ; +} + +long CBotConsole::EndProg() +{ + m_threadinfo.m_bRun = false; + + if (m_pProg->GetError(m_code, m_start, m_end)) + { + CBotString TextError; + TextError = CBotProgram::GetErrorText(m_code); + std::cout << TextError; + return 1; + } + delete m_pProg; + m_pProg = NULL; + + return 0 ; +} + + +void CBotConsole::OnOK() +{ + m_code = 0; + + std::string Commande; + std::cin >> Commande; + + std::string s = "void LaCommande() { " + Commande + " ;}"; + m_pProg = new CBotProgram(); + CBotStringArray liste; + m_pProg->Compile(s.c_str(), liste); + + int err, start, end; + if ( m_pProg->GetError(err, start, end) ) + { + CBotString TextError; + TextError = CBotProgram::GetErrorText(err); + std::cout << TextError; + return; + } + + std::cout << "\n" + Commande + " ->\n"; + +// m_Edit2.SetWindowText(""); +// m_Edit1.SetFocus(); +// m_Edit2.EnableWindow(FALSE); +// m_cOK.EnableWindow(FALSE); + + // lance un processus paralèle pour l'exécution +// m_threadinfo.m_pWndMessage = this ; + +// m_threadinfo.m_pEdit1 = &m_Edit1; +// m_threadinfo.m_pEditx = m_pEditx; + m_threadinfo.m_pProg = m_pProg; + m_threadinfo.m_bStop = false; + m_threadinfo.m_bRun = true; + + ThreadProc(&m_threadinfo); + +// here program starts +// AfxBeginThread((AFX_THREADPROC)ThreadProc, &m_threadinfo) ; +} + +void CBotConsole::OnCancel() +{ + m_threadinfo.m_bStop = true ; +} + +bool CBotConsole::OnInitDialog() +{ +// CDialog::OnInitDialog(); + + std::cout << "Following functions are availible:\n"; + for ( int i = 0; i < m_pListe->GetSize(); i++ ) + { + CBotString x = (*m_pListe)[i] + CBotString("\n"); + std::cout << x; + } + std::cout << "Enter a command:\n"; + + + return true; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} diff --git a/test/cbot/CBot_console/CBotConsole.h b/test/cbot/CBot_console/CBotConsole.h new file mode 100644 index 0000000..a155399 --- /dev/null +++ b/test/cbot/CBot_console/CBotConsole.h @@ -0,0 +1,44 @@ +/* + * CBotConsole.h + * + * Created on: 08-08-2012 + * Author: michal + */ + +#ifndef CBOTCONSOLE_H_ +#define CBOTCONSOLE_H_ +#include "CClass.h" + +struct ThreadInfo +{ +// CEdit* m_pEdit1 ; +// CEdit* m_pEditx ; + CBotProgram* m_pProg; +// CWnd* m_pWndMessage; + bool m_bStop; + bool m_bRun; +}; + +class CBotConsole { + +public: + CBotConsole(); + virtual ~CBotConsole(); + +// CEdit m_Edit1; + + CBotProgram* m_pProg; + ThreadInfo m_threadinfo; + + CBotStringArray* m_pListe; + int m_code, m_start, m_end; +// CEdit* m_pEditx; + + // Implementation + void OnOK(); + void OnCancel(); + bool OnInitDialog(); + long EndProg() ; +}; + +#endif /* CBOTCONSOLE_H_ */ diff --git a/test/cbot/CBot_console/CBotDoc.cpp b/test/cbot/CBot_console/CBotDoc.cpp new file mode 100644 index 0000000..1c694c9 --- /dev/null +++ b/test/cbot/CBot_console/CBotDoc.cpp @@ -0,0 +1,122 @@ +/* + * CBotDoc.cpp + * + * Created on: 08-08-2012 + * Author: michal + */ +#include "CBotDoc.h" +#include "CBotConsole.h" +#include + +CBotDoc::CBotDoc(std::string s) { + // TODO Auto-generated constructor stub + // TODO set m_DocText +// m_pEdit = NULL; + m_pProg = NULL; +// m_bModified = FALSE; + m_DocText = s; + std::cout << s << std::endl; +// std::cout << "Enter to continue..." << std::endl; +// getchar(); +} + +CBotDoc::~CBotDoc() { + +// delete m_pEdit; + delete m_pProg; +} + + +//static bool test = false; + +void CBotDoc::OnRun() +{ + +// m_pEdit->GetWindowText(m_DocText); + CBotString s; + + std::string TextError; + int code, start, end; + + if ( m_pProg == NULL ) m_pProg = new CBotProgram(); + + if (!m_pProg->Compile(m_DocText.c_str(), m_Liste, NULL)) + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + + TextError = CBotProgram::GetErrorText( code ); + std::cout << TextError << std::endl; + return; + } + + if( m_Liste.GetSize() == 0 ) + { + std::cout << "No function marked \"extern\" !\n"; + return; + } + + for ( int i = 0; i < m_Liste.GetSize(); i++ ) + { + int start, stop; + m_pProg->GetPosition(m_Liste[i], start, stop, GetPosNom, GetPosParam); + CBotString s(m_DocText.substr( start, stop-start ).c_str()); + m_Liste[i] = s; + } +// TODO + CBotConsole dlg; + dlg.m_pListe = &m_Liste; +// dlg.m_pEditx = m_pEdit; + + dlg.OnInitDialog(); + dlg.OnOK(); + dlg.EndProg(); +// if ( dlg.m_code>0 ) +// { +// std::string TextError; +// +// TextError = m_pProg->GetErrorText( dlg.m_code ); +// +// std::cout <GetWindowText(m_DocText); + + std::string TextError; + int code, start, end; + + if ( m_pProg == NULL ) m_pProg = new CBotProgram(); + + char buffer[100]; + strcpy(buffer, "a pointer move to see"); + + if (!m_pProg->Compile(m_DocText.c_str(), m_Liste, static_cast(buffer))) + { + m_pProg->GetError(code, start, end); + delete m_pProg; + m_pProg = NULL; + +// m_pEdit->SetSel( start, end ); +// m_pEdit->SetFocus(); // higlights part of problem + + TextError = CBotProgram::GetErrorText( code ); + std::cout << TextError ; + + return false; + } + +// if ( m_pProg->GetPosition( "TheTest", start, end) ) +// { +// m_pEdit->SetSel( start, end ); +// m_pEdit->SetFocus(); // higlights part of problem +// } + +// m_bModified = FALSE; + return true; +} diff --git a/test/cbot/CBot_console/CBotDoc.h b/test/cbot/CBot_console/CBotDoc.h new file mode 100644 index 0000000..c0a3e1d --- /dev/null +++ b/test/cbot/CBot_console/CBotDoc.h @@ -0,0 +1,39 @@ +/* + * CBotDoc.h + * + * Created on: 08-08-2012 + * Author: michal + */ + +#pragma once +#ifndef CBOTDOC_H_ +#define CBOTDOC_H_ + +#include "CClass.h" +#include + +class CBotDoc { + +public: + CBotDoc(std::string); + virtual ~CBotDoc(); + +// CEdit* m_pEdit; // to memorize the text, and display + CBotProgram* m_pProg; // the compiled program + std::string m_DocText; + CBotStringArray m_Liste; + +// Operations + + bool Compile(); + +// virtual bool OnNewDocument(); + + void OnRun(); + void OnChangeEdit1(); + void OnTest(); + +}; + + +#endif /* CBOTDOC_H_ */ diff --git a/test/cbot/CBot_console/CClass.cpp b/test/cbot/CBot_console/CClass.cpp new file mode 100644 index 0000000..9b7c842 --- /dev/null +++ b/test/cbot/CBot_console/CClass.cpp @@ -0,0 +1,97 @@ +#include "CClass.h" + +#include "routines.cpp" + +void rMajObject( CBotVar* pThis, void* pUser ) +{ + if (!pThis->IsElemOfClass("object")) + return ; + CBotVar* pPos = pThis->GetItem("position"); + CBotVar* pX = pPos->GetItem("x"); + CBotVar* pY = pPos->GetItem("y"); + CBotVar* pZ = pPos->GetItem("z"); +// CBotVar* pPt = pThis->GetItem("transport"); + + CBotString p = pX->GetValString(); + +// pX->SetValFloat( pUser == (void*)1 ? (float)12.5 : (float)44.4 ); + pZ->SetValFloat( (float)0 ); + pY->SetValFloat( (float)-3.33 ); + pX->SetValFloat( pX->GetValFloat() + 10 ) ; + +// pX = pThis->GetItem( "xx" ); +// pX->SetValFloat( (float)22 ); + + // crée une instance sur une classe object +// CBotVar* pAutre = CBotVar::Create("autre", CBotTypClass, "object"); +// pAutre->SetUserPtr( (void*)3 ); +// pPt->SetPointer( pAutre ); +// pPt->SetPointer( NULL ); +// delete pAutre; +}; + +CClass::CClass() +{ + m_pClassPoint= NULL; +} + +bool CClass::InitInstance() +{ +////////////////////////////////////////////// +// défini les mots clefs supplémentaires +// ------------------------------------------- + + CBotProgram::Init(); + +////////////////////////////////////////////// +// défini les fonctions "show()" et "print()" +// ------------------------------------------- + + //CBotProgram::AddFunction("show", rShow, cShow); + CBotProgram::AddFunction("print", rPrint, cPrint); + CBotProgram::AddFunction("println", rPrintLn, cPrint); + + +/////////////////////////////////// +// définie la classe globale CPoint +// -------------------------------- + + m_pClassPoint = new CBotClass("CPoint", NULL); + // ajoute le composant ".x" + m_pClassPoint->AddItem("x", CBotTypFloat); + // ajoute le composant ".y" + m_pClassPoint->AddItem("y", CBotTypFloat); + + // ajoute le constructeur pour cette classe + m_pClassPoint->AddFunction("CPoint", rCPoint, cCPoint); + + m_pClassPointIntr = new CBotClass("point", NULL, true); + // ajoute le composant ".x" + m_pClassPointIntr->AddItem("x", CBotTypFloat); + // ajoute le composant ".y" + m_pClassPointIntr->AddItem("y", CBotTypFloat); + // ajoute le composant ".z" + m_pClassPointIntr->AddItem("z", CBotTypFloat); + + // ajoute le constructeur pour cette classe + m_pClassPointIntr->AddFunction("point", rCPoint, cCPoint); + + // défini la classe "object" + CBotClass* pClassObject = new CBotClass( "object", NULL ) ; + pClassObject->AddItem( "xx", CBotTypFloat ); + pClassObject->AddItem( "position", CBotTypResult( CBotTypIntrinsic, "point" ) ); + pClassObject->AddItem( "transport", CBotTypResult( CBotTypPointer, "object" ) ); + pClassObject->AddUpdateFunc( rMajObject ); + + InitClassFILE(); + + return true; +} + +void CClass::ExitInstance() +{ + delete m_pFuncFile; + + CBotProgram::Free(); + +} diff --git a/test/cbot/CBot_console/CClass.h b/test/cbot/CBot_console/CClass.h new file mode 100644 index 0000000..da2c46c --- /dev/null +++ b/test/cbot/CBot_console/CClass.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +extern std::string s; + +class CClass +{ +public: + CClass(); + + CBotClass* m_pClassPoint; + CBotClass* m_pClassPointIntr; + + bool InitInstance(); + void ExitInstance(); +}; diff --git a/test/cbot/CBot_console/CMakeLists.txt b/test/cbot/CBot_console/CMakeLists.txt new file mode 100644 index 0000000..5016a77 --- /dev/null +++ b/test/cbot/CBot_console/CMakeLists.txt @@ -0,0 +1,16 @@ +set(SOURCES +CClass.cpp +main.cpp +CBotDoc.cpp +CBotConsole.cpp +) + +set(LIBS +CBot +) + +include_directories(${colobot_SOURCE_DIR}/src) + +add_executable(CBot_console ${SOURCES}) + +target_link_libraries(CBot_console ${LIBS}) diff --git a/test/cbot/CBot_console/main.cpp b/test/cbot/CBot_console/main.cpp new file mode 100644 index 0000000..a2d3668 --- /dev/null +++ b/test/cbot/CBot_console/main.cpp @@ -0,0 +1,45 @@ +#include "CClass.h" +#include "CBotDoc.h" +#include + + +#include + +std::string str; + +// routine to update the instance of the class Bot common + +int main(int argc, char* argv[]) +{ + CClass newclass; + CBotDoc *botdoc; + if (argc != 2) + { + std::cout << "Usage: "<" << std::endl; + return 0; + } + + std::ifstream in(argv[1]); + std::cout << argv[1] << std::endl; + if (!in.good()) + { + std::cout << "Oh no, error!" << std::endl; + return 1; + } + + std::string contents((std::istreambuf_iterator(in)), + std::istreambuf_iterator()); + str = contents; + + if(!newclass.InitInstance()) + { + std::cerr << "Initialization not complete!" << std::endl; + return 1; + } + + botdoc = new CBotDoc(str); +// std::cout << "Hello CBot!" << std::endl << s; + botdoc->OnRun(); + delete botdoc; + newclass.ExitInstance(); +} diff --git a/test/cbot/CBot_console/routines.cpp b/test/cbot/CBot_console/routines.cpp new file mode 100644 index 0000000..8b8a1d4 --- /dev/null +++ b/test/cbot/CBot_console/routines.cpp @@ -0,0 +1,141 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +//////////////////////////////////////////////////////////////////// +// routine show() +// utilisable depuis le programme écrit en CBot + + +#include +#include +#include "CBot/ClassFILE.cpp" +//std::string s; +/* +// execution +bool rShow( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) +{ + string::string s; + + while ( pVar != NULL ) + { + string::string ss; + + ss.LoadString( TX_TYPENAMES + pVar->GetType() ); + s += ss + " "; + + ss = pVar->GetName(); + if (ss.IsEmpty()) ss = ""; + s += ss + " = "; + + s += pVar->GetValString(); + s += "\n"; + pVar = pVar->GetNext(); + } + + AfxMessageBox(s, MB_OK|MB_ICONINFORMATION); + + return TRUE; // pas d'interruption +} + +CBotTypResult cShow( CBotVar* &pVar, void* pUser) +{ + if ( pVar == NULL ) return CBotTypResult(5028); + return CBotTypResult(0); // tous paramètres acceptés, void en retour +} + +*/ + +//////////////////////////////////////////////////////////////////// +// routine print() +// utilisable depuis le programme écrit en CBot + +// exécution + +bool rPrintLn( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) +{ + std::string s; + while ( pVar != NULL ) + { + if ( !s.empty() ) s += " "; + s += pVar->GetValString(); + pVar = pVar->GetNext(); + } + s += "\n"; + + std::cout << s; + return true; // pas d'interruption +} + +bool rPrint( CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser ) +{ + std::string s; + while ( pVar != NULL ) + { + if ( !s.empty() ) s += " "; + s += pVar->GetValString(); + pVar = pVar->GetNext(); + } + s += " "; + std::cout << s; + return true; // pas d'interruption +} + +CBotTypResult cPrint( CBotVar* &pVar, void* pUser) +{ + return CBotTypResult(0); // tous paramètres acceptés, un entier en retour +} + + +////////////////////////////////////////////////////////////////// +// class CPoint pour essayer + +// exécution +bool rCPoint( CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception ) +{ + if ( pVar == NULL )return true; // constructor with no parameters is ok + + CBotVar* pX = pThis->GetItem("x"); + pX->SetValFloat( pVar->GetValFloat() ); + pVar = pVar->GetNext(); + + CBotVar* pY = pThis->GetItem("y"); + pY->SetValFloat( pVar->GetValFloat() ); + pVar = pVar->GetNext(); + + return true; // pas d'interruption +} + +CBotTypResult cCPoint( CBotVar* pThis, CBotVar* &pVar) +{ + // ok if no parameters! + if ( pVar == NULL ) return CBotTypResult(0); + + // numeric type of parameter please + if ( pVar->GetType() > CBotTypDouble ) return CBotTypResult(5011); + pVar = pVar->GetNext(); + + // there must be a second parameter + if ( pVar == NULL ) return 5028; + // also numeric + if ( pVar->GetType() > CBotTypDouble )return CBotTypResult(5011); + pVar = pVar->GetNext(); + + // and not more than 2 parameters please + if ( pVar != NULL ) return CBotTypResult(5026); + + return CBotTypResult(0); // This function returns void +} + -- cgit v1.2.3-1-g7c22