glib/tests/test-utils.c
changeset 59 09fa7c3c5079
child 72 403e7f6ed6c5
equal deleted inserted replaced
52:bf6a71c50e42 59:09fa7c3c5079
       
     1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #undef G_DISABLE_ASSERT
       
    17 #undef G_LOG_DOMAIN
       
    18 
       
    19 #include <glib.h>
       
    20 #include <errno.h>
       
    21 #define LOG_FILE "c:\\logs\\test_utils_log.txt"
       
    22 #include "std_log_result.h"
       
    23 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    24 
       
    25 void create_xml(int result)
       
    26 {
       
    27     if(result)
       
    28         assert_failed = 1;
       
    29     
       
    30     testResultXml("test_utils_log");
       
    31     close_log_file();
       
    32 }
       
    33 
       
    34 static void
       
    35 gstring_overwrite_int (GString *gstring,
       
    36                        guint    pos,
       
    37                        guint32  vuint)
       
    38 {
       
    39   vuint = g_htonl (vuint);
       
    40   g_string_overwrite_len (gstring, pos, (const gchar*) &vuint, 4);
       
    41 }
       
    42 
       
    43 static void
       
    44 gstring_append_int (GString *gstring,
       
    45                     guint32  vuint)
       
    46 {
       
    47   vuint = g_htonl (vuint);
       
    48   g_string_append_len (gstring, (const gchar*) &vuint, 4);
       
    49 }
       
    50 
       
    51 static void
       
    52 gstring_append_double (GString *gstring,
       
    53                        double   vdouble)
       
    54 {
       
    55   union { double vdouble; guint64 vuint64; } u;
       
    56   u.vdouble = vdouble;
       
    57   u.vuint64 = GUINT64_TO_BE (u.vuint64);
       
    58   g_string_append_len (gstring, (const gchar*) &u.vuint64, 8);
       
    59 }
       
    60 
       
    61 static guint8*
       
    62 g_test_log_dump (GTestLogMsg *msg,
       
    63                  guint       *len)
       
    64 {
       
    65   GString *gstring = g_string_sized_new (1024);
       
    66   guint ui;
       
    67   gstring_append_int (gstring, 0);              /* message length */
       
    68   gstring_append_int (gstring, msg->log_type);
       
    69   gstring_append_int (gstring, msg->n_strings);
       
    70   gstring_append_int (gstring, msg->n_nums);
       
    71   gstring_append_int (gstring, 0);      /* reserved */
       
    72   for (ui = 0; ui < msg->n_strings; ui++)
       
    73     {
       
    74       guint l = strlen (msg->strings[ui]);
       
    75       gstring_append_int (gstring, l);
       
    76       g_string_append_len (gstring, msg->strings[ui], l);
       
    77     }
       
    78   for (ui = 0; ui < msg->n_nums; ui++)
       
    79     gstring_append_double (gstring, msg->nums[ui]);
       
    80   *len = gstring->len;
       
    81   gstring_overwrite_int (gstring, 0, *len);     /* message length */
       
    82   return (guint8*) g_string_free (gstring, FALSE);
       
    83 }
       
    84 
       
    85 void start_timer()
       
    86     {
       
    87     GTimer *timer;
       
    88     timer = g_timer_new ();
       
    89     g_timer_start(timer);
       
    90     g_timer_stop(timer);
       
    91     }
       
    92 
       
    93 void test_g_test_trap()
       
    94     {
       
    95     if(g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDOUT))
       
    96         {
       
    97         exit(0);
       
    98         }
       
    99     
       
   100     if(!g_test_trap_has_passed())
       
   101         {
       
   102         std_log(LOG_FILENAME_LINE, "g_test_trap_has_passed didnt work as expected");
       
   103         assert_failed = 1;
       
   104         }
       
   105     }
       
   106 
       
   107 void test_g_test_log_type_name()
       
   108     {
       
   109     const char *ret;
       
   110     ret = g_test_log_type_name(G_TEST_LOG_MESSAGE);
       
   111     
       
   112     if(strcmp(ret, "message"))
       
   113         {
       
   114         std_log(LOG_FILENAME_LINE, "g_test_log_type_name didnt work as expected");
       
   115         assert_failed = 1;
       
   116         }
       
   117     }
       
   118 
       
   119 void test_g_test_timer()
       
   120     {
       
   121     double ret_time1, ret_time2;
       
   122     
       
   123     g_test_timer_start();
       
   124     ret_time1 = g_test_timer_elapsed();
       
   125     ret_time2 = g_test_timer_last();
       
   126     
       
   127     if(!(ret_time1 == ret_time2))
       
   128         {
       
   129         std_log(LOG_FILENAME_LINE, "g_test_timer* didnt work as expected");
       
   130         assert_failed = 1;
       
   131         }
       
   132     }
       
   133 
       
   134 void test_g_log_buffer()
       
   135     {
       
   136     GTestLogBuffer* log_buffer;
       
   137     GTestLogMsg* log_msg;
       
   138 	GTestLogMsg msg_ip;
       
   139     gchar *astrings[1] = {NULL};
       
   140     guint8 *dbuffer;
       
   141     guint dbufferlen;
       
   142     int i;
       
   143 
       
   144     msg_ip.log_type = G_TEST_LOG_MESSAGE;
       
   145     msg_ip.n_strings = 1;
       
   146     msg_ip.strings = astrings;
       
   147     astrings[0] = (gchar*) "test-log-some-dummy-log";
       
   148     msg_ip.n_nums = 0;
       
   149     msg_ip.nums = 0;
       
   150     dbuffer = (guint8*)g_test_log_dump(&msg_ip, &dbufferlen);
       
   151     
       
   152     log_buffer = g_test_log_buffer_new();
       
   153     
       
   154     if(log_buffer)
       
   155         {
       
   156         g_test_log_buffer_push(log_buffer, dbufferlen, (const guint8*)dbuffer);
       
   157             
       
   158         log_msg = g_test_log_buffer_pop(log_buffer);
       
   159         
       
   160         if(log_msg)
       
   161             {
       
   162             g_test_log_msg_free(log_msg);
       
   163             }
       
   164         else
       
   165             {
       
   166             std_log(LOG_FILENAME_LINE, "g_test_log_buffer_pop returned NULL");
       
   167             assert_failed = 1;
       
   168             }
       
   169         
       
   170         g_test_log_buffer_free(log_buffer);
       
   171         }
       
   172     else
       
   173         {
       
   174         std_log(LOG_FILENAME_LINE, "g_test_log_buffer_new returned NULL");
       
   175         assert_failed = 1;
       
   176         }
       
   177 
       
   178 	g_free (dbuffer);
       
   179     }
       
   180 
       
   181 int main (int argc, char *argv[])
       
   182 {
       
   183     g_test_init(&argc, &argv);
       
   184     
       
   185     test_g_test_trap();
       
   186     test_g_test_log_type_name();
       
   187     test_g_test_timer();
       
   188     test_g_log_buffer();
       
   189     
       
   190     if(assert_failed)
       
   191           std_log(LOG_FILENAME_LINE,"Test Failed");
       
   192     else
       
   193           std_log(LOG_FILENAME_LINE,"Test Successful");
       
   194 	
       
   195     create_xml(0);
       
   196 
       
   197 	return 0;
       
   198 }