CTEST(ls_htable, collisions)
{
    ls_htable    *table;
    ls_hnode     *nodea, *nodeb, *node;

    // "09Vi" and "08vJ" collide, which WILL change if the string hashcode
    // function changes
    char *a = "09Vi";
    char *b = "08vJ";

    ASSERT_TRUE(ls_htable_create(5,
                                 ls_str_hashcode,
                                 ls_str_compare,
                                 &table,
                                 NULL));

    ASSERT_EQUAL(ls_str_hashcode(a), ls_str_hashcode(b));
    ASSERT_TRUE(ls_htable_put(table, a, "1", NULL, NULL));
    ASSERT_NULL(ls_htable_get(table, b) );
    ASSERT_TRUE(ls_htable_put(table, b, "2", NULL, NULL));
    ASSERT_NOT_NULL(ls_htable_get(table, b) );
    ASSERT_NOT_NULL(ls_htable_get(table, a) );
    ASSERT_EQUAL(ls_htable_walk(table, test_htable_nullwalk, NULL), 2);
    ASSERT_EQUAL(ls_htable_walk(table, test_htable_stopwalk, NULL), 1);

    nodea = ls_htable_get_node(table, a);
    nodeb = ls_htable_get_node(table, b);
    node = ls_htable_get_next_node(table, nodea);
    if (node != NULL)
    {
        ASSERT_TRUE(node == nodeb);
    }
    else
    {
        node = ls_htable_get_next_node(table, nodeb);
        ASSERT_TRUE(node == nodea);
    }

    ls_htable_remove(table, "non-existant");
    ls_htable_remove(table, a);
    ASSERT_TRUE(ls_htable_put(table, a, "3", NULL, NULL));
    ls_htable_remove(table, b);
    ls_htable_remove(table, a);

    ls_htable_destroy(table);
}
Exemple #2
0
CTEST(ls_htable, basics)
{
  ls_htable* table;
  ls_err     err;
  ASSERT_TRUE( ls_htable_create(7,
                                ls_str_hashcode,
                                ls_str_compare,
                                &table,
                                &err) );

  ASSERT_EQUAL(ls_htable_get_count(table), 0);
  ASSERT_NULL( ls_htable_get(table, "key1") );
  ASSERT_NULL( ls_htable_get(table, "key2") );

  pvalue = NULL;
  ASSERT_TRUE( ls_htable_put(table, "key1", "value one",
                             test_htable_store_pvalue, &err) );
  ASSERT_NULL(pvalue);
  ASSERT_EQUAL(ls_htable_get_count(table),                        1);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "value one"), 0);
  ASSERT_NULL( ls_htable_get(table, "key2") );

  ASSERT_TRUE( ls_htable_put(table, "key2", "value two",
                             test_htable_store_pvalue, &err) );
  ASSERT_NULL(pvalue);
  ASSERT_EQUAL(ls_htable_get_count(table),                        2);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "value one"), 0);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0);

  ASSERT_TRUE( ls_htable_put(table, "key1", "val 1", test_htable_store_pvalue,
                             &err) );
  ASSERT_EQUAL(strcmp( (const char*)pvalue, "value one" ),        0);
  ASSERT_EQUAL(ls_htable_get_count(table),                        2);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "val 1"),     0);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0);

  pvalue = NULL;
  ls_htable_remove(table, "key1");
  ASSERT_EQUAL(strcmp(pvalue, "val 1"),    0);
  ASSERT_EQUAL(ls_htable_get_count(table), 1);
  ASSERT_NULL( ls_htable_get(table, "key1") );
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"), 0);

  pvalue = NULL;
  ASSERT_TRUE( ls_htable_put(table, "key1", "first value",
                             test_htable_store_pvalue, &err) );
  ASSERT_NULL(pvalue);
  ASSERT_EQUAL(ls_htable_get_count(table),                          2);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key1"), "first value"), 0);
  ASSERT_EQUAL(strcmp(ls_htable_get(table, "key2"), "value two"),   0);

  ls_htable_clear(table);
  ASSERT_EQUAL(ls_htable_get_count(table), 0);
  ASSERT_NULL( ls_htable_get(table, "key1") );
  ASSERT_NULL( ls_htable_get(table, "key2") );

  ls_htable_destroy(table);
}
CTEST(ls_htable, cleaner_edges)
{
    ls_htable    *table;
    ls_err       err;

    ASSERT_TRUE(ls_htable_create(-1,
                             ls_str_hashcode,
                             ls_str_compare,
                             &table,
                             &err));
    ASSERT_EQUAL(ls_htable_walk(table, test_htable_nullwalk, NULL), 0);
    ASSERT_TRUE(ls_htable_put(table, "key1", "value one", NULL, &err));
    ASSERT_TRUE(ls_htable_put(table, "key1", "value one prime", NULL, &err));
    ASSERT_TRUE(ls_htable_put(table, "key1", NULL, NULL, &err));
    ASSERT_TRUE(ls_htable_put(table, "key1", "value one", NULL, &err));
    ls_htable_remove(table, "key1");
    ASSERT_TRUE(ls_htable_put(table, "key1", NULL, NULL, &err));
    ls_htable_remove(table, "key1");
    ASSERT_TRUE(ls_htable_put(table, "key1", "value one", NULL, &err));
    ls_htable_clear(table);
    ls_htable_destroy(table);
}
CTEST(ls_htable, iteration)
{
    ls_htable    *table;
    ls_hnode     *node;
    unsigned int count;
    ls_err       err;

    // initial bucket count of 1 to force a resize and increase coverage
    ASSERT_TRUE(ls_htable_create(1, ls_str_hashcode,
                                 ls_str_compare, &table, &err));

    node = ls_htable_get_first_node(table);
    ASSERT_NULL(node );

    ASSERT_TRUE(ls_htable_put(table, "key1", ls_data_strdup("value one"),
                              ls_htable_free_data_cleaner, NULL));
    ASSERT_TRUE(ls_htable_put(table, "key2", ls_data_strdup("value two"),
                              ls_htable_free_data_cleaner, NULL));
    ASSERT_TRUE(ls_htable_put(table, "key3", ls_data_strdup("value three"),
                              ls_htable_free_data_cleaner, NULL));

    count = 1;
    node = ls_htable_get_first_node(table);
    while ((node = ls_htable_get_next_node(table, node)) != NULL)
    {
        count++;
        ASSERT_NOT_NULL(ls_hnode_get_key(node) );
        ASSERT_NOT_NULL(ls_hnode_get_value(node) );
    }
    ASSERT_TRUE(count == ls_htable_get_count(table));

    ls_htable_remove(table, "key2");

    count = 1;
    node = ls_htable_get_first_node(table);
    while ((node = ls_htable_get_next_node(table, node)) != NULL)
    {
        count++;
        ASSERT_NOT_NULL(ls_hnode_get_key(node) );
        ASSERT_NOT_NULL(ls_hnode_get_value(node) );
    }
    ASSERT_EQUAL(count, ls_htable_get_count(table));

    ls_htable_destroy(table);
}