diff options
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/f32.rs | 7 | ||||
| -rw-r--r-- | src/libcore/num/f64.rs | 7 | ||||
| -rw-r--r-- | src/libcore/num/i16.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/i32.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/i64.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/i8.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/int.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/int_macros.rs | 9 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 16 | ||||
| -rw-r--r-- | src/libcore/num/u16.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/u32.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/u64.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/u8.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/uint.rs | 8 | ||||
| -rw-r--r-- | src/libcore/num/uint_macros.rs | 7 |
15 files changed, 95 insertions, 31 deletions
diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index ac45f52e710..c4cdc5a0a40 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -10,11 +10,12 @@ //! Operations and constants for 32-bits floats (`f32` type) -use cmp::{Eq, Ord}; use default::Default; use intrinsics; use num::{Zero, One, Bounded, Signed, Num, Primitive}; -use ops::{Add, Sub, Mul, Div, Rem, Neg}; + +#[cfg(not(test))] use cmp::{Eq, Ord}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg}; pub static RADIX: uint = 2u; @@ -100,6 +101,7 @@ pub mod consts { pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32; } +#[cfg(not(test))] impl Ord for f32 { #[inline] fn lt(&self, other: &f32) -> bool { (*self) < (*other) } @@ -110,6 +112,7 @@ impl Ord for f32 { #[inline] fn gt(&self, other: &f32) -> bool { (*self) > (*other) } } +#[cfg(not(test))] impl Eq for f32 { #[inline] fn eq(&self, other: &f32) -> bool { (*self) == (*other) } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index f83ab510b7f..b15b4566cdd 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -10,11 +10,12 @@ //! Operations and constants for 64-bits floats (`f64` type) -use cmp::{Eq, Ord}; use default::Default; use intrinsics; use num::{Zero, One, Bounded, Signed, Num, Primitive}; -use ops::{Add, Sub, Mul, Div, Rem, Neg}; + +#[cfg(not(test))] use cmp::{Eq, Ord}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg}; // FIXME(#5527): These constants should be deprecated once associated // constants are implemented in favour of referencing the respective @@ -106,6 +107,7 @@ pub mod consts { pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64; } +#[cfg(not(test))] impl Ord for f64 { #[inline] fn lt(&self, other: &f64) -> bool { (*self) < (*other) } @@ -116,6 +118,7 @@ impl Ord for f64 { #[inline] fn gt(&self, other: &f64) -> bool { (*self) > (*other) } } +#[cfg(not(test))] impl Eq for f64 { #[inline] fn eq(&self, other: &f64) -> bool { (*self) == (*other) } diff --git a/src/libcore/num/i16.rs b/src/libcore/num/i16.rs index 63ad3bf7d90..361f75b9e88 100644 --- a/src/libcore/num/i16.rs +++ b/src/libcore/num/i16.rs @@ -10,14 +10,18 @@ //! Operations and constants for signed 16-bits integers (`i16` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int}; use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul}; +use option::{Option, Some, None}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Option, Some, None}; int_module!(i16, 16) diff --git a/src/libcore/num/i32.rs b/src/libcore/num/i32.rs index 25f11be459e..9071f150292 100644 --- a/src/libcore/num/i32.rs +++ b/src/libcore/num/i32.rs @@ -10,14 +10,18 @@ //! Operations and constants for signed 32-bits integers (`i32` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int}; use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul}; +use option::{Option, Some, None}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Option, Some, None}; int_module!(i32, 32) diff --git a/src/libcore/num/i64.rs b/src/libcore/num/i64.rs index 24585009a1b..ba7b715f13d 100644 --- a/src/libcore/num/i64.rs +++ b/src/libcore/num/i64.rs @@ -10,14 +10,18 @@ //! Operations and constants for signed 64-bits integers (`i64` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int}; use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul}; +use option::{Option, Some, None}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Option, Some, None}; int_module!(i64, 64) diff --git a/src/libcore/num/i8.rs b/src/libcore/num/i8.rs index 0d8b9541f7b..6ec05eb50ee 100644 --- a/src/libcore/num/i8.rs +++ b/src/libcore/num/i8.rs @@ -10,14 +10,18 @@ //! Operations and constants for signed 8-bits integers (`i8` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int}; use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul}; +use option::{Option, Some, None}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Option, Some, None}; int_module!(i8, 8) diff --git a/src/libcore/num/int.rs b/src/libcore/num/int.rs index dacfe987d90..8273fa2b39f 100644 --- a/src/libcore/num/int.rs +++ b/src/libcore/num/int.rs @@ -10,14 +10,18 @@ //! Operations and constants for architecture-sized signed integers (`int` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Signed, Num, Primitive, Int}; use num::{CheckedDiv, CheckedAdd, CheckedSub, CheckedMul}; +use option::{Option, Some, None}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitOr, BitAnd, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Option, Some, None}; #[cfg(target_word_size = "32")] int_module!(int, 32) #[cfg(target_word_size = "64")] int_module!(int, 64) diff --git a/src/libcore/num/int_macros.rs b/src/libcore/num/int_macros.rs index 771d801dcb7..7d21764eb70 100644 --- a/src/libcore/num/int_macros.rs +++ b/src/libcore/num/int_macros.rs @@ -28,15 +28,19 @@ pub static MIN: $T = (-1 as $T) << (BITS - 1); // calling the `Bounded::max_value` function. pub static MAX: $T = !MIN; +#[cfg(not(test))] impl Ord for $T { #[inline] fn lt(&self, other: &$T) -> bool { *self < *other } } +#[cfg(not(test))] impl TotalEq for $T {} +#[cfg(not(test))] impl Eq for $T { #[inline] fn eq(&self, other: &$T) -> bool { *self == *other } } +#[cfg(not(test))] impl TotalOrd for $T { #[inline] fn cmp(&self, other: &$T) -> Ordering { @@ -221,7 +225,7 @@ impl Bounded for $T { impl CheckedDiv for $T { #[inline] fn checked_div(&self, v: &$T) -> Option<$T> { - if *v == 0 { + if *v == 0 || (*self == MIN && *v == -1) { None } else { Some(self / *v) @@ -244,12 +248,9 @@ mod tests { use super::*; use int; - use i32; use num; use num::Bitwise; use num::CheckedDiv; - use num::ToStrRadix; - use str::StrSlice; #[test] fn test_overflows() { diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index c91b0f5918d..22411fef3b2 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -858,3 +858,19 @@ pub trait CheckedDiv: Div<Self, Self> { /// `None` is returned. fn checked_div(&self, v: &Self) -> Option<Self>; } + +/// Helper function for testing numeric operations +#[cfg(test)] +pub fn test_num<T:Num + NumCast + ::std::fmt::Show>(ten: T, two: T) { + assert_eq!(ten.add(&two), cast(12).unwrap()); + assert_eq!(ten.sub(&two), cast(8).unwrap()); + assert_eq!(ten.mul(&two), cast(20).unwrap()); + assert_eq!(ten.div(&two), cast(5).unwrap()); + assert_eq!(ten.rem(&two), cast(0).unwrap()); + + assert_eq!(ten.add(&two), ten + two); + assert_eq!(ten.sub(&two), ten - two); + assert_eq!(ten.mul(&two), ten * two); + assert_eq!(ten.div(&two), ten / two); + assert_eq!(ten.rem(&two), ten % two); +} diff --git a/src/libcore/num/u16.rs b/src/libcore/num/u16.rs index 92afe4955a8..96db898e3b0 100644 --- a/src/libcore/num/u16.rs +++ b/src/libcore/num/u16.rs @@ -10,14 +10,18 @@ //! Operations and constants for unsigned 16-bits integers (`u16` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive}; use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +use option::{Some, None, Option}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Some, None, Option}; uint_module!(u16, i16, 16) diff --git a/src/libcore/num/u32.rs b/src/libcore/num/u32.rs index 2cac95e5c3b..2748b001bb8 100644 --- a/src/libcore/num/u32.rs +++ b/src/libcore/num/u32.rs @@ -10,14 +10,18 @@ //! Operations and constants for unsigned 32-bits integers (`u32` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive}; use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +use option::{Some, None, Option}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Some, None, Option}; uint_module!(u32, i32, 32) diff --git a/src/libcore/num/u64.rs b/src/libcore/num/u64.rs index 7b6498b5a69..c047df09532 100644 --- a/src/libcore/num/u64.rs +++ b/src/libcore/num/u64.rs @@ -10,14 +10,18 @@ //! Operations and constants for unsigned 64-bits integer (`u64` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive}; use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +use option::{Some, None, Option}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Some, None, Option}; uint_module!(u64, i64, 64) diff --git a/src/libcore/num/u8.rs b/src/libcore/num/u8.rs index 6916c0c9f32..a6df17956a1 100644 --- a/src/libcore/num/u8.rs +++ b/src/libcore/num/u8.rs @@ -10,14 +10,18 @@ //! Operations and constants for unsigned 8-bits integers (`u8` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive}; use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +use option::{Some, None, Option}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Some, None, Option}; uint_module!(u8, i8, 8) diff --git a/src/libcore/num/uint.rs b/src/libcore/num/uint.rs index 40e4b8327dd..f988650cc01 100644 --- a/src/libcore/num/uint.rs +++ b/src/libcore/num/uint.rs @@ -10,14 +10,18 @@ //! Operations and constants for architecture-sized unsigned integers (`uint` type) -use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; use default::Default; use intrinsics; use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive}; use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +use option::{Some, None, Option}; + +#[cfg(not(test))] +use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering}; +#[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor}; +#[cfg(not(test))] use ops::{Shl, Shr, Not}; -use option::{Some, None, Option}; uint_module!(uint, int, ::int::BITS) diff --git a/src/libcore/num/uint_macros.rs b/src/libcore/num/uint_macros.rs index 12aaf41b196..a53388bd0e3 100644 --- a/src/libcore/num/uint_macros.rs +++ b/src/libcore/num/uint_macros.rs @@ -19,15 +19,19 @@ pub static BYTES : uint = ($bits / 8); pub static MIN: $T = 0 as $T; pub static MAX: $T = 0 as $T - 1 as $T; +#[cfg(not(test))] impl Ord for $T { #[inline] fn lt(&self, other: &$T) -> bool { *self < *other } } +#[cfg(not(test))] impl TotalEq for $T {} +#[cfg(not(test))] impl Eq for $T { #[inline] fn eq(&self, other: &$T) -> bool { *self == *other } } +#[cfg(not(test))] impl TotalOrd for $T { #[inline] fn cmp(&self, other: &$T) -> Ordering { @@ -184,9 +188,6 @@ mod tests { use num; use num::CheckedDiv; use num::Bitwise; - use num::ToStrRadix; - use str::StrSlice; - use u16; #[test] fn test_overflows() { |
