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