-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.c
94 lines (77 loc) · 1.79 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <ok/ok.h>
#include <stdio.h>
#include "require.h"
#include "module.h"
#include "def.h"
// `test` module definition
module(test) {
defaults(test, CLIB_MODULE);
// a private module pointer
void *private;
int state;
int (*function)();
};
// `test` module prototypes
static int
test_init(module(test) *exports);
static void
test_deinit(module(test) *exports);
// `test` module exports
exports(test) {
.init = test_init,
.deinit = test_deinit,
};
// `private` module definition
module(private) {
define(private, CLIB_MODULE);
int (*function)();
};
// private `private` module function symbol
static int
test_private_function() {
ok("test_private_function()");
return 0;
}
// `private` module exports
exports(private) {
defaults(private, CLIB_MODULE_DEFAULT),
.function = test_private_function
};
// private `test` module function symbol
static int
test_function() {
ok("test_function()");
require(test)->state = 1;
require(private)->function();
return 0;
}
// `test` module initializer
static int
test_init(module(test) *exports) {
ok("test_init()");
exports->function = test_function;
exports->private = require(private);
exports->state = -1;
if (0 != exports->private) {
ok("exports->private");
}
return 0;
}
// `test` module deinitializer
static void
test_deinit(module(test) *exports) {
ok("test_deinit()");
clib_module_free((module(private) *) exports->private);
}
int
main(void) {
ok_expect(8);
module(test) *test = require(test);
if (0 != test) { ok("module(test) *test = require(test)"); }
if (-1 == test->state) { ok("-1 == test->state // before test->function()"); }
test->function();
if (1 == test->state) { ok("1 == test->state // after test->function()"); }
clib_module_free(test);
ok_done();
return ok_count() - ok_expected();
}