From a6b50921ea2b934bb59a6bed186c483f8cdbe83c Mon Sep 17 00:00:00 2001 From: Francois Bleibel <fbleibel@gmail.com> Date: Wed, 9 Jun 2010 19:07:59 +0900 Subject: [PATCH] Added two unit tests. Shell (ok) Factory (currently needs sotGainAdaptive to compile, will change to another entity soon) --- CMakeLists.txt | 1 + unitTesting/CMakeLists.txt | 34 +++++++++ unitTesting/test_factory.cpp | 132 +++++++++++++++++++++++++++++++++++ unitTesting/test_shell.cpp | 86 +++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 unitTesting/CMakeLists.txt create mode 100644 unitTesting/test_factory.cpp create mode 100644 unitTesting/test_shell.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ed39818..196e61fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,6 +105,7 @@ INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/dynamic-graph.pc ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(include) +ADD_SUBDIRECTORY(unitTesting) # # Documentation produced by doxygen # diff --git a/unitTesting/CMakeLists.txt b/unitTesting/CMakeLists.txt new file mode 100644 index 00000000..86ad0df1 --- /dev/null +++ b/unitTesting/CMakeLists.txt @@ -0,0 +1,34 @@ +# +# Copyright +# + +SET(EXECUTABLE_NAME test_shell) + +ADD_DEFINITIONS(-DDEBUG=2) + +ADD_EXECUTABLE(${EXECUTABLE_NAME} + test_shell.cpp) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +link_directories(${LIBRARY_OUTPUT_PATH}) +TARGET_LINK_LIBRARIES(${EXECUTABLE_NAME} + dynamicGraph + dl) + + +#NOTE on test_factory: +# test can only be compiled IF sotGainAdaptive has been compiled first +SET(EXECUTABLE_NAME test_factory) + +ADD_DEFINITIONS(-DDEBUG=2) + +ADD_EXECUTABLE(${EXECUTABLE_NAME} + test_factory.cpp) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +link_directories(${LIBRARY_OUTPUT_PATH}) +TARGET_LINK_LIBRARIES(${EXECUTABLE_NAME} + dynamicGraph + dl) \ No newline at end of file diff --git a/unitTesting/test_factory.cpp b/unitTesting/test_factory.cpp new file mode 100644 index 00000000..70f53bfc --- /dev/null +++ b/unitTesting/test_factory.cpp @@ -0,0 +1,132 @@ +/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * Copyright Projet JRL-JAPAN, Tsukuba, 2007 + *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * File: test_factory.cc + * Project: SOT + * Author: Nicolas Mansard + * + * Version control + * =============== + * + * $Id$ + * + * Description + * ============ + * + * + * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ + +/* -------------------------------------------------------------------------- */ +/* --- INCLUDES ------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + + +#include <string> +#include <iostream> +#include <cstdlib> + +#include <dynamic-graph/factory.h> +#include <dynamic-graph/entity.h> +#include <dynamic-graph/debug.h> + + +using namespace std; +using namespace dynamicgraph; + +#ifdef WIN32 +#include <Windows.h> +#else +#include <dlfcn.h> +#endif + +#ifdef WIN32 + typedef HMODULE sotPluginKey; +#else + typedef void* sotPluginKey; +#endif + +int main() +{ + + dgDEBUG(0) << "# In {"<<endl; +// Entity test(""); +// sotExceptionFeature t2(sotExceptionFeature::BAD_INIT); +// sotExceptionSignal t4(sotExceptionSignal::COPY_NOT_INITIALIZED); +// sotFlags t3; +// TestFeature t5; + +#ifndef WIN32 + sotPluginKey dlib = dlopen("lib/plugin/sotFeatureVisualPoint.so", RTLD_NOW); +#else + sotPluginKey dlib = LoadLibrary ( "lib/plugin/sotFeatureVisualPoint.lib"); +#endif +if( NULL==dlib ) + { + cerr << " Error dl"<<endl; +#ifndef WIN32 + cerr << dlerror() <<endl; +#else + // Retrieve the system error message for the last-error code + LPTSTR pszMessage; + DWORD dwLastError = GetLastError(); + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dwLastError, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&pszMessage, + 0, NULL ); + + + cerr << pszMessage <<endl; + LocalFree(pszMessage); +#endif + + exit(0); + } + + +#ifndef WIN32 + dlib = dlopen("lib/plugin/sotGainAdaptative.so", RTLD_NOW); +#else + dlib = LoadLibrary ("lib/plugin/sotGainAdaptative.lib"); +#endif + if( NULL==dlib ) + { + cerr << " Error dl"<<endl; +#ifndef WIN32 + cerr << dlerror() <<endl; +#else + // Retrieve the system error message for the last-error code + LPTSTR pszMessage; + DWORD dwLastError = GetLastError(); + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dwLastError, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&pszMessage, + 0, NULL ); + + + cerr << pszMessage <<endl; + LocalFree(pszMessage); +#endif + exit(0); + } + + Entity* gain = factory.newEntity("GainAdaptative","Gain"); + + + gain->display(cout); cout << endl; + cout <<gain->getClassName(); cout << endl; + + + dgDEBUG(0) << "# Out }"<<endl; +} + diff --git a/unitTesting/test_shell.cpp b/unitTesting/test_shell.cpp new file mode 100644 index 00000000..11e8ee98 --- /dev/null +++ b/unitTesting/test_shell.cpp @@ -0,0 +1,86 @@ +/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * Copyright Projet JRL-JAPAN, Tsukuba, 2007 + *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * File: test_shell.cc + * Project: SOT + * Author: Nicolas Mansard + * + * Version control + * =============== + * + * $Id$ + * + * Description + * ============ + * + * + * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ + +/* -------------------------------------------------------------------------- */ +/* --- INCLUDES ------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +#include <dynamic-graph/entity.h> +#include <dynamic-graph/plugin-loader.h> +#include <dynamic-graph/interpreter.h> +#include <dynamic-graph/debug.h> + +#include <sstream> +using namespace std; +using namespace dynamicgraph; + + +extern std::ofstream debugfile; + +int main( int argc,char** argv ) +{ + dgDEBUGIN(15); + + dgDEBUG(5) << " Loading..." << endl; + PluginLoader pl; + Shell.referencePluginLoader( &pl ); + + int fileIdx; + try + { + for( fileIdx=1;fileIdx<argc;++fileIdx ) + { + std::istringstream script( argv[fileIdx] ); + cout << "Run "<< argv[fileIdx] << endl; + Shell.cmd( "run",script,cout ); + } + } + catch( ExceptionAbstract& e ) + { + cout << "!! In file <" << argv[fileIdx] << "> : " << e <<endl; + } + catch ( const char* str ) { + cout << "!! In file <" << argv[fileIdx] << "> : " + << "Unknown exception " << str << endl; + } + catch( ... ){ dgDEBUG(5) << "!! Unknown! " <<endl ; } + + while(1) + { + try + { + dgDEBUG(5) << "Run shell." << endl; + Shell.shell(cin,cout); + dgDEBUG(5) << "Shell over." << endl; + if( cin.eof() ) break; + } + catch( ExceptionAbstract& e ) + { + cout << "!! " << e <<endl; + } + catch( ... ){ dgDEBUG(5) << "!! Unknown! " <<endl ; } + } + + dgDEBUGOUT(15); + return 0; +} + + + + -- GitLab