2010-08-03 20:43:57 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
|
|
|
use std;
|
|
|
|
import std.map;
|
2010-08-26 21:44:38 -05:00
|
|
|
import std.util;
|
2010-08-03 20:43:57 -05:00
|
|
|
|
|
|
|
fn test_simple() {
|
2010-08-05 01:09:25 -05:00
|
|
|
log "*** starting test_simple";
|
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn eq(&uint x, &uint y) -> bool { ret x == y; }
|
2010-08-24 19:23:09 -05:00
|
|
|
fn hash(&uint u) -> uint {
|
|
|
|
// FIXME: can't use std.util.id since we'd be capturing a type param,
|
|
|
|
// and presently we can't close items over type params.
|
|
|
|
ret u;
|
|
|
|
}
|
2010-08-03 20:43:57 -05:00
|
|
|
|
2010-08-26 16:44:03 -05:00
|
|
|
let map.hashfn[uint] hasher = hash;
|
|
|
|
let map.eqfn[uint] eqer = eq;
|
2010-08-03 20:43:57 -05:00
|
|
|
let map.hashmap[uint, uint] hm = map.mk_hashmap[uint, uint](hasher, eqer);
|
2010-08-26 15:03:28 -05:00
|
|
|
|
2010-08-26 21:44:38 -05:00
|
|
|
check (hm.insert(10u, 12u));
|
|
|
|
check (hm.insert(11u, 13u));
|
|
|
|
check (hm.insert(12u, 14u));
|
2010-08-24 19:38:04 -05:00
|
|
|
|
|
|
|
check (hm.get(11u) == 13u);
|
|
|
|
check (hm.get(12u) == 14u);
|
2010-08-25 13:08:37 -05:00
|
|
|
check (hm.get(10u) == 12u);
|
2010-08-24 19:38:04 -05:00
|
|
|
|
2010-08-26 21:44:38 -05:00
|
|
|
check (!hm.insert(12u, 14u));
|
2010-08-26 15:03:28 -05:00
|
|
|
check (hm.get(12u) == 14u);
|
|
|
|
|
2010-08-26 21:44:38 -05:00
|
|
|
check (!hm.insert(12u, 12u));
|
2010-08-26 15:03:28 -05:00
|
|
|
check (hm.get(12u) == 12u);
|
|
|
|
|
2010-08-05 01:09:25 -05:00
|
|
|
log "*** finished test_simple";
|
2010-08-03 20:43:57 -05:00
|
|
|
}
|
|
|
|
|
2010-08-26 15:03:28 -05:00
|
|
|
/**
|
|
|
|
* Force map growth and rehashing.
|
|
|
|
*/
|
|
|
|
fn test_growth() {
|
|
|
|
log "*** starting test_growth";
|
|
|
|
|
2010-08-26 16:44:03 -05:00
|
|
|
let uint num_to_insert = 64u;
|
2010-08-26 15:03:28 -05:00
|
|
|
|
|
|
|
fn eq(&uint x, &uint y) -> bool { ret x == y; }
|
|
|
|
fn hash(&uint u) -> uint {
|
|
|
|
// FIXME: can't use std.util.id since we'd be capturing a type param,
|
|
|
|
// and presently we can't close items over type params.
|
|
|
|
ret u;
|
|
|
|
}
|
|
|
|
|
2010-08-26 16:44:03 -05:00
|
|
|
let map.hashfn[uint] hasher = hash;
|
|
|
|
let map.eqfn[uint] eqer = eq;
|
2010-08-26 15:03:28 -05:00
|
|
|
let map.hashmap[uint, uint] hm = map.mk_hashmap[uint, uint](hasher, eqer);
|
|
|
|
|
|
|
|
let uint i = 0u;
|
2010-08-26 16:44:03 -05:00
|
|
|
while (i < num_to_insert) {
|
2010-08-26 21:44:38 -05:00
|
|
|
check (hm.insert(i, i * i));
|
2010-08-26 15:03:28 -05:00
|
|
|
log "inserting " + std._uint.to_str(i, 10u)
|
|
|
|
+ " -> " + std._uint.to_str(i * i, 10u);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
i = 0u;
|
2010-08-26 16:44:03 -05:00
|
|
|
while (i < num_to_insert) {
|
2010-08-26 15:03:28 -05:00
|
|
|
log "get(" + std._uint.to_str(i, 10u) + ") = "
|
|
|
|
+ std._uint.to_str(hm.get(i), 10u);
|
|
|
|
check (hm.get(i) == i * i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
2010-08-26 21:44:38 -05:00
|
|
|
check (hm.insert(num_to_insert, 17u));
|
2010-08-26 16:44:03 -05:00
|
|
|
check (hm.get(num_to_insert) == 17u);
|
2010-08-26 15:03:28 -05:00
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
hm.rehash();
|
|
|
|
|
|
|
|
i = 0u;
|
2010-08-26 16:44:03 -05:00
|
|
|
while (i < num_to_insert) {
|
2010-08-26 15:03:28 -05:00
|
|
|
log "get(" + std._uint.to_str(i, 10u) + ") = "
|
|
|
|
+ std._uint.to_str(hm.get(i), 10u);
|
|
|
|
check (hm.get(i) == i * i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "*** finished test_growth";
|
|
|
|
}
|
|
|
|
|
2010-08-26 21:44:38 -05:00
|
|
|
fn test_removal() {
|
|
|
|
log "*** starting test_removal";
|
|
|
|
|
|
|
|
let uint num_to_insert = 64u;
|
|
|
|
|
|
|
|
fn eq(&uint x, &uint y) -> bool { ret x == y; }
|
|
|
|
fn hash(&uint u) -> uint {
|
|
|
|
// This hash function intentionally causes collisions between
|
|
|
|
// consecutive integer pairs.
|
|
|
|
ret (u / 2u) * 2u;
|
|
|
|
}
|
|
|
|
|
2010-08-26 22:02:07 -05:00
|
|
|
check (hash(0u) == hash(1u));
|
|
|
|
check (hash(2u) == hash(3u));
|
|
|
|
check (hash(0u) != hash(2u));
|
|
|
|
|
2010-08-26 21:44:38 -05:00
|
|
|
let map.hashfn[uint] hasher = hash;
|
|
|
|
let map.eqfn[uint] eqer = eq;
|
|
|
|
let map.hashmap[uint, uint] hm = map.mk_hashmap[uint, uint](hasher, eqer);
|
|
|
|
|
|
|
|
let uint i = 0u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
check (hm.insert(i, i * i));
|
|
|
|
log "inserting " + std._uint.to_str(i, 10u)
|
|
|
|
+ " -> " + std._uint.to_str(i * i, 10u);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
check (hm.size() == num_to_insert);
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
log "removing evens";
|
|
|
|
|
|
|
|
i = 0u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
/**
|
|
|
|
* FIXME (issue #150): we want to check the removed value as in the
|
|
|
|
* following:
|
|
|
|
|
|
|
|
let util.option[uint] v = hm.remove(i);
|
|
|
|
alt (v) {
|
|
|
|
case (util.some[uint](u)) {
|
|
|
|
check (u == (i * i));
|
|
|
|
}
|
|
|
|
case (util.none[uint]()) { fail; }
|
|
|
|
}
|
|
|
|
|
|
|
|
* but we util.option is a tag type so util.some and util.none are
|
|
|
|
* off limits until we parse the dwarf for tag types.
|
|
|
|
*/
|
|
|
|
|
|
|
|
hm.remove(i);
|
|
|
|
i += 2u;
|
|
|
|
}
|
|
|
|
|
|
|
|
check (hm.size() == (num_to_insert / 2u));
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
i = 1u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
log "get(" + std._uint.to_str(i, 10u) + ") = "
|
|
|
|
+ std._uint.to_str(hm.get(i), 10u);
|
|
|
|
check (hm.get(i) == i * i);
|
|
|
|
i += 2u;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
log "rehashing";
|
|
|
|
|
|
|
|
hm.rehash();
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
i = 1u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
log "get(" + std._uint.to_str(i, 10u) + ") = "
|
|
|
|
+ std._uint.to_str(hm.get(i), 10u);
|
|
|
|
check (hm.get(i) == i * i);
|
|
|
|
i += 2u;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
i = 0u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
check (hm.insert(i, i * i));
|
|
|
|
log "inserting " + std._uint.to_str(i, 10u)
|
|
|
|
+ " -> " + std._uint.to_str(i * i, 10u);
|
|
|
|
i += 2u;
|
|
|
|
}
|
|
|
|
|
|
|
|
check (hm.size() == num_to_insert);
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
i = 0u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
log "get(" + std._uint.to_str(i, 10u) + ") = "
|
|
|
|
+ std._uint.to_str(hm.get(i), 10u);
|
|
|
|
check (hm.get(i) == i * i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
log "rehashing";
|
|
|
|
|
|
|
|
hm.rehash();
|
|
|
|
|
|
|
|
log "-----";
|
|
|
|
|
|
|
|
check (hm.size() == num_to_insert);
|
|
|
|
|
|
|
|
i = 0u;
|
|
|
|
while (i < num_to_insert) {
|
|
|
|
log "get(" + std._uint.to_str(i, 10u) + ") = "
|
|
|
|
+ std._uint.to_str(hm.get(i), 10u);
|
|
|
|
check (hm.get(i) == i * i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
log "*** finished test_removal";
|
|
|
|
}
|
|
|
|
|
2010-08-03 20:43:57 -05:00
|
|
|
fn main() {
|
|
|
|
test_simple();
|
2010-08-26 15:03:28 -05:00
|
|
|
test_growth();
|
2010-08-26 21:44:38 -05:00
|
|
|
test_removal();
|
2010-08-05 01:09:25 -05:00
|
|
|
}
|