GHashTable strange behavior

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I am facing some strange behavior with GHashTable. When I use g_hash_table_insert to insert the same key twice, it does not return FALSE, but TRUE:

typedef struct {
    int a, b;
} key_t;

typedef struct {
    int a, b;
} value_t;

guint hash_func(gconstpointer key)
{
    return hash((const char *)key, sizeof(key_t));
}

gboolean equal_func(gconstpointer a, gconstpointer b)
{
    return (memcmp(a, b, sizeof(key_t)) == 0);
}

void destroy_key(gpointer data)
{
    free(data);
}

void destroy_value(gpointer data)
{
    free(data);
}

TEST_F(HashTableTests, test_add)
{

    GHashTable *hash_table = g_hash_table_new_full(hash_func, equal_func, destroy_key, destroy_value);

    key_t *key1 = (key_t *)malloc(sizeof(*key1));
    value_t *value1 = (value_t *)malloc(sizeof(*value1));

    key_t *key2 = (key_t *)malloc(sizeof(*key2));
    value_t *value2 = (value_t *)malloc(sizeof(*value2));

    key1->a = 1;
    key1->b = 2;
    value1->a = 3;
    value1->b = 4;

    key2->a = 1;
    key2->b = 2;
    value2->a = 3;
    value2->b = 4;

    ASSERT_TRUE(g_hash_table_insert(hash_table, key1, value1));

    ASSERT_FALSE(g_hash_table_insert(hash_table, key2, value2)); // Assertion fails

    g_hash_table_destroy(hash_table);
}

Any ideas?

BR,
Stathis 		 	   		  
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list





[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux