2
2
3
3
use std;
4
4
import std. map ;
5
+ import std. util ;
5
6
6
7
fn test_simple ( ) {
7
8
log "*** starting test_simple" ;
@@ -17,18 +18,18 @@ fn test_simple() {
17
18
let map. eqfn[ uint] eqer = eq;
18
19
let map. hashmap[ uint, uint] hm = map. mk_hashmap [ uint, uint] ( hasher, eqer) ;
19
20
20
- hm. insert ( 10 u, 12 u) ;
21
- hm. insert ( 11 u, 13 u) ;
22
- hm. insert ( 12 u, 14 u) ;
21
+ check ( hm. insert ( 10 u, 12 u) ) ;
22
+ check ( hm. insert ( 11 u, 13 u) ) ;
23
+ check ( hm. insert ( 12 u, 14 u) ) ;
23
24
24
25
check ( hm. get ( 11 u) == 13 u) ;
25
26
check ( hm. get ( 12 u) == 14 u) ;
26
27
check ( hm. get ( 10 u) == 12 u) ;
27
28
28
- hm. insert ( 12 u, 14 u) ;
29
+ check ( ! hm. insert ( 12 u, 14 u) ) ;
29
30
check ( hm. get ( 12 u) == 14 u) ;
30
31
31
- hm. insert ( 12 u, 12 u) ;
32
+ check ( ! hm. insert ( 12 u, 12 u) ) ;
32
33
check ( hm. get ( 12 u) == 12 u) ;
33
34
34
35
log "*** finished test_simple" ;
@@ -55,7 +56,7 @@ fn test_growth() {
55
56
56
57
let uint i = 0 u;
57
58
while ( i < num_to_insert) {
58
- hm. insert ( i, i * i) ;
59
+ check ( hm. insert ( i, i * i) ) ;
59
60
log "inserting " + std. _uint . to_str ( i, 10 u)
60
61
+ " -> " + std. _uint . to_str ( i * i, 10 u) ;
61
62
i += 1 u;
@@ -71,7 +72,7 @@ fn test_growth() {
71
72
i += 1u;
72
73
}
73
74
74
- hm.insert(num_to_insert, 17u);
75
+ check ( hm.insert(num_to_insert, 17u) );
75
76
check (hm.get(num_to_insert) == 17u);
76
77
77
78
log " -----";
@@ -89,7 +90,128 @@ fn test_growth() {
89
90
log " * * * finished test_growth";
90
91
}
91
92
93
+ fn test_removal ( ) {
94
+ log "*** starting test_removal" ;
95
+
96
+ let uint num_to_insert = 64 u;
97
+
98
+ fn eq ( & uint x, & uint y) -> bool { ret x == y; }
99
+ fn hash ( & uint u) -> uint {
100
+ // This hash function intentionally causes collisions between
101
+ // consecutive integer pairs.
102
+ ret ( u / 2 u) * 2 u;
103
+ }
104
+
105
+ let map. hashfn[ uint] hasher = hash;
106
+ let map. eqfn[ uint] eqer = eq;
107
+ let map. hashmap[ uint, uint] hm = map. mk_hashmap [ uint, uint] ( hasher, eqer) ;
108
+
109
+ let uint i = 0 u;
110
+ while ( i < num_to_insert) {
111
+ check ( hm. insert ( i, i * i) ) ;
112
+ log "inserting " + std. _uint . to_str ( i, 10 u)
113
+ + " -> " + std. _uint . to_str ( i * i, 10 u) ;
114
+ i += 1 u;
115
+ }
116
+
117
+ check ( hm. size ( ) == num_to_insert) ;
118
+
119
+ log "-----";
120
+ log " removing evens";
121
+
122
+ i = 0 u;
123
+ while ( i < num_to_insert) {
124
+ /**
125
+ * FIXME (issue #150): we want to check the removed value as in the
126
+ * following:
127
+
128
+ let util.option[uint] v = hm.remove(i);
129
+ alt (v) {
130
+ case (util.some[uint](u)) {
131
+ check (u == (i * i));
132
+ }
133
+ case (util.none[uint]()) { fail; }
134
+ }
135
+
136
+ * but we util.option is a tag type so util.some and util.none are
137
+ * off limits until we parse the dwarf for tag types.
138
+ */
139
+
140
+ hm. remove ( i) ;
141
+ i += 2 u;
142
+ }
143
+
144
+ check ( hm. size ( ) == ( num_to_insert / 2 u) ) ;
145
+
146
+ log "-----";
147
+
148
+ i = 1u;
149
+ while (i < num_to_insert) {
150
+ log " get( " + std._uint.to_str(i, 10u) + " ) = "
151
+ + std._uint.to_str(hm.get(i), 10u);
152
+ check (hm.get(i) == i * i);
153
+ i += 2u;
154
+ }
155
+
156
+ log " -----";
157
+ log " rehashing";
158
+
159
+ hm. rehash ( ) ;
160
+
161
+ log "-----";
162
+
163
+ i = 1u;
164
+ while (i < num_to_insert) {
165
+ log " get( " + std._uint.to_str(i, 10u) + " ) = "
166
+ + std._uint.to_str(hm.get(i), 10u);
167
+ check (hm.get(i) == i * i);
168
+ i += 2u;
169
+ }
170
+
171
+ log " -----";
172
+
173
+ i = 0u;
174
+ while (i < num_to_insert) {
175
+ check (hm.insert(i, i * i));
176
+ log " inserting " + std._uint.to_str(i, 10u)
177
+ + " -> " + std._uint.to_str(i * i, 10u);
178
+ i += 2u;
179
+ }
180
+
181
+ check (hm.size() == num_to_insert);
182
+
183
+ log " -----";
184
+
185
+ i = 0u;
186
+ while (i < num_to_insert) {
187
+ log " get( " + std._uint.to_str(i, 10u) + " ) = "
188
+ + std._uint.to_str(hm.get(i), 10u);
189
+ check (hm.get(i) == i * i);
190
+ i += 1u;
191
+ }
192
+
193
+ log " -----";
194
+ log " rehashing";
195
+
196
+ hm. rehash ( ) ;
197
+
198
+ log "-----";
199
+
200
+ check (hm.size() == num_to_insert);
201
+
202
+ i = 0u;
203
+ while (i < num_to_insert) {
204
+ log " get( " + std._uint.to_str(i, 10u) + " ) = "
205
+ + std._uint.to_str(hm.get(i), 10u);
206
+ check (hm.get(i) == i * i);
207
+ i += 1u;
208
+ }
209
+
210
+ log " * * * finished test_removal";
211
+ }
212
+
92
213
fn main ( ) {
93
214
test_simple ( ) ;
94
215
test_growth ( ) ;
216
+ test_removal ( ) ;
95
217
}
0 commit comments