diff options
| author | bors <bors@rust-lang.org> | 2015-08-22 09:59:07 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-08-22 09:59:07 +0000 |
| commit | 94ee3b5a54a9f4965b82f5e4eda512966e96ac63 (patch) | |
| tree | d404e61ade0e002ee43c690d64e4fb2e2900c694 /src/libcoretest/num | |
| parent | e9b74a90172e99e361ff46277f52bfc03d0a7ba4 (diff) | |
| parent | a2b932c0b614aa5a68f7d24977fab241998a89ef (diff) | |
Auto merge of #27871 - alexcrichton:stabilize-libcore, r=aturon
These commits move libcore into a state so that it's ready for stabilization, performing some minor cleanup: * The primitive modules for integers in the standard library were all removed from the source tree as they were just straight reexports of the libcore variants. * The `core::atomic` module now lives in `core::sync::atomic`. The `core::sync` module is otherwise empty, but ripe for expansion! * The `core::prelude::v1` module was stabilized after auditing that it is a subset of the standard library's prelude plus some primitive extension traits (char, str, and slice) * Some unstable-hacks for float parsing errors were shifted around to not use the same unstable hacks (e.g. the `flt2dec` module is now used for "privacy"). After this commit, the remaining large unstable functionality specific to libcore is: * `raw`, `intrinsics`, `nonzero`, `array`, `panicking`, `simd` -- these modules are all unstable or not reexported in the standard library, so they're just remaining in the same status quo as before * `num::Float` - this extension trait for floats needs to be audited for functionality (much of that is happening in #27823) and may also want to be renamed to `FloatExt` or `F32Ext`/`F64Ext`. * Should the extension traits for primitives be stabilized in libcore? I believe other unstable pieces are not isolated to just libcore but also affect the standard library. cc #27701
Diffstat (limited to 'src/libcoretest/num')
| -rw-r--r-- | src/libcoretest/num/dec2flt/mod.rs | 51 | ||||
| -rw-r--r-- | src/libcoretest/num/uint_macros.rs | 32 |
2 files changed, 56 insertions, 27 deletions
diff --git a/src/libcoretest/num/dec2flt/mod.rs b/src/libcoretest/num/dec2flt/mod.rs index b7ef956055e..131cf629575 100644 --- a/src/libcoretest/num/dec2flt/mod.rs +++ b/src/libcoretest/num/dec2flt/mod.rs @@ -12,7 +12,6 @@ use std::{i64, f32, f64}; use test; -use core::num::dec2flt::{to_f32, to_f64}; mod parse; mod rawfp; @@ -27,11 +26,11 @@ macro_rules! test_literal { let inputs = &[stringify!($x).into(), format!("{:?}", x64), format!("{:e}", x64)]; for input in inputs { if input != "inf" { - assert_eq!(to_f64(input), Ok(x64)); - assert_eq!(to_f32(input), Ok(x32)); + assert_eq!(input.parse(), Ok(x64)); + assert_eq!(input.parse(), Ok(x32)); let neg_input = &format!("-{}", input); - assert_eq!(to_f64(neg_input), Ok(-x64)); - assert_eq!(to_f32(neg_input), Ok(-x32)); + assert_eq!(neg_input.parse(), Ok(-x64)); + assert_eq!(neg_input.parse(), Ok(-x32)); } } }) @@ -99,83 +98,83 @@ fn fast_path_correct() { #[test] fn lonely_dot() { - assert_eq!(to_f64("."), Ok(0.0)); + assert_eq!(".".parse(), Ok(0.0)); } #[test] fn nan() { - assert!(to_f64("NaN").unwrap().is_nan()); - assert!(to_f32("NaN").unwrap().is_nan()); + assert!("NaN".parse::<f32>().unwrap().is_nan()); + assert!("NaN".parse::<f64>().unwrap().is_nan()); } #[test] fn inf() { - assert_eq!(to_f64("inf"), Ok(f64::INFINITY)); - assert_eq!(to_f64("-inf"), Ok(f64::NEG_INFINITY)); - assert_eq!(to_f32("inf"), Ok(f32::INFINITY)); - assert_eq!(to_f32("-inf"), Ok(f32::NEG_INFINITY)); + assert_eq!("inf".parse(), Ok(f64::INFINITY)); + assert_eq!("-inf".parse(), Ok(f64::NEG_INFINITY)); + assert_eq!("inf".parse(), Ok(f32::INFINITY)); + assert_eq!("-inf".parse(), Ok(f32::NEG_INFINITY)); } #[test] fn massive_exponent() { let max = i64::MAX; - assert_eq!(to_f64(&format!("1e{}000", max)), Ok(f64::INFINITY)); - assert_eq!(to_f64(&format!("1e-{}000", max)), Ok(0.0)); - assert_eq!(to_f64(&format!("1e{}000", max)), Ok(f64::INFINITY)); + assert_eq!(format!("1e{}000", max).parse(), Ok(f64::INFINITY)); + assert_eq!(format!("1e-{}000", max).parse(), Ok(0.0)); + assert_eq!(format!("1e{}000", max).parse(), Ok(f64::INFINITY)); } #[bench] fn bench_0(b: &mut test::Bencher) { - b.iter(|| to_f64("0.0")); + b.iter(|| "0.0".parse::<f64>()); } #[bench] fn bench_42(b: &mut test::Bencher) { - b.iter(|| to_f64("42")); + b.iter(|| "42".parse::<f64>()); } #[bench] fn bench_huge_int(b: &mut test::Bencher) { // 2^128 - 1 - b.iter(|| to_f64("170141183460469231731687303715884105727")); + b.iter(|| "170141183460469231731687303715884105727".parse::<f64>()); } #[bench] fn bench_short_decimal(b: &mut test::Bencher) { - b.iter(|| to_f64("1234.5678")); + b.iter(|| "1234.5678".parse::<f64>()); } #[bench] fn bench_pi_long(b: &mut test::Bencher) { - b.iter(|| to_f64("3.14159265358979323846264338327950288")); + b.iter(|| "3.14159265358979323846264338327950288".parse::<f64>()); } #[bench] fn bench_pi_short(b: &mut test::Bencher) { - b.iter(|| to_f64("3.141592653589793")) + b.iter(|| "3.141592653589793".parse::<f64>()) } #[bench] fn bench_1e150(b: &mut test::Bencher) { - b.iter(|| to_f64("1e150")); + b.iter(|| "1e150".parse::<f64>()); } #[bench] fn bench_long_decimal_and_exp(b: &mut test::Bencher) { - b.iter(|| to_f64("727501488517303786137132964064381141071e-123")); + b.iter(|| "727501488517303786137132964064381141071e-123".parse::<f64>()); } #[bench] fn bench_min_subnormal(b: &mut test::Bencher) { - b.iter(|| to_f64("5e-324")); + b.iter(|| "5e-324".parse::<f64>()); } #[bench] fn bench_min_normal(b: &mut test::Bencher) { - b.iter(|| to_f64("2.2250738585072014e-308")); + b.iter(|| "2.2250738585072014e-308".parse::<f64>()); } #[bench] fn bench_max(b: &mut test::Bencher) { - b.iter(|| to_f64("1.7976931348623157e308")); + b.iter(|| "1.7976931348623157e308".parse::<f64>()); } diff --git a/src/libcoretest/num/uint_macros.rs b/src/libcoretest/num/uint_macros.rs index 1712345f9d9..25591db64d9 100644 --- a/src/libcoretest/num/uint_macros.rs +++ b/src/libcoretest/num/uint_macros.rs @@ -14,6 +14,7 @@ mod tests { use core::$T_i::*; use num; use core::ops::{BitOr, BitAnd, BitXor, Shl, Shr, Not}; + use std::str::FromStr; #[test] fn test_overflows() { @@ -121,6 +122,35 @@ mod tests { assert!((10 as $T).checked_div(2) == Some(5)); assert!((5 as $T).checked_div(0) == None); } -} + fn from_str<T: FromStr>(t: &str) -> Option<T> { + FromStr::from_str(t).ok() + } + + #[test] + pub fn test_from_str() { + assert_eq!(from_str::<$T>("0"), Some(0 as $T)); + assert_eq!(from_str::<$T>("3"), Some(3 as $T)); + assert_eq!(from_str::<$T>("10"), Some(10 as $T)); + assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32)); + assert_eq!(from_str::<$T>("00100"), Some(100 as $T)); + + assert_eq!(from_str::<$T>(""), None); + assert_eq!(from_str::<$T>(" "), None); + assert_eq!(from_str::<$T>("x"), None); + } + + #[test] + pub fn test_parse_bytes() { + assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T)); + assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T)); + assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T)); + assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16)); + assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16)); + assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T)); + + assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>); + assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>); + } +} )} |
