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