(test case) GHash bug?
- From: Jeff Garzik <jgarzik pobox com>
- To: gtk-list redhat com
- Subject: (test case) GHash bug?
- Date: Sat, 23 Jan 1999 20:23:53 -0500 (EST)
The following test (attached) inserts 20 values into a hash table. The
key is a number (0-19) stored as a string, hashed w/ a custom CRC hash
function. The value is a g_strdup'd copy of that same number, stored as
a string formatted from "%d value".
The current ghash code seems to be failing miserably on this easy test.
Jeff
create table
table insert 0, key="0", val="0 value"
the_hash = 113246208
table insert 1, key="1", val="1 value"
the_hash = 1321205760
table insert 2, key="2", val="2 value"
the_hash = 583008256
comparing "2" and "2", returning 1
[...]
table insert 18, key="18", val="18 value"
the_hash = 257785856
comparing "18" and "18", returning 1
table insert 19, key="19", val="19 value"
the_hash = 1197309952
comparing "19" and "19", returning 1
table lookup 0
the_hash = 113246208
comparing "0" and "0", returning 1
table lookup("0") returned "18 value"
** ERROR **: file hash-test.c: line 123 (second_hash_test): assertion
failed: (atoi (v) == i)
aborting...
Aborted (core dumped)
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#undef G_LOG_DOMAIN
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#define POLY 0x48000000L /* 31-bit polynomial (avoids sign problems) */
static guint CrcTable[128];
static void crcinit(void)
{
int i, j;
guint sum;
for (i = 0; i < 128; ++i) {
sum = 0L;
for (j = 7 - 1; j >= 0; --j)
if (i & (1 << j))
sum ^= POLY >> j;
CrcTable[i] = sum;
}
}
static guint hash(const char *name, int size)
{
guint sum = 0;
while (size--) {
sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
}
return(sum);
}
static guint second_hash (gconstpointer key)
{
guint the_hash = hash ((const char *) key, strlen((const char *) key));
g_print("the_hash = %u\n", the_hash);
return the_hash;
}
static gint second_hash_cmp (gconstpointer a, gconstpointer b)
{
gint rc = (strcmp (a, b) == 0);
g_print("comparing \"%s\" and \"%s\", returning %d\n",
a ? (char*)a : "", b ? (char*)b : "", rc);
return rc;
}
static void second_hash_test (void)
{
int i;
char new_key[20] = "", new_val[20]="", *v, *new_val_copy;
GHashTable *h;
crcinit ();
g_print("create table\n");
h = g_hash_table_new (second_hash, second_hash_cmp);
for (i=0; i<20; i++)
{
sprintf (new_key, "%d", i);
g_assert (atoi (new_key) == i);
sprintf (new_val, "%d value", i);
g_assert (atoi (new_val) == i);
new_val_copy = g_strdup (new_val);
g_assert (new_val_copy != NULL);
g_assert (*new_val_copy != 0);
g_print("table insert %d, key=\"%s\", val=\"%s\"\n",
i, new_key, new_val_copy);
g_hash_table_insert (h, new_key, new_val_copy);
}
for (i=0; i<20; i++)
{
sprintf (new_key, "%d", i);
g_assert (atoi(new_key) == i);
g_print("table lookup %d\n", i);
v = (char *) g_hash_table_lookup (h, new_key);
g_print("table lookup(\"%s\") returned \"%s\"\n",
new_key, v ? v : "");
g_assert (v != NULL);
g_assert (*v != 0);
g_assert (atoi (v) == i);
}
}
int main (int argc, char *argv[])
{
second_hash_test ();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]