]>
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 | // 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 | ||
22 | // ---------------------------------------------------------------------------- | |
23 | // test class | |
24 | // ---------------------------------------------------------------------------- | |
25 | ||
26 | class DynamicLibraryTestCase : public CppUnit::TestCase | |
27 | { | |
28 | public: | |
29 | DynamicLibraryTestCase() { } | |
30 | ||
31 | private: | |
32 | CPPUNIT_TEST_SUITE( DynamicLibraryTestCase ); | |
33 | CPPUNIT_TEST( Load ); | |
34 | CPPUNIT_TEST_SUITE_END(); | |
35 | ||
36 | void Load(); | |
37 | ||
38 | DECLARE_NO_COPY_CLASS(DynamicLibraryTestCase) | |
39 | }; | |
40 | ||
41 | // register in the unnamed registry so that these tests are run by default | |
42 | CPPUNIT_TEST_SUITE_REGISTRATION( DynamicLibraryTestCase ); | |
43 | ||
44 | // also include in its own registry so that these tests can be run alone | |
45 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DynamicLibraryTestCase, "DynamicLibraryTestCase" ); | |
46 | ||
47 | void DynamicLibraryTestCase::Load() | |
48 | { | |
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 | #ifdef __DARWIN__ | |
54 | static const wxChar *LIB_NAME = wxT("/usr/lib/libc.dylib"); | |
55 | #else | |
56 | // weird: using just libc.so does *not* work! | |
57 | static const wxChar *LIB_NAME = wxT("/lib/libc.so.6"); | |
58 | #endif | |
59 | static const wxChar *FUNC_NAME = wxT("strlen"); | |
60 | #else | |
61 | #error "don't know how to test wxDllLoader on this platform" | |
62 | #endif | |
63 | ||
64 | wxDynamicLibrary lib(LIB_NAME); | |
65 | CPPUNIT_ASSERT( lib.IsLoaded() ); | |
66 | ||
67 | typedef int (wxSTDCALL *wxStrlenType)(const char *); | |
68 | wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME); | |
69 | ||
70 | wxString errMsg = wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n", | |
71 | FUNC_NAME, LIB_NAME); | |
72 | CPPUNIT_ASSERT_MESSAGE( errMsg.ToStdString(), pfnStrlen ); | |
73 | ||
74 | // Call the function dynamically loaded | |
75 | CPPUNIT_ASSERT( pfnStrlen("foo") == 3 ); | |
76 | ||
77 | #ifdef __WXMSW__ | |
78 | static const wxChar *FUNC_NAME_AW = wxT("lstrlen"); | |
79 | ||
80 | typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *); | |
81 | wxStrlenTypeAorW | |
82 | pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW); | |
83 | ||
84 | wxString errMsg2 = wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n", | |
85 | FUNC_NAME_AW, LIB_NAME); | |
86 | CPPUNIT_ASSERT_MESSAGE( errMsg2.ToStdString(), pfnStrlenAorW ); | |
87 | ||
88 | CPPUNIT_ASSERT( pfnStrlenAorW(wxT("foobar")) == 6 ); | |
89 | #endif // __WXMSW__ | |
90 | } |