Skip to content

Commit 7b13ef7

Browse files
killerswanbrson
authored andcommitted
Test insert_with_key...
1 parent a343e43 commit 7b13ef7

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/libstd/map.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,4 +773,25 @@ mod tests {
773773
assert map.get(~"b") == 2;
774774
assert map.get(~"c") == 3;
775775
}
776+
777+
#[test]
778+
fn test_insert_with_key() {
779+
let map = map::HashMap::<~str, uint>();
780+
781+
fn inc(k: ~str, v0: uint, v1: uint) -> uint {
782+
v0 + v1
783+
}
784+
785+
map.insert_with_key(inc, ~"cat", 1);
786+
map.insert_with_key(inc, ~"mongoose", 1);
787+
map.insert_with_key(inc, ~"cat", 7);
788+
map.insert_with_key(inc, ~"ferret", 3);
789+
map.insert_with_key(inc, ~"cat", 2);
790+
791+
assert 10 == option::get(map.find(~"cat"));
792+
assert 3 == option::get(map.find(~"ferret"));
793+
assert 1 == option::get(map.find(~"mongoose"));
794+
795+
assert None == map.find(~"unicorn");
796+
}
776797
}

src/test/bench/shootout-k-nucleotide-pipes.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ fn find(mm: HashMap<~[u8], uint>, key: ~str) -> uint {
6969
// given a map, increment the counter for a key
7070
fn update_freq(mm: HashMap<~[u8], uint>, key: &[u8]) {
7171
let key = vec::slice(key, 0, key.len());
72-
match mm.find(key) {
73-
option::None => { mm.insert(key, 1u ); }
74-
option::Some(val) => { mm.insert(key, 1u + val); }
75-
}
72+
mm.insert_with_key(|k,v,v1| {v + v1}, key, 1);
7673
}
7774

7875
// given a ~[u8], for each window call a function

src/test/bench/shootout-k-nucleotide.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ fn find(mm: HashMap<~[u8], uint>, key: ~str) -> uint {
6666
// given a map, increment the counter for a key
6767
fn update_freq(mm: HashMap<~[u8], uint>, key: &[u8]) {
6868
let key = vec::slice(key, 0, key.len());
69-
match mm.find(key) {
70-
option::None => { mm.insert(key, 1u ); }
71-
option::Some(val) => { mm.insert(key, 1u + val); }
72-
}
69+
mm.insert_with_key(|k,v,v1| {v + v1}, key, 1);
7370
}
7471

7572
// given a ~[u8], for each window call a function

0 commit comments

Comments
 (0)