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
173
174
|
#include "portmidi.h"
#include "pmutil.h"
#include "stdlib.h"
#include "stdio.h"
/* make_msg -- make a psuedo-random message of length n whose content
* is purely a function of i
*/
void make_msg(long msg[], int n, int i)
{
int j;
for (j = 0; j < n; j++) {
msg[j] = i % (j + 5);
}
}
/* print_msg -- print the content of msg of length n to stdout */
/**/
void print_msg(long msg[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf(" %li", msg[i]);
}
}
/* cmp_msg -- compare two messages of length n */
/**/
int cmp_msg(long msg[], long msg2[], int n, int i)
{
int j;
for (j = 0; j < n; j++) {
if (msg[j] != msg2[j]) {
printf("Received message %d doesn't match sent message\n", i);
printf("in: "); print_msg(msg, n); printf("\n");
printf("out:"); print_msg(msg2, n); printf("\n");
return FALSE;
}
}
return TRUE;
}
int main()
{
int msg_len;
for (msg_len = 4; msg_len < 100; msg_len += 5) {
PmQueue *queue = Pm_QueueCreate(100, msg_len * sizeof(long));
int i;
long msg[100];
long msg2[100];
printf("msg_len = %d\n", msg_len);
if (!queue) {
printf("Could not allocate queue\n");
return 1;
}
/* insert/remove 1000 messages */
printf("test 1\n");
for (i = 0; i < 1357; i++) {
make_msg(msg, msg_len, i);
if (Pm_Enqueue(queue, msg)) {
printf("Pm_Enqueue error\n");
return 1;
}
if (Pm_Dequeue(queue, msg2) != 1) {
printf("Pm_Dequeue error\n");
return 1;
}
if (!cmp_msg(msg, msg2, msg_len, i)) {
return 1;
}
}
/* make full */
printf("test 2\n");
for (i = 0; i < 100; i++) {
make_msg(msg, msg_len, i);
if (Pm_Enqueue(queue, msg)) {
printf("Pm_Enqueue error\n");
return 1;
}
}
/* alternately remove and insert */
for (i = 100; i < 1234; i++) {
make_msg(msg, msg_len, i - 100); /* what we expect */
if (Pm_Dequeue(queue, msg2) != 1) {
printf("Pm_Dequeue error\n");
return 1;
}
if (!cmp_msg(msg, msg2, msg_len, i)) {
return 1;
}
make_msg(msg, msg_len, i);
if (Pm_Enqueue(queue, msg)) {
printf("Pm_Enqueue error\n");
return 1;
}
}
/* remove all */
while (!Pm_QueueEmpty(queue)) {
make_msg(msg, msg_len, i - 100); /* what we expect */
if (Pm_Dequeue(queue, msg2) != 1) {
printf("Pm_Dequeue error\n");
return 1;
}
if (!cmp_msg(msg, msg2, msg_len, i)) {
return 1;
}
i++;
}
if (i != 1334) {
printf("Message count error\n");
return 1;
}
/* now test overflow */
printf("test 3\n");
for (i = 0; i < 110; i++) {
make_msg(msg, msg_len, i);
if (Pm_Enqueue(queue, msg) == pmBufferOverflow) {
break; /* this is supposed to execute after 100 messages */
}
}
for (i = 0; i < 100; i++) {
make_msg(msg, msg_len, i);
if (Pm_Dequeue(queue, msg2) != 1) {
printf("Pm_Dequeue error\n");
return 1;
}
if (!cmp_msg(msg, msg2, msg_len, i)) {
return 1;
}
}
/* we should detect overflow after removing 100 messages */
if (Pm_Dequeue(queue, msg2) != pmBufferOverflow) {
printf("Pm_Dequeue overflow expected\n");
return 1;
}
/* after overflow is detected (and cleared), sender can
* send again
*/
/* see if function is restored, also test peek */
printf("test 4\n");
for (i = 0; i < 1357; i++) {
long *peek;
make_msg(msg, msg_len, i);
if (Pm_Enqueue(queue, msg)) {
printf("Pm_Enqueue error\n");
return 1;
}
peek = (long *) Pm_QueuePeek(queue);
if (!cmp_msg(msg, peek, msg_len, i)) {
return 1;
}
if (Pm_Dequeue(queue, msg2) != 1) {
printf("Pm_Dequeue error\n");
return 1;
}
if (!cmp_msg(msg, msg2, msg_len, i)) {
return 1;
}
}
Pm_QueueDestroy(queue);
}
return 0;
}
|