os/ossrv/glib/tsrc/BC/tests/hash-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
 * Copyright (C) 1999 The Free Software Foundation
sl@0
     4
 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     5
 * This library is free software; you can redistribute it and/or
sl@0
     6
 * modify it under the terms of the GNU Lesser General Public
sl@0
     7
 * License as published by the Free Software Foundation; either
sl@0
     8
 * version 2 of the License, or (at your option) any later version.
sl@0
     9
 *
sl@0
    10
 * This library is distributed in the hope that it will be useful,
sl@0
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    13
 * Lesser General Public License for more details.
sl@0
    14
 *
sl@0
    15
 * You should have received a copy of the GNU Lesser General Public
sl@0
    16
 * License along with this library; if not, write to the
sl@0
    17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    18
 * Boston, MA 02111-1307, USA.
sl@0
    19
 */
sl@0
    20
sl@0
    21
/*
sl@0
    22
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
sl@0
    23
 * file for a list of people on the GLib Team.  See the ChangeLog
sl@0
    24
 * files for a list of changes.  These files are distributed with
sl@0
    25
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
sl@0
    26
 */
sl@0
    27
sl@0
    28
#undef G_DISABLE_ASSERT
sl@0
    29
#undef G_LOG_DOMAIN
sl@0
    30
sl@0
    31
#ifdef HAVE_CONFIG_H
sl@0
    32
#  include <config.h>
sl@0
    33
#endif
sl@0
    34
sl@0
    35
#if STDC_HEADERS
sl@0
    36
#include <stdio.h>
sl@0
    37
#include <string.h>
sl@0
    38
#include <stdlib.h>
sl@0
    39
#endif
sl@0
    40
sl@0
    41
#include <glib.h>
sl@0
    42
sl@0
    43
sl@0
    44
#ifdef SYMBIAN
sl@0
    45
#include "mrt2_glib2_test.h"
sl@0
    46
#endif /*SYMBIAN*/
sl@0
    47
sl@0
    48
sl@0
    49
sl@0
    50
int array[10000];
sl@0
    51
sl@0
    52
sl@0
    53
sl@0
    54
static gboolean
sl@0
    55
my_hash_callback_remove (gpointer key,
sl@0
    56
			 gpointer value,
sl@0
    57
			 gpointer user_data)
sl@0
    58
{
sl@0
    59
  int *d = value;
sl@0
    60
sl@0
    61
  if ((*d) % 2)
sl@0
    62
    return TRUE;
sl@0
    63
sl@0
    64
  return FALSE;
sl@0
    65
}
sl@0
    66
sl@0
    67
static void
sl@0
    68
my_hash_callback_remove_test (gpointer key,
sl@0
    69
			      gpointer value,
sl@0
    70
			      gpointer user_data)
sl@0
    71
{
sl@0
    72
  int *d = value;
sl@0
    73
sl@0
    74
  if ((*d) % 2)
sl@0
    75
    g_assert_not_reached ();
sl@0
    76
}
sl@0
    77
sl@0
    78
static void
sl@0
    79
my_hash_callback (gpointer key,
sl@0
    80
		  gpointer value,
sl@0
    81
		  gpointer user_data)
sl@0
    82
{
sl@0
    83
  int *d = value;
sl@0
    84
  *d = 1;
sl@0
    85
}
sl@0
    86
sl@0
    87
static guint
sl@0
    88
my_hash (gconstpointer key)
sl@0
    89
{
sl@0
    90
  return (guint) *((const gint*) key);
sl@0
    91
}
sl@0
    92
sl@0
    93
static gboolean
sl@0
    94
my_hash_equal (gconstpointer a,
sl@0
    95
	       gconstpointer b)
sl@0
    96
{
sl@0
    97
  return *((const gint*) a) == *((const gint*) b);
sl@0
    98
}
sl@0
    99
sl@0
   100
sl@0
   101
sl@0
   102
/*
sl@0
   103
 * This is a simplified version of the pathalias hashing function.
sl@0
   104
 * Thanks to Steve Belovin and Peter Honeyman
sl@0
   105
 *
sl@0
   106
 * hash a string into a long int.  31 bit crc (from andrew appel).
sl@0
   107
 * the crc table is computed at run time by crcinit() -- we could
sl@0
   108
 * precompute, but it takes 1 clock tick on a 750.
sl@0
   109
 *
sl@0
   110
 * This fast table calculation works only if POLY is a prime polynomial
sl@0
   111
 * in the field of integers modulo 2.  Since the coefficients of a
sl@0
   112
 * 32-bit polynomial won't fit in a 32-bit word, the high-order bit is
sl@0
   113
 * implicit.  IT MUST ALSO BE THE CASE that the coefficients of orders
sl@0
   114
 * 31 down to 25 are zero.  Happily, we have candidates, from
sl@0
   115
 * E. J.  Watson, "Primitive Polynomials (Mod 2)", Math. Comp. 16 (1962):
sl@0
   116
 *	x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
sl@0
   117
 *	x^31 + x^3 + x^0
sl@0
   118
 *
sl@0
   119
 * We reverse the bits to get:
sl@0
   120
 *	111101010000000000000000000000001 but drop the last 1
sl@0
   121
 *         f   5   0   0   0   0   0   0
sl@0
   122
 *	010010000000000000000000000000001 ditto, for 31-bit crc
sl@0
   123
 *	   4   8   0   0   0   0   0   0
sl@0
   124
 */
sl@0
   125
sl@0
   126
#define POLY 0x48000000L	/* 31-bit polynomial (avoids sign problems) */
sl@0
   127
sl@0
   128
static guint CrcTable[128];
sl@0
   129
sl@0
   130
/*
sl@0
   131
 - crcinit - initialize tables for hash function
sl@0
   132
 */
sl@0
   133
static void crcinit(void)
sl@0
   134
{
sl@0
   135
	int i, j;
sl@0
   136
	guint sum;
sl@0
   137
sl@0
   138
	for (i = 0; i < 128; ++i) {
sl@0
   139
		sum = 0L;
sl@0
   140
		for (j = 7 - 1; j >= 0; --j)
sl@0
   141
			if (i & (1 << j))
sl@0
   142
				sum ^= POLY >> j;
sl@0
   143
		CrcTable[i] = sum;
sl@0
   144
	}
sl@0
   145
}
sl@0
   146
sl@0
   147
/*
sl@0
   148
 - hash - Honeyman's nice hashing function
sl@0
   149
 */
sl@0
   150
static guint honeyman_hash(gconstpointer key)
sl@0
   151
{
sl@0
   152
	const gchar *name = (const gchar *) key;
sl@0
   153
	gint size;
sl@0
   154
	guint sum = 0;
sl@0
   155
sl@0
   156
	g_assert (name != NULL);
sl@0
   157
	g_assert (*name != 0);
sl@0
   158
sl@0
   159
	size = strlen(name);
sl@0
   160
sl@0
   161
	while (size--) {
sl@0
   162
		sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
sl@0
   163
	}
sl@0
   164
sl@0
   165
	return(sum);
sl@0
   166
}
sl@0
   167
sl@0
   168
sl@0
   169
static gboolean second_hash_cmp (gconstpointer a, gconstpointer b)
sl@0
   170
{
sl@0
   171
  return (strcmp (a, b) == 0);
sl@0
   172
}
sl@0
   173
sl@0
   174
sl@0
   175
sl@0
   176
static guint one_hash(gconstpointer key)
sl@0
   177
{
sl@0
   178
  return 1;
sl@0
   179
}
sl@0
   180
sl@0
   181
sl@0
   182
static void not_even_foreach (gpointer       key,
sl@0
   183
				 gpointer       value,
sl@0
   184
				 gpointer	user_data)
sl@0
   185
{
sl@0
   186
  const char *_key = (const char *) key;
sl@0
   187
  const char *_value = (const char *) value;
sl@0
   188
  int i;
sl@0
   189
  char val [20];
sl@0
   190
sl@0
   191
  g_assert (_key != NULL);
sl@0
   192
  g_assert (*_key != 0);
sl@0
   193
  g_assert (_value != NULL);
sl@0
   194
  g_assert (*_value != 0);
sl@0
   195
sl@0
   196
  i = atoi (_key);
sl@0
   197
sl@0
   198
  sprintf (val, "%d value", i);
sl@0
   199
  g_assert (strcmp (_value, val) == 0);
sl@0
   200
sl@0
   201
  g_assert ((i % 2) != 0);
sl@0
   202
  g_assert (i != 3);
sl@0
   203
}
sl@0
   204
sl@0
   205
sl@0
   206
static gboolean remove_even_foreach (gpointer       key,
sl@0
   207
				 gpointer       value,
sl@0
   208
				 gpointer	user_data)
sl@0
   209
{
sl@0
   210
  const char *_key = (const char *) key;
sl@0
   211
  const char *_value = (const char *) value;
sl@0
   212
  int i;
sl@0
   213
  char val [20];
sl@0
   214
sl@0
   215
  g_assert (_key != NULL);
sl@0
   216
  g_assert (*_key != 0);
sl@0
   217
  g_assert (_value != NULL);
sl@0
   218
  g_assert (*_value != 0);
sl@0
   219
sl@0
   220
  i = atoi (_key);
sl@0
   221
sl@0
   222
  sprintf (val, "%d value", i);
sl@0
   223
  g_assert (strcmp (_value, val) == 0);
sl@0
   224
sl@0
   225
  return ((i % 2) == 0) ? TRUE : FALSE;
sl@0
   226
}
sl@0
   227
sl@0
   228
sl@0
   229
sl@0
   230
sl@0
   231
static void second_hash_test (gboolean simple_hash)
sl@0
   232
{
sl@0
   233
     int       i;
sl@0
   234
     char      key[20] = "", val[20]="", *v, *orig_key, *orig_val;
sl@0
   235
     GHashTable     *h;
sl@0
   236
     gboolean found;
sl@0
   237
sl@0
   238
     crcinit ();
sl@0
   239
sl@0
   240
     h = g_hash_table_new (simple_hash ? one_hash : honeyman_hash,
sl@0
   241
     			   second_hash_cmp);
sl@0
   242
     g_assert (h != NULL);
sl@0
   243
     for (i=0; i<20; i++)
sl@0
   244
          {
sl@0
   245
          sprintf (key, "%d", i);
sl@0
   246
	  g_assert (atoi (key) == i);
sl@0
   247
sl@0
   248
	  sprintf (val, "%d value", i);
sl@0
   249
	  g_assert (atoi (val) == i);
sl@0
   250
sl@0
   251
          g_hash_table_insert (h, g_strdup (key), g_strdup (val));
sl@0
   252
          }
sl@0
   253
sl@0
   254
     g_assert (g_hash_table_size (h) == 20);
sl@0
   255
sl@0
   256
     for (i=0; i<20; i++)
sl@0
   257
          {
sl@0
   258
          sprintf (key, "%d", i);
sl@0
   259
	  g_assert (atoi(key) == i);
sl@0
   260
sl@0
   261
          v = (char *) g_hash_table_lookup (h, key);
sl@0
   262
sl@0
   263
	  g_assert (v != NULL);
sl@0
   264
	  g_assert (*v != 0);
sl@0
   265
	  g_assert (atoi (v) == i);
sl@0
   266
          }
sl@0
   267
sl@0
   268
     sprintf (key, "%d", 3);
sl@0
   269
     g_hash_table_remove (h, key);
sl@0
   270
     g_hash_table_foreach_remove (h, remove_even_foreach, NULL);
sl@0
   271
     g_hash_table_foreach (h, not_even_foreach, NULL);
sl@0
   272
sl@0
   273
     for (i=0; i<20; i++)
sl@0
   274
          {
sl@0
   275
	  if ((i % 2) == 0 || i == 3)
sl@0
   276
  	      continue;
sl@0
   277
sl@0
   278
          sprintf (key, "%d", i);
sl@0
   279
	  g_assert (atoi(key) == i);
sl@0
   280
sl@0
   281
	  sprintf (val, "%d value", i);
sl@0
   282
	  g_assert (atoi (val) == i);
sl@0
   283
sl@0
   284
	  orig_key = orig_val = NULL;
sl@0
   285
          found = g_hash_table_lookup_extended (h, key,
sl@0
   286
	  					(gpointer)&orig_key,
sl@0
   287
						(gpointer)&orig_val);
sl@0
   288
	  g_assert (found);
sl@0
   289
sl@0
   290
	  g_hash_table_remove (h, key);
sl@0
   291
sl@0
   292
	  g_assert (orig_key != NULL);
sl@0
   293
	  g_assert (strcmp (key, orig_key) == 0);
sl@0
   294
	  g_free (orig_key);
sl@0
   295
sl@0
   296
	  g_assert (orig_val != NULL);
sl@0
   297
	  g_assert (strcmp (val, orig_val) == 0);
sl@0
   298
	  g_free (orig_val);
sl@0
   299
          }
sl@0
   300
sl@0
   301
    g_hash_table_destroy (h);
sl@0
   302
}
sl@0
   303
sl@0
   304
static gboolean find_first     (gpointer key, 
sl@0
   305
				gpointer value, 
sl@0
   306
				gpointer user_data)
sl@0
   307
{
sl@0
   308
  gint *v = value; 
sl@0
   309
  gint *test = user_data;
sl@0
   310
  return (*v == *test);
sl@0
   311
}
sl@0
   312
sl@0
   313
sl@0
   314
static void direct_hash_test (void)
sl@0
   315
{
sl@0
   316
     gint       i, rc;
sl@0
   317
     GHashTable     *h;
sl@0
   318
sl@0
   319
     h = g_hash_table_new (NULL, NULL);
sl@0
   320
     g_assert (h != NULL);
sl@0
   321
     for (i=1; i<=20; i++)
sl@0
   322
          {
sl@0
   323
          g_hash_table_insert (h, GINT_TO_POINTER (i),
sl@0
   324
	  		       GINT_TO_POINTER (i + 42));
sl@0
   325
          }
sl@0
   326
sl@0
   327
     g_assert (g_hash_table_size (h) == 20);
sl@0
   328
sl@0
   329
     for (i=1; i<=20; i++)
sl@0
   330
          {
sl@0
   331
          rc = GPOINTER_TO_INT (
sl@0
   332
	  	g_hash_table_lookup (h, GINT_TO_POINTER (i)));
sl@0
   333
sl@0
   334
	  g_assert (rc != 0);
sl@0
   335
	  g_assert ((rc - 42) == i);
sl@0
   336
          }
sl@0
   337
sl@0
   338
    g_hash_table_destroy (h);
sl@0
   339
}
sl@0
   340
sl@0
   341
sl@0
   342
sl@0
   343
int
sl@0
   344
main (int   argc,
sl@0
   345
      char *argv[])
sl@0
   346
{
sl@0
   347
  GHashTable *hash_table;
sl@0
   348
  gint i;
sl@0
   349
  gint value = 120;
sl@0
   350
  gint *pvalue; 
sl@0
   351
  
sl@0
   352
  #ifdef SYMBIAN
sl@0
   353
  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
   354
  g_set_print_handler(mrtPrintHandler);
sl@0
   355
  #endif /*SYMBIAN*/
sl@0
   356
	  
sl@0
   357
sl@0
   358
  hash_table = g_hash_table_new (my_hash, my_hash_equal);
sl@0
   359
  for (i = 0; i < 10000; i++)
sl@0
   360
    {
sl@0
   361
      array[i] = i;
sl@0
   362
      g_hash_table_insert (hash_table, &array[i], &array[i]);
sl@0
   363
    }
sl@0
   364
  pvalue = g_hash_table_find (hash_table, find_first, &value);
sl@0
   365
  if (!pvalue || *pvalue != value)
sl@0
   366
    g_assert_not_reached();
sl@0
   367
  
sl@0
   368
  g_hash_table_foreach (hash_table, my_hash_callback, NULL);
sl@0
   369
sl@0
   370
  for (i = 0; i < 10000; i++)
sl@0
   371
    if (array[i] == 0)
sl@0
   372
      g_assert_not_reached();
sl@0
   373
sl@0
   374
  for (i = 0; i < 10000; i++)
sl@0
   375
    g_hash_table_remove (hash_table, &array[i]);
sl@0
   376
sl@0
   377
  for (i = 0; i < 10000; i++)
sl@0
   378
    {
sl@0
   379
      array[i] = i;
sl@0
   380
      g_hash_table_insert (hash_table, &array[i], &array[i]);
sl@0
   381
    }
sl@0
   382
sl@0
   383
  if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
sl@0
   384
      g_hash_table_size (hash_table) != 5000)
sl@0
   385
    g_assert_not_reached();
sl@0
   386
sl@0
   387
  g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
sl@0
   388
sl@0
   389
sl@0
   390
  g_hash_table_destroy (hash_table);
sl@0
   391
sl@0
   392
  second_hash_test (TRUE);
sl@0
   393
  second_hash_test (FALSE);
sl@0
   394
  direct_hash_test ();
sl@0
   395
  
sl@0
   396
  #if SYMBIAN
sl@0
   397
  testResultXml("hash-test");
sl@0
   398
  #endif /* EMULATOR */
sl@0
   399
sl@0
   400
  return 0;
sl@0
   401
sl@0
   402
}