|
| 1 | +use num::ToPrimitive; |
| 2 | + |
| 3 | +use super::*; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_parse_rational() { |
| 7 | + assert_eq!(Rational::parse("1234").expect_finite(), BigRational::new(1234.into(), 1.into())); |
| 8 | + assert_eq!( |
| 9 | + Rational::parse("-1234").expect_finite(), |
| 10 | + BigRational::new((-1234).into(), 1.into()) |
| 11 | + ); |
| 12 | + assert_eq!(Rational::parse("1e+6").expect_finite(), BigRational::new(1000000.into(), 1.into())); |
| 13 | + assert_eq!(Rational::parse("1e-6").expect_finite(), BigRational::new(1.into(), 1000000.into())); |
| 14 | + assert_eq!( |
| 15 | + Rational::parse("10.4e6").expect_finite(), |
| 16 | + BigRational::new(10400000.into(), 1.into()) |
| 17 | + ); |
| 18 | + assert_eq!( |
| 19 | + Rational::parse("10.4e+6").expect_finite(), |
| 20 | + BigRational::new(10400000.into(), 1.into()) |
| 21 | + ); |
| 22 | + assert_eq!( |
| 23 | + Rational::parse("10.4e-6").expect_finite(), |
| 24 | + BigRational::new(13.into(), 1250000.into()) |
| 25 | + ); |
| 26 | + assert_eq!( |
| 27 | + Rational::parse("10.4243566462342456234124").expect_finite(), |
| 28 | + BigRational::new(104243566462342456234124_i128.into(), 10000000000000000000000_i128.into()) |
| 29 | + ); |
| 30 | + assert_eq!(Rational::parse("inf"), Rational::Inf); |
| 31 | + assert_eq!(Rational::parse("+inf"), Rational::Inf); |
| 32 | + assert_eq!(Rational::parse("-inf"), Rational::NegInf); |
| 33 | + assert_eq!(Rational::parse("NaN"), Rational::Nan); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_decode() { |
| 38 | + assert_eq!(decode(0f32, false), FloatRes::Zero); |
| 39 | + assert_eq!(decode(f32::INFINITY, false), FloatRes::Inf); |
| 40 | + assert_eq!(decode(f32::NEG_INFINITY, false), FloatRes::NegInf); |
| 41 | + assert_eq!(decode(1.0f32, false).normalize(), FloatRes::Real { sig: 1, exp: 0 }); |
| 42 | + assert_eq!(decode(-1.0f32, false).normalize(), FloatRes::Real { sig: -1, exp: 0 }); |
| 43 | + assert_eq!(decode(100.0f32, false).normalize(), FloatRes::Real { sig: 100, exp: 0 }); |
| 44 | + assert_eq!(decode(100.5f32, false).normalize(), FloatRes::Real { sig: 201, exp: -1 }); |
| 45 | + assert_eq!(decode(-4.004f32, false).normalize(), FloatRes::Real { sig: -8396997, exp: -21 }); |
| 46 | + assert_eq!(decode(0.0004f32, false).normalize(), FloatRes::Real { sig: 13743895, exp: -35 }); |
| 47 | + assert_eq!( |
| 48 | + decode(f32::from_bits(0x1), false).normalize(), |
| 49 | + FloatRes::Real { sig: 1, exp: -149 } |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_validate() { |
| 55 | + validate::<f32>("0", false).unwrap(); |
| 56 | + validate::<f32>("-0", false).unwrap(); |
| 57 | + validate::<f32>("1", false).unwrap(); |
| 58 | + validate::<f32>("-1", false).unwrap(); |
| 59 | + validate::<f32>("1.1", false).unwrap(); |
| 60 | + validate::<f32>("-1.1", false).unwrap(); |
| 61 | + validate::<f32>("1e10", false).unwrap(); |
| 62 | + validate::<f32>("1e1000", false).unwrap(); |
| 63 | + validate::<f32>("-1e1000", false).unwrap(); |
| 64 | + validate::<f32>("1e-1000", false).unwrap(); |
| 65 | + validate::<f32>("-1e-1000", false).unwrap(); |
| 66 | +} |
| 67 | + |
| 68 | +#[test] |
| 69 | +fn test_check() { |
| 70 | + // Most of the arbitrary values come from checking against <http://weitz.de/ieee/>. |
| 71 | + let r = &BigRational::from_float(10.0).unwrap(); |
| 72 | + FloatRes::<f32>::validate_real(r.clone(), 10, 0).unwrap(); |
| 73 | + FloatRes::<f32>::validate_real(r.clone(), 10, -1).unwrap_err(); |
| 74 | + FloatRes::<f32>::validate_real(r.clone(), 10, 1).unwrap_err(); |
| 75 | + |
| 76 | + let r = &BigRational::from_float(0.25).unwrap(); |
| 77 | + FloatRes::<f32>::validate_real(r.clone(), 1, -2).unwrap(); |
| 78 | + FloatRes::<f32>::validate_real(r.clone(), 2, -2).unwrap_err(); |
| 79 | + |
| 80 | + let r = &BigRational::from_float(1234.5678).unwrap(); |
| 81 | + FloatRes::<f32>::validate_real(r.clone(), 0b100110100101001000101011, -13).unwrap(); |
| 82 | + FloatRes::<f32>::validate_real(r.clone(), 0b100110100101001000101010, -13).unwrap_err(); |
| 83 | + FloatRes::<f32>::validate_real(r.clone(), 0b100110100101001000101100, -13).unwrap_err(); |
| 84 | + |
| 85 | + let r = &BigRational::from_float(-1234.5678).unwrap(); |
| 86 | + FloatRes::<f32>::validate_real(r.clone(), -0b100110100101001000101011, -13).unwrap(); |
| 87 | + FloatRes::<f32>::validate_real(r.clone(), -0b100110100101001000101010, -13).unwrap_err(); |
| 88 | + FloatRes::<f32>::validate_real(r.clone(), -0b100110100101001000101100, -13).unwrap_err(); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn check_constants() { |
| 93 | + assert_eq!(f32::constants().max.to_f32().unwrap(), f32::MAX); |
| 94 | + assert_eq!(f32::constants().min_subnormal.to_f32().unwrap(), f32::from_bits(0x1)); |
| 95 | + assert_eq!(f64::constants().max.to_f64().unwrap(), f64::MAX); |
| 96 | + assert_eq!(f64::constants().min_subnormal.to_f64().unwrap(), f64::from_bits(0x1)); |
| 97 | +} |
0 commit comments