Files
wxWidgets/tests/misc/dynamiclib.cpp
Vadim Zeitlin 02434dcc1f Workaround a crash with MSYS2 gcc 9.1 again
The changes of 2144ca38d2 (Get rid of CppUnit boilerplate in
DynamicLibraryTestCase, 2022-04-17) accidentally undid the workaround
from 054cb35b39 (Workaround for a crash with gcc 9.1 from MSYS2 MinGW
32bit, 2019-08-03), so work around the same problem again by avoiding
using CHECK() with function pointers.
2022-04-18 14:36:52 +02:00

93 lines
2.7 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/misc/dynamiclib.cpp
// Purpose: Test wxDynamicLibrary
// Author: Francesco Montorsi (extracted from console sample)
// Created: 2010-06-13
// Copyright: (c) 2010 wxWidgets team
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#include "wx/dynlib.h"
#ifdef __UNIX__
#include "wx/filename.h"
#include "wx/log.h"
#endif
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
TEST_CASE("DynamicLibrary::Load", "[dynlib]")
{
#if defined(__WINDOWS__)
static const char* const LIB_NAME = "kernel32.dll";
static const char* const FUNC_NAME = "lstrlenA";
#elif defined(__UNIX__)
#ifdef __DARWIN__
static const char* const LIB_NAME = "/usr/lib/libc.dylib";
#else
// weird: using just libc.so does *not* work!
static const char* const LIB_NAME = "/lib/libc.so.6";
#endif
static const char* const FUNC_NAME = "strlen";
// Under macOS 12+ we can actually load the libc dylib even though the
// corresponding file doesn't exist on disk, so skip this check there.
#ifndef __DARWIN__
if ( !wxFileName::Exists(LIB_NAME) )
{
WARN("Shared library \"" << LIB_NAME << "\" doesn't exist, "
"skipping DynamicLibraryTestCase::Load() test.");
return;
}
#endif // !__DARWIN__
#else
#error "don't know how to test wxDllLoader on this platform"
#endif
wxDynamicLibrary lib(LIB_NAME);
REQUIRE( lib.IsLoaded() );
SECTION("strlen")
{
typedef int (wxSTDCALL *wxStrlenType)(const char *);
wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
if ( pfnStrlen )
{
// Call the function dynamically loaded
CHECK( pfnStrlen("foo") == 3 );
}
else
{
FAIL(FUNC_NAME << " wasn't found in " << LIB_NAME);
}
}
#ifdef __WINDOWS__
SECTION("A/W")
{
static const wxChar *FUNC_NAME_AW = wxT("lstrlen");
typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
wxStrlenTypeAorW
pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
if ( pfnStrlenAorW )
{
CHECK( pfnStrlenAorW(wxT("foobar")) == 6 );
}
else
{
FAIL(FUNC_NAME_AW << " wasn't found in " << LIB_NAME);
}
}
#endif // __WINDOWS__
}