コード例 #1
0
ファイル: t_ht_free.c プロジェクト: Niichan/snircd
int
main(int argc, char **argv)
{
  int i;
  hash_table_t table[] = { /* some tables to operate on */
    HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE0),
    HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE1),
    { DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADPTR,
      (hash_func_t)DEADPTR, (hash_comp_t)DEADPTR, (hash_resize_t)DEADPTR,
      DEADPTR } /* table[2] */
  };
  hash_entry_t entry[] = { /* some entries to operate on */
    HASH_ENTRY_INIT(OBJECT0),
    HASH_ENTRY_INIT(OBJECT1),
    HASH_ENTRY_INIT(OBJECT2),
    HASH_ENTRY_INIT(OBJECT3),
    HASH_ENTRY_INIT(OBJECT4),
  };
  db_key_t key[] = { /* some keys... */
    DB_KEY_INIT("obj0", 0),
    DB_KEY_INIT("obj1", 1),
    DB_KEY_INIT("obj2", 2),
    DB_KEY_INIT("obj3", 3),
    DB_KEY_INIT("obj4", 4),
  };

  /* initialize the tables with a size */
  if (ht_init(&table[0], 0, check_func, check_comp, 0, TABLE0, 7) ||
      ht_init(&table[1], 0, check_func, check_comp, 0, TABLE1, 7) ||
      !table[0].ht_table || !table[1].ht_table)
    return -1; /* failed to initialize test */

  /* Add some entries to various hash tables */
  for (i = 0; i < 5; i++)
    if (ht_add(&table[0], &entry[i], &key[i]))
      return -1; /* failed to initialize test */

  /* Check handling of bad arguments */
  check_result(ht_free(0), DB_ERR_BADARGS, "ht_free_noargs",
	       "ht_free() with no valid arguments", 0);
  check_result(ht_free(&table[2]), DB_ERR_BADARGS, "ht_free_badtable",
	       "ht_free() with bad table", 0);

  /* Freeze the table temporarily */
  ht_flags(&table[1]) |= HASH_FLAG_FREEZE;
  /* check if frozen tables are excluded */
  check_result(ht_free(&table[1]), DB_ERR_FROZEN, "ht_free_frozen",
	       "ht_free() on frozen table", 1);
  /* Unfreeze the table */
  ht_flags(&table[1]) &= ~HASH_FLAG_FREEZE;

  /* Check if non-empty tables are excluded */
  check_result(ht_free(&table[0]), DB_ERR_NOTEMPTY, "ht_free_nonempty",
	       "ht_free() on non-empty table", 0);

  /* OK, now try to free the table */
  check_result(ht_free(&table[1]), 0, "ht_free_t0", "ht_free() on empty table",
	       1);

  /* Verify that table pointer is now 0 */
  if (table[1].ht_table != 0)
    printf("FAIL/ht_free_t0_table:Table not cleared properly\n");
  else
    printf("PASS/ht_free_t0_table:Table properly cleared\n");

  return 0;
}
コード例 #2
0
ファイル: t_ht_addfind.c プロジェクト: jast/ircu-now
int
main(int argc, char **argv)
{
  hash_table_t table[] = { /* some tables to operate on */
    HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE0),
    HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE1),
    { DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADPTR,
      (hash_func_t)DEADPTR, (hash_comp_t)DEADPTR, (hash_resize_t)DEADPTR,
      DEADPTR } /* table[2] */
  };
  hash_entry_t entry[] = { /* some entries to operate on */
    HASH_ENTRY_INIT(OBJECT0),
    HASH_ENTRY_INIT(OBJECT1),
    HASH_ENTRY_INIT(OBJECT2),
    HASH_ENTRY_INIT(OBJECT3),
    HASH_ENTRY_INIT(OBJECT4),
    HASH_ENTRY_INIT(OBJECT5),
    { DEADINT, { DEADINT, DEADPTR, DEADPTR, DEADPTR, DEADPTR, DEADINT },
      DEADPTR, DEADINT, { DEADPTR, DEADINT }, DEADPTR } /* entry[6] */
  };
  hash_entry_t *entry_p;
  db_key_t key[] = { /* some keys... */
    DB_KEY_INIT("obj0", 0),
    DB_KEY_INIT("obj1", 1),
    DB_KEY_INIT("obj2", 2),
    DB_KEY_INIT("obj3", 3),
    DB_KEY_INIT("obj4", 4),
    DB_KEY_INIT("obj5", 5),
    DB_KEY_INIT("obj6", 6)
  };

  /* initialize the tables with a size */
  if (ht_init(&table[0], 0, check_func, check_comp, 0, TABLE0, 7) ||
      ht_init(&table[1], 0, check_func, check_comp, 0, TABLE1, 7))
    return -1; /* failed to initialize test */

  /* Check ht_find()'s handling of bad arguments */
  check_result(ht_find(0, 0, 0), DB_ERR_BADARGS, "ht_find_noargs",
	       "ht_find() with no valid arguments", 0);
  check_result(ht_find(&table[2], 0, &key[6]), DB_ERR_BADARGS,
	       "ht_find_badtable", "ht_find() with bad table", 0);
  check_result(ht_find(&table[0], 0, 0), DB_ERR_BADARGS,
	       "ht_find_badkey", "ht_find() with bad key", 0);

  /* Check if empty tables return DB_ERR_NOENTRY */
  check_result(ht_find(&table[0], 0, &key[6]), DB_ERR_NOENTRY,
	       "ht_find_emptytable", "ht_find() with empty table", 1);

  /* Check ht_add()'s handling of bad arguments */
  check_result(ht_add(0, 0, 0), DB_ERR_BADARGS, "ht_add_noargs",
	       "ht_add() with no valid arguments", 0);
  check_result(ht_add(&table[2], &entry[0], &key[0]), DB_ERR_BADARGS,
	       "ht_add_badtable", "ht_add() with bad table", 1);
  check_result(ht_add(&table[0], &entry[6], &key[6]), DB_ERR_BADARGS,
	       "ht_add_badentry", "ht_add() with bad entry", 1);
  check_result(ht_add(&table[0], &entry[0], 0), DB_ERR_BADARGS, "ht_add_nokey",
	       "ht_add() with no key", 1);

  /* Freeze the table temporarily */
  ht_flags(&table[0]) |= HASH_FLAG_FREEZE;
  /* Check adds to frozen tables */
  check_result(ht_add(&table[0], &entry[0], &key[0]), DB_ERR_FROZEN,
	       "ht_add_frozen", "ht_add() on frozen table", 1);
  /* Unfreeze the table */
  ht_flags(&table[0]) &= ~HASH_FLAG_FREEZE;

  /* Add an element to a hash table */
  check_result(ht_add(&table[1], &entry[5], &key[5]), 0, "ht_add_t1e5",
	       "Add entry 5 to table 1", 1);

  /* Now try to add the same element to another hash table */
  check_result(ht_add(&table[0], &entry[5], &key[5]), DB_ERR_BUSY,
	       "ht_add_busy", "Add busy entry 5 to table 0", 1);

  /* Try ht_find() to see if it can find elements */
  check_result(ht_find(&table[1], &entry_p, &key[5]), 0, "ht_find_t1e5",
	       "Look up entry 5 in table 1", 1);
  if (entry_p != &entry[5]) {
    printf("FAIL/ht_find_t1e5_entry:Attempt to look up entry 5 retrieved "
	   "%p (correct answer is %p)\n", (void *)entry_p, (void *)&entry[5]);
    return 0;
  } else
    printf("PASS/ht_find_t1e5_entry:Retrieved correct entry %p\n",
	   (void *)entry_p);

  /* Try looking up an element that isn't there in a populated table */
  check_result(ht_find(&table[1], 0, &key[6]), DB_ERR_NOENTRY,
	       "ht_find_t1e6", "Look up non-existant entry 5 in table 1", 1);

  /* Now we know that ht_find() works properly--finish testing ht_add() */
  check_result(ht_add(&table[1], &entry[0], &key[5]), DB_ERR_DUPLICATE,
	       "ht_add_duplicate", "Attempt to add duplicate entry to table",
	       1);

  /* Now try adding several entries to the table */
  check_result(ht_add(&table[0], &entry[0], &key[0]), 0, "ht_add_t0e0",
	       "Add entry 0 to table 0", 1);
  check_result(ht_add(&table[0], &entry[1], &key[1]), 0, "ht_add_t0e1",
	       "Add entry 1 to table 0", 1);
  check_result(ht_add(&table[0], &entry[2], &key[2]), 0, "ht_add_t0e2",
	       "Add entry 2 to table 0", 1);
  check_result(ht_add(&table[0], &entry[3], &key[3]), 0, "ht_add_t0e3",
	       "Add entry 3 to table 0", 1);
  check_result(ht_add(&table[0], &entry[4], &key[4]), 0, "ht_add_t0e4",
	       "Add entry 4 to table 0", 1);

  /* Check to see if an element can be found */
  check_result(ht_find(&table[0], 0, &key[2]), 0, "ht_find_t0e2",
	       "Find entry 2 in table 0", 1);

  return 0;
}