1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * memfile_test.c: Unittests for memfile.c
12 * Mostly by Ivan Krasilnikov.
18 /* Must include main.c because it contains much more than just main() */
22 /* This file has to be included because the tested functions are static */
25 #define index_to_key(i) ((i) ^ 15167)
26 #define TEST_COUNT 50000
28 static void test_mf_hash __ARGS((void));
31 * Test mf_hash_*() functions.
44 /* insert some items and check invariants */
45 for (i = 0; i < TEST_COUNT; i++)
47 assert(ht.mht_count == i);
49 /* check that number of buckets is a power of 2 */
50 num_buckets = ht.mht_mask + 1;
51 assert(num_buckets > 0 && (num_buckets & (num_buckets - 1)) == 0);
53 /* check load factor */
54 assert(ht.mht_count <= (num_buckets << MHT_LOG_LOAD_FACTOR));
56 if (i < (MHT_INIT_SIZE << MHT_LOG_LOAD_FACTOR))
58 /* first expansion shouldn't have occurred yet */
59 assert(num_buckets == MHT_INIT_SIZE);
60 assert(ht.mht_buckets == ht.mht_small_buckets);
64 assert(num_buckets > MHT_INIT_SIZE);
65 assert(ht.mht_buckets != ht.mht_small_buckets);
68 key = index_to_key(i);
69 assert(mf_hash_find(&ht, key) == NULL);
71 /* allocate and add new item */
72 item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);
75 mf_hash_add_item(&ht, item);
77 assert(mf_hash_find(&ht, key) == item);
79 if (ht.mht_mask + 1 != num_buckets)
81 /* hash table was expanded */
82 assert(ht.mht_mask + 1 == num_buckets * MHT_GROWTH_FACTOR);
83 assert(i + 1 == (num_buckets << MHT_LOG_LOAD_FACTOR));
87 /* check presence of inserted items */
88 for (i = 0; i < TEST_COUNT; i++)
90 key = index_to_key(i);
91 item = mf_hash_find(&ht, key);
93 assert(item->mhi_key == key);
96 /* delete some items */
97 for (i = 0; i < TEST_COUNT; i++)
101 key = index_to_key(i);
102 item = mf_hash_find(&ht, key);
103 assert(item != NULL);
104 assert(item->mhi_key == key);
106 mf_hash_rem_item(&ht, item);
107 assert(mf_hash_find(&ht, key) == NULL);
109 mf_hash_add_item(&ht, item);
110 assert(mf_hash_find(&ht, key) == item);
112 mf_hash_rem_item(&ht, item);
113 assert(mf_hash_find(&ht, key) == NULL);
120 for (i = 0; i < TEST_COUNT; i++)
122 key = index_to_key(i);
123 item = mf_hash_find(&ht, key);
127 assert(item == NULL);
131 assert(item != NULL);
132 assert(item->mhi_key == key);
136 /* free hash table and all remaining items */
137 mf_hash_free_all(&ht);