2017-01-07 07:10:06 -07:00
|
|
|
/* vi:set ts=8 sts=4 sw=4 noet:
|
|
|
|
*
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
*
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* memfile_test.c: Unittests for memfile.c
|
|
|
|
* Mostly by Ivan Krasilnikov.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// Must include main.c because it contains much more than just main()
|
2017-01-07 07:10:06 -07:00
|
|
|
#define NO_VIM_MAIN
|
|
|
|
#include "main.c"
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// This file has to be included because the tested functions are static
|
2017-01-07 07:10:06 -07:00
|
|
|
#include "memfile.c"
|
|
|
|
|
|
|
|
#define index_to_key(i) ((i) ^ 15167)
|
|
|
|
#define TEST_COUNT 50000
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test mf_hash_*() functions.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_mf_hash(void)
|
|
|
|
{
|
|
|
|
mf_hashtab_T ht;
|
|
|
|
mf_hashitem_T *item;
|
|
|
|
blocknr_T key;
|
2023-03-07 19:46:09 -07:00
|
|
|
long_u i;
|
|
|
|
long_u num_buckets;
|
2017-01-07 07:10:06 -07:00
|
|
|
|
|
|
|
mf_hash_init(&ht);
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// insert some items and check invariants
|
2017-01-07 07:10:06 -07:00
|
|
|
for (i = 0; i < TEST_COUNT; i++)
|
|
|
|
{
|
|
|
|
assert(ht.mht_count == i);
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// check that number of buckets is a power of 2
|
2017-01-07 07:10:06 -07:00
|
|
|
num_buckets = ht.mht_mask + 1;
|
|
|
|
assert(num_buckets > 0 && (num_buckets & (num_buckets - 1)) == 0);
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// check load factor
|
2017-01-07 07:10:06 -07:00
|
|
|
assert(ht.mht_count <= (num_buckets << MHT_LOG_LOAD_FACTOR));
|
|
|
|
|
|
|
|
if (i < (MHT_INIT_SIZE << MHT_LOG_LOAD_FACTOR))
|
|
|
|
{
|
2023-03-07 19:46:09 -07:00
|
|
|
// first expansion shouldn't have occurred yet
|
2017-01-07 07:10:06 -07:00
|
|
|
assert(num_buckets == MHT_INIT_SIZE);
|
|
|
|
assert(ht.mht_buckets == ht.mht_small_buckets);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(num_buckets > MHT_INIT_SIZE);
|
|
|
|
assert(ht.mht_buckets != ht.mht_small_buckets);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = index_to_key(i);
|
|
|
|
assert(mf_hash_find(&ht, key) == NULL);
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// allocate and add new item
|
|
|
|
item = LALLOC_CLEAR_ONE(mf_hashitem_T);
|
2017-01-07 07:10:06 -07:00
|
|
|
assert(item != NULL);
|
|
|
|
item->mhi_key = key;
|
|
|
|
mf_hash_add_item(&ht, item);
|
|
|
|
|
|
|
|
assert(mf_hash_find(&ht, key) == item);
|
|
|
|
|
|
|
|
if (ht.mht_mask + 1 != num_buckets)
|
|
|
|
{
|
2023-03-07 19:46:09 -07:00
|
|
|
// hash table was expanded
|
2017-01-07 07:10:06 -07:00
|
|
|
assert(ht.mht_mask + 1 == num_buckets * MHT_GROWTH_FACTOR);
|
|
|
|
assert(i + 1 == (num_buckets << MHT_LOG_LOAD_FACTOR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// check presence of inserted items
|
2017-01-07 07:10:06 -07:00
|
|
|
for (i = 0; i < TEST_COUNT; i++)
|
|
|
|
{
|
|
|
|
key = index_to_key(i);
|
|
|
|
item = mf_hash_find(&ht, key);
|
|
|
|
assert(item != NULL);
|
|
|
|
assert(item->mhi_key == key);
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// delete some items
|
2017-01-07 07:10:06 -07:00
|
|
|
for (i = 0; i < TEST_COUNT; i++)
|
|
|
|
{
|
|
|
|
if (i % 100 < 70)
|
|
|
|
{
|
|
|
|
key = index_to_key(i);
|
|
|
|
item = mf_hash_find(&ht, key);
|
|
|
|
assert(item != NULL);
|
|
|
|
assert(item->mhi_key == key);
|
|
|
|
|
|
|
|
mf_hash_rem_item(&ht, item);
|
|
|
|
assert(mf_hash_find(&ht, key) == NULL);
|
|
|
|
|
|
|
|
mf_hash_add_item(&ht, item);
|
|
|
|
assert(mf_hash_find(&ht, key) == item);
|
|
|
|
|
|
|
|
mf_hash_rem_item(&ht, item);
|
|
|
|
assert(mf_hash_find(&ht, key) == NULL);
|
|
|
|
|
|
|
|
vim_free(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// check again
|
2017-01-07 07:10:06 -07:00
|
|
|
for (i = 0; i < TEST_COUNT; i++)
|
|
|
|
{
|
|
|
|
key = index_to_key(i);
|
|
|
|
item = mf_hash_find(&ht, key);
|
|
|
|
|
|
|
|
if (i % 100 < 70)
|
|
|
|
{
|
|
|
|
assert(item == NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(item != NULL);
|
|
|
|
assert(item->mhi_key == key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:46:09 -07:00
|
|
|
// free hash table and all remaining items
|
2017-01-07 07:10:06 -07:00
|
|
|
mf_hash_free_all(&ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
test_mf_hash();
|
|
|
|
return 0;
|
|
|
|
}
|