Mercurial > vim
annotate src/memfile_test.c @ 3835:9963f7a9b41b v7.3.675
updated for version 7.3.675
Problem: Using uninitialized memory with very long file name.
Solution: Put NUL after text when it is truncated. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 03 Oct 2012 17:12:47 +0200 |
parents | afb476746692 |
children | 37c929c4a073 |
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 static void test_mf_hash __ARGS((void)); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
29 |
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 * Test mf_hash_*() functions. |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
32 */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
33 static void |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
34 test_mf_hash() |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
35 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
36 mf_hashtab_T ht; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
37 mf_hashitem_T *item; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
38 blocknr_T key; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
39 long_u i; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
40 long_u num_buckets; |
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 mf_hash_init(&ht); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
43 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
44 /* insert some items and check invariants */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
45 for (i = 0; i < TEST_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 assert(ht.mht_count == i); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
48 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
49 /* 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
|
50 num_buckets = ht.mht_mask + 1; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
51 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
|
52 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
53 /* check load factor */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
54 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
|
55 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
56 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
|
57 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
58 /* first expansion shouldn't have occurred yet */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
59 assert(num_buckets == MHT_INIT_SIZE); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
60 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
|
61 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
62 else |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
63 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
64 assert(num_buckets > MHT_INIT_SIZE); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
65 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
|
66 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
67 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
68 key = index_to_key(i); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
69 assert(mf_hash_find(&ht, key) == NULL); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
70 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
71 /* allocate and add new item */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
72 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
|
73 assert(item != NULL); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
74 item->mhi_key = key; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
75 mf_hash_add_item(&ht, 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 assert(mf_hash_find(&ht, key) == item); |
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 if (ht.mht_mask + 1 != num_buckets) |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
80 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
81 /* hash table was expanded */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
82 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
|
83 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
|
84 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
85 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
86 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
87 /* check presence of inserted items */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
88 for (i = 0; i < TEST_COUNT; i++) |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
89 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
90 key = index_to_key(i); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
91 item = mf_hash_find(&ht, key); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
92 assert(item != NULL); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
93 assert(item->mhi_key == key); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
94 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
95 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
96 /* delete some items */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
97 for (i = 0; i < TEST_COUNT; i++) |
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 if (i % 100 < 70) |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
100 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
101 key = index_to_key(i); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
102 item = mf_hash_find(&ht, key); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
103 assert(item != NULL); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
104 assert(item->mhi_key == key); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
105 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
106 mf_hash_rem_item(&ht, item); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
107 assert(mf_hash_find(&ht, key) == NULL); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
108 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
109 mf_hash_add_item(&ht, item); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
110 assert(mf_hash_find(&ht, key) == item); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
111 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
112 mf_hash_rem_item(&ht, item); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
113 assert(mf_hash_find(&ht, key) == NULL); |
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 vim_free(item); |
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 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
118 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
119 /* check again */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
120 for (i = 0; i < TEST_COUNT; i++) |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
121 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
122 key = index_to_key(i); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
123 item = mf_hash_find(&ht, key); |
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 if (i % 100 < 70) |
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 assert(item == NULL); |
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 else |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
130 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
131 assert(item != NULL); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
132 assert(item->mhi_key == key); |
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 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
135 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
136 /* free hash table and all remaining items */ |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
137 mf_hash_free_all(&ht); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
138 } |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
139 |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
140 int |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
141 main() |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
142 { |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
143 test_mf_hash(); |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
144 return 0; |
afb476746692
Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
145 } |