os/ossrv/glib/tsrc/BC/tests/array-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
sl@0
     3
 *
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General Public
sl@0
    15
 * License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
/*
sl@0
    21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
sl@0
    22
 * file for a list of people on the GLib Team.  See the ChangeLog
sl@0
    23
 * files for a list of changes.  These files are distributed with
sl@0
    24
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
sl@0
    25
 */
sl@0
    26
sl@0
    27
#undef G_DISABLE_ASSERT
sl@0
    28
#undef G_LOG_DOMAIN
sl@0
    29
sl@0
    30
#include <stdio.h>
sl@0
    31
#include <string.h>
sl@0
    32
#include "glib.h"
sl@0
    33
sl@0
    34
#ifdef SYMBIAN
sl@0
    35
#include "mrt2_glib2_test.h"
sl@0
    36
#endif /*SYMBIAN*/
sl@0
    37
sl@0
    38
static void 
sl@0
    39
sum_up (gpointer data, 
sl@0
    40
	gpointer user_data)
sl@0
    41
{
sl@0
    42
  gint *sum = (gint *)user_data;
sl@0
    43
sl@0
    44
  *sum += GPOINTER_TO_INT (data);
sl@0
    45
}
sl@0
    46
sl@0
    47
int
sl@0
    48
main (int   argc,
sl@0
    49
      char *argv[])
sl@0
    50
{
sl@0
    51
  gint i;
sl@0
    52
  GArray *garray;
sl@0
    53
  GPtrArray *gparray;
sl@0
    54
  GByteArray *gbarray;
sl@0
    55
  gint sum = 0;
sl@0
    56
sl@0
    57
  #ifdef SYMBIAN
sl@0
    58
  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);
sl@0
    59
  g_set_print_handler(mrtPrintHandler);
sl@0
    60
  #endif /*SYMBIAN*/
sl@0
    61
	  
sl@0
    62
  /* array tests */
sl@0
    63
  garray = g_array_new (FALSE, FALSE, sizeof (gint));
sl@0
    64
  for (i = 0; i < 10000; i++)
sl@0
    65
    g_array_append_val (garray, i);
sl@0
    66
sl@0
    67
  for (i = 0; i < 10000; i++)
sl@0
    68
    g_assert (g_array_index (garray, gint, i) == i);
sl@0
    69
sl@0
    70
  g_array_free (garray, TRUE);
sl@0
    71
sl@0
    72
  garray = g_array_new (FALSE, FALSE, sizeof (gint));
sl@0
    73
  for (i = 0; i < 100; i++)
sl@0
    74
    g_array_prepend_val (garray, i);
sl@0
    75
sl@0
    76
  for (i = 0; i < 100; i++)
sl@0
    77
    g_assert (g_array_index (garray, gint, i) == (100 - i - 1));
sl@0
    78
sl@0
    79
  g_array_free (garray, TRUE);
sl@0
    80
sl@0
    81
  /* pointer arrays */
sl@0
    82
  gparray = g_ptr_array_new ();
sl@0
    83
  for (i = 0; i < 10000; i++)
sl@0
    84
    g_ptr_array_add (gparray, GINT_TO_POINTER (i));
sl@0
    85
sl@0
    86
  for (i = 0; i < 10000; i++)
sl@0
    87
    if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
sl@0
    88
      g_print ("array fails: %p ( %p )\n",
sl@0
    89
	       g_ptr_array_index (gparray, i),
sl@0
    90
	       GINT_TO_POINTER (i));
sl@0
    91
  
sl@0
    92
  g_ptr_array_foreach (gparray, sum_up, &sum);
sl@0
    93
  g_assert (sum == 49995000);
sl@0
    94
 
sl@0
    95
sl@0
    96
sl@0
    97
sl@0
    98
  g_ptr_array_free (gparray, TRUE);
sl@0
    99
sl@0
   100
  /* byte arrays */
sl@0
   101
  gbarray = g_byte_array_new ();
sl@0
   102
  for (i = 0; i < 10000; i++)
sl@0
   103
    g_byte_array_append (gbarray, (guint8*) "abcd", 4);
sl@0
   104
sl@0
   105
  for (i = 0; i < 10000; i++)
sl@0
   106
    {
sl@0
   107
      g_assert (gbarray->data[4*i] == 'a');
sl@0
   108
      g_assert (gbarray->data[4*i+1] == 'b');
sl@0
   109
      g_assert (gbarray->data[4*i+2] == 'c');
sl@0
   110
      g_assert (gbarray->data[4*i+3] == 'd');
sl@0
   111
    }
sl@0
   112
sl@0
   113
  g_byte_array_free (gbarray, TRUE);
sl@0
   114
  
sl@0
   115
  #if SYMBIAN
sl@0
   116
  testResultXml("array-test");
sl@0
   117
  #endif /* EMULATOR */
sl@0
   118
sl@0
   119
  return 0;
sl@0
   120
}
sl@0
   121