about summary refs log tree commit diff
path: root/src/libstd/num
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2015-02-19 15:14:48 +0100
committerFelix S. Klock II <pnkfelix@pnkfx.org>2015-03-03 12:10:57 +0100
commitc8db89aa82573b89481fde598da6e54371f266cb (patch)
treebf3fda792aca94eeca3c413b64c0837a896bf448 /src/libstd/num
parent4394720dae12c119a9b1aed492b1b24ee089d808 (diff)
downloadrust-c8db89aa82573b89481fde598da6e54371f266cb.tar.gz
rust-c8db89aa82573b89481fde598da6e54371f266cb.zip
Accommodate arith-overflow in `core::num`, `std::num`, `coretest::num`.
 * `core::num`: adjust `UnsignedInt::is_power_of_two`,
   `UnsignedInt::next_power_of_two`, `Int::pow`.

   In particular for `Int::pow`: (1.) do not panic when `base`
   overflows if `acc` never observes the overflowed `base`, and (2.)
   if `acc` does observe the overflowed `base`, make sure we only
   panic if we would have otherwise (e.g. during a computation of
   `base * base`).

 * also in `core::num`: avoid underflow during computation of `uint::MAX`.

 * `std::num`: adjust tests `uint::test_uint_from_str_overflow`,
   `uint::test_uint_to_str_overflow`, `strconv`

 * `coretest::num`: adjust `test::test_int_from_str_overflow`.
Diffstat (limited to 'src/libstd/num')
-rw-r--r--src/libstd/num/mod.rs16
-rw-r--r--src/libstd/num/strconv.rs9
2 files changed, 13 insertions, 12 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs
index d4428282b14..0bca60ed1a0 100644
--- a/src/libstd/num/mod.rs
+++ b/src/libstd/num/mod.rs
@@ -1758,25 +1758,25 @@ mod tests {
         let mut u8_val: u8 = 255_u8;
         assert_eq!(u8_val.to_string(), "255");
 
-        u8_val += 1 as u8;
+        u8_val = u8_val.wrapping_add(1);
         assert_eq!(u8_val.to_string(), "0");
 
         let mut u16_val: u16 = 65_535_u16;
         assert_eq!(u16_val.to_string(), "65535");
 
-        u16_val += 1 as u16;
+        u16_val = u16_val.wrapping_add(1);
         assert_eq!(u16_val.to_string(), "0");
 
         let mut u32_val: u32 = 4_294_967_295_u32;
         assert_eq!(u32_val.to_string(), "4294967295");
 
-        u32_val += 1 as u32;
+        u32_val = u32_val.wrapping_add(1);
         assert_eq!(u32_val.to_string(), "0");
 
         let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
         assert_eq!(u64_val.to_string(), "18446744073709551615");
 
-        u64_val += 1 as u64;
+        u64_val = u64_val.wrapping_add(1);
         assert_eq!(u64_val.to_string(), "0");
     }
 
@@ -1790,7 +1790,7 @@ mod tests {
         assert_eq!(from_str::<u8>("255"), Some(u8_val));
         assert_eq!(from_str::<u8>("256"), None);
 
-        u8_val += 1 as u8;
+        u8_val = u8_val.wrapping_add(1);
         assert_eq!(from_str::<u8>("0"), Some(u8_val));
         assert_eq!(from_str::<u8>("-1"), None);
 
@@ -1798,7 +1798,7 @@ mod tests {
         assert_eq!(from_str::<u16>("65535"), Some(u16_val));
         assert_eq!(from_str::<u16>("65536"), None);
 
-        u16_val += 1 as u16;
+        u16_val = u16_val.wrapping_add(1);
         assert_eq!(from_str::<u16>("0"), Some(u16_val));
         assert_eq!(from_str::<u16>("-1"), None);
 
@@ -1806,7 +1806,7 @@ mod tests {
         assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
         assert_eq!(from_str::<u32>("4294967296"), None);
 
-        u32_val += 1 as u32;
+        u32_val = u32_val.wrapping_add(1);
         assert_eq!(from_str::<u32>("0"), Some(u32_val));
         assert_eq!(from_str::<u32>("-1"), None);
 
@@ -1814,7 +1814,7 @@ mod tests {
         assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
         assert_eq!(from_str::<u64>("18446744073709551616"), None);
 
-        u64_val += 1 as u64;
+        u64_val = u64_val.wrapping_add(1);
         assert_eq!(from_str::<u64>("0"), Some(u64_val));
         assert_eq!(from_str::<u64>("-1"), None);
     }
diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs
index ca2e6ba5d5d..8ec19c01098 100644
--- a/src/libstd/num/strconv.rs
+++ b/src/libstd/num/strconv.rs
@@ -427,6 +427,7 @@ static DIGIT_E_RADIX: u32 = ('e' as u32) - ('a' as u32) + 11;
 
 #[cfg(test)]
 mod tests {
+    use core::num::wrapping::WrappingOps;
     use string::ToString;
 
     #[test]
@@ -434,25 +435,25 @@ mod tests {
         let mut i8_val: i8 = 127_i8;
         assert_eq!(i8_val.to_string(), "127");
 
-        i8_val += 1 as i8;
+        i8_val = i8_val.wrapping_add(1);
         assert_eq!(i8_val.to_string(), "-128");
 
         let mut i16_val: i16 = 32_767_i16;
         assert_eq!(i16_val.to_string(), "32767");
 
-        i16_val += 1 as i16;
+        i16_val = i16_val.wrapping_add(1);
         assert_eq!(i16_val.to_string(), "-32768");
 
         let mut i32_val: i32 = 2_147_483_647_i32;
         assert_eq!(i32_val.to_string(), "2147483647");
 
-        i32_val += 1 as i32;
+        i32_val = i32_val.wrapping_add(1);
         assert_eq!(i32_val.to_string(), "-2147483648");
 
         let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
         assert_eq!(i64_val.to_string(), "9223372036854775807");
 
-        i64_val += 1 as i64;
+        i64_val = i64_val.wrapping_add(1);
         assert_eq!(i64_val.to_string(), "-9223372036854775808");
     }
 }