You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
4.3 KiB

4 years ago
//! Tests
use super::*;
use std::collections::{
HashMap,
};
#[test]
fn macros()
{
let map1 = smallmap!{
{"One" => 1},
{"Two" => 2},
{"Four" => 40}
};
let map2 = {
let mut map = Map::new();
map.insert("One", 1);
map.insert("Two", 2);
map.insert("Four", 40);
map
};
assert_eq!(map1.len(), 3);
assert_eq!(map1,map2);
}
#[test]
fn is_empty()
{
let map1 = smallmap!{
{"One" => 1},
{"Two" => 2},
{"Four" => 40}
};
let map2: Map<(),()> = Map::new();
assert!(!map1.is_empty());
assert!(map2.is_empty());
}
4 years ago
#[test]
fn it_works()
{
let mut map = Map::new();
map.insert('>', false);
map.insert('<', true);
map.insert('ł', true);
let clone = map.clone();
for (k, v) in clone.into_iter()
{
assert!(map.get(&k) == Some(&v))
}
}
macro_rules! test_insert_type {
($ty:ty, $data:expr) => {
{
print!("Testing {}... ", std::any::type_name::<$ty>());
let mut small = Map::new();
let mut hash = HashMap::new();
for (i,x) in (0..).zip($data)
{
small.insert(x, i);
hash.insert(x, i);
}
assert_eq!(small.len(), hash.len());
for (k,v) in hash.iter()
{
assert_eq!(v, small.get(k).unwrap());
}
println!("OK");
}
};
($ty:tt) => {
test_insert_type!($ty, $ty::MIN..$ty::MAX);
}
}
#[test]
fn type_char()
{
test_insert_type!(char, TEST_STRING.chars());
}
#[test]
fn type_primitive()
{
test_insert_type!(i8, TEST_STRING.chars());
test_insert_type!(u8);
test_insert_type!(i16);
test_insert_type!(u16);
test_insert_type!(i32, -100..1000);
test_insert_type!(u32, 0..10000);
test_insert_type!(u64, -100..1000);
test_insert_type!(i64, 0..10000);
test_insert_type!(u128, -100..1000);
test_insert_type!(i128, 0..10000);
}
#[cfg(nightly)]
mod benchmarks
{
use super::*;
4 years ago
use std::collections::BTreeMap;
4 years ago
use test::{Bencher, black_box};
macro_rules! map_bench {
($b:expr, $map:ident) => {
let mut map = $map::new();
$b.iter(|| {
for chr in TEST_STRING.chars()
{
if let Some(ent) = map.get_mut(&chr) {
*ent += 1;
} else {
black_box(map.insert(chr, 0));
}
}
})
};
}
macro_rules! ent_bench {
($b:expr, $map:ident) => {
let mut map = $map::new();
$b.iter(|| {
for chr in TEST_STRING.chars()
{
black_box(*map.entry(chr).or_insert(0usize) += 1);
}
})
};
}
#[bench]
fn es_char(b: &mut Bencher)
{
ent_bench!(b, Map);
}
#[bench]
fn eh_char(b: &mut Bencher)
{
ent_bench!(b, HashMap);
}
#[bench]
fn eb_char(b: &mut Bencher)
{
ent_bench!(b, BTreeMap);
}
#[bench]
fn s_char(b: &mut Bencher)
{
map_bench!(b, Map);
}
#[bench]
fn h_char(b: &mut Bencher)
{
map_bench!(b, HashMap);
}
#[bench]
fn b_char(b: &mut Bencher)
{
map_bench!(b, BTreeMap);
}
}
const TEST_STRING: &str = r#"
"#;