]>
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)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
20 #include "wx/dynlib.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 class DynamicLibraryTestCase
: public CppUnit::TestCase
29 DynamicLibraryTestCase() { }
32 CPPUNIT_TEST_SUITE( DynamicLibraryTestCase
);
34 CPPUNIT_TEST_SUITE_END();
38 DECLARE_NO_COPY_CLASS(DynamicLibraryTestCase
)
41 // register in the unnamed registry so that these tests are run by default
42 CPPUNIT_TEST_SUITE_REGISTRATION( DynamicLibraryTestCase
);
44 // also include in it's own registry so that these tests can be run alone
45 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DynamicLibraryTestCase
, "DynamicLibraryTestCase" );
47 void DynamicLibraryTestCase::Load()
49 #if defined(__WXMSW__)
50 static const wxChar
*LIB_NAME
= wxT("kernel32.dll");
51 static const wxChar
*FUNC_NAME
= wxT("lstrlenA");
52 #elif defined(__UNIX__)
53 // weird: using just libc.so does *not* work!
54 static const wxChar
*LIB_NAME
= wxT("/lib/libc.so.6");
55 static const wxChar
*FUNC_NAME
= wxT("strlen");
57 #error "don't know how to test wxDllLoader on this platform"
60 wxDynamicLibrary
lib(LIB_NAME
);
61 CPPUNIT_ASSERT( lib
.IsLoaded() );
63 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
64 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
66 wxString errMsg
= wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n",
68 CPPUNIT_ASSERT_MESSAGE( errMsg
.ToStdString(), pfnStrlen
);
70 // Call the function dynamically loaded
71 CPPUNIT_ASSERT( pfnStrlen("foo") == 3 );
74 static const wxChar
*FUNC_NAME_AW
= wxT("lstrlen");
76 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
78 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
80 wxString errMsg2
= wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n",
81 FUNC_NAME_AW
, LIB_NAME
);
82 CPPUNIT_ASSERT_MESSAGE( errMsg2
.ToStdString(), pfnStrlenAorW
);
84 CPPUNIT_ASSERT( pfnStrlenAorW(wxT("foobar")) == 6 );