@@ -13,9 +13,13 @@ fn test_simple() {
13
13
ret u;
14
14
}
15
15
16
- let map. hashfn[ uint] hasher = hash;
17
- let map. eqfn[ uint] eqer = eq;
16
+ // FIXME we don't really want to bind here but if we don't then the
17
+ // hashmap's drop glue UMRs when trying to drop these functions, which
18
+ // it stores internally.
19
+ let map. hashfn[ uint] hasher = bind hash ( _) ;
20
+ let map. eqfn[ uint] eqer = bind eq( _, _) ;
18
21
let map. hashmap[ uint, uint] hm = map. mk_hashmap [ uint, uint] ( hasher, eqer) ;
22
+
19
23
hm. insert ( 10 u, 12 u) ;
20
24
hm. insert ( 11 u, 13 u) ;
21
25
hm. insert ( 12 u, 14 u) ;
@@ -24,9 +28,72 @@ fn test_simple() {
24
28
check ( hm. get ( 12 u) == 14 u) ;
25
29
check ( hm. get ( 10 u) == 12 u) ;
26
30
31
+ hm. insert ( 12 u, 14 u) ;
32
+ check ( hm. get ( 12 u) == 14 u) ;
33
+
34
+ hm. insert ( 12 u, 12 u) ;
35
+ check ( hm. get ( 12 u) == 12 u) ;
36
+
27
37
log "*** finished test_simple" ;
28
38
}
29
39
40
+ /**
41
+ * Force map growth and rehashing.
42
+ */
43
+ fn test_growth ( ) {
44
+ log "*** starting test_growth" ;
45
+
46
+ let uint map_capacity = 64 u; // Keep in sync with map.mk_hashmap
47
+
48
+ fn eq ( & uint x, & uint y) -> bool { ret x == y; }
49
+ fn hash ( & uint u) -> uint {
50
+ // FIXME: can't use std.util.id since we'd be capturing a type param,
51
+ // and presently we can't close items over type params.
52
+ ret u;
53
+ }
54
+
55
+ // FIXME: as in test_simple(), don't really want to bind.
56
+ let map. hashfn[ uint] hasher = bind hash ( _) ;
57
+ let map. eqfn[ uint] eqer = bind eq( _, _) ;
58
+ let map. hashmap[ uint, uint] hm = map. mk_hashmap [ uint, uint] ( hasher, eqer) ;
59
+
60
+ let uint i = 0 u;
61
+ while ( i < map_capacity) {
62
+ hm. insert ( i, i * i) ;
63
+ log "inserting " + std. _uint . to_str ( i, 10 u)
64
+ + " -> " + std. _uint . to_str ( i * i, 10 u) ;
65
+ i += 1 u;
66
+ }
67
+
68
+ log "-----";
69
+
70
+ i = 0u;
71
+ while (i < map_capacity) {
72
+ log " get( " + std._uint.to_str(i, 10u) + " ) = "
73
+ + std._uint.to_str(hm.get(i), 10u);
74
+ check (hm.get(i) == i * i);
75
+ i += 1u;
76
+ }
77
+
78
+ hm.insert(map_capacity, 17u);
79
+ check (hm.get(map_capacity) == 17u);
80
+
81
+ log " -----";
82
+
83
+ hm.rehash();
84
+
85
+ i = 0u;
86
+ while (i < map_capacity) {
87
+ log " get( " + std._uint.to_str(i, 10u) + " ) = "
88
+ + std._uint.to_str(hm.get(i), 10u);
89
+ check (hm.get(i) == i * i);
90
+ i += 1u;
91
+ }
92
+
93
+ log " * * * finished test_growth";
94
+ }
95
+
30
96
fn main ( ) {
31
97
test_simple ( ) ;
98
+ test_growth ( ) ;
32
99
}
0 commit comments