ENH: Improve test coverage

This commit is contained in:
Mathieu Malaterre 2006-03-09 16:40:34 -05:00
parent 7ca1c6d2d8
commit 9b55f2579a
1 changed files with 26 additions and 8 deletions

View File

@ -23,25 +23,33 @@
# include "kwsys_ios_iostream.h.in" # include "kwsys_ios_iostream.h.in"
#endif #endif
int TestDynamicLoader(const char* libname, const char* symbol) #include "testSystemTools.h"
/* libname = Library name
* System = symbol to lookup in libname
* r1: should OpenLibrary succeed ?
* r2: should GetSymbolAddress succeed ?
* r3: should CloseLibrary succeed ?
*/
int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, int r3)
{ {
kwsys::LibHandle l = kwsys::DynamicLoader::OpenLibrary(libname); kwsys::LibHandle l = kwsys::DynamicLoader::OpenLibrary(libname);
if( l ) // If result is incompatible with expectation just fails (xor):
if( (r1 && !l) || (!r1 && l) )
{ {
kwsys_ios::cerr kwsys_ios::cerr
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl; << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
return 1; return 1;
} }
kwsys::DynamicLoaderFunction f = kwsys::DynamicLoaderFunction f = kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
kwsys::DynamicLoader::GetSymbolAddress(l, symbol); if( (r2 && !f) || (!r2 && f) )
if( f )
{ {
kwsys_ios::cerr kwsys_ios::cerr
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl; << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
return 1; return 1;
} }
int success = kwsys::DynamicLoader::CloseLibrary(l); int s = kwsys::DynamicLoader::CloseLibrary(l);
if( success ) if( (r3 && !s) || (!r3 && s) )
{ {
kwsys_ios::cerr kwsys_ios::cerr
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl; << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
@ -52,7 +60,17 @@ int TestDynamicLoader(const char* libname, const char* symbol)
int main(int , char *[]) int main(int , char *[])
{ {
int res = TestDynamicLoader("foobar.lib", "foobar"); int res;
// Make sure that inexistant lib is giving correct result
res = TestDynamicLoader("foobar.lib", "foobar",0,0,0);
// Make sure that random binary file cannnot be assimilated as dylib
res += TestDynamicLoader(TEST_SYSTEMTOOLS_BIN_FILE, "wp",0,0,0);
#ifdef __linux__
// This one is actually fun to test, since dlopen is by default loaded...wonder why :)
res += TestDynamicLoader("foobar.lib", "dlopen",0,1,0);
res += TestDynamicLoader("libdl.so", "dlopen",1,1,1);
res += TestDynamicLoader("libdl.so", "TestDynamicLoader",1,0,1);
#endif
return res; return res;
} }