Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore Num trait and use macros to implement and test NumCast #5869

Merged
merged 7 commits into from
Apr 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub use vec::{OwnedVector, OwnedCopyableVector};
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};

pub use num::NumCast;
pub use num::{Num, NumCast};
pub use ptr::Ptr;
pub use to_str::ToStr;
pub use clone::Clone;
Expand Down
82 changes: 0 additions & 82 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use cmath;
use libc::{c_float, c_int};
use num::NumCast;
use num::strconv;
use num;
use option::Option;
Expand Down Expand Up @@ -287,30 +286,6 @@ impl num::One for f32 {
fn one() -> f32 { 1.0 }
}

impl NumCast for f32 {
/**
* Cast `n` to an `f32`
*/
#[inline(always)]
fn from<N:NumCast>(n: N) -> f32 { n.to_f32() }

#[inline(always)] fn to_u8(&self) -> u8 { *self as u8 }
#[inline(always)] fn to_u16(&self) -> u16 { *self as u16 }
#[inline(always)] fn to_u32(&self) -> u32 { *self as u32 }
#[inline(always)] fn to_u64(&self) -> u64 { *self as u64 }
#[inline(always)] fn to_uint(&self) -> uint { *self as uint }

#[inline(always)] fn to_i8(&self) -> i8 { *self as i8 }
#[inline(always)] fn to_i16(&self) -> i16 { *self as i16 }
#[inline(always)] fn to_i32(&self) -> i32 { *self as i32 }
#[inline(always)] fn to_i64(&self) -> i64 { *self as i64 }
#[inline(always)] fn to_int(&self) -> int { *self as int }

#[inline(always)] fn to_f32(&self) -> f32 { *self }
#[inline(always)] fn to_f64(&self) -> f64 { *self as f64 }
#[inline(always)] fn to_float(&self) -> float { *self as float }
}

#[cfg(notest)]
impl ops::Add<f32,f32> for f32 {
fn add(&self, other: &f32) -> f32 { *self + *other }
Expand Down Expand Up @@ -580,63 +555,6 @@ impl num::FromStrRadix for f32 {
}
}

#[test]
pub fn test_num() {
let ten: f32 = num::cast(10);
let two: f32 = num::cast(2);

assert!((ten.add(&two) == num::cast(12)));
assert!((ten.sub(&two) == num::cast(8)));
assert!((ten.mul(&two) == num::cast(20)));
assert!((ten.div(&two) == num::cast(5)));
assert!((ten.modulo(&two) == num::cast(0)));
}

#[test]
fn test_numcast() {
assert!((20u == 20f32.to_uint()));
assert!((20u8 == 20f32.to_u8()));
assert!((20u16 == 20f32.to_u16()));
assert!((20u32 == 20f32.to_u32()));
assert!((20u64 == 20f32.to_u64()));
assert!((20i == 20f32.to_int()));
assert!((20i8 == 20f32.to_i8()));
assert!((20i16 == 20f32.to_i16()));
assert!((20i32 == 20f32.to_i32()));
assert!((20i64 == 20f32.to_i64()));
assert!((20f == 20f32.to_float()));
assert!((20f32 == 20f32.to_f32()));
assert!((20f64 == 20f32.to_f64()));

assert!((20f32 == NumCast::from(20u)));
assert!((20f32 == NumCast::from(20u8)));
assert!((20f32 == NumCast::from(20u16)));
assert!((20f32 == NumCast::from(20u32)));
assert!((20f32 == NumCast::from(20u64)));
assert!((20f32 == NumCast::from(20i)));
assert!((20f32 == NumCast::from(20i8)));
assert!((20f32 == NumCast::from(20i16)));
assert!((20f32 == NumCast::from(20i32)));
assert!((20f32 == NumCast::from(20i64)));
assert!((20f32 == NumCast::from(20f)));
assert!((20f32 == NumCast::from(20f32)));
assert!((20f32 == NumCast::from(20f64)));

assert!((20f32 == num::cast(20u)));
assert!((20f32 == num::cast(20u8)));
assert!((20f32 == num::cast(20u16)));
assert!((20f32 == num::cast(20u32)));
assert!((20f32 == num::cast(20u64)));
assert!((20f32 == num::cast(20i)));
assert!((20f32 == num::cast(20i8)));
assert!((20f32 == num::cast(20i16)));
assert!((20f32 == num::cast(20i32)));
assert!((20f32 == num::cast(20i64)));
assert!((20f32 == num::cast(20f)));
assert!((20f32 == num::cast(20f32)));
assert!((20f32 == num::cast(20f64)));
}

//
// Local Variables:
// mode: rust
Expand Down
82 changes: 0 additions & 82 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use cmath;
use libc::{c_double, c_int};
use num::NumCast;
use num::strconv;
use num;
use option::Option;
Expand Down Expand Up @@ -299,30 +298,6 @@ impl cmp::Ord for f64 {
fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
}

impl NumCast for f64 {
/**
* Cast `n` to an `f64`
*/
#[inline(always)]
fn from<N:NumCast>(n: N) -> f64 { n.to_f64() }

#[inline(always)] fn to_u8(&self) -> u8 { *self as u8 }
#[inline(always)] fn to_u16(&self) -> u16 { *self as u16 }
#[inline(always)] fn to_u32(&self) -> u32 { *self as u32 }
#[inline(always)] fn to_u64(&self) -> u64 { *self as u64 }
#[inline(always)] fn to_uint(&self) -> uint { *self as uint }

#[inline(always)] fn to_i8(&self) -> i8 { *self as i8 }
#[inline(always)] fn to_i16(&self) -> i16 { *self as i16 }
#[inline(always)] fn to_i32(&self) -> i32 { *self as i32 }
#[inline(always)] fn to_i64(&self) -> i64 { *self as i64 }
#[inline(always)] fn to_int(&self) -> int { *self as int }

#[inline(always)] fn to_f32(&self) -> f32 { *self as f32 }
#[inline(always)] fn to_f64(&self) -> f64 { *self }
#[inline(always)] fn to_float(&self) -> float { *self as float }
}

impl num::Zero for f64 {
#[inline(always)]
fn zero() -> f64 { 0.0 }
Expand Down Expand Up @@ -602,63 +577,6 @@ impl num::FromStrRadix for f64 {
}
}

#[test]
pub fn test_num() {
let ten: f64 = num::cast(10);
let two: f64 = num::cast(2);

assert!((ten.add(&two) == num::cast(12)));
assert!((ten.sub(&two) == num::cast(8)));
assert!((ten.mul(&two) == num::cast(20)));
assert!((ten.div(&two) == num::cast(5)));
assert!((ten.modulo(&two) == num::cast(0)));
}

#[test]
fn test_numcast() {
assert!((20u == 20f64.to_uint()));
assert!((20u8 == 20f64.to_u8()));
assert!((20u16 == 20f64.to_u16()));
assert!((20u32 == 20f64.to_u32()));
assert!((20u64 == 20f64.to_u64()));
assert!((20i == 20f64.to_int()));
assert!((20i8 == 20f64.to_i8()));
assert!((20i16 == 20f64.to_i16()));
assert!((20i32 == 20f64.to_i32()));
assert!((20i64 == 20f64.to_i64()));
assert!((20f == 20f64.to_float()));
assert!((20f32 == 20f64.to_f32()));
assert!((20f64 == 20f64.to_f64()));

assert!((20f64 == NumCast::from(20u)));
assert!((20f64 == NumCast::from(20u8)));
assert!((20f64 == NumCast::from(20u16)));
assert!((20f64 == NumCast::from(20u32)));
assert!((20f64 == NumCast::from(20u64)));
assert!((20f64 == NumCast::from(20i)));
assert!((20f64 == NumCast::from(20i8)));
assert!((20f64 == NumCast::from(20i16)));
assert!((20f64 == NumCast::from(20i32)));
assert!((20f64 == NumCast::from(20i64)));
assert!((20f64 == NumCast::from(20f)));
assert!((20f64 == NumCast::from(20f32)));
assert!((20f64 == NumCast::from(20f64)));

assert!((20f64 == num::cast(20u)));
assert!((20f64 == num::cast(20u8)));
assert!((20f64 == num::cast(20u16)));
assert!((20f64 == num::cast(20u32)));
assert!((20f64 == num::cast(20u64)));
assert!((20f64 == num::cast(20i)));
assert!((20f64 == num::cast(20i8)));
assert!((20f64 == num::cast(20i16)));
assert!((20f64 == num::cast(20i32)));
assert!((20f64 == num::cast(20i64)));
assert!((20f64 == num::cast(20f)));
assert!((20f64 == num::cast(20f32)));
assert!((20f64 == num::cast(20f64)));
}

//
// Local Variables:
// mode: rust
Expand Down
82 changes: 0 additions & 82 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// PORT this must match in width according to architecture

use f64;
use num::NumCast;
use num::strconv;
use num;
use option::Option;
Expand Down Expand Up @@ -417,30 +416,6 @@ impl num::One for float {
fn one() -> float { 1.0 }
}

impl NumCast for float {
/**
* Cast `n` to a `float`
*/
#[inline(always)]
fn from<N:NumCast>(n: N) -> float { n.to_float() }

#[inline(always)] fn to_u8(&self) -> u8 { *self as u8 }
#[inline(always)] fn to_u16(&self) -> u16 { *self as u16 }
#[inline(always)] fn to_u32(&self) -> u32 { *self as u32 }
#[inline(always)] fn to_u64(&self) -> u64 { *self as u64 }
#[inline(always)] fn to_uint(&self) -> uint { *self as uint }

#[inline(always)] fn to_i8(&self) -> i8 { *self as i8 }
#[inline(always)] fn to_i16(&self) -> i16 { *self as i16 }
#[inline(always)] fn to_i32(&self) -> i32 { *self as i32 }
#[inline(always)] fn to_i64(&self) -> i64 { *self as i64 }
#[inline(always)] fn to_int(&self) -> int { *self as int }

#[inline(always)] fn to_f32(&self) -> f32 { *self as f32 }
#[inline(always)] fn to_f64(&self) -> f64 { *self as f64 }
#[inline(always)] fn to_float(&self) -> float { *self }
}

impl num::Round for float {
#[inline(always)]
fn round(&self, mode: num::RoundMode) -> float {
Expand Down Expand Up @@ -688,63 +663,6 @@ pub fn test_round() {
assert!(round(-3.5) == -4.0);
}

#[test]
pub fn test_num() {
let ten: float = num::cast(10);
let two: float = num::cast(2);

assert!((ten.add(&two) == num::cast(12)));
assert!((ten.sub(&two) == num::cast(8)));
assert!((ten.mul(&two) == num::cast(20)));
assert!((ten.div(&two) == num::cast(5)));
assert!((ten.modulo(&two) == num::cast(0)));
}

#[test]
fn test_numcast() {
assert!((20u == 20f.to_uint()));
assert!((20u8 == 20f.to_u8()));
assert!((20u16 == 20f.to_u16()));
assert!((20u32 == 20f.to_u32()));
assert!((20u64 == 20f.to_u64()));
assert!((20i == 20f.to_int()));
assert!((20i8 == 20f.to_i8()));
assert!((20i16 == 20f.to_i16()));
assert!((20i32 == 20f.to_i32()));
assert!((20i64 == 20f.to_i64()));
assert!((20f == 20f.to_float()));
assert!((20f32 == 20f.to_f32()));
assert!((20f64 == 20f.to_f64()));

assert!((20f == NumCast::from(20u)));
assert!((20f == NumCast::from(20u8)));
assert!((20f == NumCast::from(20u16)));
assert!((20f == NumCast::from(20u32)));
assert!((20f == NumCast::from(20u64)));
assert!((20f == NumCast::from(20i)));
assert!((20f == NumCast::from(20i8)));
assert!((20f == NumCast::from(20i16)));
assert!((20f == NumCast::from(20i32)));
assert!((20f == NumCast::from(20i64)));
assert!((20f == NumCast::from(20f)));
assert!((20f == NumCast::from(20f32)));
assert!((20f == NumCast::from(20f64)));

assert!((20f == num::cast(20u)));
assert!((20f == num::cast(20u8)));
assert!((20f == num::cast(20u16)));
assert!((20f == num::cast(20u32)));
assert!((20f == num::cast(20u64)));
assert!((20f == num::cast(20i)));
assert!((20f == num::cast(20i8)));
assert!((20f == num::cast(20i16)));
assert!((20f == num::cast(20i32)));
assert!((20f == num::cast(20i64)));
assert!((20f == num::cast(20f)));
assert!((20f == num::cast(20f32)));
assert!((20f == num::cast(20f64)));
}


//
// Local Variables:
Expand Down
12 changes: 0 additions & 12 deletions src/libcore/num/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,6 @@ fn test_int_from_str_overflow() {
assert!((i64::from_str(~"-9223372036854775809").is_none()));
}

#[test]
pub fn test_num() {
let ten: T = num::cast(10);
let two: T = num::cast(2);

assert!((ten.add(&two) == num::cast(12)));
assert!((ten.sub(&two) == num::cast(8)));
assert!((ten.mul(&two) == num::cast(20)));
assert!((ten.div(&two) == num::cast(5)));
assert!((ten.modulo(&two) == num::cast(0)));
}

#[test]
pub fn test_ranges() {
let mut l = ~[];
Expand Down
Loading