Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_module.c
Go to the documentation of this file.
1
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <unistd.h>
18
19#include "nilorea/n_common.h"
20#include "nilorea/n_log.h"
21#include "nilorea/n_module.h"
22
23#if defined(__windows__)
25#define EX_LIBRARY "kernel32.dll"
27#define EX_SYMBOL "GetTickCount"
28#elif defined(__APPLE__)
29#define EX_LIBRARY "libSystem.dylib"
30#define EX_SYMBOL "cos"
31#else
34#define EX_LIBRARY "libm.so.6"
36#define EX_SYMBOL "cos"
37#endif
38
39static int log_level = LOG_ERR;
40static int failures = 0;
41
43static void expect_true(const char* what, int ok) {
44 if (!ok) {
45 n_log(LOG_ERR, "FAIL: %s", what);
46 failures++;
47 } else {
48 n_log(LOG_INFO, "ok: %s", what);
49 }
50}
51
53static int parse_args(int argc, char** argv) {
54 int opt = 0;
55
56 while ((opt = getopt(argc, argv, "V:")) != -1) {
57 if (opt == 'V') {
58 log_level = atoi(optarg);
59 } else {
60 return -1;
61 }
62 }
63 return 0;
64}
65
66int main(int argc, char** argv) {
67 N_MODULE* module = NULL;
68 void* symbol = NULL;
69
71 if (parse_args(argc, argv) != 0) {
72 fprintf(stderr, "usage: %s [-V LOGLEVEL]\n", argv[0]);
73 return 2;
74 }
76
77 expect_true("the extension is not empty", n_module_extension()[0] != '\0');
78 expect_true("no error before any call", n_module_error()[0] == '\0');
79
80 module = n_module_open(EX_LIBRARY);
81 expect_true("the system library opens", module != NULL);
82 if (module) {
83 symbol = n_module_symbol(module, EX_SYMBOL);
84 expect_true("its function is found", symbol != NULL);
85#if !defined(__windows__)
86 if (symbol) {
87 double (*fn)(double) = NULL;
88
89 memcpy((void*)&fn, (const void*)&symbol, sizeof(fn));
90 expect_true("calling it gives cos(0) = 1", fn(0.0) == 1.0);
91 }
92#endif
93 expect_true("a missing symbol is NULL", n_module_symbol(module, "n_module_no_such_symbol") == NULL);
94 expect_true("and says why", n_module_error()[0] != '\0');
95 expect_true("a found symbol clears the error", n_module_symbol(module, EX_SYMBOL) != NULL && n_module_error()[0] == '\0');
96 expect_true("close succeeds", n_module_close(&module) == TRUE);
97 expect_true("close sets the handle to NULL", module == NULL);
98 }
99 expect_true("closing NULL is a no-op", n_module_close(&module) == TRUE);
100 expect_true("closing a NULL address is a no-op", n_module_close(NULL) == TRUE);
101
102 module = n_module_open("n_module_no_such_library.so");
103 expect_true("a missing library is NULL", module == NULL);
104 expect_true("and says why", n_module_error()[0] != '\0');
105 n_log(LOG_INFO, "the reason given: %s", n_module_error());
106
107 if (failures) {
108 n_log(LOG_ERR, "%d check(s) failed", failures);
109 return 1;
110 }
111 n_log(LOG_NOTICE, "all module checks passed");
112 return 0;
113}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
#define EX_SYMBOL
a function whose answer is known
Definition ex_module.c:36
static int parse_args(int argc, char **argv)
read -V LOGLEVEL
Definition ex_module.c:53
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
const char * n_module_extension(void)
The file extension shared libraries have on this platform.
Definition n_module.c:161
int n_module_close(N_MODULE **module)
Close a library and free its handle.
Definition n_module.c:136
void * n_module_symbol(N_MODULE *module, const char *name)
Find a symbol in an opened library.
Definition n_module.c:110
const char * n_module_error(void)
What the system said about this thread's last failed call.
Definition n_module.c:157
Common headers and low-level functions & define.
Generic log system.
an opened shared library
Definition n_module.c:40
Load a shared library at runtime and find its symbols.