source: libcf/trunk/test/test.c@ 27

Last change on this file since 27 was 27, checked in by cheese, 11 years ago

#1 revert test code

File size: 7.6 KB
Line 
1/**
2 * test.c
3 */
4#include "cf_file.h"
5#include "cf_log.h"
6#include "cf_socket.h"
7#include "cf_thread.h"
8
9/*
10 * before including "cf_debug.h",
11 * if _DEBUG is defined, the debugging code will be act.
12 */
13#include "cf_debug.h"
14
15#include <stdlib.h>
16#include <string.h>
17#include <errno.h>
18
19#define THREAD_POOL 5
20#define PORT 1234
21
22CF_Debug_Ctx gDebugCtx;
23CF_Debug_CallStack gDebugCallstack;
24
25CF_Log_Ctx gLogCtx;
26
27const char * file = "./log.txt";
28
29void test_log (const char * message)
30{
31 int i, j;
32 char idname[16] = {0x00,};
33
34 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
35
36 /* initialize */
37 CF_Log_Initialize (10);
38
39 /* context {{{ */
40 gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
41 if (gLogCtx == NULL)
42 CF_DEBUG_PRINT (stderr, "create log ctx error\n");
43
44 for (i = 0 ; i < 10000 ; i++)
45 CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i);
46
47 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
48
49 CF_Log_Write (gLogCtx, message,
50 "here is the end of function [file:%s line:%d func:%s]\n",
51 gDebugCallstack.file,
52 gDebugCallstack.line,
53 gDebugCallstack.function);
54
55 CF_Log_DestroyCtx (gLogCtx);
56 /* }}} context */
57
58 /* id {{{ */
59 for (i = 0 ; i < 10 ; i++)
60 {
61 sprintf (idname, "logid%d.txt", i);
62 CF_LOG_OPEN (i, idname, CF_LOG_BUFFER_NO);
63 }
64
65 for (i = 0 ; i < 10 ; i++)
66 {
67 for (j = 0 ; j < 10000 ; j++)
68 CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j);
69
70 CF_LOG_CLOSE (i);
71 }
72 /* }}} id */
73
74 /* finalize */
75 CF_Log_Finalize ();
76}
77
78void test_file (const char * message)
79{
80 int fd = 0;
81 char buffer[128] = {0x00,};
82
83 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
84
85 fd = CF_File_Open (file, CF_FILE_RO);
86 if (fd < 0)
87 CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
88
89 if (CF_File_Read (fd, buffer, sizeof (buffer) - 1) < 0)
90 CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
91 else
92 CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
93 sizeof (buffer) - 1,
94 "-- %d bytes of %d bytes\n",
95 sizeof (buffer) - 1,
96 CF_File_GetSize (fd));
97
98 CF_File_Close (fd);
99
100 CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
101}
102
103void test_callstack3 (void)
104{
105 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
106 while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
107 {
108 CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
109 gDebugCallstack.file,
110 gDebugCallstack.line,
111 gDebugCallstack.function);
112 }
113 CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
114}
115void test_callstack2 (void)
116{
117 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
118 test_callstack3 ();
119 CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
120}
121void test_callstack1 (void)
122{
123 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
124 test_callstack2 ();
125 CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
126}
127
128CF_THREAD_RETURN CF_THREAD_CALL socket_echo_server (void * arg)
129{
130 int srvsock = *((int *)arg);
131 int clntsock = 0;
132 int recvd = 0;
133 char buf[1024] = {0x00,};
134
135 /*------------------------------------------------------------*/
136 clntsock = CF_Socket_Accept (srvsock, NULL);
137 if (clntsock < 0)
138 {
139 CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
140 return (CF_THREAD_RETURN)-1;
141 }
142 CF_DEBUG_PRINT (stderr, "SERVER : accepted\n");
143 CF_Log_Write (gLogCtx, "SERVER", "accepted\n");
144
145 if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
146 {
147 CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
148 return (CF_THREAD_RETURN)-2;
149 }
150 CF_DEBUG_PRINT (stderr, "SERVER : recv {%s}\n", buf);
151 CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf);
152
153 if (CF_Socket_Send (clntsock, buf, recvd) < 0)
154 {
155 CF_DEBUG_PRINT (stderr, "failed to send on server\n");
156 return (CF_THREAD_RETURN)-3;
157 }
158 CF_DEBUG_PRINT (stderr, "SERVER : resp {%s}\n", buf);
159 CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf);
160
161 return (CF_THREAD_RETURN)0;
162 /*------------------------------------------------------------*/
163}
164
165CF_THREAD_RETURN CF_THREAD_CALL socket_echo_client (void * arg)
166{
167 int sock = 0;
168 int recvd = 0;
169 char buf[1024] = {0x00,};
170
171 sprintf (buf, "...wow ? is it succeed ?");
172
173 /*------------------------------------------------------------*/
174 sock = CF_Socket_Connect ("localhost", PORT);
175 if (sock < 0)
176 {
177 CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
178 return (CF_THREAD_RETURN)-1;
179 }
180 CF_DEBUG_PRINT (stderr, "CLIENT : connected\n");
181 CF_Log_Write (gLogCtx, "CLIENT", "connected\n");
182
183 if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
184 {
185 CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
186 return (CF_THREAD_RETURN)-2;
187 }
188 CF_DEBUG_PRINT (stderr, "CLIENT : sent {%s}\n", buf);
189 CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf);
190
191 memset (buf, 0x00, sizeof (buf));
192
193 if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
194 {
195 CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
196 return (CF_THREAD_RETURN)-3;
197 }
198 CF_DEBUG_PRINT (stderr, "CLIENT : recv {%s}\n", buf);
199 CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf);
200
201 CF_Socket_Close (sock);
202
203 return (CF_THREAD_RETURN)0;
204 /*------------------------------------------------------------*/
205}
206
207void test_socket (void)
208{
209 CF_Thread tid[THREAD_POOL];
210
211 int sock = 0;
212 int iter = 0;
213 int reuse = 1;
214
215 /*------------------------------------------------------------*/
216 gLogCtx = CF_Log_CreateCtx ("socket.txt", CF_LOG_BUFFER_DEFAULT);
217 if (gLogCtx == NULL)
218 CF_DEBUG_PRINT (stderr, "failed to open log\n");
219
220 CF_Log_Write (gLogCtx, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
221
222 if (CF_Socket_Initialize () < 0)
223 {
224 CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
225 return ;
226 }
227 CF_Log_Write (gLogCtx, "SOCKET", "socket initialized\n");
228
229 sock = CF_Socket_Server (PORT, 5);
230 if (sock < 0)
231 {
232 CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
233 return ;
234 }
235 CF_Log_Write (gLogCtx, "SOCKET", "socket ready\n");
236
237 if (CF_Socket_SetOption (sock, SO_REUSEADDR, &reuse, sizeof (reuse)) < 0)
238 {
239 CF_DEBUG_PRINT (stderr, "failed to set option\n");
240 return ;
241 }
242 CF_Log_Write (gLogCtx, "SOCKET", "set socket option\n");
243
244 for (iter = 0 ; iter < THREAD_POOL ; iter++)
245 {
246 if (CF_Thread_Create (&tid[iter], socket_echo_server, &sock) < 0)
247 {
248 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
249 return ;
250 }
251 CF_Log_Write (gLogCtx, "SOCKET", "create server thread-%d\n", iter);
252 }
253
254 for (iter = 0 ; iter < THREAD_POOL ; iter++)
255 {
256 CF_Thread dummy;
257 if (CF_Thread_Create (&dummy, socket_echo_client, &sock) < 0)
258 {
259 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
260 return ;
261 }
262 CF_Log_Write (gLogCtx, "SOCKET", "create client thread-%d\n", iter);
263 }
264
265 for (iter = 0 ; iter < THREAD_POOL ; iter++)
266 {
267 CF_DEBUG_PRINT (stderr, "tid[%d] 0x%08x\n", iter, tid[iter]);
268 CF_Thread_Join (&tid[iter]);
269 CF_Thread_Release (&tid[iter]);
270 CF_Log_Write (gLogCtx, "SOCKET", "join server thread-%d\n", iter);
271 }
272
273 CF_Socket_Close (sock);
274
275 CF_Socket_Finalize ();
276
277 CF_Log_DestroyCtx (gLogCtx);
278 /*------------------------------------------------------------*/
279}
280
281int main (int argc, char ** argv)
282{
283 gDebugCtx = CF_Debug_CreateCtx ();
284
285 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
286
287 // 1
288 CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
289 test_log ("LOG_TEST");
290
291 // 2
292 CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
293 test_file ("FILE_READ_TEST");
294
295 // 3
296 CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
297 test_callstack1 ();
298
299 // 4
300 CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST with MULTI-THREADED LOGGING ==\n");
301 test_socket ();
302
303 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
304
305 CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
306 CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
307 gDebugCallstack.file,
308 gDebugCallstack.line,
309 gDebugCallstack.function);
310
311 CF_Debug_DestroyCtx (gDebugCtx);
312
313 return 0;
314}
315
Note: See TracBrowser for help on using the repository browser.