#[cfg(test)] mod tests { use fts_units::ops::*; use fts_units::quantity::*; use fts_units::si_system::quantities::*; use fts_units::si_system::*; use typenum::consts::*; macro_rules! convert_into_eq { ($x:expr, $y:ty, $z:expr) => { let temp: $y = $x.convert_into(); assert_eq!(temp.amount(), $z); }; } #[test] fn length_new() { assert_eq!(Kilometers::::new(5.0).amount(), 5.0); assert_eq!(Meters::::new(5.0).amount(), 5.0); assert_eq!(Centimeters::::new(5.0).amount(), 5.0); assert_eq!(Millimeters::::new(5.0).amount(), 5.0); assert_eq!(Micrometers::::new(5.0).amount(), 5.0); assert_eq!(Nanometers::::new(5.0).amount(), 5.0); assert_eq!(Picometers::::new(5.0).amount(), 5.0); assert_eq!(Femtometers::::new(5.0).amount(), 5.0); } #[test] fn mass_new() { assert_eq!(Kilograms::::new(5.0).amount(), 5.0); assert_eq!(Grams::::new(5.0).amount(), 5.0); assert_eq!(Centigrams::::new(5.0).amount(), 5.0); assert_eq!(Milligrams::::new(5.0).amount(), 5.0); assert_eq!(Micrograms::::new(5.0).amount(), 5.0); assert_eq!(Nanograms::::new(5.0).amount(), 5.0); } #[test] fn basic_add() { let m1 = Meters::::new(6.0); let m2 = Meters::::new(8.5); assert_eq!(Meters::::new(14.5), m1 + m2); let m3 = Meters::::new(-2.4); assert_eq!(Meters::::new(6.1), m2 + m3); } #[test] fn basic_sub() { let m1 = Meters::::new(6.0); let m2 = Meters::::new(8.5); assert_eq!(Meters::::new(-2.5), m1 - m2); let m3 = Meters::::new(-2.4); assert_eq!(Meters::::new(10.9), m2 - m3); } #[test] fn basic_mul() { let _l1 = Meters::::new(3.0); let _l2 = Meters::::new(5.0); //type MetersSquared = QuantityT, AddOutput>>; //let a : MetersSquared = l1 * l2; //let a /*: impl Area*/ = l1 * l2; //assert_eq!(MetersSquared::new(15.0), a); //assert_eq!(l1 * l2, QuantityT::>::new(15.0)); //assert_eq!(l1 * l2, AreaT::::new(15.0)); } #[test] fn basic_cast() { let _f32 = Meters::::new(42.5); let _i8: Meters = _f32.cast_into(); let _i16: Meters = _f32.cast_into(); let _i32: Meters = _f32.cast_into(); let _i64: Meters = _f32.cast_into(); let _i128: Meters = _f32.cast_into(); assert_eq!(_i8.amount(), 42); assert_eq!(_i16.amount(), 42); assert_eq!(_i32.amount(), 42); assert_eq!(_i64.amount(), 42); assert_eq!(_i128.amount(), 42); } #[test] fn cast_truncate() { let m = Meters::::new(7.73); let i: Meters = m.cast_into(); assert_eq!(i.amount(), 7); } #[test] fn convert_syntax() { let m = Meters::::new(7.73); let km: Kilometers = m.convert_into(); assert_eq!(km.amount(), 0.00773); } #[test] fn length_convert() { let m = Meters::::new(7.73); convert_into_eq!(&m, Kilometers, 0.00773); convert_into_eq!(&m, Centimeters, 773.0); convert_into_eq!(&m, Millimeters, 7730.0); convert_into_eq!(&m, Micrometers, 7730000.0); convert_into_eq!(&m, Nanometers, 7730000000.0); convert_into_eq!(&m, Picometers, 7730000000000.0); convert_into_eq!(&m, Femtometers, 7730000000000000.0); } #[test] fn velocity_calc() { let d = Meters::::new(12.0); let t = Seconds::::new(8.0); let v = d / t; assert_eq!(v, MetersPerSecond::::new(1.5)); } fn verify_velocity(_value: T) where T: traits::Velocity, { } #[test] fn si_traits() { //let _ : Box = Box::new(7.0); let v = MetersPerSecond::::new(1.5); verify_velocity(v); } #[test] fn velocity_convert() { convert_into_eq!( &MetersPerSecond::::new(100.0), KilometersPerHour, 360.0 ); convert_into_eq!(&Velocity::::new(100.0), Velocity, 0.006); } #[test] fn dimensionless() { let l = Meters::::new(12.0); let d = Dimensionless::::new(5.0); assert_eq!(l * d, Meters::::new(60.0)); assert_eq!(l / d, Meters::::new(2.4)); } #[test] fn invert() { let mps = MetersPerSecond::::new(5.0); let spm: QuantityT<_, SIUnitsT<_, SIExponentsT>> = mps.invert(); assert_eq!(spm.amount(), 0.2); assert_eq!(mps, spm.invert()); } #[test] fn sqrt_simple() { let d = Meters::::new(7.0); let d2: QuantityT<_, SIUnitsT<_, SIExponentsT>> = d * d; assert_eq!(d2.amount(), 49.0); let d1: QuantityT<_, SIUnitsT<_, SIExponentsT>> = d2.sqrt(); assert_eq!(d, d1); let s = Seconds::::new(4.0); let s4: QuantityT<_, SIUnitsT<_, SIExponentsT>> = s * s * s * s; let d2s4 = d2 * s4; let d1s2: QuantityT<_, SIUnitsT<_, SIExponentsT>> = d2s4.sqrt(); assert_eq!(d1s2.amount(), (7.0f32.powi(2) * 4.0f32.powi(4)).sqrt()); } #[test] fn sqrt_inv() { let d = Meters::::new(4.0); let d2 = d * d; let di = d.invert(); let di2 = di * di; assert_eq!(di, di2.sqrt()); assert_eq!(d, di2.sqrt().invert()); assert_eq!(d2, (di * di).invert()); } #[test] fn reduce() { let m = Meters::::new(7.0); let s = Seconds::::new(0.8); let mps = m / s; let mps2 = mps / s; let s = Seconds::::new(1.5); let m: Meters = mps2 * s * s; let mph: QuantityT, SIExponentsT>> = m / Hours::::new(0.5); assert_eq!(mph.amount(), 49.21875); } #[test] fn format() { assert_eq!( format!("{}", MetersPerSecond2::::new(9.8)), "9.8 m·s⁻²".to_string() ); assert_eq!( format!("{}", KilometersPerHour::::new(65.5)), "65.5 km·h⁻¹".to_string() ); } #[test] fn easy_from() { assert_eq!(Meters::::from(2.3).amount(), 2.3); assert_eq!(Kilometers::::new(42.4), 42.4.into()); } #[test] fn size_check() { use std::mem::size_of; assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); assert_eq!(size_of::>(), size_of::()); } #[test] fn try_build() { let t = trybuild::TestCases::new(); t.compile_fail("tests/compile_fail/*.rs"); } }