dynamiclib.cpp 3.2 KB

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