Changeset 15 in libcf for trunk/test/test.c


Ignore:
Timestamp:
01/31/13 23:37:06 (11 years ago)
Author:
cheese
Message:

#1 add test code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/test.c

    r14 r15  
    88#include "cf_thread.h"
    99
     10#include <stdlib.h>
     11
    1012CF_Debug_Ctx        gDebugCtx;
    1113CF_Debug_CallStack  gDebugCallstack;
     
    1315CF_Log_Ctx          gLogCtx;
    1416
    15 void func1 (const char * message)
     17const char * file = "./log.txt";
     18
     19void log_test (const char * message)
    1620{
     21    int i = 0;
     22
    1723    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    1824
     25    gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
     26    if (gDebugCtx == NULL)
     27        CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n");
     28
     29    for (i = 0 ; i < 10000 ; i++)
     30        CF_Log_Write (gLogCtx, "TEST", "turn %d\n", i);
    1931
    2032    CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
    21     CF_Log_Write (gLogCtx, "test log",
     33
     34    CF_Log_Write (gLogCtx, message,
    2235                            "here is the end of function [file:%s line:%d func:%s]\n",
    2336                            gDebugCallstack.file,
    2437                            gDebugCallstack.line,
    2538                            gDebugCallstack.function);
     39
     40    CF_Log_DestroyCtx (gLogCtx);
     41}
     42
     43void file_test (const char * message)
     44{
     45    int     size = 0;
     46    int     fd = 0;
     47    char    * buffer = NULL;
     48
     49    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     50
     51    fd = CF_File_Open (file, RO);
     52    if (fd < 0)
     53        CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
     54
     55    size = CF_File_GetSize (fd);
     56    if (size < 0)
     57        CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n");
     58
     59    buffer = (char *) calloc (1, (size_t) (size + 1));
     60    if (buffer == NULL)
     61        CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n");
     62
     63    if (CF_File_Read (fd, buffer, (size_t)size) < 0)
     64        CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
     65    else
     66        CF_DEBUG_TRACE (gDebugCtx, "%s", buffer);
     67
     68    CF_File_Close (fd);
     69
     70    CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     71}
     72
     73void callee3 (void)
     74{
     75    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     76    while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
     77    {
     78        CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
     79                                    gDebugCallstack.file,
     80                                    gDebugCallstack.line,
     81                                    gDebugCallstack.function);
     82    }
     83    CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     84}
     85void callee2 (void)
     86{
     87    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     88    callee3 ();
     89    CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     90}
     91void callee1 (void)
     92{
     93    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     94    callee2 ();
     95    CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
    2696}
    2797
    2898int main (int argc, char ** argv)
    2999{
    30     gDebugCtx   = CF_Debug_CreateCtx ();
    31     gLogCtx     = CF_Log_CreateCtx ("./log.txt", CF_LOG_BUFFER_DEFAULT);
    32 
    33     if (gDebugCtx == NULL)
    34     {
    35         CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n");
    36     }
     100    gDebugCtx = CF_Debug_CreateCtx ();
    37101
    38102    if (gLogCtx == NULL)
    39     {
    40103        CF_DEBUG_TRACE (gDebugCtx, "create log ctx error\n");
    41     }
    42104
    43105    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    44106
    45     func1 ("merong kkkkkkkkkkk");
     107    CF_DEBUG_TRACE (gDebugCtx, " == LOG TEST ==\n");
     108    log_test ("LOG_TEST");
     109
     110    CF_DEBUG_TRACE (gDebugCtx, " == FILE READ TEST ==\n");
     111    file_test ("FILE_READ_TEST");
     112
     113    CF_DEBUG_TRACE (gDebugCtx, " == CALLSTACK TEST ==\n");
     114    callee1 ();
    46115
    47116    CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
     117
     118    CF_DEBUG_TRACE (gDebugCtx, " == END OF TEST ==\n");
    48119    CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
    49120                                gDebugCallstack.file,
     
    51122                                gDebugCallstack.function);
    52123
    53     CF_Log_DestroyCtx (gLogCtx);
    54124    CF_Debug_DestroyCtx (gDebugCtx);
    55125
Note: See TracChangeset for help on using the changeset viewer.