Skip to content

Commit

Permalink
Generate NumCast impls and tests using macros
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Apr 13, 2013
1 parent 3ffd735 commit 353ce87
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 922 deletions.
70 changes: 0 additions & 70 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 @@ -592,51 +567,6 @@ pub fn test_num() {
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
70 changes: 0 additions & 70 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 @@ -614,51 +589,6 @@ pub fn test_num() {
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
70 changes: 0 additions & 70 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 @@ -700,51 +675,6 @@ pub fn test_num() {
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
71 changes: 0 additions & 71 deletions src/libcore/num/int-template/i16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,7 @@

//! Operations and constants for `i16`
use num::NumCast;

mod inst {
pub type T = i16;
pub static bits: uint = ::u16::bits;
}

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

#[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 }
#[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 as float }
}

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

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

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

0 comments on commit 353ce87

Please sign in to comment.