]>
git.saurik.com Git - wxWidgets.git/blob - tests/misc/dynamiclib.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/dynamiclib.cpp
3 // Purpose: Test wxDynamicLibrary
4 // Author: Francesco Montorsi (extracted from console sample)
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 #include "wx/dynlib.h"
22 #include "wx/filename.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class DynamicLibraryTestCase
: public CppUnit::TestCase
33 DynamicLibraryTestCase() { }
36 CPPUNIT_TEST_SUITE( DynamicLibraryTestCase
);
38 CPPUNIT_TEST_SUITE_END();
42 DECLARE_NO_COPY_CLASS(DynamicLibraryTestCase
)
45 // register in the unnamed registry so that these tests are run by default
46 CPPUNIT_TEST_SUITE_REGISTRATION( DynamicLibraryTestCase
);
48 // also include in its own registry so that these tests can be run alone
49 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DynamicLibraryTestCase
, "DynamicLibraryTestCase" );
51 void DynamicLibraryTestCase::Load()
53 #if defined(__WINDOWS__)
54 static const wxChar
*LIB_NAME
= wxT("kernel32.dll");
55 static const wxChar
*FUNC_NAME
= wxT("lstrlenA");
56 #elif defined(__UNIX__)
58 static const wxChar
*LIB_NAME
= wxT("/usr/lib/libc.dylib");
60 // weird: using just libc.so does *not* work!
61 static const wxChar
*LIB_NAME
= wxT("/lib/libc.so.6");
63 static const wxChar
*FUNC_NAME
= wxT("strlen");
65 if ( !wxFileName::Exists(LIB_NAME
) )
67 wxLogWarning("Shared library \"%s\" doesn't exist, "
68 "skipping DynamicLibraryTestCase::Load() test.");
72 #error "don't know how to test wxDllLoader on this platform"
75 wxDynamicLibrary
lib(LIB_NAME
);
76 CPPUNIT_ASSERT( lib
.IsLoaded() );
78 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
79 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
81 wxString errMsg
= wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n",
83 CPPUNIT_ASSERT_MESSAGE( errMsg
.ToStdString(), pfnStrlen
);
85 // Call the function dynamically loaded
86 CPPUNIT_ASSERT( pfnStrlen("foo") == 3 );
89 static const wxChar
*FUNC_NAME_AW
= wxT("lstrlen");
91 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
93 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
95 wxString errMsg2
= wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n",
96 FUNC_NAME_AW
, LIB_NAME
);
97 CPPUNIT_ASSERT_MESSAGE( errMsg2
.ToStdString(), pfnStrlenAorW
);
99 CPPUNIT_ASSERT( pfnStrlenAorW(wxT("foobar")) == 6 );
100 #endif // __WINDOWS__