mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Move util crate unit tests into a new unit_tests crate
This commit is contained in:
parent
3fb666cf60
commit
6d5406efc1
13 changed files with 222 additions and 183 deletions
|
@ -12,8 +12,6 @@ use rand;
|
|||
use std::slice::Iter;
|
||||
use std::default::Default;
|
||||
|
||||
#[cfg(test)]
|
||||
use std::cell::Cell;
|
||||
|
||||
pub struct HashCache<K, V> {
|
||||
entries: HashMap<K, V, DefaultState<SipHasher>>,
|
||||
|
@ -56,19 +54,6 @@ impl<K, V> HashCache<K,V>
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hashcache() {
|
||||
let mut cache: HashCache<usize, Cell<&str>> = HashCache::new();
|
||||
|
||||
cache.insert(1, Cell::new("one"));
|
||||
assert!(cache.find(&1).is_some());
|
||||
assert!(cache.find(&2).is_none());
|
||||
|
||||
cache.find_or_create(&2, |_v| { Cell::new("two") });
|
||||
assert!(cache.find(&1).is_some());
|
||||
assert!(cache.find(&2).is_some());
|
||||
}
|
||||
|
||||
pub struct LRUCache<K, V> {
|
||||
entries: Vec<(K, V)>,
|
||||
cache_size: usize,
|
||||
|
@ -183,37 +168,3 @@ impl<K:Clone+Eq+Hash,V:Clone> SimpleHashCache<K,V> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lru_cache() {
|
||||
let one = Cell::new("one");
|
||||
let two = Cell::new("two");
|
||||
let three = Cell::new("three");
|
||||
let four = Cell::new("four");
|
||||
|
||||
// Test normal insertion.
|
||||
let mut cache: LRUCache<usize,Cell<&str>> = LRUCache::new(2); // (_, _) (cache is empty)
|
||||
cache.insert(1, one); // (1, _)
|
||||
cache.insert(2, two); // (1, 2)
|
||||
cache.insert(3, three); // (2, 3)
|
||||
|
||||
assert!(cache.find(&1).is_none()); // (2, 3) (no change)
|
||||
assert!(cache.find(&3).is_some()); // (2, 3)
|
||||
assert!(cache.find(&2).is_some()); // (3, 2)
|
||||
|
||||
// Test that LRU works (this insertion should replace 3, not 2).
|
||||
cache.insert(4, four); // (2, 4)
|
||||
|
||||
assert!(cache.find(&1).is_none()); // (2, 4) (no change)
|
||||
assert!(cache.find(&2).is_some()); // (4, 2)
|
||||
assert!(cache.find(&3).is_none()); // (4, 2) (no change)
|
||||
assert!(cache.find(&4).is_some()); // (2, 4) (no change)
|
||||
|
||||
// Test find_or_create.
|
||||
cache.find_or_create(&1, |_| { Cell::new("one") }); // (4, 1)
|
||||
|
||||
assert!(cache.find(&1).is_some()); // (4, 1) (no change)
|
||||
assert!(cache.find(&2).is_none()); // (4, 1) (no change)
|
||||
assert!(cache.find(&3).is_none()); // (4, 1) (no change)
|
||||
assert!(cache.find(&4).is_some()); // (1, 4)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue