partial_function 0.6.0

A clean way to define function as a set of subfunctions where each has defined start and end bounds.
Documentation
extern crate partial_function;

#[cfg(test)]
#[allow(unused_variables)]
mod tests {
    use partial_function::*;
    #[cfg(target_family = "wasm")]
    use wasm_bindgen_test::*;

    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn single() {
        let p = PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .build();
        assert_eq!(Some(0.5), p.eval(0.5));
    }
    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn single_start() {
        let p = PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .build();
        assert_eq!(Some(0.0), p.eval(0.0));
    }
    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn single_ending() {
        let p = PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .build();
        assert_eq!(Some(1.0), p.eval(1.0));
    }
    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn single_nan() {
        let p = PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .build();
        assert!(p.eval(999.0).is_none());
    }
    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn dual_start() {
        let p = PartialFunction::builder()
            .with(1.0, 2.0, Box::new(|x| 5.0))
            .with(0.0, 1.0, Box::new(|x| x))
            .build();
        assert_eq!(Some(5.0), p.eval(1.0));
    }
    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn dual_end() {
        let p = PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .with(1.0, 2.0, Box::new(|x| 5.0))
            .build();
        assert_eq!(Some(5.0), p.eval(1.0));
    }
    #[test]
    #[should_panic]
    fn intersect_start() {
        PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .with(-0.5, 0.5, Box::new(|x| 5.0))
            .build();
    }
    #[test]
    #[should_panic]
    fn intersect_end() {
        PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .with(0.5, 2.0, Box::new(|x| 5.0))
            .build();
    }
    #[test]
    #[should_panic]
    fn intersect_inner() {
        PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .with(0.4, 0.6, Box::new(|x| 5.0))
            .build();
    }
    #[test]
    #[should_panic]
    fn intersect_outer() {
        PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .with(-2.0, 2.0, Box::new(|x| 5.0))
            .build();
    }
    #[test]
    #[should_panic]
    fn intersect_same() {
        PartialFunction::builder()
            .with(0.0, 1.0, Box::new(|x| x))
            .with(0.0, 1.0, Box::new(|x| 5.0))
            .build();
    }

    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn lower_partial_normal() {
        let f = LowerPartialFunction::builder()
            .with(0.0, Box::new(|x| 1))
            .with(1.0, Box::new(|x| 2))
            .build();
        assert_eq!(f.eval(-1.0), None);
        assert_eq!(f.eval(0.0), Some(1));
        assert_eq!(f.eval(0.5), Some(1));
        assert_eq!(f.eval(1.0), Some(2));
        assert_eq!(f.eval(1000.0), Some(2));
    }

    #[test]
    #[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
    fn lower_partial_inverse_insert() {
        let f = LowerPartialFunction::builder()
            .with(1.0, Box::new(|x| 2))
            .with(0.0, Box::new(|x| 1))
            .build();
        assert_eq!(f.eval(-1.0), None);
        assert_eq!(f.eval(0.0), Some(1));
        assert_eq!(f.eval(0.5), Some(1));
        assert_eq!(f.eval(1.0), Some(2));
        assert_eq!(f.eval(1000.0), Some(2));
    }

    #[test]
    #[should_panic]
    fn lower_partial_overlap() {
        let f = LowerPartialFunction::builder()
            .with(0.0, Box::new(|x| 1))
            .with(0.0, Box::new(|x| 2))
            .build();
    }
}