]>
Commit | Line | Data |
---|---|---|
69fc8587 FM |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/misc/dynamiclib.cpp | |
3 | // Purpose: Test wxDynamicLibrary | |
4 | // Author: Francesco Montorsi (extracted from console sample) | |
5 | // Created: 2010-06-13 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2010 wxWidgets team | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | # pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #include "wx/dynlib.h" | |
21 | ||
e282b73a VZ |
22 | #ifdef __UNIX__ |
23 | #include "wx/filename.h" | |
f94a9528 | 24 | #include "wx/log.h" |
e282b73a VZ |
25 | #endif |
26 | ||
69fc8587 FM |
27 | // ---------------------------------------------------------------------------- |
28 | // test class | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
31 | class DynamicLibraryTestCase : public CppUnit::TestCase | |
32 | { | |
33 | public: | |
34 | DynamicLibraryTestCase() { } | |
35 | ||
36 | private: | |
37 | CPPUNIT_TEST_SUITE( DynamicLibraryTestCase ); | |
38 | CPPUNIT_TEST( Load ); | |
39 | CPPUNIT_TEST_SUITE_END(); | |
40 | ||
41 | void Load(); | |
42 | ||
43 | DECLARE_NO_COPY_CLASS(DynamicLibraryTestCase) | |
44 | }; | |
45 | ||
46 | // register in the unnamed registry so that these tests are run by default | |
47 | CPPUNIT_TEST_SUITE_REGISTRATION( DynamicLibraryTestCase ); | |
48 | ||
e3778b4d | 49 | // also include in its own registry so that these tests can be run alone |
69fc8587 FM |
50 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DynamicLibraryTestCase, "DynamicLibraryTestCase" ); |
51 | ||
52 | void DynamicLibraryTestCase::Load() | |
53 | { | |
bb5a9514 | 54 | #if defined(__WINDOWS__) |
69fc8587 FM |
55 | static const wxChar *LIB_NAME = wxT("kernel32.dll"); |
56 | static const wxChar *FUNC_NAME = wxT("lstrlenA"); | |
57 | #elif defined(__UNIX__) | |
6eb1e122 SC |
58 | #ifdef __DARWIN__ |
59 | static const wxChar *LIB_NAME = wxT("/usr/lib/libc.dylib"); | |
60 | #else | |
69fc8587 FM |
61 | // weird: using just libc.so does *not* work! |
62 | static const wxChar *LIB_NAME = wxT("/lib/libc.so.6"); | |
6eb1e122 | 63 | #endif |
69fc8587 | 64 | static const wxChar *FUNC_NAME = wxT("strlen"); |
e282b73a VZ |
65 | |
66 | if ( !wxFileName::Exists(LIB_NAME) ) | |
67 | { | |
68 | wxLogWarning("Shared library \"%s\" doesn't exist, " | |
69 | "skipping DynamicLibraryTestCase::Load() test."); | |
70 | return; | |
71 | } | |
69fc8587 FM |
72 | #else |
73 | #error "don't know how to test wxDllLoader on this platform" | |
74 | #endif | |
75 | ||
76 | wxDynamicLibrary lib(LIB_NAME); | |
77 | CPPUNIT_ASSERT( lib.IsLoaded() ); | |
78 | ||
79 | typedef int (wxSTDCALL *wxStrlenType)(const char *); | |
80 | wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME); | |
81 | ||
82 | wxString errMsg = wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n", | |
83 | FUNC_NAME, LIB_NAME); | |
84 | CPPUNIT_ASSERT_MESSAGE( errMsg.ToStdString(), pfnStrlen ); | |
85 | ||
86 | // Call the function dynamically loaded | |
87 | CPPUNIT_ASSERT( pfnStrlen("foo") == 3 ); | |
88 | ||
bb5a9514 | 89 | #ifdef __WINDOWS__ |
69fc8587 FM |
90 | static const wxChar *FUNC_NAME_AW = wxT("lstrlen"); |
91 | ||
92 | typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *); | |
93 | wxStrlenTypeAorW | |
94 | pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW); | |
95 | ||
96 | wxString errMsg2 = wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n", | |
97 | FUNC_NAME_AW, LIB_NAME); | |
98 | CPPUNIT_ASSERT_MESSAGE( errMsg2.ToStdString(), pfnStrlenAorW ); | |
99 | ||
100 | CPPUNIT_ASSERT( pfnStrlenAorW(wxT("foobar")) == 6 ); | |
bb5a9514 | 101 | #endif // __WINDOWS__ |
69fc8587 | 102 | } |