diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 5e672ea0dfa86..404f1a82de5d0 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -288,26 +288,32 @@ impl num::One for f32 { #[cfg(notest)] impl ops::Add for f32 { + #[inline(always)] fn add(&self, other: &f32) -> f32 { *self + *other } } #[cfg(notest)] impl ops::Sub for f32 { + #[inline(always)] fn sub(&self, other: &f32) -> f32 { *self - *other } } #[cfg(notest)] impl ops::Mul for f32 { + #[inline(always)] fn mul(&self, other: &f32) -> f32 { *self * *other } } #[cfg(notest)] impl ops::Div for f32 { + #[inline(always)] fn div(&self, other: &f32) -> f32 { *self / *other } } #[cfg(notest)] impl ops::Modulo for f32 { + #[inline(always)] fn modulo(&self, other: &f32) -> f32 { *self % *other } } #[cfg(notest)] impl ops::Neg for f32 { + #[inline(always)] fn neg(&self) -> f32 { -*self } } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 4c96da73d2130..b4eaa0e7fdc60 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -310,26 +310,32 @@ impl num::One for f64 { #[cfg(notest)] impl ops::Add for f64 { + #[inline(always)] fn add(&self, other: &f64) -> f64 { *self + *other } } #[cfg(notest)] impl ops::Sub for f64 { + #[inline(always)] fn sub(&self, other: &f64) -> f64 { *self - *other } } #[cfg(notest)] impl ops::Mul for f64 { + #[inline(always)] fn mul(&self, other: &f64) -> f64 { *self * *other } } #[cfg(notest)] impl ops::Div for f64 { + #[inline(always)] fn div(&self, other: &f64) -> f64 { *self / *other } } #[cfg(notest)] impl ops::Modulo for f64 { + #[inline(always)] fn modulo(&self, other: &f64) -> f64 { *self % *other } } #[cfg(notest)] impl ops::Neg for f64 { + #[inline(always)] fn neg(&self) -> f64 { -*self } } diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index 488756787b5cd..38111210c73c1 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -387,15 +387,21 @@ pub fn tan(x: float) -> float { #[cfg(notest)] impl Eq for float { + #[inline(always)] fn eq(&self, other: &float) -> bool { (*self) == (*other) } + #[inline(always)] fn ne(&self, other: &float) -> bool { (*self) != (*other) } } #[cfg(notest)] impl Ord for float { + #[inline(always)] fn lt(&self, other: &float) -> bool { (*self) < (*other) } + #[inline(always)] fn le(&self, other: &float) -> bool { (*self) <= (*other) } + #[inline(always)] fn ge(&self, other: &float) -> bool { (*self) >= (*other) } + #[inline(always)] fn gt(&self, other: &float) -> bool { (*self) > (*other) } } @@ -444,26 +450,32 @@ impl num::Round for float { #[cfg(notest)] impl ops::Add for float { + #[inline(always)] fn add(&self, other: &float) -> float { *self + *other } } #[cfg(notest)] impl ops::Sub for float { + #[inline(always)] fn sub(&self, other: &float) -> float { *self - *other } } #[cfg(notest)] impl ops::Mul for float { + #[inline(always)] fn mul(&self, other: &float) -> float { *self * *other } } #[cfg(notest)] impl ops::Div for float { + #[inline(always)] fn div(&self, other: &float) -> float { *self / *other } } #[cfg(notest)] impl ops::Modulo for float { + #[inline(always)] fn modulo(&self, other: &float) -> float { *self % *other } } #[cfg(notest)] impl ops::Neg for float { + #[inline(always)] fn neg(&self) -> float { -*self } } @@ -474,29 +486,29 @@ mod tests { #[test] pub fn test_to_str_exact_do_decimal() { let s = to_str_exact(5.0, 4u); - assert!(s == ~"5.0000"); + assert_eq!(s, ~"5.0000"); } #[test] pub fn test_from_str() { - assert!(from_str(~"3") == Some(3.)); - assert!(from_str(~"3.14") == Some(3.14)); - assert!(from_str(~"+3.14") == Some(3.14)); - assert!(from_str(~"-3.14") == Some(-3.14)); - assert!(from_str(~"2.5E10") == Some(25000000000.)); - assert!(from_str(~"2.5e10") == Some(25000000000.)); - assert!(from_str(~"25000000000.E-10") == Some(2.5)); - assert!(from_str(~".") == Some(0.)); - assert!(from_str(~".e1") == Some(0.)); - assert!(from_str(~".e-1") == Some(0.)); - assert!(from_str(~"5.") == Some(5.)); - assert!(from_str(~".5") == Some(0.5)); - assert!(from_str(~"0.5") == Some(0.5)); - assert!(from_str(~"-.5") == Some(-0.5)); - assert!(from_str(~"-5") == Some(-5.)); - assert!(from_str(~"inf") == Some(infinity)); - assert!(from_str(~"+inf") == Some(infinity)); - assert!(from_str(~"-inf") == Some(neg_infinity)); + assert_eq!(from_str(~"3"), Some(3.)); + assert_eq!(from_str(~"3.14"), Some(3.14)); + assert_eq!(from_str(~"+3.14"), Some(3.14)); + assert_eq!(from_str(~"-3.14"), Some(-3.14)); + assert_eq!(from_str(~"2.5E10"), Some(25000000000.)); + assert_eq!(from_str(~"2.5e10"), Some(25000000000.)); + assert_eq!(from_str(~"25000000000.E-10"), Some(2.5)); + assert_eq!(from_str(~"."), Some(0.)); + assert_eq!(from_str(~".e1"), Some(0.)); + assert_eq!(from_str(~".e-1"), Some(0.)); + assert_eq!(from_str(~"5."), Some(5.)); + assert_eq!(from_str(~".5"), Some(0.5)); + assert_eq!(from_str(~"0.5"), Some(0.5)); + assert_eq!(from_str(~"-.5"), Some(-0.5)); + assert_eq!(from_str(~"-5"), Some(-5.)); + assert_eq!(from_str(~"inf"), Some(infinity)); + assert_eq!(from_str(~"+inf"), Some(infinity)); + assert_eq!(from_str(~"-inf"), Some(neg_infinity)); // note: NaN != NaN, hence this slightly complex test match from_str(~"NaN") { Some(f) => assert!(is_NaN(f)), @@ -526,24 +538,24 @@ mod tests { #[test] pub fn test_from_str_hex() { - assert!(from_str_hex(~"a4") == Some(164.)); - assert!(from_str_hex(~"a4.fe") == Some(164.9921875)); - assert!(from_str_hex(~"-a4.fe") == Some(-164.9921875)); - assert!(from_str_hex(~"+a4.fe") == Some(164.9921875)); - assert!(from_str_hex(~"ff0P4") == Some(0xff00 as float)); - assert!(from_str_hex(~"ff0p4") == Some(0xff00 as float)); - assert!(from_str_hex(~"ff0p-4") == Some(0xff as float)); - assert!(from_str_hex(~".") == Some(0.)); - assert!(from_str_hex(~".p1") == Some(0.)); - assert!(from_str_hex(~".p-1") == Some(0.)); - assert!(from_str_hex(~"f.") == Some(15.)); - assert!(from_str_hex(~".f") == Some(0.9375)); - assert!(from_str_hex(~"0.f") == Some(0.9375)); - assert!(from_str_hex(~"-.f") == Some(-0.9375)); - assert!(from_str_hex(~"-f") == Some(-15.)); - assert!(from_str_hex(~"inf") == Some(infinity)); - assert!(from_str_hex(~"+inf") == Some(infinity)); - assert!(from_str_hex(~"-inf") == Some(neg_infinity)); + assert_eq!(from_str_hex(~"a4"), Some(164.)); + assert_eq!(from_str_hex(~"a4.fe"), Some(164.9921875)); + assert_eq!(from_str_hex(~"-a4.fe"), Some(-164.9921875)); + assert_eq!(from_str_hex(~"+a4.fe"), Some(164.9921875)); + assert_eq!(from_str_hex(~"ff0P4"), Some(0xff00 as float)); + assert_eq!(from_str_hex(~"ff0p4"), Some(0xff00 as float)); + assert_eq!(from_str_hex(~"ff0p-4"), Some(0xff as float)); + assert_eq!(from_str_hex(~"."), Some(0.)); + assert_eq!(from_str_hex(~".p1"), Some(0.)); + assert_eq!(from_str_hex(~".p-1"), Some(0.)); + assert_eq!(from_str_hex(~"f."), Some(15.)); + assert_eq!(from_str_hex(~".f"), Some(0.9375)); + assert_eq!(from_str_hex(~"0.f"), Some(0.9375)); + assert_eq!(from_str_hex(~"-.f"), Some(-0.9375)); + assert_eq!(from_str_hex(~"-f"), Some(-15.)); + assert_eq!(from_str_hex(~"inf"), Some(infinity)); + assert_eq!(from_str_hex(~"+inf"), Some(infinity)); + assert_eq!(from_str_hex(~"-inf"), Some(neg_infinity)); // note: NaN != NaN, hence this slightly complex test match from_str_hex(~"NaN") { Some(f) => assert!(is_NaN(f)), @@ -558,11 +570,11 @@ mod tests { Some(v) if is_zero(v) => assert!(is_positive(v)), _ => fail!() } - assert!(from_str_hex(~"e") == Some(14.)); - assert!(from_str_hex(~"E") == Some(14.)); - assert!(from_str_hex(~"E1") == Some(225.)); - assert!(from_str_hex(~"1e1e1") == Some(123361.)); - assert!(from_str_hex(~"1e1.1") == Some(481.0625)); + assert_eq!(from_str_hex(~"e"), Some(14.)); + assert_eq!(from_str_hex(~"E"), Some(14.)); + assert_eq!(from_str_hex(~"E1"), Some(225.)); + assert_eq!(from_str_hex(~"1e1e1"), Some(123361.)); + assert_eq!(from_str_hex(~"1e1.1"), Some(481.0625)); assert!(from_str_hex(~"").is_none()); assert!(from_str_hex(~"x").is_none()); @@ -578,92 +590,92 @@ mod tests { #[test] pub fn test_to_str_hex() { - assert!(to_str_hex(164.) == ~"a4"); - assert!(to_str_hex(164.9921875) == ~"a4.fe"); - assert!(to_str_hex(-164.9921875) == ~"-a4.fe"); - assert!(to_str_hex(0xff00 as float) == ~"ff00"); - assert!(to_str_hex(-(0xff00 as float)) == ~"-ff00"); - assert!(to_str_hex(0.) == ~"0"); - assert!(to_str_hex(15.) == ~"f"); - assert!(to_str_hex(-15.) == ~"-f"); - assert!(to_str_hex(0.9375) == ~"0.f"); - assert!(to_str_hex(-0.9375) == ~"-0.f"); - assert!(to_str_hex(infinity) == ~"inf"); - assert!(to_str_hex(neg_infinity) == ~"-inf"); - assert!(to_str_hex(NaN) == ~"NaN"); - assert!(to_str_hex(0.) == ~"0"); - assert!(to_str_hex(-0.) == ~"-0"); + assert_eq!(to_str_hex(164.), ~"a4"); + assert_eq!(to_str_hex(164.9921875), ~"a4.fe"); + assert_eq!(to_str_hex(-164.9921875), ~"-a4.fe"); + assert_eq!(to_str_hex(0xff00 as float), ~"ff00"); + assert_eq!(to_str_hex(-(0xff00 as float)), ~"-ff00"); + assert_eq!(to_str_hex(0.), ~"0"); + assert_eq!(to_str_hex(15.), ~"f"); + assert_eq!(to_str_hex(-15.), ~"-f"); + assert_eq!(to_str_hex(0.9375), ~"0.f"); + assert_eq!(to_str_hex(-0.9375), ~"-0.f"); + assert_eq!(to_str_hex(infinity), ~"inf"); + assert_eq!(to_str_hex(neg_infinity), ~"-inf"); + assert_eq!(to_str_hex(NaN), ~"NaN"); + assert_eq!(to_str_hex(0.), ~"0"); + assert_eq!(to_str_hex(-0.), ~"-0"); } #[test] pub fn test_to_str_radix() { - assert!(to_str_radix(36., 36u) == ~"10"); - assert!(to_str_radix(8.125, 2u) == ~"1000.001"); + assert_eq!(to_str_radix(36., 36u), ~"10"); + assert_eq!(to_str_radix(8.125, 2u), ~"1000.001"); } #[test] pub fn test_from_str_radix() { - assert!(from_str_radix(~"10", 36u) == Some(36.)); - assert!(from_str_radix(~"1000.001", 2u) == Some(8.125)); + assert_eq!(from_str_radix(~"10", 36u), Some(36.)); + assert_eq!(from_str_radix(~"1000.001", 2u), Some(8.125)); } #[test] pub fn test_positive() { - assert!((is_positive(infinity))); - assert!((is_positive(1.))); - assert!((is_positive(0.))); - assert!((!is_positive(-1.))); - assert!((!is_positive(neg_infinity))); - assert!((!is_positive(1./neg_infinity))); - assert!((!is_positive(NaN))); + assert!(is_positive(infinity)); + assert!(is_positive(1.)); + assert!(is_positive(0.)); + assert!(!is_positive(-1.)); + assert!(!is_positive(neg_infinity)); + assert!(!is_positive(1./neg_infinity)); + assert!(!is_positive(NaN)); } #[test] pub fn test_negative() { - assert!((!is_negative(infinity))); - assert!((!is_negative(1.))); - assert!((!is_negative(0.))); - assert!((is_negative(-1.))); - assert!((is_negative(neg_infinity))); - assert!((is_negative(1./neg_infinity))); - assert!((!is_negative(NaN))); + assert!(!is_negative(infinity)); + assert!(!is_negative(1.)); + assert!(!is_negative(0.)); + assert!(is_negative(-1.)); + assert!(is_negative(neg_infinity)); + assert!(is_negative(1./neg_infinity)); + assert!(!is_negative(NaN)); } #[test] pub fn test_nonpositive() { - assert!((!is_nonpositive(infinity))); - assert!((!is_nonpositive(1.))); - assert!((!is_nonpositive(0.))); - assert!((is_nonpositive(-1.))); - assert!((is_nonpositive(neg_infinity))); - assert!((is_nonpositive(1./neg_infinity))); - assert!((!is_nonpositive(NaN))); + assert!(!is_nonpositive(infinity)); + assert!(!is_nonpositive(1.)); + assert!(!is_nonpositive(0.)); + assert!(is_nonpositive(-1.)); + assert!(is_nonpositive(neg_infinity)); + assert!(is_nonpositive(1./neg_infinity)); + assert!(!is_nonpositive(NaN)); } #[test] pub fn test_nonnegative() { - assert!((is_nonnegative(infinity))); - assert!((is_nonnegative(1.))); - assert!((is_nonnegative(0.))); - assert!((!is_nonnegative(-1.))); - assert!((!is_nonnegative(neg_infinity))); - assert!((!is_nonnegative(1./neg_infinity))); - assert!((!is_nonnegative(NaN))); + assert!(is_nonnegative(infinity)); + assert!(is_nonnegative(1.)); + assert!(is_nonnegative(0.)); + assert!(!is_nonnegative(-1.)); + assert!(!is_nonnegative(neg_infinity)); + assert!(!is_nonnegative(1./neg_infinity)); + assert!(!is_nonnegative(NaN)); } #[test] pub fn test_to_str_inf() { - assert!(to_str_digits(infinity, 10u) == ~"inf"); - assert!(to_str_digits(-infinity, 10u) == ~"-inf"); + assert_eq!(to_str_digits(infinity, 10u), ~"inf"); + assert_eq!(to_str_digits(-infinity, 10u), ~"-inf"); } #[test] pub fn test_round() { - assert!(round(5.8) == 6.0); - assert!(round(5.2) == 5.0); - assert!(round(3.0) == 3.0); - assert!(round(2.5) == 3.0); - assert!(round(-3.5) == -4.0); + assert_eq!(round(5.8), 6.0); + assert_eq!(round(5.2), 5.0); + assert_eq!(round(3.0), 3.0); + assert_eq!(round(2.5), 3.0); + assert_eq!(round(-3.5), -4.0); } } diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs index b495f9e7088fe..8fd61fb6187e4 100644 --- a/src/libcore/num/int-template.rs +++ b/src/libcore/num/int-template.rs @@ -177,29 +177,66 @@ impl num::One for T { #[cfg(notest)] impl ops::Add for T { + #[inline(always)] fn add(&self, other: &T) -> T { *self + *other } } #[cfg(notest)] impl ops::Sub for T { + #[inline(always)] fn sub(&self, other: &T) -> T { *self - *other } } #[cfg(notest)] impl ops::Mul for T { + #[inline(always)] fn mul(&self, other: &T) -> T { *self * *other } } #[cfg(notest)] impl ops::Div for T { + #[inline(always)] fn div(&self, other: &T) -> T { *self / *other } } #[cfg(notest)] impl ops::Modulo for T { + #[inline(always)] fn modulo(&self, other: &T) -> T { *self % *other } } #[cfg(notest)] impl ops::Neg for T { + #[inline(always)] fn neg(&self) -> T { -*self } } +#[cfg(notest)] +impl ops::BitOr for T { + #[inline(always)] + fn bitor(&self, other: &T) -> T { *self | *other } +} +#[cfg(notest)] +impl ops::BitAnd for T { + #[inline(always)] + fn bitand(&self, other: &T) -> T { *self & *other } +} +#[cfg(notest)] +impl ops::BitXor for T { + #[inline(always)] + fn bitxor(&self, other: &T) -> T { *self ^ *other } +} +#[cfg(notest)] +impl ops::Shl for T { + #[inline(always)] + fn shl(&self, other: &T) -> T { *self << *other } +} +#[cfg(notest)] +impl ops::Shr for T { + #[inline(always)] + fn shr(&self, other: &T) -> T { *self >> *other } +} +#[cfg(notest)] +impl ops::Not for T { + #[inline(always)] + fn not(&self) -> T { !*self } +} + // String conversion functions and impl str -> num /// Parse a string as a number in base 10. @@ -283,19 +320,29 @@ mod tests { use super::inst::T; use prelude::*; + #[test] + fn test_bitwise_ops() { + assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T))); + assert_eq!(0b1000 as T, (0b1100 as T).bitand(&(0b1010 as T))); + assert_eq!(0b0110 as T, (0b1100 as T).bitxor(&(0b1010 as T))); + assert_eq!(0b1110 as T, (0b0111 as T).shl(&(1 as T))); + assert_eq!(0b0111 as T, (0b1110 as T).shr(&(1 as T))); + assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not()); + } + #[test] fn test_from_str() { - assert!(from_str(~"0") == Some(0 as T)); - assert!(from_str(~"3") == Some(3 as T)); - assert!(from_str(~"10") == Some(10 as T)); - assert!(i32::from_str(~"123456789") == Some(123456789 as i32)); - assert!(from_str(~"00100") == Some(100 as T)); - - assert!(from_str(~"-1") == Some(-1 as T)); - assert!(from_str(~"-3") == Some(-3 as T)); - assert!(from_str(~"-10") == Some(-10 as T)); - assert!(i32::from_str(~"-123456789") == Some(-123456789 as i32)); - assert!(from_str(~"-00100") == Some(-100 as T)); + assert_eq!(from_str(~"0"), Some(0 as T)); + assert_eq!(from_str(~"3"), Some(3 as T)); + assert_eq!(from_str(~"10"), Some(10 as T)); + assert_eq!(i32::from_str(~"123456789"), Some(123456789 as i32)); + assert_eq!(from_str(~"00100"), Some(100 as T)); + + assert_eq!(from_str(~"-1"), Some(-1 as T)); + assert_eq!(from_str(~"-3"), Some(-3 as T)); + assert_eq!(from_str(~"-10"), Some(-10 as T)); + assert_eq!(i32::from_str(~"-123456789"), Some(-123456789 as i32)); + assert_eq!(from_str(~"-00100"), Some(-100 as T)); assert!(from_str(~" ").is_none()); assert!(from_str(~"x").is_none()); @@ -304,28 +351,23 @@ mod tests { #[test] fn test_parse_bytes() { use str::to_bytes; - assert!(parse_bytes(to_bytes(~"123"), 10u) == Some(123 as T)); - assert!(parse_bytes(to_bytes(~"1001"), 2u) == Some(9 as T)); - assert!(parse_bytes(to_bytes(~"123"), 8u) == Some(83 as T)); - assert!(i32::parse_bytes(to_bytes(~"123"), 16u) == Some(291 as i32)); - assert!(i32::parse_bytes(to_bytes(~"ffff"), 16u) == - Some(65535 as i32)); - assert!(i32::parse_bytes(to_bytes(~"FFFF"), 16u) == - Some(65535 as i32)); - assert!(parse_bytes(to_bytes(~"z"), 36u) == Some(35 as T)); - assert!(parse_bytes(to_bytes(~"Z"), 36u) == Some(35 as T)); - - assert!(parse_bytes(to_bytes(~"-123"), 10u) == Some(-123 as T)); - assert!(parse_bytes(to_bytes(~"-1001"), 2u) == Some(-9 as T)); - assert!(parse_bytes(to_bytes(~"-123"), 8u) == Some(-83 as T)); - assert!(i32::parse_bytes(to_bytes(~"-123"), 16u) == - Some(-291 as i32)); - assert!(i32::parse_bytes(to_bytes(~"-ffff"), 16u) == - Some(-65535 as i32)); - assert!(i32::parse_bytes(to_bytes(~"-FFFF"), 16u) == - Some(-65535 as i32)); - assert!(parse_bytes(to_bytes(~"-z"), 36u) == Some(-35 as T)); - assert!(parse_bytes(to_bytes(~"-Z"), 36u) == Some(-35 as T)); + assert_eq!(parse_bytes(to_bytes(~"123"), 10u), Some(123 as T)); + assert_eq!(parse_bytes(to_bytes(~"1001"), 2u), Some(9 as T)); + assert_eq!(parse_bytes(to_bytes(~"123"), 8u), Some(83 as T)); + assert_eq!(i32::parse_bytes(to_bytes(~"123"), 16u), Some(291 as i32)); + assert_eq!(i32::parse_bytes(to_bytes(~"ffff"), 16u), Some(65535 as i32)); + assert_eq!(i32::parse_bytes(to_bytes(~"FFFF"), 16u), Some(65535 as i32)); + assert_eq!(parse_bytes(to_bytes(~"z"), 36u), Some(35 as T)); + assert_eq!(parse_bytes(to_bytes(~"Z"), 36u), Some(35 as T)); + + assert_eq!(parse_bytes(to_bytes(~"-123"), 10u), Some(-123 as T)); + assert_eq!(parse_bytes(to_bytes(~"-1001"), 2u), Some(-9 as T)); + assert_eq!(parse_bytes(to_bytes(~"-123"), 8u), Some(-83 as T)); + assert_eq!(i32::parse_bytes(to_bytes(~"-123"), 16u), Some(-291 as i32)); + assert_eq!(i32::parse_bytes(to_bytes(~"-ffff"), 16u), Some(-65535 as i32)); + assert_eq!(i32::parse_bytes(to_bytes(~"-FFFF"), 16u), Some(-65535 as i32)); + assert_eq!(parse_bytes(to_bytes(~"-z"), 36u), Some(-35 as T)); + assert_eq!(parse_bytes(to_bytes(~"-Z"), 36u), Some(-35 as T)); assert!(parse_bytes(to_bytes(~"Z"), 35u).is_none()); assert!(parse_bytes(to_bytes(~"-9"), 2u).is_none()); @@ -333,74 +375,74 @@ mod tests { #[test] fn test_to_str() { - assert!((to_str_radix(0 as T, 10u) == ~"0")); - assert!((to_str_radix(1 as T, 10u) == ~"1")); - assert!((to_str_radix(-1 as T, 10u) == ~"-1")); - assert!((to_str_radix(127 as T, 16u) == ~"7f")); - assert!((to_str_radix(100 as T, 10u) == ~"100")); + assert_eq!(to_str_radix(0 as T, 10u), ~"0"); + assert_eq!(to_str_radix(1 as T, 10u), ~"1"); + assert_eq!(to_str_radix(-1 as T, 10u), ~"-1"); + assert_eq!(to_str_radix(127 as T, 16u), ~"7f"); + assert_eq!(to_str_radix(100 as T, 10u), ~"100"); } #[test] fn test_int_to_str_overflow() { let mut i8_val: i8 = 127_i8; - assert!((i8::to_str(i8_val) == ~"127")); + assert_eq!(i8::to_str(i8_val), ~"127"); i8_val += 1 as i8; - assert!((i8::to_str(i8_val) == ~"-128")); + assert_eq!(i8::to_str(i8_val), ~"-128"); let mut i16_val: i16 = 32_767_i16; - assert!((i16::to_str(i16_val) == ~"32767")); + assert_eq!(i16::to_str(i16_val), ~"32767"); i16_val += 1 as i16; - assert!((i16::to_str(i16_val) == ~"-32768")); + assert_eq!(i16::to_str(i16_val), ~"-32768"); let mut i32_val: i32 = 2_147_483_647_i32; - assert!((i32::to_str(i32_val) == ~"2147483647")); + assert_eq!(i32::to_str(i32_val), ~"2147483647"); i32_val += 1 as i32; - assert!((i32::to_str(i32_val) == ~"-2147483648")); + assert_eq!(i32::to_str(i32_val), ~"-2147483648"); let mut i64_val: i64 = 9_223_372_036_854_775_807_i64; - assert!((i64::to_str(i64_val) == ~"9223372036854775807")); + assert_eq!(i64::to_str(i64_val), ~"9223372036854775807"); i64_val += 1 as i64; - assert!((i64::to_str(i64_val) == ~"-9223372036854775808")); + assert_eq!(i64::to_str(i64_val), ~"-9223372036854775808"); } #[test] fn test_int_from_str_overflow() { let mut i8_val: i8 = 127_i8; - assert!((i8::from_str(~"127") == Some(i8_val))); - assert!((i8::from_str(~"128").is_none())); + assert_eq!(i8::from_str(~"127"), Some(i8_val)); + assert!(i8::from_str(~"128").is_none()); i8_val += 1 as i8; - assert!((i8::from_str(~"-128") == Some(i8_val))); - assert!((i8::from_str(~"-129").is_none())); + assert_eq!(i8::from_str(~"-128"), Some(i8_val)); + assert!(i8::from_str(~"-129").is_none()); let mut i16_val: i16 = 32_767_i16; - assert!((i16::from_str(~"32767") == Some(i16_val))); - assert!((i16::from_str(~"32768").is_none())); + assert_eq!(i16::from_str(~"32767"), Some(i16_val)); + assert!(i16::from_str(~"32768").is_none()); i16_val += 1 as i16; - assert!((i16::from_str(~"-32768") == Some(i16_val))); - assert!((i16::from_str(~"-32769").is_none())); + assert_eq!(i16::from_str(~"-32768"), Some(i16_val)); + assert!(i16::from_str(~"-32769").is_none()); let mut i32_val: i32 = 2_147_483_647_i32; - assert!((i32::from_str(~"2147483647") == Some(i32_val))); - assert!((i32::from_str(~"2147483648").is_none())); + assert_eq!(i32::from_str(~"2147483647"), Some(i32_val)); + assert!(i32::from_str(~"2147483648").is_none()); i32_val += 1 as i32; - assert!((i32::from_str(~"-2147483648") == Some(i32_val))); - assert!((i32::from_str(~"-2147483649").is_none())); + assert_eq!(i32::from_str(~"-2147483648"), Some(i32_val)); + assert!(i32::from_str(~"-2147483649").is_none()); let mut i64_val: i64 = 9_223_372_036_854_775_807_i64; - assert!((i64::from_str(~"9223372036854775807") == Some(i64_val))); - assert!((i64::from_str(~"9223372036854775808").is_none())); + assert_eq!(i64::from_str(~"9223372036854775807"), Some(i64_val)); + assert!(i64::from_str(~"9223372036854775808").is_none()); i64_val += 1 as i64; - assert!((i64::from_str(~"-9223372036854775808") == Some(i64_val))); - assert!((i64::from_str(~"-9223372036854775809").is_none())); + assert_eq!(i64::from_str(~"-9223372036854775808"), Some(i64_val)); + assert!(i64::from_str(~"-9223372036854775809").is_none()); } #[test] diff --git a/src/libcore/num/num.rs b/src/libcore/num/num.rs index 19a1e276d3754..5834214475219 100644 --- a/src/libcore/num/num.rs +++ b/src/libcore/num/num.rs @@ -77,7 +77,7 @@ pub enum RoundMode { * * ~~~ * let twenty: f32 = num::cast(0x14); - * assert!(twenty == 20f32); + * assert_eq!(twenty, 20f32); * ~~~ */ #[inline(always)] @@ -196,17 +196,17 @@ pub fn pow_with_uint+Mul>( #[cfg(test)] fn test_num(ten: T, two: T) { - assert!(ten.add(&two) == cast(12)); - assert!(ten.sub(&two) == cast(8)); - assert!(ten.mul(&two) == cast(20)); - assert!(ten.div(&two) == cast(5)); - assert!(ten.modulo(&two) == cast(0)); - - assert!(ten.add(&two) == ten + two); - assert!(ten.sub(&two) == ten - two); - assert!(ten.mul(&two) == ten * two); - assert!(ten.div(&two) == ten / two); - assert!(ten.modulo(&two) == ten % two); + assert_eq!(ten.add(&two), cast(12)); + assert_eq!(ten.sub(&two), cast(8)); + assert_eq!(ten.mul(&two), cast(20)); + assert_eq!(ten.div(&two), cast(5)); + assert_eq!(ten.modulo(&two), cast(0)); + + 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.modulo(&two), ten % two); } #[test] fn test_u8_num() { test_num(10u8, 2u8) } @@ -227,47 +227,47 @@ macro_rules! test_cast_20( ($_20:expr) => ({ let _20 = $_20; - assert!(20u == _20.to_uint()); - assert!(20u8 == _20.to_u8()); - assert!(20u16 == _20.to_u16()); - assert!(20u32 == _20.to_u32()); - assert!(20u64 == _20.to_u64()); - assert!(20i == _20.to_int()); - assert!(20i8 == _20.to_i8()); - assert!(20i16 == _20.to_i16()); - assert!(20i32 == _20.to_i32()); - assert!(20i64 == _20.to_i64()); - assert!(20f == _20.to_float()); - assert!(20f32 == _20.to_f32()); - assert!(20f64 == _20.to_f64()); - - assert!(_20 == NumCast::from(20u)); - assert!(_20 == NumCast::from(20u8)); - assert!(_20 == NumCast::from(20u16)); - assert!(_20 == NumCast::from(20u32)); - assert!(_20 == NumCast::from(20u64)); - assert!(_20 == NumCast::from(20i)); - assert!(_20 == NumCast::from(20i8)); - assert!(_20 == NumCast::from(20i16)); - assert!(_20 == NumCast::from(20i32)); - assert!(_20 == NumCast::from(20i64)); - assert!(_20 == NumCast::from(20f)); - assert!(_20 == NumCast::from(20f32)); - assert!(_20 == NumCast::from(20f64)); - - assert!(_20 == cast(20u)); - assert!(_20 == cast(20u8)); - assert!(_20 == cast(20u16)); - assert!(_20 == cast(20u32)); - assert!(_20 == cast(20u64)); - assert!(_20 == cast(20i)); - assert!(_20 == cast(20i8)); - assert!(_20 == cast(20i16)); - assert!(_20 == cast(20i32)); - assert!(_20 == cast(20i64)); - assert!(_20 == cast(20f)); - assert!(_20 == cast(20f32)); - assert!(_20 == cast(20f64)); + assert_eq!(20u, _20.to_uint()); + assert_eq!(20u8, _20.to_u8()); + assert_eq!(20u16, _20.to_u16()); + assert_eq!(20u32, _20.to_u32()); + assert_eq!(20u64, _20.to_u64()); + assert_eq!(20i, _20.to_int()); + assert_eq!(20i8, _20.to_i8()); + assert_eq!(20i16, _20.to_i16()); + assert_eq!(20i32, _20.to_i32()); + assert_eq!(20i64, _20.to_i64()); + assert_eq!(20f, _20.to_float()); + assert_eq!(20f32, _20.to_f32()); + assert_eq!(20f64, _20.to_f64()); + + assert_eq!(_20, NumCast::from(20u)); + assert_eq!(_20, NumCast::from(20u8)); + assert_eq!(_20, NumCast::from(20u16)); + assert_eq!(_20, NumCast::from(20u32)); + assert_eq!(_20, NumCast::from(20u64)); + assert_eq!(_20, NumCast::from(20i)); + assert_eq!(_20, NumCast::from(20i8)); + assert_eq!(_20, NumCast::from(20i16)); + assert_eq!(_20, NumCast::from(20i32)); + assert_eq!(_20, NumCast::from(20i64)); + assert_eq!(_20, NumCast::from(20f)); + assert_eq!(_20, NumCast::from(20f32)); + assert_eq!(_20, NumCast::from(20f64)); + + assert_eq!(_20, cast(20u)); + assert_eq!(_20, cast(20u8)); + assert_eq!(_20, cast(20u16)); + assert_eq!(_20, cast(20u32)); + assert_eq!(_20, cast(20u64)); + assert_eq!(_20, cast(20i)); + assert_eq!(_20, cast(20i8)); + assert_eq!(_20, cast(20i16)); + assert_eq!(_20, cast(20i32)); + assert_eq!(_20, cast(20i64)); + assert_eq!(_20, cast(20f)); + assert_eq!(_20, cast(20f32)); + assert_eq!(_20, cast(20f64)); }) ) diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs index af6557e9881f2..0109c915c6014 100644 --- a/src/libcore/num/uint-template.rs +++ b/src/libcore/num/uint-template.rs @@ -142,29 +142,66 @@ impl num::One for T { #[cfg(notest)] impl ops::Add for T { + #[inline(always)] fn add(&self, other: &T) -> T { *self + *other } } #[cfg(notest)] impl ops::Sub for T { + #[inline(always)] fn sub(&self, other: &T) -> T { *self - *other } } #[cfg(notest)] impl ops::Mul for T { + #[inline(always)] fn mul(&self, other: &T) -> T { *self * *other } } #[cfg(notest)] impl ops::Div for T { + #[inline(always)] fn div(&self, other: &T) -> T { *self / *other } } #[cfg(notest)] impl ops::Modulo for T { + #[inline(always)] fn modulo(&self, other: &T) -> T { *self % *other } } #[cfg(notest)] impl ops::Neg for T { + #[inline(always)] fn neg(&self) -> T { -*self } } +#[cfg(notest)] +impl ops::BitOr for T { + #[inline(always)] + fn bitor(&self, other: &T) -> T { *self | *other } +} +#[cfg(notest)] +impl ops::BitAnd for T { + #[inline(always)] + fn bitand(&self, other: &T) -> T { *self & *other } +} +#[cfg(notest)] +impl ops::BitXor for T { + #[inline(always)] + fn bitxor(&self, other: &T) -> T { *self ^ *other } +} +#[cfg(notest)] +impl ops::Shl for T { + #[inline(always)] + fn shl(&self, other: &T) -> T { *self << *other } +} +#[cfg(notest)] +impl ops::Shr for T { + #[inline(always)] + fn shr(&self, other: &T) -> T { *self >> *other } +} +#[cfg(notest)] +impl ops::Not for T { + #[inline(always)] + fn not(&self) -> T { !*self } +} + // String conversion functions and impl str -> num /// Parse a string as a number in base 10. @@ -247,24 +284,35 @@ mod tests { use super::*; use super::inst::T; use prelude::*; + + #[test] + fn test_bitwise_ops() { + assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T))); + assert_eq!(0b1000 as T, (0b1100 as T).bitand(&(0b1010 as T))); + assert_eq!(0b0110 as T, (0b1100 as T).bitxor(&(0b1010 as T))); + assert_eq!(0b1110 as T, (0b0111 as T).shl(&(1 as T))); + assert_eq!(0b0111 as T, (0b1110 as T).shr(&(1 as T))); + assert_eq!(max_value - (0b1011 as T), (0b1011 as T).not()); + } + #[test] pub fn test_to_str() { - assert!(to_str_radix(0 as T, 10u) == ~"0"); - assert!(to_str_radix(1 as T, 10u) == ~"1"); - assert!(to_str_radix(2 as T, 10u) == ~"2"); - assert!(to_str_radix(11 as T, 10u) == ~"11"); - assert!(to_str_radix(11 as T, 16u) == ~"b"); - assert!(to_str_radix(255 as T, 16u) == ~"ff"); - assert!(to_str_radix(0xff as T, 10u) == ~"255"); + assert_eq!(to_str_radix(0 as T, 10u), ~"0"); + assert_eq!(to_str_radix(1 as T, 10u), ~"1"); + assert_eq!(to_str_radix(2 as T, 10u), ~"2"); + assert_eq!(to_str_radix(11 as T, 10u), ~"11"); + assert_eq!(to_str_radix(11 as T, 16u), ~"b"); + assert_eq!(to_str_radix(255 as T, 16u), ~"ff"); + assert_eq!(to_str_radix(0xff as T, 10u), ~"255"); } #[test] pub fn test_from_str() { - assert!(from_str(~"0") == Some(0u as T)); - assert!(from_str(~"3") == Some(3u as T)); - assert!(from_str(~"10") == Some(10u as T)); - assert!(u32::from_str(~"123456789") == Some(123456789 as u32)); - assert!(from_str(~"00100") == Some(100u as T)); + assert_eq!(from_str(~"0"), Some(0u as T)); + assert_eq!(from_str(~"3"), Some(3u as T)); + assert_eq!(from_str(~"10"), Some(10u as T)); + assert_eq!(u32::from_str(~"123456789"), Some(123456789 as u32)); + assert_eq!(from_str(~"00100"), Some(100u as T)); assert!(from_str(~"").is_none()); assert!(from_str(~" ").is_none()); @@ -274,14 +322,12 @@ mod tests { #[test] pub fn test_parse_bytes() { use str::to_bytes; - assert!(parse_bytes(to_bytes(~"123"), 10u) == Some(123u as T)); - assert!(parse_bytes(to_bytes(~"1001"), 2u) == Some(9u as T)); - assert!(parse_bytes(to_bytes(~"123"), 8u) == Some(83u as T)); - assert!(u16::parse_bytes(to_bytes(~"123"), 16u) == - Some(291u as u16)); - assert!(u16::parse_bytes(to_bytes(~"ffff"), 16u) == - Some(65535u as u16)); - assert!(parse_bytes(to_bytes(~"z"), 36u) == Some(35u as T)); + assert_eq!(parse_bytes(to_bytes(~"123"), 10u), Some(123u as T)); + assert_eq!(parse_bytes(to_bytes(~"1001"), 2u), Some(9u as T)); + assert_eq!(parse_bytes(to_bytes(~"123"), 8u), Some(83u as T)); + assert_eq!(u16::parse_bytes(to_bytes(~"123"), 16u), Some(291u as u16)); + assert_eq!(u16::parse_bytes(to_bytes(~"ffff"), 16u), Some(65535u as u16)); + assert_eq!(parse_bytes(to_bytes(~"z"), 36u), Some(35u as T)); assert!(parse_bytes(to_bytes(~"Z"), 10u).is_none()); assert!(parse_bytes(to_bytes(~"_"), 2u).is_none()); @@ -290,63 +336,63 @@ mod tests { #[test] fn test_uint_to_str_overflow() { let mut u8_val: u8 = 255_u8; - assert!((u8::to_str(u8_val) == ~"255")); + assert_eq!(u8::to_str(u8_val), ~"255"); u8_val += 1 as u8; - assert!((u8::to_str(u8_val) == ~"0")); + assert_eq!(u8::to_str(u8_val), ~"0"); let mut u16_val: u16 = 65_535_u16; - assert!((u16::to_str(u16_val) == ~"65535")); + assert_eq!(u16::to_str(u16_val), ~"65535"); u16_val += 1 as u16; - assert!((u16::to_str(u16_val) == ~"0")); + assert_eq!(u16::to_str(u16_val), ~"0"); let mut u32_val: u32 = 4_294_967_295_u32; - assert!((u32::to_str(u32_val) == ~"4294967295")); + assert_eq!(u32::to_str(u32_val), ~"4294967295"); u32_val += 1 as u32; - assert!((u32::to_str(u32_val) == ~"0")); + assert_eq!(u32::to_str(u32_val), ~"0"); let mut u64_val: u64 = 18_446_744_073_709_551_615_u64; - assert!((u64::to_str(u64_val) == ~"18446744073709551615")); + assert_eq!(u64::to_str(u64_val), ~"18446744073709551615"); u64_val += 1 as u64; - assert!((u64::to_str(u64_val) == ~"0")); + assert_eq!(u64::to_str(u64_val), ~"0"); } #[test] fn test_uint_from_str_overflow() { let mut u8_val: u8 = 255_u8; - assert!((u8::from_str(~"255") == Some(u8_val))); - assert!((u8::from_str(~"256").is_none())); + assert_eq!(u8::from_str(~"255"), Some(u8_val)); + assert!(u8::from_str(~"256").is_none()); u8_val += 1 as u8; - assert!((u8::from_str(~"0") == Some(u8_val))); - assert!((u8::from_str(~"-1").is_none())); + assert_eq!(u8::from_str(~"0"), Some(u8_val)); + assert!(u8::from_str(~"-1").is_none()); let mut u16_val: u16 = 65_535_u16; - assert!((u16::from_str(~"65535") == Some(u16_val))); - assert!((u16::from_str(~"65536").is_none())); + assert_eq!(u16::from_str(~"65535"), Some(u16_val)); + assert!(u16::from_str(~"65536").is_none()); u16_val += 1 as u16; - assert!((u16::from_str(~"0") == Some(u16_val))); - assert!((u16::from_str(~"-1").is_none())); + assert_eq!(u16::from_str(~"0"), Some(u16_val)); + assert!(u16::from_str(~"-1").is_none()); let mut u32_val: u32 = 4_294_967_295_u32; - assert!((u32::from_str(~"4294967295") == Some(u32_val))); - assert!((u32::from_str(~"4294967296").is_none())); + assert_eq!(u32::from_str(~"4294967295"), Some(u32_val)); + assert!(u32::from_str(~"4294967296").is_none()); u32_val += 1 as u32; - assert!((u32::from_str(~"0") == Some(u32_val))); - assert!((u32::from_str(~"-1").is_none())); + assert_eq!(u32::from_str(~"0"), Some(u32_val)); + assert!(u32::from_str(~"-1").is_none()); let mut u64_val: u64 = 18_446_744_073_709_551_615_u64; - assert!((u64::from_str(~"18446744073709551615") == Some(u64_val))); - assert!((u64::from_str(~"18446744073709551616").is_none())); + assert_eq!(u64::from_str(~"18446744073709551615"), Some(u64_val)); + assert!(u64::from_str(~"18446744073709551616").is_none()); u64_val += 1 as u64; - assert!((u64::from_str(~"0") == Some(u64_val))); - assert!((u64::from_str(~"-1").is_none())); + assert_eq!(u64::from_str(~"0"), Some(u64_val)); + assert!(u64::from_str(~"-1").is_none()); } #[test]