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

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

#1 fix some symbol names, test code and exception handling logic

File size: 9.5 KB
RevLine 
[23]1/**
[35]2 * @file test.c
3 * @author myusgun <myusgun@gmail.com>
[14]4 */
5#include "cf_file.h"
6#include "cf_log.h"
7#include "cf_socket.h"
8#include "cf_thread.h"
[68]9#include "cf_codec.h"
[14]10
[25]11/*
12 * before including "cf_debug.h",
13 * if _DEBUG is defined, the debugging code will be act.
14 */
15#include "cf_debug.h"
16
[15]17#include <stdlib.h>
[21]18#include <string.h>
19#include <errno.h>
[15]20
[21]21#define THREAD_POOL 5
22#define PORT 1234
23
[49]24CF_Mutex globalMutex;
[14]25
[40]26#define LOG_SOCKET 0
27#define LOG_MT 1
[21]28
[15]29const char * file = "./log.txt";
30
[63]31int test_log_mt (void * arg);
32void test_log (const char * message);
33void test_file (const char * message);
34void test_callstack3 (void);
35void test_callstack2 (void);
36void test_callstack1 (void);
37int socket_echo_server (void * arg);
38int socket_echo_client (void * arg);
39void test_socket (void);
[68]40void test_codec (void);
[63]41
42int main (int argc, char ** argv)
43{
44 CF_DEBUG_INITIALIZE;
45
46 // 1
47 CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
48 test_log ("LOG_TEST");
49
50 // 2
51 CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
52 test_file ("FILE_READ_TEST");
53
54 // 3
55 CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
[64]56 CF_DEBUG_BEGIN_FUNCTION;
[63]57 test_callstack1 ();
[64]58 CF_DEBUG_END_FUNCTION;
[63]59
60 // 4
61 CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
62 test_socket ();
63
[68]64 // 5
65 CF_DEBUG_PRINT (stderr, " == CODEC TEST ==\n");
66 test_codec ();
67
[63]68 CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
69
70 CF_DEBUG_FINALIZE;
71
72 return 0;
73}
74
[57]75int test_log_mt (void * arg)
[38]76{
[49]77 static int cnt = 0;
[38]78 int i = 0;
[49]79 int th = 0;
80
81 if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section
82 // error
83 }
84 th = cnt++;
85 if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section
86 // error
87 }
[38]88
[49]89 CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
90
[38]91 for (i = 0 ; i < 100000 ; i++)
92 {
[40]93 CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
[38]94 }
95
[49]96 CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
97
[57]98 return 0;
[38]99}
100
[24]101void test_log (const char * message)
[14]102{
[19]103 int i, j;
104 char idname[16] = {0x00,};
[38]105 CF_Thread tid[10];
[15]106
[21]107 /* initialize */
[101]108 if (CF_Log_Initialize (10, 5) < 0)
[40]109 {
110 fprintf (stderr, "failed to init. log\n");
111 return ;
112 }
[19]113
114 for (i = 0 ; i < 10 ; i++)
115 {
116 sprintf (idname, "logid%d.txt", i);
[103]117 if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0)
[40]118 fprintf (stderr, "failed to open log\n");
[19]119 }
120
121 for (i = 0 ; i < 10 ; i++)
122 {
123 for (j = 0 ; j < 10000 ; j++)
[40]124 {
125 int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
126 if (result < 0)
127 fprintf (stderr, "failed to write log %d\n", result);
128 }
[19]129
[40]130 if (CF_Log_Close (i) < 0)
131 fprintf (stderr, "failed to close log\n");
[19]132 }
133
[38]134 /* mt {{{ */
[103]135 if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_DEFAULT_BUFFER) < 0)
[38]136 CF_DEBUG_PRINT (stderr, "create log ctx error\n");
137
[40]138 if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
[38]139 CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
140
[49]141 if (CF_Mutex_Create (&globalMutex) < 0) {
142 // error
143 }
144
[38]145 for (i = 0 ; i < 10 ; i++)
146 {
147 if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
148 {
149 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
150 return ;
151 }
152 }
153
154 for (i = 0 ; i < 10 ; i++)
155 {
156 if (CF_Thread_Join (&tid[i]) < 0)
157 CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
[62]158 if (CF_Thread_Release (&tid[i]) < 0)
159 CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
[38]160 }
[61]161
162 if (CF_Mutex_Destory (&globalMutex) < 0) {
163 // error
164 }
[38]165 /* }}} mt */
166
[21]167 /* finalize */
[19]168 CF_Log_Finalize ();
[14]169}
170
[24]171void test_file (const char * message)
[14]172{
[15]173 int fd = 0;
[21]174 char buffer[128] = {0x00,};
[14]175
[86]176 fd = CF_File_Open (file, CF_FILE_READ);
[15]177 if (fd < 0)
[51]178 CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
[15]179
[38]180 if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
[51]181 CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
[15]182 else
[51]183 CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
184 sizeof (buffer),
185 "-- %d bytes of %d bytes\n",
186 sizeof (buffer),
[94]187 CF_File_GetSize (file));
[15]188
189 CF_File_Close (fd);
190}
191
[24]192void test_callstack3 (void)
[15]193{
[64]194 // ...
195
[51]196 CF_DEBUG_BEGIN_FUNCTION;
[64]197 CF_DEBUG_PRINT_CALLSTACK (stderr);
[51]198 CF_DEBUG_END_FUNCTION;
[64]199
200 // ...
[15]201}
[24]202void test_callstack2 (void)
[15]203{
[64]204 // ...
205
[51]206 CF_DEBUG_BEGIN_FUNCTION;
[24]207 test_callstack3 ();
[51]208 CF_DEBUG_END_FUNCTION;
[64]209
210 // ...
[15]211}
[24]212void test_callstack1 (void)
[15]213{
[64]214 // ...
215
[51]216 CF_DEBUG_BEGIN_FUNCTION;
[24]217 test_callstack2 ();
[51]218 CF_DEBUG_END_FUNCTION;
[64]219
220 // ...
[15]221}
[14]222
[57]223int socket_echo_server (void * arg)
[21]224{
225 int srvsock = *((int *)arg);
226 int clntsock = 0;
227 int recvd = 0;
228 char buf[1024] = {0x00,};
[38]229 int i = 0;
[21]230
231 /*------------------------------------------------------------*/
[56]232 clntsock = CF_Socket_Accept (srvsock);
[21]233 if (clntsock < 0)
234 {
235 CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
[57]236 return -1;
[21]237 }
[40]238 CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
[21]239
[103]240 for (i = 0 ; i < 5 ; i++)
[21]241 {
[38]242 if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
243 {
244 CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
[57]245 return -2;
[38]246 }
[40]247 CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
[72]248 CF_DEBUG_PRINT (stderr, "[SERVER] recv {%s}\n", buf);
[21]249
[38]250 if (CF_Socket_Send (clntsock, buf, recvd) < 0)
251 {
[57]252 return -3;
[38]253 }
[40]254 CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
[72]255 CF_DEBUG_PRINT (stderr, "[SERVER] resp {%s}\n", buf);
[21]256 }
257
[38]258 CF_Socket_Close (clntsock);
259
[57]260 return 0;
[21]261 /*------------------------------------------------------------*/
262}
263
[57]264int socket_echo_client (void * arg)
[21]265{
266 int sock = 0;
267 int recvd = 0;
268 char buf[1024] = {0x00,};
[38]269 int i = 0;
[21]270
271 sprintf (buf, "...wow ? is it succeed ?");
272
273 /*------------------------------------------------------------*/
274 sock = CF_Socket_Connect ("localhost", PORT);
275 if (sock < 0)
276 {
277 CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
[57]278 return -1;
[21]279 }
[40]280 CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
[21]281
[103]282 for (i = 0 ; i < 5 ; i++)
[21]283 {
[38]284 if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
285 {
286 CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
[57]287 return -2;
[38]288 }
[40]289 CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
[72]290 CF_DEBUG_PRINT (stderr, "[CLIENT] sent {%s}\n", buf);
[21]291
[38]292 memset (buf, 0x00, sizeof (buf));
[21]293
[38]294 if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
295 {
296 CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
[57]297 return -3;
[38]298 }
[40]299 CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
[72]300 CF_DEBUG_PRINT (stderr, "[CLIENT] recv {%s}\n", buf);
[21]301 }
302
303 CF_Socket_Close (sock);
304
[57]305 return 0;
[21]306 /*------------------------------------------------------------*/
307}
308
[24]309void test_socket (void)
[21]310{
[62]311 CF_Thread stid[THREAD_POOL];
312 CF_Thread ctid[THREAD_POOL];
[21]313
314 int sock = 0;
315 int iter = 0;
316
317 /*------------------------------------------------------------*/
[103]318 if (CF_Log_Initialize (1, CF_LOG_NO_LEVEL) < 0)
[40]319 {
320 fprintf (stderr, "failed to init. log\n");
321 return ;
322 }
323
[103]324 if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_DEFAULT_BUFFER) < 0)
[21]325 CF_DEBUG_PRINT (stderr, "failed to open log\n");
326
[40]327 CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
[21]328
329 if (CF_Socket_Initialize () < 0)
330 {
331 CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
332 return ;
333 }
[40]334 CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
[21]335
336 sock = CF_Socket_Server (PORT, 5);
337 if (sock < 0)
338 {
339 CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
340 return ;
341 }
[40]342 CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
[21]343
344 for (iter = 0 ; iter < THREAD_POOL ; iter++)
345 {
[62]346 if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
[21]347 {
348 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
349 return ;
350 }
[40]351 CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
[21]352 }
353
354 for (iter = 0 ; iter < THREAD_POOL ; iter++)
355 {
[62]356 if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
[21]357 {
358 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
359 return ;
360 }
[40]361 CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
[21]362 }
363
364 for (iter = 0 ; iter < THREAD_POOL ; iter++)
365 {
[62]366 CF_Thread_Join (&ctid[iter]);
367 CF_Thread_Release (&ctid[iter]);
368
369 CF_Thread_Join (&stid[iter]);
370 CF_Thread_Release (&stid[iter]);
371
[40]372 CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
[21]373 }
374
375 CF_Socket_Close (sock);
376
377 CF_Socket_Finalize ();
378
[40]379 CF_Log_Close (LOG_SOCKET);
380
381 CF_Log_Finalize ();
[21]382 /*------------------------------------------------------------*/
383}
384
[68]385void test_codec (void)
386{
387 char data[] = "ONE OK ROCK - Nothing Helps";
388
[88]389 char encode[512] = {0x00,};
[68]390 unsigned char bin[512] = {0x00,};
[96]391 size_t length = 0;
[68]392
[88]393 /* hex */
394 CF_DEBUG_PRINT (stderr, "------------------- codec/hex ----------------\n");
[72]395 CF_DEBUG_PRINT (stderr, "data : %s\n", data);
396 CF_DEBUG_PRINT (stderr, "= Convert binary to hex =\n");
[88]397 CF_Codec_Hex_Encode ((unsigned char *)data, strlen (data), encode);
398 CF_DEBUG_PRINT (stderr, "hex : %s\n", encode);
[72]399 CF_DEBUG_PRINT_BIN (stderr, (unsigned char *) data, strlen (data), "data : %s\n", data);
[68]400
[72]401 CF_DEBUG_PRINT (stderr, "= Convert hex to binary =\n");
[96]402 if (CF_Codec_Hex_Decode (encode, bin, &length) < 0) {
[90]403 // error
404 }
[96]405 else
406 CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin);
[88]407
408 memset (bin , 0x00, sizeof (bin));
409 memset (encode, 0x00, sizeof (encode));
410 length = 0;
411
412 /* base64 */
413 CF_DEBUG_PRINT (stderr, "----------------- codec/base64 ---------------\n");
414 CF_DEBUG_PRINT (stderr, "data : %s\n", data);
415 CF_DEBUG_PRINT (stderr, "= Convert binary to base64 =\n");
416 CF_Codec_Base64_Encode ((unsigned char *)data, strlen (data), encode);
417 CF_DEBUG_PRINT (stderr, "base64 : %s\n", encode);
418
419 CF_DEBUG_PRINT (stderr, "= Convert base64 to binary =\n");
[96]420 if (CF_Codec_Base64_Decode (encode, bin, &length) < 0) {
[90]421 // error
422 }
[96]423 else
424 CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin);
[68]425}
Note: See TracBrowser for help on using the repository browser.