Ejemplo n.º 1
0
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys,
		const struct closest_lookup_info *cl_infos,
		unsigned n_cl_infos) {
    dict_itor *itor = dict_itor_new(dct);

    CU_ASSERT_TRUE(dict_verify(dct));

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_TRUE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NULL(*datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));

	for (unsigned j = 0; j <= i; ++j)
	    test_search(dct, itor, keys[j].key, keys[j].value);
	for (unsigned j = i + 1; j < nkeys; ++j)
	    test_search(dct, itor, keys[j].key, NULL);
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    if (dct->_vtable->insert == (dict_insert_func)hashtable_insert ||
	dct->_vtable->insert == (dict_insert_func)hashtable2_insert) {
	/* Verify that hashtable_resize works as expected. */
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_TRUE(dict_verify(dct));
	if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) {
	    CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3));
	} else {
	    CU_ASSERT_TRUE(hashtable2_resize(dict_private(clone), 3));
	}
	CU_ASSERT_TRUE(dict_verify(dct));
	for (unsigned j = 0; j < nkeys; ++j)
	    test_search(clone, NULL, keys[j].key, keys[j].value);
	dict_free(clone);
    }

    if (dct->_vtable->clone) {
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_PTR_NOT_NULL(clone);
	CU_ASSERT_TRUE(dict_verify(clone));
	CU_ASSERT_EQUAL(dict_count(clone), nkeys);
	for (unsigned i = 0; i < nkeys; ++i) {
	    test_search(clone, itor, keys[i].key, keys[i].value);
	}
	for (unsigned i = 0; i < nkeys; ++i) {
	    CU_ASSERT_TRUE(dict_remove(clone, keys[i].key));
	}
	dict_free(clone);
    }

    for (unsigned i = 0; i < nkeys; ++i)
	test_search(dct, itor, keys[i].key, keys[i].value);

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_FALSE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_EQUAL(*datum_location, keys[i].value);

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    CU_ASSERT_PTR_NOT_NULL(itor);
    char *last_key = NULL;
    unsigned n = 0;
    for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));

	char *key = dict_itor_key(itor);
	bool key_matched = false;
	for (unsigned i = 0; i < nkeys; ++i) {
	    if (keys[i].key == key) {
		CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value);
		key_matched = true;
		break;
	    }
	}
	CU_ASSERT_TRUE(key_matched);

	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert &&
	    dct->_vtable->insert != (dict_insert_func)hashtable2_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0);
	    }
	    last_key = dict_itor_key(itor);
	}

	++n;
    }
    CU_ASSERT_EQUAL(n, nkeys);
    last_key = NULL;
    n = 0;
    for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	CU_ASSERT_PTR_NOT_NULL(*dict_itor_data(itor));

	char *key = dict_itor_key(itor);
	bool key_matched = false;
	for (unsigned i = 0; i < nkeys; ++i) {
	    if (keys[i].key == key) {
		CU_ASSERT_EQUAL(*dict_itor_data(itor), keys[i].value);
		key_matched = true;
		break;
	    }
	}
	CU_ASSERT_TRUE(key_matched);

	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert &&
	    dct->_vtable->insert != (dict_insert_func)hashtable2_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0);
	    }
	    last_key = dict_itor_key(itor);
	}

	++n;
    }
    CU_ASSERT_EQUAL(n, nkeys);

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_FALSE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NOT_NULL(*datum_location);
	*datum_location = keys[i].alt;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i)
	test_search(dct, itor, keys[i].key, keys[i].alt);

    for (unsigned i = 0; i < nkeys; ++i) {
	test_search(dct, itor, keys[i].key, keys[i].alt);
	CU_ASSERT_TRUE(dict_remove(dct, keys[i].key));
	CU_ASSERT_TRUE(dict_verify(dct));

	CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false);
	for (unsigned j = 0; j <= i; ++j) {
	    test_search(dct, itor, keys[j].key, NULL);
	}
	for (unsigned j = i + 1; j < nkeys; ++j) {
	    test_search(dct, itor, keys[j].key, keys[j].alt);
	}
    }

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_TRUE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NULL(*datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_clear(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, keys[i].key, &inserted);
	CU_ASSERT_TRUE(inserted);
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_PTR_NULL(*datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    test_closest_lookup(dct, cl_infos, n_cl_infos);
    dict_itor_free(itor);
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_free(dct), nkeys);
}
Ejemplo n.º 2
0
static void
test_closest_lookup(
    dict *dct, const struct closest_lookup_info *cl_infos, unsigned n_cl_infos)
{
    dict_itor *itor;
    unsigned i;

    if (dct->_vtable->search_le == NULL)
	return;

    itor = dict_itor_new(dct);
    for (i = 0; i < n_cl_infos; i++) {
	if (cl_infos[i].le_key) {
	    CU_ASSERT_STRING_EQUAL(dict_search_le(dct, cl_infos[i].key),
				   cl_infos[i].le_val);
	    CU_ASSERT_EQUAL(dict_itor_search_le(itor, cl_infos[i].key), true);
	    CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].le_key);
	    CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].le_val);
	} else {
	    CU_ASSERT_PTR_NULL(dict_search_le(dct, cl_infos[i].key));
	    CU_ASSERT_EQUAL(dict_itor_search_le(itor, cl_infos[i].key), false);
	    CU_ASSERT_PTR_NULL(dict_itor_key(itor));
	    CU_ASSERT_PTR_NULL(dict_itor_data(itor));
	}
	if (cl_infos[i].lt_key) {
	    CU_ASSERT_STRING_EQUAL(dict_search_lt(dct, cl_infos[i].key),
				   cl_infos[i].lt_val);
	    CU_ASSERT_EQUAL(dict_itor_search_lt(itor, cl_infos[i].key), true);
	    CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].lt_key);
	    CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].lt_val);
	} else {
	    CU_ASSERT_PTR_NULL(dict_search_lt(dct, cl_infos[i].key));
	    CU_ASSERT_EQUAL(dict_itor_search_lt(itor, cl_infos[i].key), false);
	    CU_ASSERT_PTR_NULL(dict_itor_key(itor));
	    CU_ASSERT_PTR_NULL(dict_itor_data(itor));
	}
	if (cl_infos[i].ge_key) {
	    CU_ASSERT_STRING_EQUAL(dict_search_ge(dct, cl_infos[i].key),
				   cl_infos[i].ge_val);
	    CU_ASSERT_EQUAL(dict_itor_search_ge(itor, cl_infos[i].key), true);
	    CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].ge_key);
	    CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].ge_val);
	} else {
	    CU_ASSERT_PTR_NULL(dict_search_ge(dct, cl_infos[i].key));
	    CU_ASSERT_EQUAL(dict_itor_search_ge(itor, cl_infos[i].key), false);
	    CU_ASSERT_PTR_NULL(dict_itor_key(itor));
	    CU_ASSERT_PTR_NULL(dict_itor_data(itor));
	}
	if (cl_infos[i].gt_key) {
	    CU_ASSERT_STRING_EQUAL(dict_search_gt(dct, cl_infos[i].key),
				   cl_infos[i].gt_val);
	    CU_ASSERT_EQUAL(dict_itor_search_gt(itor, cl_infos[i].key), true);
	    CU_ASSERT_STRING_EQUAL(dict_itor_key(itor), cl_infos[i].gt_key);
	    CU_ASSERT_STRING_EQUAL(*dict_itor_data(itor), cl_infos[i].gt_val);
	} else {
	    CU_ASSERT_PTR_NULL(dict_search_gt(dct, cl_infos[i].key));
	    CU_ASSERT_EQUAL(dict_itor_search_gt(itor, cl_infos[i].key), false);
	    CU_ASSERT_PTR_NULL(dict_itor_key(itor));
	    CU_ASSERT_PTR_NULL(dict_itor_data(itor));
	}
    }
    dict_itor_free(itor);
}
Ejemplo n.º 3
0
int
main(int argc, char **argv)
{
    if (argc != 3) {
	fprintf(stderr, "usage: %s [type] [input]\n", appname);
	fprintf(stderr, "type: specifies the dictionary type:\n");
	fprintf(stderr, "   h: height-balanced tree\n");
	fprintf(stderr, "   p: path-reduction tree\n");
	fprintf(stderr, "   r: red-black tree\n");
	fprintf(stderr, "   t: treap\n");
	fprintf(stderr, "   s: splay tree\n");
	fprintf(stderr, "   w: weight-balanced tree\n");
	fprintf(stderr, "   S: skiplist\n");
	fprintf(stderr, "   H: hashtable\n");
	fprintf(stderr, "   2: hashtable 2\n");
	fprintf(stderr, "input: text file consisting of newline-separated keys\n");
	exit(EXIT_FAILURE);
    }

    srand(0xdeadbeef);

    dict_malloc_func = xmalloc;

    const char type = argv[1][0];
    const char *container_name = NULL;
    dict *dct = create_dictionary(type, &container_name);
    if (!dct)
	quit("can't create container");

    ASSERT(dict_verify(dct));

    const size_t malloced_save = malloced;

    FILE *fp = fopen(argv[2], "r");
    if (fp == NULL)
	quit("cant open file '%s': %s", argv[2], strerror(errno));

    unsigned nwords = 0;
    char buf[512];
    while (fgets(buf, sizeof(buf), fp))
	++nwords;

    if (!nwords)
	quit("nothing read from file");

    char **words = xmalloc(sizeof(*words) * nwords);
    rewind(fp);
    for (unsigned i = 0; i < nwords && fgets(buf, sizeof(buf), fp); i++) {
	strtok(buf, "\n");
	words[i] = xstrdup(buf);
    }
    fclose(fp);

    malloced = malloced_save;
    size_t total_comp = 0, total_hash = 0, total_rotations = 0;

    struct rusage start, end;
    struct timeval total = { 0, 0 };

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	bool inserted = false;
	void **datum_location = dict_insert(dct, words[i], &inserted);
	if (!inserted)
	    quit("insert #%d failed for '%s'", i, words[i]);
	ASSERT(datum_location != NULL);
	ASSERT(*datum_location == NULL);
	*datum_location = words[i];
    }
    timer_end(&start, &end, &total);
    printf("    %s container: %.02fkB\n", container_name, malloced_save * 1e-3);
    printf("       %s memory: %.02fkB\n", container_name, malloced * 1e-3);
    printf("       %s insert: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("insert rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    ASSERT(dict_verify(dct));

    unsigned n = dict_count(dct);
    if (n != nwords)
	quit("bad count (%u - should be %u)!", n, nwords);

    dict_itor *itor = dict_itor_new(dct);

    timer_start(&start);
    n = 0;
    ASSERT(dict_itor_first(itor));
    do {
	ASSERT(dict_itor_valid(itor));
	ASSERT(dict_itor_key(itor) == *dict_itor_data(itor));
	++n;
    } while (dict_itor_next(itor));
    timer_end(&start, &end, &total);
    printf("  %s fwd iterate: %6.03f s\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
    if (n != nwords)
	warn("Fwd iteration returned %u items - should be %u", n, nwords);

    timer_start(&start);
    n = 0;
    ASSERT(dict_itor_last(itor));
    do {
	ASSERT(dict_itor_valid(itor));
	ASSERT(dict_itor_key(itor) == *dict_itor_data(itor));
	++n;
    } while (dict_itor_prev(itor));
    timer_end(&start, &end, &total);
    printf("  %s rev iterate: %6.03f s\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
    if (n != nwords)
	warn("Rev iteration returned %u items - should be %u", n, nwords);

    dict_itor_free(itor);

    /* shuffle(words, nwords); */

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	char *p = dict_search(dct, words[i]);
	if (!p)
	    quit("lookup failed for '%s'", buf);
	if (p != words[i])
	    quit("bad data for '%s', got '%s' instead", words[i], p);
    }
    timer_end(&start, &end, &total);
    printf("  %s good search: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("search rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	int rv = rand() % strlen(words[i]);
	words[i][rv]++;
	dict_search(dct, words[i]);
	words[i][rv]--;
    }
    timer_end(&start, &end, &total);
    printf("   %s bad search: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;

    /* shuffle(words, nwords); */

    timer_start(&start);
    for (unsigned i = 0; i < nwords; i++) {
	if (!dict_remove(dct, words[i]))
	    quit("removing #%d '%s' failed!\n", i, words[i]);
    }
    timer_end(&start, &end, &total);
    printf("       %s remove: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
	   comp_count, hash_count);
    total_comp += comp_count; comp_count = 0;
    total_hash += hash_count; hash_count = 0;
    if (type != 'H' && type != '2' && type != 'S') {
	tree_base *tree = dict_private(dct);
	printf("remove rotations: %zu\n", tree->rotation_count);
	total_rotations += tree->rotation_count;
	tree->rotation_count = 0;
    }

    ASSERT(dict_verify(dct));

    if ((n = dict_count(dct)) != 0)
	quit("error - count not zero (%u)!", n);

    dict_free(dct);

    printf("        %s total: %6.03f s (%9zu cmp, %9zu hash)\n",
	   container_name,
	   (total.tv_sec * 1000000 + total.tv_usec) * 1e-6,
	   total_comp, total_hash);

    if (type != 'H' && type != '2' && type != 'S') {
	printf(" total rotations: %zu\n", total_rotations);
    }

    FREE(words);

    exit(EXIT_SUCCESS);
}
Ejemplo n.º 4
0
int
main(int argc, char **argv)
{
    char buf[512], *p, *ptr, *ptr2;
    int rv;
    dict *dct;

    if (argc != 2)
	quit("usage: %s [type]", appname);

    srand((unsigned)time(NULL));

    dict_malloc_func = xmalloc;

    ++argv;
    switch (argv[0][0]) {
	case 'h':
	    dct = hb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'p':
	    dct = pr_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'r':
	    dct = rb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 't':
	    dct = tr_dict_new((dict_compare_func)strcmp, NULL, key_val_free);
	    break;
	case 's':
	    dct = sp_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'w':
	    dct = wb_dict_new((dict_compare_func)strcmp, key_val_free);
	    break;
	case 'H':
	    dct = hashtable_dict_new((dict_compare_func)strcmp,
				     dict_str_hash,
				     key_val_free, HSIZE);
	    break;
	default:
	    quit("type must be one of h, p, r, t, s, w, or H");
    }

    if (!dct)
	quit("can't create container");

    for (;;) {
	printf("> ");
	fflush(stdout);
	if (fgets(buf, sizeof(buf), stdin) == NULL)
	    break;
	if ((p = strchr(buf, '\n')) != NULL)
	    *p = 0;
	for (p = buf; isspace(*p); p++)
	    /* void */;
	strcpy(buf, p);
	ptr2 = (ptr = strtok(buf, " ") ? strtok(NULL, " ") : NULL) ?
	    strtok(NULL, " ") : NULL;
	if (*buf == 0)
	    continue;
	if (strcmp(buf, "insert") == 0) {
	    if (!ptr2) {
		printf("usage: insert <key> <data>\n");
		continue;
	    }
	    void **datum_location;
	    if (dict_insert(dct, xstrdup(ptr), &datum_location)) {
		*datum_location = xstrdup(ptr2);
		printf("inserted '%s': '%s'\n",
		       ptr, *datum_location);
	    } else {
		printf("key '%s' already in dict: '%s'\n",
		       ptr, *datum_location);
	    }
	} else if (strcmp(buf, "search") == 0) {
	    if (ptr2) {
		printf("usage: search <key>\n");
		continue;
	    }
	    ptr2 = dict_search(dct, ptr);
	    if (ptr2)
		printf("found '%s': '%s'\n", ptr, ptr2);
	    else
		printf("key '%s' not in dict!\n", ptr);
	} else if (strcmp(buf, "remove") == 0) {
	    if (!ptr || ptr2) {
		printf("usage: remove <key>\n");
		continue;
	    }
	    rv = dict_remove(dct, ptr);
	    if (rv == 0)
		printf("removed '%s' from dict\n", ptr);
	    else
		printf("key '%s' not in dict!\n", ptr);
	} else if (strcmp(buf, "show") == 0) {
	    if (ptr) {
		printf("usage: show\n");
		continue;
	    }
	    dict_itor *itor = dict_itor_new(dct);
	    dict_itor_first(itor);
	    for (; dict_itor_valid(itor); dict_itor_next(itor))
		printf("'%s': '%s'\n",
		       (char *)dict_itor_key(itor),
		       (char *)dict_itor_data(itor));
	    dict_itor_free(itor);
	} else if (strcmp(buf, "reverse") == 0) {
	    if (ptr) {
		printf("usage: reverse\n");
		continue;
	    }
	    dict_itor *itor = dict_itor_new(dct);
	    dict_itor_last(itor);
	    for (; dict_itor_valid(itor); dict_itor_prev(itor))
		printf("'%s': '%s'\n",
		       (char *)dict_itor_key(itor),
		       (char *)dict_itor_data(itor));
	    dict_itor_free(itor);
	} else if (strcmp(buf, "clear") == 0) {
	    if (ptr) {
		printf("usage: clear\n");
		continue;
	    }
	    dict_clear(dct);
	} else if (strcmp(buf, "count") == 0) {
	    if (ptr) {
		printf("usage: count\n");
		continue;
	    }
	    printf("count = %zu\n", dict_count(dct));
	} else if (strcmp(buf, "quit") == 0) {
	    break;
	} else {
	    printf("Usage summary:\n");
	    printf("  insert <key> <data>\n");
	    printf("  search <key>\n");
	    printf("  remove <key>\n");
	    printf("  clear\n");
	    printf("  count\n");
	    printf("  show\n");
	    printf("  reverse\n");
	    printf("  quit\n");
	}
    }

    dict_free(dct);

    exit(0);
}
Ejemplo n.º 5
0
void test_basic(dict *dct, const struct key_info *keys, const unsigned nkeys) {
    CU_ASSERT_TRUE(dict_verify(dct));

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));

	for (unsigned j = 0; j <= i; ++j)
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), keys[j].value);
	for (unsigned j = i + 1; j < nkeys; ++j)
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), NULL);
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    if (dct->_vtable->insert == (dict_insert_func)hashtable_insert) {
	/* Verify that hashtable_resize works as expected. */
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_TRUE(dict_verify(dct));
	CU_ASSERT_TRUE(hashtable_resize(dict_private(clone), 3));
	CU_ASSERT_TRUE(dict_verify(dct));
	for (unsigned j = 0; j < nkeys; ++j)
	    CU_ASSERT_EQUAL(dict_search(clone, keys[j].key), keys[j].value);
	dict_free(clone);
    }

    if (dct->_vtable->clone) {
	dict *clone = dict_clone(dct, NULL);
	CU_ASSERT_PTR_NOT_NULL(clone);
	CU_ASSERT_TRUE(dict_verify(clone));
	CU_ASSERT_EQUAL(dict_count(clone), nkeys);
	for (unsigned i = 0; i < nkeys; ++i) {
	    CU_ASSERT_EQUAL(dict_search(clone, keys[i].key), keys[i].value);
	}
	for (unsigned i = 0; i < nkeys; ++i) {
	    CU_ASSERT_TRUE(dict_remove(clone, keys[i].key));
	}
	dict_free(clone);
    }

    for (unsigned i = 0; i < nkeys; ++i)
	CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].value);

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	CU_ASSERT_EQUAL(*datum_location, keys[i].value);

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    dict_itor *itor = dict_itor_new(dct);
    CU_ASSERT_PTR_NOT_NULL(itor);
    char *last_key = NULL;
    unsigned n = 0;
    for (dict_itor_first(itor); dict_itor_valid(itor); dict_itor_next(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	++n;
	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) < 0);
	    }
	    last_key = dict_itor_key(itor);
	}
    }
    CU_ASSERT_EQUAL(n, nkeys);
    last_key = NULL;
    n = 0;
    for (dict_itor_last(itor); dict_itor_valid(itor); dict_itor_prev(itor)) {
	CU_ASSERT_PTR_NOT_NULL(dict_itor_key(itor));
	CU_ASSERT_PTR_NOT_NULL(dict_itor_data(itor));
	++n;
	if (dct->_vtable->insert != (dict_insert_func)hashtable_insert) {
	    if (last_key) {
		CU_ASSERT_TRUE(strcmp(last_key, dict_itor_key(itor)) > 0);
	    }
	    last_key = dict_itor_key(itor);
	}
    }
    CU_ASSERT_EQUAL(n, nkeys);
    dict_itor_free(itor);

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_FALSE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].alt;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i)
	CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].alt);

    for (unsigned i = 0; i < nkeys; ++i) {
	CU_ASSERT_EQUAL(dict_search(dct, keys[i].key), keys[i].alt);
	CU_ASSERT_TRUE(dict_remove(dct, keys[i].key));
	CU_ASSERT_TRUE(dict_verify(dct));

	CU_ASSERT_EQUAL(dict_remove(dct, keys[i].key), false);
	for (unsigned j = 0; j <= i; ++j) {
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), NULL);
	}
	for (unsigned j = i + 1; j < nkeys; ++j) {
	    CU_ASSERT_EQUAL(dict_search(dct, keys[j].key), keys[j].alt);
	}
    }

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_clear(dct), nkeys);

    for (unsigned i = 0; i < nkeys; ++i) {
	void **datum_location = NULL;
	CU_ASSERT_TRUE(dict_insert(dct, keys[i].key, &datum_location));
	CU_ASSERT_PTR_NOT_NULL(datum_location);
	*datum_location = keys[i].value;

	CU_ASSERT_TRUE(dict_verify(dct));
    }
    CU_ASSERT_EQUAL(dict_count(dct), nkeys);
    CU_ASSERT_EQUAL(dict_free(dct), nkeys);
}