Skip to content

Commit

Permalink
shift int/uint tests around to avoid code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
glacjay committed Feb 22, 2015
1 parent 522d09d commit c2a2b10
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 62 deletions.
66 changes: 66 additions & 0 deletions src/libstd/num/mod.rs
Expand Up @@ -1751,6 +1751,72 @@ mod tests {
assert_pow!((8, 3 ) => 512);
assert_pow!((2u64, 50) => 1125899906842624);
}

#[test]
fn test_uint_to_str_overflow() {
let mut u8_val: u8 = 255_u8;
assert_eq!(u8_val.to_string(), "255");

u8_val += 1 as u8;
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;
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;
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;
assert_eq!(u64_val.to_string(), "0");
}

fn from_str<T: ::str::FromStr>(t: &str) -> Option<T> {
::str::FromStr::from_str(t).ok()
}

#[test]
fn test_uint_from_str_overflow() {
let mut u8_val: u8 = 255_u8;
assert_eq!(from_str::<u8>("255"), Some(u8_val));
assert_eq!(from_str::<u8>("256"), None);

u8_val += 1 as u8;
assert_eq!(from_str::<u8>("0"), Some(u8_val));
assert_eq!(from_str::<u8>("-1"), None);

let mut u16_val: u16 = 65_535_u16;
assert_eq!(from_str::<u16>("65535"), Some(u16_val));
assert_eq!(from_str::<u16>("65536"), None);

u16_val += 1 as u16;
assert_eq!(from_str::<u16>("0"), Some(u16_val));
assert_eq!(from_str::<u16>("-1"), None);

let mut u32_val: u32 = 4_294_967_295_u32;
assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
assert_eq!(from_str::<u32>("4294967296"), None);

u32_val += 1 as u32;
assert_eq!(from_str::<u32>("0"), Some(u32_val));
assert_eq!(from_str::<u32>("-1"), None);

let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
assert_eq!(from_str::<u64>("18446744073709551616"), None);

u64_val += 1 as u64;
assert_eq!(from_str::<u64>("0"), Some(u64_val));
assert_eq!(from_str::<u64>("-1"), None);
}
}


Expand Down
62 changes: 0 additions & 62 deletions src/libstd/num/uint_macros.rs
Expand Up @@ -48,68 +48,6 @@ mod tests {
assert_eq!(FromStrRadix::from_str_radix("Z", 10).ok(), None::<$T>);
assert_eq!(FromStrRadix::from_str_radix("_", 2).ok(), None::<$T>);
}

#[test]
fn test_uint_to_str_overflow() {
let mut u8_val: u8 = 255_u8;
assert_eq!(u8_val.to_string(), "255");

u8_val += 1 as u8;
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;
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;
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;
assert_eq!(u64_val.to_string(), "0");
}

#[test]
fn test_uint_from_str_overflow() {
let mut u8_val: u8 = 255_u8;
assert_eq!(from_str::<u8>("255"), Some(u8_val));
assert_eq!(from_str::<u8>("256"), None);

u8_val += 1 as u8;
assert_eq!(from_str::<u8>("0"), Some(u8_val));
assert_eq!(from_str::<u8>("-1"), None);

let mut u16_val: u16 = 65_535_u16;
assert_eq!(from_str::<u16>("65535"), Some(u16_val));
assert_eq!(from_str::<u16>("65536"), None);

u16_val += 1 as u16;
assert_eq!(from_str::<u16>("0"), Some(u16_val));
assert_eq!(from_str::<u16>("-1"), None);

let mut u32_val: u32 = 4_294_967_295_u32;
assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
assert_eq!(from_str::<u32>("4294967296"), None);

u32_val += 1 as u32;
assert_eq!(from_str::<u32>("0"), Some(u32_val));
assert_eq!(from_str::<u32>("-1"), None);

let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
assert_eq!(from_str::<u64>("18446744073709551616"), None);

u64_val += 1 as u64;
assert_eq!(from_str::<u64>("0"), Some(u64_val));
assert_eq!(from_str::<u64>("-1"), None);
}
}

) }

0 comments on commit c2a2b10

Please sign in to comment.