about summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorJakub Bukaj <jakub@jakub.cc>2014-11-16 10:21:42 +0100
committerJakub Bukaj <jakub@jakub.cc>2014-11-16 10:21:42 +0100
commit4c30cb25642d2cd4b228554e768068ba07504797 (patch)
treeea124407c05314a1862322b1fccc28ac21a3ba09 /src/libcoretest
parent42c77f4958fcd6c2238d883c49f52341e0631999 (diff)
parentd82a7ea57a69954dcc9b58869907a0a070ef432d (diff)
downloadrust-4c30cb25642d2cd4b228554e768068ba07504797.tar.gz
rust-4c30cb25642d2cd4b228554e768068ba07504797.zip
rollup merge of #18976: bjz/rfc369-numerics
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/num/int_macros.rs46
-rw-r--r--src/libcoretest/num/mod.rs70
-rw-r--r--src/libcoretest/str.rs7
3 files changed, 122 insertions, 1 deletions
diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs
index 5e2530ef2a9..e25f10bd0da 100644
--- a/src/libcoretest/num/int_macros.rs
+++ b/src/libcoretest/num/int_macros.rs
@@ -15,7 +15,8 @@ macro_rules! int_module (($T:ty, $T_i:ident) => (
 mod tests {
     use core::$T_i::*;
     use core::int;
-    use core::num::{Int, SignedInt};
+    use core::num::{FromStrRadix, Int, SignedInt};
+    use core::str::from_str;
     use num;
 
     #[test]
@@ -156,6 +157,49 @@ mod tests {
         assert!(5i.checked_div(0) == None);
         assert!(int::MIN.checked_div(-1) == None);
     }
+
+    #[test]
+    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::<i32>("123456789"), Some(123456789 as i32));
+        assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
+
+        assert_eq!(from_str::<$T>("-1"), Some(-1 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::<i32>("-123456789"), Some(-123456789 as i32));
+        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]
+    fn test_from_str_radix() {
+        assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291 as i32));
+        assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535 as i32));
+        assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Some(65535 as i32));
+        assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("Z", 36), Some(35 as $T));
+
+        assert_eq!(FromStrRadix::from_str_radix("-123", 10), Some(-123 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Some(-9 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("-123", 8), Some(-83 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("-123", 16), Some(-291 as i32));
+        assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Some(-65535 as i32));
+        assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Some(-65535 as i32));
+        assert_eq!(FromStrRadix::from_str_radix("-z", 36), Some(-35 as $T));
+        assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Some(-35 as $T));
+
+        assert_eq!(FromStrRadix::from_str_radix("Z", 35), None::<$T>);
+        assert_eq!(FromStrRadix::from_str_radix("-9", 2), None::<$T>);
+    }
 }
 
 ))
diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs
index 38502321c1d..0cd1ded21d6 100644
--- a/src/libcoretest/num/mod.rs
+++ b/src/libcoretest/num/mod.rs
@@ -45,3 +45,73 @@ pub fn test_num<T>(ten: T, two: T) where
     assert_eq!(ten.div(&two),  ten / two);
     assert_eq!(ten.rem(&two),  ten % two);
 }
+
+#[cfg(test)]
+mod test {
+    use core::option::{Option, Some, None};
+    use core::num::Float;
+    use core::num::from_str_radix;
+
+    #[test]
+    fn from_str_issue7588() {
+        let u : Option<u8> = from_str_radix("1000", 10);
+        assert_eq!(u, None);
+        let s : Option<i16> = from_str_radix("80000", 10);
+        assert_eq!(s, None);
+        let f : Option<f32> = from_str_radix("10000000000000000000000000000000000000000", 10);
+        assert_eq!(f, Some(Float::infinity()))
+        let fe : Option<f32> = from_str_radix("1e40", 10);
+        assert_eq!(fe, Some(Float::infinity()))
+    }
+
+    #[test]
+    fn test_from_str_radix_float() {
+        let x1 : Option<f64> = from_str_radix("-123.456", 10);
+        assert_eq!(x1, Some(-123.456));
+        let x2 : Option<f32> = from_str_radix("123.456", 10);
+        assert_eq!(x2, Some(123.456));
+        let x3 : Option<f32> = from_str_radix("-0.0", 10);
+        assert_eq!(x3, Some(-0.0));
+        let x4 : Option<f32> = from_str_radix("0.0", 10);
+        assert_eq!(x4, Some(0.0));
+        let x4 : Option<f32> = from_str_radix("1.0", 10);
+        assert_eq!(x4, Some(1.0));
+        let x5 : Option<f32> = from_str_radix("-1.0", 10);
+        assert_eq!(x5, Some(-1.0));
+    }
+
+    #[test]
+    fn test_int_from_str_overflow() {
+        let mut i8_val: i8 = 127_i8;
+        assert_eq!(from_str::<i8>("127"), Some(i8_val));
+        assert_eq!(from_str::<i8>("128"), None);
+
+        i8_val += 1 as i8;
+        assert_eq!(from_str::<i8>("-128"), Some(i8_val));
+        assert_eq!(from_str::<i8>("-129"), None);
+
+        let mut i16_val: i16 = 32_767_i16;
+        assert_eq!(from_str::<i16>("32767"), Some(i16_val));
+        assert_eq!(from_str::<i16>("32768"), None);
+
+        i16_val += 1 as i16;
+        assert_eq!(from_str::<i16>("-32768"), Some(i16_val));
+        assert_eq!(from_str::<i16>("-32769"), None);
+
+        let mut i32_val: i32 = 2_147_483_647_i32;
+        assert_eq!(from_str::<i32>("2147483647"), Some(i32_val));
+        assert_eq!(from_str::<i32>("2147483648"), None);
+
+        i32_val += 1 as i32;
+        assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val));
+        assert_eq!(from_str::<i32>("-2147483649"), None);
+
+        let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
+        assert_eq!(from_str::<i64>("9223372036854775807"), Some(i64_val));
+        assert_eq!(from_str::<i64>("9223372036854775808"), None);
+
+        i64_val += 1 as i64;
+        assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
+        assert_eq!(from_str::<i64>("-9223372036854775809"), None);
+    }
+}
diff --git a/src/libcoretest/str.rs b/src/libcoretest/str.rs
index d3f77c47c44..5f44fd807cc 100644
--- a/src/libcoretest/str.rs
+++ b/src/libcoretest/str.rs
@@ -8,6 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[test]
+fn test_bool_from_str() {
+    assert_eq!(from_str::<bool>("true"), Some(true));
+    assert_eq!(from_str::<bool>("false"), Some(false));
+    assert_eq!(from_str::<bool>("not even a boolean"), None);
+}
+
 fn check_contains_all_substrings(s: &str) {
     assert!(s.contains(""));
     for i in range(0, s.len()) {