-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueTest.c
176 lines (161 loc) · 3.73 KB
/
QueueTest.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_QUEUE_THREADS /* Enable to test multithreaded */
#include "booltype.h"
#include "Queue.h"
#ifdef TEST_QUEUE_THREADS
#include <pthread.h>
#define PUSH_THREADS 100
#define POP_THREADS 2
bool bExit_PopThreads_m;
#endif /* TEST_QUEUE_THREADS */
#define QUEUE_ITEMS_ADD 0x10
void vQueue_Test_g(void);
void vQueue_TestThreads_g(void);
void *pvThread_Push_m(void *pvArg);
void *pvThread_Pop_m(void *pvArg);
char pcData[10][10]=
{
"Anna",
"Bertha",
"Caesar",
"Daedalus",
"Eugen",
"Ferdinand",
"Guido",
"Hugo",
"Ida",
"Jakob"
};
int main(int argc, char *argv[])
{
(void)argc; (void)argv;
puts("Starting Test...");
vQueue_Test_g();
#ifdef TEST_QUEUE_THREADS
puts("Testing Multithreaded queues...");
vQueue_TestThreads_g();
#endif
puts("Test passed!");
return(EXIT_SUCCESS);
}
void vQueue_Test_g(void)
{
void *pvData;
size_t sSize=0;
int iIndex;
TagQueue *ptagQueue;
if(!(ptagQueue=ptagQueue_New_g(0)))
{
puts("ptagQueue_New_g() failed");
exit(EXIT_FAILURE);
}
for(iIndex=0; iIndex<QUEUE_ITEMS_ADD; ++iIndex)
{
if(!bQueue_Push_g(ptagQueue,pcData[0],strlen(pcData[0])+1,eQueue_DataUseCopy))
{
puts("push failed");
exit(EXIT_FAILURE);
}
}
for(iIndex=0;iIndex<QUEUE_ITEMS_ADD;++iIndex)
{
if(iIndex%2)
{
if(!bQueue_Pop_g(ptagQueue,&pvData,&sSize,NULL))
{
puts("bQueue_Pop_g() failed");
exit(EXIT_FAILURE);
}
free(pvData);
}
else
{
if(!bQueue_DeleteNextElement_g(ptagQueue,NULL))
{
puts("bQueue_DeleteNextElement_g() failed");
exit(EXIT_FAILURE);
}
}
}
printf("Queue Elements left = %d\n",iQueue_GetElementsCount_g(ptagQueue));
vQueue_Delete_g(ptagQueue);
}
#ifdef TEST_QUEUE_THREADS
void vQueue_TestThreads_g(void)
{
int iIndex;
TagQueue *ptagQueue;
pthread_t threadsPush[PUSH_THREADS];
pthread_t threadsPop[POP_THREADS];
if(!(ptagQueue = ptagQueue_New_g(QUEUE_OPTION_SYNCHRONIZED)))
{
puts("ptagQueue_New_g() failed");
exit(EXIT_FAILURE);
}
bExit_PopThreads_m=false;
for(iIndex=0;iIndex<PUSH_THREADS;++iIndex)
{
if(pthread_create(&threadsPush[iIndex],NULL,pvThread_Push_m,ptagQueue))
{
printf("pthread_create() failed\n");
exit(EXIT_FAILURE);
}
}
for(iIndex=0;iIndex<POP_THREADS;++iIndex)
{
if(pthread_create(&threadsPop[iIndex],NULL,pvThread_Pop_m,ptagQueue))
{
printf("pthread_create() failed\n");
exit(EXIT_FAILURE);
}
}
for(iIndex=0;iIndex<PUSH_THREADS;++iIndex)
{
if(pthread_join(threadsPush[iIndex],NULL))
{
printf("pthread_join() failed\n");
exit(EXIT_FAILURE);
}
}
bExit_PopThreads_m=true;
for(iIndex=0;iIndex<POP_THREADS;++iIndex)
{
if(pthread_join(threadsPop[iIndex],NULL))
{
printf("pthread_join() failed\n");
exit(EXIT_FAILURE);
}
}
printf("Queue Elements left = %d\n",iQueue_GetElementsCount_g(ptagQueue));
vQueue_Delete_g(ptagQueue);
}
void *pvThread_Push_m(void *pvArg)
{
TagQueue *ptagQueue=(TagQueue*)pvArg;
int iIndex;
for(iIndex=0;iIndex<100;iIndex++)
{
printf("---> _%s_ %d\n",pcData[iIndex/10],strlen(pcData[iIndex/10])+1);
if(!bQueue_Push_g(ptagQueue,pcData[iIndex/10],strlen(pcData[iIndex/10])+1,eQueue_DataUseCopy))
puts("pvThread_Push_m(): bQueue_Push_g() failed!");
}
return(NULL);
}
void *pvThread_Pop_m(void *pvArg)
{
TagQueue *ptagQueue=(TagQueue*)pvArg;
void *pvData;
size_t sBufferSize=0;
bool bGotData;
while(((bGotData=bQueue_Pop_g(ptagQueue,&pvData,&sBufferSize,NULL))) || (!bExit_PopThreads_m))
{
if(!bGotData)
continue;
printf("<--- _%s_\n",(char*)pvData);
free(pvData);
}
return(NULL);
}
#endif