diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index acf5eb43377..c42dc862a73 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -16,6 +16,7 @@ dependencies = [ "profile 0.0.1", "script 0.0.1", "time 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", + "unit_tests 0.0.1", "url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "util 0.0.1", "webdriver_server 0.0.1", @@ -920,6 +921,14 @@ name = "unicase" version = "0.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unit_tests" +version = "0.0.1" +dependencies = [ + "geom 0.1.0 (git+https://github.com/servo/rust-geom)", + "util 0.0.1", +] + [[package]] name = "url" version = "0.2.23" diff --git a/components/servo/Cargo.toml b/components/servo/Cargo.toml index a72401d3ad5..37644696810 100644 --- a/components/servo/Cargo.toml +++ b/components/servo/Cargo.toml @@ -16,6 +16,9 @@ test = false doc = false bench = false +[dev-dependencies.unit_tests] +path = "../../tests/unit" + [[test]] name = "reftest" path = "../../tests/reftest.rs" diff --git a/components/util/cache.rs b/components/util/cache.rs index 8719c176921..26eab254ccf 100644 --- a/components/util/cache.rs +++ b/components/util/cache.rs @@ -12,8 +12,6 @@ use rand; use std::slice::Iter; use std::default::Default; -#[cfg(test)] -use std::cell::Cell; pub struct HashCache { entries: HashMap>, @@ -56,19 +54,6 @@ impl HashCache } } -#[test] -fn test_hashcache() { - let mut cache: HashCache> = 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 { entries: Vec<(K, V)>, cache_size: usize, @@ -183,37 +168,3 @@ impl SimpleHashCache { } } } - -#[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> = 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) -} diff --git a/components/util/logical_geometry.rs b/components/util/logical_geometry.rs index f464412fb46..ef85866fb97 100644 --- a/components/util/logical_geometry.rs +++ b/components/util/logical_geometry.rs @@ -965,65 +965,3 @@ impl + Sub> Sub> for Lo } } } - -#[cfg(test)] -fn modes() -> [WritingMode; 10] { - [ - WritingMode::empty(), - FLAG_VERTICAL, - FLAG_VERTICAL | FLAG_VERTICAL_LR, - FLAG_VERTICAL | FLAG_VERTICAL_LR | FLAG_SIDEWAYS_LEFT, - FLAG_VERTICAL | FLAG_SIDEWAYS_LEFT, - FLAG_RTL, - FLAG_VERTICAL | FLAG_RTL, - FLAG_VERTICAL | FLAG_VERTICAL_LR | FLAG_RTL, - FLAG_VERTICAL | FLAG_VERTICAL_LR | FLAG_SIDEWAYS_LEFT | FLAG_RTL, - FLAG_VERTICAL | FLAG_SIDEWAYS_LEFT | FLAG_RTL, - ] -} - -#[test] -fn test_size_round_trip() { - let physical = Size2D(1u32, 2u32); - for &mode in modes().iter() { - let logical = LogicalSize::from_physical(mode, physical); - assert!(logical.to_physical(mode) == physical); - assert!(logical.width(mode) == 1); - assert!(logical.height(mode) == 2); - } -} - -#[test] -fn test_point_round_trip() { - let physical = Point2D(1u32, 2u32); - let container = Size2D(100, 200); - for &mode in modes().iter() { - let logical = LogicalPoint::from_physical(mode, physical, container); - assert!(logical.to_physical(mode, container) == physical); - assert!(logical.x(mode, container) == 1); - assert!(logical.y(mode, container) == 2); - } -} - -#[test] -fn test_margin_round_trip() { - let physical = SideOffsets2D::new(1u32, 2u32, 3u32, 4u32); - for &mode in modes().iter() { - let logical = LogicalMargin::from_physical(mode, physical); - assert!(logical.to_physical(mode) == physical); - assert!(logical.top(mode) == 1); - assert!(logical.right(mode) == 2); - assert!(logical.bottom(mode) == 3); - assert!(logical.left(mode) == 4); - } -} - -#[test] -fn test_rect_round_trip() { - let physical = Rect(Point2D(1u32, 2u32), Size2D(3u32, 4u32)); - let container = Size2D(100, 200); - for &mode in modes().iter() { - let logical = LogicalRect::from_physical(mode, physical, container); - assert!(logical.to_physical(mode, container) == physical); - } -} diff --git a/components/util/task.rs b/components/util/task.rs index 97770269259..de206526f44 100644 --- a/components/util/task.rs +++ b/components/util/task.rs @@ -42,10 +42,3 @@ pub fn spawn_named_with_send_on_failure(name: &'static str, } }).unwrap(); } - -#[test] -fn spawn_named_test() { - spawn_named("Test".to_owned(), move || { - debug!("I can run!"); - }); -} diff --git a/components/util/vec.rs b/components/util/vec.rs index a1b07126e06..cce3e867676 100644 --- a/components/util/vec.rs +++ b/components/util/vec.rs @@ -5,9 +5,6 @@ use std::cmp::{PartialOrd, PartialEq, Ordering}; use std::iter::range_step; -#[cfg(test)] -use std::fmt::Debug; - /// FIXME(pcwalton): Workaround for lack of unboxed closures. This is called in /// performance-critical code, so a closure is insufficient. pub trait Comparator { @@ -75,65 +72,3 @@ pub fn byte_swap(data: &mut [u8]) { data[i + 0] = r; } } - -#[cfg(test)] -fn test_find_all_elems(arr: &[T]) { - let mut i = 0; - while i < arr.len() { - assert!(test_match(&arr[i], arr.binary_search_(&arr[i]))); - i += 1; - } -} - -#[cfg(test)] -fn test_miss_all_elems(arr: &[T], misses: &[T]) { - let mut i = 0; - while i < misses.len() { - let res = arr.binary_search_(&misses[i]); - debug!("{:?} == {:?} ?", misses[i], res); - assert!(!test_match(&misses[i], arr.binary_search_(&misses[i]))); - i += 1; - } -} - -#[cfg(test)] -fn test_match(b: &T, a: Option<&T>) -> bool { - match a { - None => false, - Some(t) => t == b - } -} - -#[test] -fn should_find_all_elements() { - let arr_odd = [1u32, 2, 4, 6, 7, 8, 9]; - let arr_even = [1u32, 2, 5, 6, 7, 8, 9, 42]; - let arr_double = [1u32, 1, 2, 2, 6, 8, 22]; - let arr_one = [234986325u32]; - let arr_two = [3044u32, 8393]; - let arr_three = [12u32, 23, 34]; - - test_find_all_elems(&arr_odd); - test_find_all_elems(&arr_even); - test_find_all_elems(&arr_double); - test_find_all_elems(&arr_one); - test_find_all_elems(&arr_two); - test_find_all_elems(&arr_three); -} - -#[test] -fn should_not_find_missing_elements() { - let arr_odd = [1u32, 2, 4, 6, 7, 8, 9]; - let arr_even = [1u32, 2, 5, 6, 7, 8, 9, 42]; - let arr_double = [1u32, 1, 2, 2, 6, 8, 22]; - let arr_one = [234986325u32]; - let arr_two = [3044u32, 8393]; - let arr_three = [12u32, 23, 34]; - - test_miss_all_elems(&arr_odd, &[-22, 0, 3, 5, 34938, 10, 11, 12]); - test_miss_all_elems(&arr_even, &[-1, 0, 3, 34938, 10, 11, 12]); - test_miss_all_elems(&arr_double, &[-1, 0, 3, 4, 34938, 10, 11, 12, 234, 234, 33]); - test_miss_all_elems(&arr_one, &[-1, 0, 3, 34938, 10, 11, 12, 234, 234, 33]); - test_miss_all_elems(&arr_two, &[-1, 0, 3, 34938, 10, 11, 12, 234, 234, 33]); - test_miss_all_elems(&arr_three, &[-2, 0, 1, 2, 3, 34938, 10, 11, 234, 234, 33]); -} diff --git a/tests/unit/Cargo.toml b/tests/unit/Cargo.toml new file mode 100644 index 00000000000..8f3fcd0545b --- /dev/null +++ b/tests/unit/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "unit_tests" +version = "0.0.1" +authors = ["The Servo Project Developers"] + +[lib] +name = "unit_tests" +path = "lib.rs" +doctest = false + +[dependencies.util] +path = "../../components/util" + +[dependencies.geom] +git = "https://github.com/servo/rust-geom" diff --git a/tests/unit/lib.rs b/tests/unit/lib.rs new file mode 100644 index 00000000000..506b8bf9236 --- /dev/null +++ b/tests/unit/lib.rs @@ -0,0 +1,4 @@ +extern crate geom; +extern crate util; + +#[cfg(test)] #[path="util/mod.rs"] mod util_tests; diff --git a/tests/unit/util/cache.rs b/tests/unit/util/cache.rs new file mode 100644 index 00000000000..27345cf421a --- /dev/null +++ b/tests/unit/util/cache.rs @@ -0,0 +1,49 @@ +use std::cell::Cell; +use util::cache::{HashCache, LRUCache}; + +#[test] +fn test_hashcache() { + let mut cache: HashCache> = 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()); +} + +#[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> = 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) +} diff --git a/tests/unit/util/logical_geometry.rs b/tests/unit/util/logical_geometry.rs new file mode 100644 index 00000000000..c872d050788 --- /dev/null +++ b/tests/unit/util/logical_geometry.rs @@ -0,0 +1,65 @@ +use geom::{Size2D, Point2D, SideOffsets2D, Rect}; +use util::logical_geometry::{WritingMode, LogicalSize, LogicalPoint, LogicalMargin, LogicalRect, + FLAG_RTL, FLAG_VERTICAL, FLAG_VERTICAL_LR, FLAG_SIDEWAYS_LEFT}; + +#[cfg(test)] +fn modes() -> [WritingMode; 10] { + [ + WritingMode::empty(), + FLAG_VERTICAL, + FLAG_VERTICAL | FLAG_VERTICAL_LR, + FLAG_VERTICAL | FLAG_VERTICAL_LR | FLAG_SIDEWAYS_LEFT, + FLAG_VERTICAL | FLAG_SIDEWAYS_LEFT, + FLAG_RTL, + FLAG_VERTICAL | FLAG_RTL, + FLAG_VERTICAL | FLAG_VERTICAL_LR | FLAG_RTL, + FLAG_VERTICAL | FLAG_VERTICAL_LR | FLAG_SIDEWAYS_LEFT | FLAG_RTL, + FLAG_VERTICAL | FLAG_SIDEWAYS_LEFT | FLAG_RTL, + ] +} + +#[test] +fn test_size_round_trip() { + let physical = Size2D(1u32, 2u32); + for &mode in modes().iter() { + let logical = LogicalSize::from_physical(mode, physical); + assert!(logical.to_physical(mode) == physical); + assert!(logical.width(mode) == 1); + assert!(logical.height(mode) == 2); + } +} + +#[test] +fn test_point_round_trip() { + let physical = Point2D(1u32, 2u32); + let container = Size2D(100, 200); + for &mode in modes().iter() { + let logical = LogicalPoint::from_physical(mode, physical, container); + assert!(logical.to_physical(mode, container) == physical); + assert!(logical.x(mode, container) == 1); + assert!(logical.y(mode, container) == 2); + } +} + +#[test] +fn test_margin_round_trip() { + let physical = SideOffsets2D::new(1u32, 2u32, 3u32, 4u32); + for &mode in modes().iter() { + let logical = LogicalMargin::from_physical(mode, physical); + assert!(logical.to_physical(mode) == physical); + assert!(logical.top(mode) == 1); + assert!(logical.right(mode) == 2); + assert!(logical.bottom(mode) == 3); + assert!(logical.left(mode) == 4); + } +} + +#[test] +fn test_rect_round_trip() { + let physical = Rect(Point2D(1u32, 2u32), Size2D(3u32, 4u32)); + let container = Size2D(100, 200); + for &mode in modes().iter() { + let logical = LogicalRect::from_physical(mode, physical, container); + assert!(logical.to_physical(mode, container) == physical); + } +} diff --git a/tests/unit/util/mod.rs b/tests/unit/util/mod.rs new file mode 100644 index 00000000000..a208f13c3db --- /dev/null +++ b/tests/unit/util/mod.rs @@ -0,0 +1,4 @@ +mod cache; +mod logical_geometry; +mod task; +mod vec; diff --git a/tests/unit/util/task.rs b/tests/unit/util/task.rs new file mode 100644 index 00000000000..d139d5eed37 --- /dev/null +++ b/tests/unit/util/task.rs @@ -0,0 +1,9 @@ +use std::borrow::ToOwned; +use util::task::spawn_named; + +#[test] +fn spawn_named_test() { + spawn_named("Test".to_owned(), move || { + println!("I can run!"); + }); +} diff --git a/tests/unit/util/vec.rs b/tests/unit/util/vec.rs new file mode 100644 index 00000000000..ff727224517 --- /dev/null +++ b/tests/unit/util/vec.rs @@ -0,0 +1,64 @@ +use std::fmt::Debug; +use util::vec::BinarySearchMethods; + +#[cfg(test)] +fn test_find_all_elems(arr: &[T]) { + let mut i = 0; + while i < arr.len() { + assert!(test_match(&arr[i], arr.binary_search_(&arr[i]))); + i += 1; + } +} + +#[cfg(test)] +fn test_miss_all_elems(arr: &[T], misses: &[T]) { + let mut i = 0; + while i < misses.len() { + let res = arr.binary_search_(&misses[i]); + println!("{:?} == {:?} ?", misses[i], res); + assert!(!test_match(&misses[i], arr.binary_search_(&misses[i]))); + i += 1; + } +} + +#[cfg(test)] +fn test_match(b: &T, a: Option<&T>) -> bool { + match a { + None => false, + Some(t) => t == b + } +} + +#[test] +fn should_find_all_elements() { + let arr_odd = [1u32, 2, 4, 6, 7, 8, 9]; + let arr_even = [1u32, 2, 5, 6, 7, 8, 9, 42]; + let arr_double = [1u32, 1, 2, 2, 6, 8, 22]; + let arr_one = [234986325u32]; + let arr_two = [3044u32, 8393]; + let arr_three = [12u32, 23, 34]; + + test_find_all_elems(&arr_odd); + test_find_all_elems(&arr_even); + test_find_all_elems(&arr_double); + test_find_all_elems(&arr_one); + test_find_all_elems(&arr_two); + test_find_all_elems(&arr_three); +} + +#[test] +fn should_not_find_missing_elements() { + let arr_odd = [1u32, 2, 4, 6, 7, 8, 9]; + let arr_even = [1u32, 2, 5, 6, 7, 8, 9, 42]; + let arr_double = [1u32, 1, 2, 2, 6, 8, 22]; + let arr_one = [234986325u32]; + let arr_two = [3044u32, 8393]; + let arr_three = [12u32, 23, 34]; + + test_miss_all_elems(&arr_odd, &[-22, 0, 3, 5, 34938, 10, 11, 12]); + test_miss_all_elems(&arr_even, &[-1, 0, 3, 34938, 10, 11, 12]); + test_miss_all_elems(&arr_double, &[-1, 0, 3, 4, 34938, 10, 11, 12, 234, 234, 33]); + test_miss_all_elems(&arr_one, &[-1, 0, 3, 34938, 10, 11, 12, 234, 234, 33]); + test_miss_all_elems(&arr_two, &[-1, 0, 3, 34938, 10, 11, 12, 234, 234, 33]); + test_miss_all_elems(&arr_three, &[-2, 0, 1, 2, 3, 34938, 10, 11, 234, 234, 33]); +}