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