| 1 |
#include <stdlib.h> |
| 2 |
#include <stddef.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <assert.h> |
| 5 |
|
| 6 |
#ifndef _WIN32 |
| 7 |
#include <dlfcn.h> |
| 8 |
#else |
| 9 |
#include <windows.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
typedef struct { |
| 13 |
const char *library_name; |
| 14 |
const char *func_name; |
| 15 |
char expected_result; |
| 16 |
} test_t; |
| 17 |
|
| 18 |
static void |
| 19 |
load (test_t *test) |
| 20 |
{ |
| 21 |
#ifndef _WIN32 |
| 22 |
void *h = dlopen (test->library_name, RTLD_NOW | RTLD_LOCAL); |
| 23 |
if (h == NULL) { |
| 24 |
fprintf (stderr, "dlopen (%s) failed: %s\n", |
| 25 |
test->library_name, dlerror ()); |
| 26 |
exit (EXIT_FAILURE); |
| 27 |
} |
| 28 |
|
| 29 |
typedef char (*func_t)(void); |
| 30 |
func_t func = (func_t) dlsym (h, test->func_name); |
| 31 |
assert (func != NULL); |
| 32 |
|
| 33 |
assert (func () == test->expected_result); |
| 34 |
dlclose (h); |
| 35 |
#else /* _WIN32 */ |
| 36 |
HMODULE h = LoadLibraryA (test->library_name); |
| 37 |
if (h == NULL) { |
| 38 |
fprintf (stderr, "LoadLibrary (%s) failed with error code %u\n", |
| 39 |
test->library_name, (unsigned int) GetLastError ()); |
| 40 |
exit (EXIT_FAILURE); |
| 41 |
} |
| 42 |
|
| 43 |
typedef char (*func_t)(void); |
| 44 |
func_t func = (func_t) GetProcAddress (h, test->func_name); |
| 45 |
assert (func != NULL); |
| 46 |
|
| 47 |
assert (func () == test->expected_result); |
| 48 |
FreeLibrary (h); |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
#define STRINGIFY_HELPER(x) #x |
| 53 |
#define STRINGIFY(x) STRINGIFY_HELPER(x) |
| 54 |
|
| 55 |
int |
| 56 |
main (void) |
| 57 |
{ |
| 58 |
test_t tests[] = { |
| 59 |
{STRINGIFY (LIBA), "func_a", 'a'}, |
| 60 |
{STRINGIFY (LIBB), "func_b", 'b'}, |
| 61 |
}; |
| 62 |
|
| 63 |
for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++) |
| 64 |
load (&tests[i]); |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |