-
Notifications
You must be signed in to change notification settings - Fork 0
/
zv_theaptest.c
161 lines (127 loc) · 4.1 KB
/
zv_theaptest.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#include "zv.h"
/* tests for timer heap */
static void theap_test_initdesotry(void **state) {
(void)state; /* unused */
zv_loop *lp = (zv_loop *)test_calloc(1, sizeof(zv_loop));
theap_init(lp);
assert_int_equal(lp -> timer_cnt, 0);
assert_int_equal(lp -> timer_max, TIMER_BLK);
assert_non_null(lp -> timers);
assert_non_null(lp -> timers[0]);
assert_true((lp -> timers[0] -> at) == -1.0);
theap_destroy(lp);
assert_null(lp -> timers);
assert_int_equal(lp -> timer_cnt, 0);
assert_int_equal(lp -> timer_max, 0);
test_free(lp);
}
static int theap_test_setup(void **state) {
zv_loop *lp = (zv_loop *)test_calloc(1, sizeof(zv_loop));
theap_init(lp);
*state = (void *)lp;
return 0;
}
static int theap_test_teardown(void **state) {
theap_destroy((zv_loop *)(*state));
test_free(*state);
return 0;
}
static void theap_test_insert(void **state) {
zv_loop *lp = (zv_loop *)(*state);
zv_timer *timers = (zv_timer *)test_calloc(TIMER_BLK*2, sizeof(zv_timer));
for (int i=0; i<TIMER_BLK*2; i++) {
timers[i].at = i+1;
theap_insert(&timers[i], lp);
}
assert_int_equal(lp -> timer_cnt, TIMER_BLK*2);
assert_int_equal(lp -> timer_max, TIMER_BLK*2);
assert_non_null(lp -> timers);
assert_true((lp -> timers[0] -> at) == -1.0);
zv_timer *t = (zv_timer *)test_calloc(1, sizeof(zv_timer));
t -> at = 0.5;
theap_insert(t, lp);
assert_int_equal(lp -> timer_cnt, TIMER_BLK * 2 + 1);
assert_int_equal(lp -> timer_max, TIMER_BLK * 3);
assert_true((lp -> timers[1] -> at) == 0.5);
/* free memory */
test_free(timers);
test_free(t);
}
static void theap_test_deletemin(void **state) {
zv_loop *lp = (zv_loop *)(*state);
zv_timer *timers = (zv_timer *)test_calloc(TIMER_BLK*2, sizeof(zv_timer));
for (int i=0; i<TIMER_BLK*2; i++) {
timers[i].at = i+1;
theap_insert(&timers[i], lp);
}
zv_timer *t;
int cnt = TIMER_BLK*2;
for (int i=1; i<=TIMER_BLK*2; i++) {
t = theap_deletemin(lp);
assert_true(t -> at == (zv_tstamp)i);
assert_int_equal(lp -> timer_cnt, (--cnt));
}
test_free(timers);
}
static void theap_test_findmin(void **state) {
zv_loop *lp = (zv_loop *)(*state);
zv_timer *timers = (zv_timer *)test_calloc(TIMER_BLK*2, sizeof(zv_timer));
for (int i=0; i<TIMER_BLK*2; i++) {
timers[i].at = i+1;
theap_insert(&timers[0], lp);
}
zv_timer *t = theap_findmin(lp);
assert_non_null(t);
assert_true(t -> at == 1.0);
test_free(timers);
}
static void theap_test_isempty(void **state) {
zv_loop *lp = (zv_loop *)(*state);
assert_true(theap_isempty(lp));
zv_timer *t = (zv_timer *)test_calloc(1, sizeof(zv_timer));
t -> at = 1.0;
theap_insert(t, lp);
assert_false(theap_isempty(lp));
test_free(t);
}
static void theap_test_makeempty(void **state) {
zv_loop *lp = (zv_loop *)(*state);
zv_timer *timers = (zv_timer *)test_calloc(TIMER_BLK*2, sizeof(zv_timer));
for (int i=0; i<TIMER_BLK*2; i++) {
timers[i].at = i+1;
theap_insert(&timers[i], lp);
}
theap_makeempty(lp);
assert_int_equal(lp -> timer_cnt, 0);
assert_int_equal(lp -> timer_max, TIMER_BLK);
assert_non_null(lp -> timers[0]);
assert_true((lp -> timers[0] -> at) == -1.0);
test_free(timers);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(theap_test_initdesotry),
cmocka_unit_test_setup_teardown(theap_test_findmin,
theap_test_setup,
theap_test_teardown),
cmocka_unit_test_setup_teardown(theap_test_isempty,
theap_test_setup,
theap_test_teardown),
cmocka_unit_test_setup_teardown(theap_test_insert,
theap_test_setup,
theap_test_teardown),
cmocka_unit_test_setup_teardown(theap_test_deletemin,
theap_test_setup,
theap_test_teardown),
cmocka_unit_test_setup_teardown(theap_test_makeempty,
theap_test_setup,
theap_test_teardown),
};
return cmocka_run_group_tests_name("Timer Heap Test", tests, NULL, NULL);
}