#[cfg(test)]
mod tests {
use super::Interval;
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
#[test]
fn check_open_containment() {
let interval = Interval::new(0u8,10u8);
assert!(interval.contains(3u8));
}
#[test]
fn check_closed_containment() {
let interval = Interval::new(-3f32, 4f32);
assert!(interval.contains_closed(-3f32));
}
}
use std::cmp::PartialOrd;
pub struct Interval<T :PartialOrd> {
low :T,
high :T,
}
impl<T :PartialOrd> Interval<T> {
pub fn new(bound1 :T, bound2 :T) -> Interval<T> {
if bound1 < bound2 {
Interval{low : bound1, high :bound2}
} else {
Interval{low : bound2, high : bound1}
}
}
pub fn contains(&self, value :T) -> bool {
self.low < value && value < self.high
}
pub fn contains_closed(&self, value :T) -> bool {
self.low <= value && value <= self.high
}
}