// License: see LICENSE file at root directory of `master` branch use std::{ mem, str::FromStr, }; use dia_range::Range; #[test] fn ranges() { assert_eq!(Range::new(0, 9), Range::new(9, 0)); assert!(Range::new(0, 1) < Range::new(1, 2)); assert!(Range::new(0, 1) == Range::new(1, 0)); assert!(Range::new(8, 9) > Range::new(7, 0)); assert_eq!(Range::new(0, 1).merge(&Range::new(2, 5)), Some(Range::new(0, 5))); assert_eq!(Range::new(0, 10).merge(&Range::new(2, 5)), Some(Range::new(0, 10))); assert_eq!(Range::new(0, 10).merge(&Range::new(2, 20)), Some(Range::new(0, 20))); assert_eq!(Range::new(6, 4).merge(&Range::new(4, 6)), Some(Range::new(6, 4))); assert_eq!(Range::new(0, 1).merge(&Range::new(3, 5)), None); let mut range = Range::new(5, 5); assert!(range.expand(6)); assert!(range.expand(6) == false); assert!(range.expand(9) == false); assert!(range.expand(4)); assert!(range.expand(3)); assert_eq!(range, Range::new(3, 6)); assert_eq!(Range::new(0_i32, 0).estimate_size(), 1_u32); assert_eq!(Range::new(0_u8, 1).estimate_size(), 2); assert_eq!(Range::new(199_i16, 9).estimate_size(), 191_u16); assert!(Range::new(199, 9).contains(&99)); assert!(Range::new(1, 9).contains(&99) == false); assert!(Range::new(1, 9).contains(&9)); assert_eq!(dia_range::merge(vec![Range::new(0, 1), Range::new(2, 3), Range::new(5, 6)].into_iter()), [Range::new(0, 3), Range::new(5, 6)]); assert_eq!(dia_range::merge(vec![Range::new(6, 5), Range::new(2, 3), Range::new(1, 0)].into_iter()), [Range::new(0, 3), Range::new(5, 6)]); assert_eq!(Range::new(-1, 2), Range::from_str("-1..2").unwrap()); assert_eq!(Range::from(6), Range::from_str("6..6").unwrap()); assert_eq!(Range::new(0_u8, u8::max_value()), Range::from_str("0..").unwrap()); assert_eq!(Range::new(i8::min_value(), i8::max_value()), Range::from_str("..").unwrap()); assert_eq!(Range::new(i8::min_value(), 5), Range::from_str(" .. 5 ").unwrap()); Range::::from_str(". .").unwrap_err(); Range::::from_str(" ").unwrap_err(); Range::::from_str("").unwrap_err(); Range::::from_str("2..-3").unwrap_err(); Range::::from_str("2..-3").unwrap_err(); } /// # This function tests that implementations for integer types are correct /// /// For example: `i8 -> u8`, `i16 -> u16`, `u64 -> u64`... #[test] fn impls() { macro_rules! assert_size_of_val { ($($ty: ty,)+) => { $( let start: $ty = 0; let r = Range::from(start); assert_eq!(mem::size_of_val(&r.start()), mem::size_of_val(&r.estimate_size())); )+ }} assert_size_of_val!(i8, i16, i32, i64, i128, isize,); macro_rules! assert_same_type { ($($ty: ty,)+) => { $( let start: $ty = 0; let r = Range::from(start); assert!(r.start() <= r.estimate_size()); )+ }} assert_same_type!(u8, u16, u32, u64, u128, usize,); }