robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
353a4be7e8
master
1/* vi:set ts=8 sts=4 sw=4 noet: 2* 3* VIM - Vi IMproved by Bram Moolenaar 4* 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. 8*/ 9 10/* 11* memfile_test.c: Unittests for memfile.c 12* Mostly by Ivan Krasilnikov. 13*/ 14 15#undef NDEBUG 16#include <assert.h> 17 18// Must include main.c because it contains much more than just main() 19#define NO_VIM_MAIN 20#include "main.c" 21 22// This file has to be included because the tested functions are static 23#include "memfile.c" 24 25#define index_to_key (i ) ((i) ^ 15167) 26#define TEST_COUNT 50000 27 28/* 29* Test mf_hash_*() functions. 30*/ 31static void 32test_mf_hash (void ) 33{ 34mf_hashtab_T ht ; 35mf_hashitem_T * item ; 36blocknr_T key ; 37long_u i ; 38long_u num_buckets ; 39 40mf_hash_init (& ht ); 41 42// insert some items and check invariants 43for (i = 0 ;i < TEST_COUNT ;i ++ ) 44 { 45assert (ht .mht_count == i ); 46 47// check that number of buckets is a power of 2 48num_buckets = ht .mht_mask + 1 ; 49assert (num_buckets > 0 && (num_buckets & (num_buckets - 1 ))== 0 ); 50 51// check load factor 52assert (ht .mht_count <= (num_buckets <<MHT_LOG_LOAD_FACTOR )); 53 54if (i < (MHT_INIT_SIZE <<MHT_LOG_LOAD_FACTOR )) 55 { 56// first expansion shouldn't have occurred yet 57assert (num_buckets == MHT_INIT_SIZE ); 58assert (ht .mht_buckets == ht .mht_small_buckets ); 59 } 60else 61 { 62assert (num_buckets > MHT_INIT_SIZE ); 63assert (ht .mht_buckets != ht .mht_small_buckets ); 64 } 65 66key = index_to_key (i ); 67assert (mf_hash_find (& ht ,key )== NULL ); 68 69// allocate and add new item 70item = LALLOC_CLEAR_ONE (mf_hashitem_T ); 71assert (item != NULL ); 72item -> mhi_key = key ; 73mf_hash_add_item (& ht ,item ); 74 75assert (mf_hash_find (& ht ,key )== item ); 76 77if (ht .mht_mask + 1 != num_buckets ) 78 { 79// hash table was expanded 80assert (ht .mht_mask + 1 == num_buckets * MHT_GROWTH_FACTOR ); 81assert (i + 1 == (num_buckets <<MHT_LOG_LOAD_FACTOR )); 82 } 83 } 84 85// check presence of inserted items 86for (i = 0 ;i < TEST_COUNT ;i ++ ) 87 { 88key = index_to_key (i ); 89item = mf_hash_find (& ht ,key ); 90assert (item != NULL ); 91assert (item -> mhi_key == key ); 92 } 93 94// delete some items 95for (i = 0 ;i < TEST_COUNT ;i ++ ) 96 { 97if (i %100 < 70 ) 98 { 99key = index_to_key (i ); 100item = mf_hash_find (& ht ,key ); 101assert (item != NULL ); 102assert (item -> mhi_key == key ); 103 104mf_hash_rem_item (& ht ,item ); 105assert (mf_hash_find (& ht ,key )== NULL ); 106 107mf_hash_add_item (& ht ,item ); 108assert (mf_hash_find (& ht ,key )== item ); 109 110mf_hash_rem_item (& ht ,item ); 111assert (mf_hash_find (& ht ,key )== NULL ); 112 113vim_free (item ); 114 } 115 } 116 117// check again 118for (i = 0 ;i < TEST_COUNT ;i ++ ) 119 { 120key = index_to_key (i ); 121item = mf_hash_find (& ht ,key ); 122 123if (i %100 < 70 ) 124 { 125assert (item == NULL ); 126 } 127else 128 { 129assert (item != NULL ); 130assert (item -> mhi_key == key ); 131 } 132 } 133 134// free hash table and all remaining items 135mf_hash_free_all (& ht ); 136} 137 138int 139main (void ) 140{ 141test_mf_hash (); 142return 0 ; 143}