Beispiel #1
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);
}