mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +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
9
components/servo/Cargo.lock
generated
9
components/servo/Cargo.lock
generated
|
@ -16,6 +16,7 @@ dependencies = [
|
||||||
"profile 0.0.1",
|
"profile 0.0.1",
|
||||||
"script 0.0.1",
|
"script 0.0.1",
|
||||||
"time 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"url 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"util 0.0.1",
|
"util 0.0.1",
|
||||||
"webdriver_server 0.0.1",
|
"webdriver_server 0.0.1",
|
||||||
|
@ -920,6 +921,14 @@ name = "unicase"
|
||||||
version = "0.0.5"
|
version = "0.0.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
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]]
|
[[package]]
|
||||||
name = "url"
|
name = "url"
|
||||||
version = "0.2.23"
|
version = "0.2.23"
|
||||||
|
|
|
@ -16,6 +16,9 @@ test = false
|
||||||
doc = false
|
doc = false
|
||||||
bench = false
|
bench = false
|
||||||
|
|
||||||
|
[dev-dependencies.unit_tests]
|
||||||
|
path = "../../tests/unit"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "reftest"
|
name = "reftest"
|
||||||
path = "../../tests/reftest.rs"
|
path = "../../tests/reftest.rs"
|
||||||
|
|
|
@ -12,8 +12,6 @@ use rand;
|
||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use std::cell::Cell;
|
|
||||||
|
|
||||||
pub struct HashCache<K, V> {
|
pub struct HashCache<K, V> {
|
||||||
entries: HashMap<K, V, DefaultState<SipHasher>>,
|
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> {
|
pub struct LRUCache<K, V> {
|
||||||
entries: Vec<(K, V)>,
|
entries: Vec<(K, V)>,
|
||||||
cache_size: usize,
|
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)
|
|
||||||
}
|
|
||||||
|
|
|
@ -965,65 +965,3 @@ impl<T: Copy + Add<T, Output=T> + Sub<T, Output=T>> Sub<LogicalMargin<T>> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -42,10 +42,3 @@ pub fn spawn_named_with_send_on_failure<F, T>(name: &'static str,
|
||||||
}
|
}
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn spawn_named_test() {
|
|
||||||
spawn_named("Test".to_owned(), move || {
|
|
||||||
debug!("I can run!");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,9 +5,6 @@
|
||||||
use std::cmp::{PartialOrd, PartialEq, Ordering};
|
use std::cmp::{PartialOrd, PartialEq, Ordering};
|
||||||
use std::iter::range_step;
|
use std::iter::range_step;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use std::fmt::Debug;
|
|
||||||
|
|
||||||
/// FIXME(pcwalton): Workaround for lack of unboxed closures. This is called in
|
/// FIXME(pcwalton): Workaround for lack of unboxed closures. This is called in
|
||||||
/// performance-critical code, so a closure is insufficient.
|
/// performance-critical code, so a closure is insufficient.
|
||||||
pub trait Comparator<K,T> {
|
pub trait Comparator<K,T> {
|
||||||
|
@ -75,65 +72,3 @@ pub fn byte_swap(data: &mut [u8]) {
|
||||||
data[i + 0] = r;
|
data[i + 0] = r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn test_find_all_elems<T: PartialEq + PartialOrd + Eq + Ord>(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<T: PartialEq + PartialOrd + Eq + Ord + Debug>(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<T: PartialEq>(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]);
|
|
||||||
}
|
|
||||||
|
|
15
tests/unit/Cargo.toml
Normal file
15
tests/unit/Cargo.toml
Normal file
|
@ -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"
|
4
tests/unit/lib.rs
Normal file
4
tests/unit/lib.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
extern crate geom;
|
||||||
|
extern crate util;
|
||||||
|
|
||||||
|
#[cfg(test)] #[path="util/mod.rs"] mod util_tests;
|
49
tests/unit/util/cache.rs
Normal file
49
tests/unit/util/cache.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use std::cell::Cell;
|
||||||
|
use util::cache::{HashCache, LRUCache};
|
||||||
|
|
||||||
|
#[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());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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)
|
||||||
|
}
|
65
tests/unit/util/logical_geometry.rs
Normal file
65
tests/unit/util/logical_geometry.rs
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
4
tests/unit/util/mod.rs
Normal file
4
tests/unit/util/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
mod cache;
|
||||||
|
mod logical_geometry;
|
||||||
|
mod task;
|
||||||
|
mod vec;
|
9
tests/unit/util/task.rs
Normal file
9
tests/unit/util/task.rs
Normal file
|
@ -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!");
|
||||||
|
});
|
||||||
|
}
|
64
tests/unit/util/vec.rs
Normal file
64
tests/unit/util/vec.rs
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use util::vec::BinarySearchMethods;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
fn test_find_all_elems<T: PartialEq + PartialOrd + Eq + Ord>(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<T: PartialEq + PartialOrd + Eq + Ord + Debug>(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<T: PartialEq>(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]);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue