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
162
163
164
165
166
167
168
169
170
171
172
|
#include "../src/itc.h"
#include "test.h"
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
static void test_itc_alloc_free(void)
{
itc *ctx;
TEST((ctx = itc_alloc(0)) == NULL);
TEST((ctx = itc_alloc(1)) != NULL);
itc_free(&ctx, NULL);
TEST(ctx == NULL);
}
static void test_itc_inject_retrive(void)
{
unsigned int i;
unsigned int *e[3] = {NULL};
int *r;
itc *ctx;
for (i = 0; i < sizeof(e)/sizeof(e[0]); i++) {
e[i] = malloc(sizeof(*e[i]));
*e[i] = i;
}
ctx = itc_alloc(3);
TEST(itc_inject(ctx, 0, e[0]) == 0);
TEST(itc_inject(ctx, 0, e[1]) == 0);
TEST(itc_inject(ctx, 0, e[2]) == 0);
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 0);
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 1);
TEST(itc_inject(ctx, 0, e[0]) == 0);
TEST(itc_inject(ctx, 0, e[1]) == 0);
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 2);
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 0);
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 1);
TEST(itc_retrive(ctx, 0) == NULL);
TEST(itc_inject(ctx, 0, e[0]) == 0);
itc_free(&ctx, free);
e[0] = malloc(sizeof(*e[0]));
*e[0] = 0;
ctx = itc_alloc(2);
TEST(itc_inject(ctx, 0, e[0]) == 0);
TEST(itc_inject(ctx, 0, e[1]) == 0);
TEST(itc_inject(ctx, 0, e[2]));
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 0);
TEST((r = itc_retrive(ctx, 0)) != NULL);
TEST(*r == 1);
TEST(itc_retrive(ctx, 0) == NULL);
itc_free(&ctx, free);
for (i = 0; i < sizeof(e)/sizeof(e[0]); i++) {
free(e[i]);
}
}
static void test_itc_queued_slots(void)
{
itc *ctx;
ctx = itc_alloc(3);
TEST(itc_get_queued(NULL) < 0);
TEST(itc_get_queued(ctx) == 0);
TEST(itc_get_slots(NULL) < 0);
TEST(itc_get_slots(ctx) == 3);
itc_inject(ctx, 0, NULL);
TEST(itc_get_queued(ctx) == 0);
TEST(itc_get_slots(ctx) == 3);
itc_inject(ctx, 0, ctx);
TEST(itc_get_queued(ctx) == 1);
TEST(itc_get_slots(ctx) == 3);
itc_inject(ctx, 0, ctx);
TEST(itc_get_queued(ctx) == 2);
TEST(itc_get_slots(ctx) == 3);
itc_retrive(ctx, 0);
TEST(itc_get_queued(ctx) == 1);
TEST(itc_get_slots(ctx) == 3);
itc_retrive(ctx, 0);
TEST(itc_get_queued(ctx) == 0);
TEST(itc_get_slots(ctx) == 3);
itc_free(&ctx, NULL);
}
static void *test_itc_wait_empty_th(void *_ctx)
{
itc *ctx = _ctx;
int *e;
sleep(2);
TEST(ctx != NULL);
TEST((e = itc_retrive(ctx, 0)) != NULL);
TEST(*e == 0);
free(e);
TEST((e = itc_retrive(ctx, 0)) != NULL);
TEST(*e == 1);
free(e);
TEST((e = itc_retrive(ctx, 0)) != NULL);
TEST(*e == 2);
free(e);
TEST(itc_retrive(ctx, 0) == NULL);
return NULL;
}
static void test_itc_wait_empty(void)
{
itc *ctx;
pthread_t th;
int *e[3];
int i;
for (i = 0; i < 3; i++) {
e[i] = malloc(sizeof(int));
*e[i] = i;
}
ctx = itc_alloc(3);
itc_inject(ctx, 0, e[0]);
itc_inject(ctx, 0, e[1]);
itc_inject(ctx, 0, e[2]);
pthread_create(&th, NULL, test_itc_wait_empty_th, ctx);
pthread_detach(th);
itc_wait_empty(ctx);
sleep(2);
TEST(itc_get_queued(ctx) == 0);
TEST(itc_retrive(ctx, 0) == NULL);
itc_free(&ctx, NULL);
}
static void test_itc_discard_all(void)
{
itc *ctx;
unsigned int *e[4];
unsigned int i;
for (i = 0; i < sizeof(e)/sizeof(e[0]); i++) {
e[i] = malloc(sizeof(*e[i]));
*e[i] = i;
}
ctx = itc_alloc(5);
itc_discard_all(ctx, free);
TEST(itc_get_queued(ctx) == 0);
itc_inject(ctx, 0, e[0]);
itc_inject(ctx, 0, e[1]);
itc_inject(ctx, 0, e[2]);
itc_inject(ctx, 0, e[3]);
itc_discard_all(ctx, free);
TEST(itc_get_queued(ctx) == 0);
itc_free(&ctx, NULL);
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
test_itc_alloc_free();
test_itc_inject_retrive();
test_itc_queued_slots();
test_itc_wait_empty();
test_itc_discard_all();
return 0;
}
|