annotate src/memfile_test.c @ 8581:fd454847836d v7.4.1580

commit https://github.com/vim/vim/commit/7a5c46a9df7ef01a4f6a620861c35400d5ad28d9 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Mar 16 20:41:21 2016 +0100 patch 7.4.1580 Problem: Crash when using function reference. (Luchr) Solution: Set initial refcount. (Ken Takata, closes https://github.com/vim/vim/issues/690)
author Christian Brabandt <cb@256bit.org>
date Wed, 16 Mar 2016 20:45:05 +0100
parents 98a96e0ca73b
children 4aead6a9b7a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 *
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
4 *
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 /*
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 * memfile_test.c: Unittests for memfile.c
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 * Mostly by Ivan Krasilnikov.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
15 #undef NDEBUG
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
16 #include <assert.h>
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
17
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18 /* Must include main.c because it contains much more than just main() */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 #define NO_VIM_MAIN
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 #include "main.c"
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 /* This file has to be included because the tested functions are static */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 #include "memfile.c"
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 #define index_to_key(i) ((i) ^ 15167)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 #define TEST_COUNT 50000
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 /*
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 * Test mf_hash_*() functions.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 static void
7827
41789f16d6b2 commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents: 7803
diff changeset
32 test_mf_hash(void)
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 mf_hashtab_T ht;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 mf_hashitem_T *item;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 blocknr_T key;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 long_u i;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 long_u num_buckets;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 mf_hash_init(&ht);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42 /* insert some items and check invariants */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45 assert(ht.mht_count == i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 /* check that number of buckets is a power of 2 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 num_buckets = ht.mht_mask + 1;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
49 assert(num_buckets > 0 && (num_buckets & (num_buckets - 1)) == 0);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51 /* check load factor */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 assert(ht.mht_count <= (num_buckets << MHT_LOG_LOAD_FACTOR));
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 if (i < (MHT_INIT_SIZE << MHT_LOG_LOAD_FACTOR))
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
56 /* first expansion shouldn't have occurred yet */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 assert(num_buckets == MHT_INIT_SIZE);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 assert(ht.mht_buckets == ht.mht_small_buckets);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 else
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 assert(num_buckets > MHT_INIT_SIZE);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63 assert(ht.mht_buckets != ht.mht_small_buckets);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67 assert(mf_hash_find(&ht, key) == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 /* allocate and add new item */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 item->mhi_key = key;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73 mf_hash_add_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75 assert(mf_hash_find(&ht, key) == item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 if (ht.mht_mask + 1 != num_buckets)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 /* hash table was expanded */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 assert(ht.mht_mask + 1 == num_buckets * MHT_GROWTH_FACTOR);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 assert(i + 1 == (num_buckets << MHT_LOG_LOAD_FACTOR));
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 /* check presence of inserted items */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 item = mf_hash_find(&ht, key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 assert(item->mhi_key == key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 /* delete some items */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 if (i % 100 < 70)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 item = mf_hash_find(&ht, key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 assert(item->mhi_key == key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 mf_hash_rem_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 assert(mf_hash_find(&ht, key) == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 mf_hash_add_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 assert(mf_hash_find(&ht, key) == item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 mf_hash_rem_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 assert(mf_hash_find(&ht, key) == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 vim_free(item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 /* check again */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 item = mf_hash_find(&ht, key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
122
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
123 if (i % 100 < 70)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
124 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 assert(item == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 else
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 assert(item->mhi_key == key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
134 /* free hash table and all remaining items */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135 mf_hash_free_all(&ht);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138 int
7827
41789f16d6b2 commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents: 7803
diff changeset
139 main(void)
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141 test_mf_hash();
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
142 return 0;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 }