2014-06-28 15:57:36 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-09 23:26:10 -06:00
|
|
|
use core::cmp::PartialEq;
|
2015-01-20 17:45:07 -06:00
|
|
|
use core::fmt::Debug;
|
2014-11-09 23:26:10 -06:00
|
|
|
use core::ops::{Add, Sub, Mul, Div, Rem};
|
2015-01-06 16:33:42 -06:00
|
|
|
use core::marker::Copy;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-01-06 11:24:46 -06:00
|
|
|
#[macro_use]
|
2014-06-28 15:57:36 -05:00
|
|
|
mod int_macros;
|
2014-12-18 22:09:57 -06:00
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
mod i8;
|
|
|
|
mod i16;
|
|
|
|
mod i32;
|
|
|
|
mod i64;
|
2014-12-18 22:09:57 -06:00
|
|
|
|
2015-01-06 11:24:46 -06:00
|
|
|
#[macro_use]
|
2014-06-28 15:57:36 -05:00
|
|
|
mod uint_macros;
|
2014-12-18 22:09:57 -06:00
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
mod u8;
|
|
|
|
mod u16;
|
|
|
|
mod u32;
|
|
|
|
mod u64;
|
|
|
|
|
2015-04-19 00:19:54 -05:00
|
|
|
mod flt2dec;
|
2015-07-26 10:50:29 -05:00
|
|
|
mod dec2flt;
|
2015-09-20 11:34:33 -05:00
|
|
|
mod bignum;
|
2015-04-19 00:19:54 -05:00
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
/// Helper function for testing numeric operations
|
2014-11-09 23:26:10 -06:00
|
|
|
pub fn test_num<T>(ten: T, two: T) where
|
2015-04-18 01:45:55 -05:00
|
|
|
T: PartialEq
|
2014-12-31 14:45:13 -06:00
|
|
|
+ Add<Output=T> + Sub<Output=T>
|
|
|
|
+ Mul<Output=T> + Div<Output=T>
|
2015-01-20 17:45:07 -06:00
|
|
|
+ Rem<Output=T> + Debug
|
2014-12-01 17:04:46 -06:00
|
|
|
+ Copy
|
2014-11-09 23:26:10 -06:00
|
|
|
{
|
2014-12-01 17:04:46 -06:00
|
|
|
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);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
2014-11-14 22:52:00 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2015-04-24 10:30:41 -05:00
|
|
|
mod tests {
|
2014-11-28 10:57:41 -06:00
|
|
|
use core::option::Option;
|
|
|
|
use core::option::Option::{Some, None};
|
2014-11-14 22:52:00 -06:00
|
|
|
use core::num::Float;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_str_issue7588() {
|
2015-04-18 01:45:55 -05:00
|
|
|
let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(u, None);
|
2015-04-18 01:45:55 -05:00
|
|
|
let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(s, None);
|
2015-04-18 01:45:55 -05:00
|
|
|
let s = "10000000000000000000000000000000000000000";
|
|
|
|
let f : Option<f32> = f32::from_str_radix(s, 10).ok();
|
2014-11-14 11:18:10 -06:00
|
|
|
assert_eq!(f, Some(Float::infinity()));
|
2015-04-18 01:45:55 -05:00
|
|
|
let fe : Option<f32> = f32::from_str_radix("1e40", 10).ok();
|
2014-11-14 11:18:10 -06:00
|
|
|
assert_eq!(fe, Some(Float::infinity()));
|
2014-11-14 22:52:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_str_radix_float() {
|
2015-04-18 01:45:55 -05:00
|
|
|
let x1 : Option<f64> = f64::from_str_radix("-123.456", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(x1, Some(-123.456));
|
2015-04-18 01:45:55 -05:00
|
|
|
let x2 : Option<f32> = f32::from_str_radix("123.456", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(x2, Some(123.456));
|
2015-04-18 01:45:55 -05:00
|
|
|
let x3 : Option<f32> = f32::from_str_radix("-0.0", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(x3, Some(-0.0));
|
2015-04-18 01:45:55 -05:00
|
|
|
let x4 : Option<f32> = f32::from_str_radix("0.0", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(x4, Some(0.0));
|
2015-04-18 01:45:55 -05:00
|
|
|
let x4 : Option<f32> = f32::from_str_radix("1.0", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(x4, Some(1.0));
|
2015-04-18 01:45:55 -05:00
|
|
|
let x5 : Option<f32> = f32::from_str_radix("-1.0", 10).ok();
|
2014-11-14 22:52:00 -06:00
|
|
|
assert_eq!(x5, Some(-1.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_int_from_str_overflow() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut i8_val: i8 = 127;
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
|
|
|
|
assert_eq!("128".parse::<i8>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-02-19 08:14:48 -06:00
|
|
|
i8_val = i8_val.wrapping_add(1);
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
|
|
|
|
assert_eq!("-129".parse::<i8>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut i16_val: i16 = 32_767;
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
|
|
|
|
assert_eq!("32768".parse::<i16>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-02-19 08:14:48 -06:00
|
|
|
i16_val = i16_val.wrapping_add(1);
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
|
|
|
|
assert_eq!("-32769".parse::<i16>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut i32_val: i32 = 2_147_483_647;
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
|
|
|
|
assert_eq!("2147483648".parse::<i32>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-02-19 08:14:48 -06:00
|
|
|
i32_val = i32_val.wrapping_add(1);
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
|
|
|
|
assert_eq!("-2147483649".parse::<i32>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut i64_val: i64 = 9_223_372_036_854_775_807;
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
|
|
|
|
assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
|
2015-02-19 08:14:48 -06:00
|
|
|
i64_val = i64_val.wrapping_add(1);
|
2015-01-28 00:52:32 -06:00
|
|
|
assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
|
|
|
|
assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
|
2014-11-14 22:52:00 -06:00
|
|
|
}
|
2015-02-23 19:40:32 -06:00
|
|
|
|
2015-10-03 10:35:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_leading_plus() {
|
|
|
|
assert_eq!("+127".parse::<u8>().ok(), Some(127u8));
|
|
|
|
assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807i64));
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:40:32 -06:00
|
|
|
#[test]
|
2015-07-18 16:59:38 -05:00
|
|
|
fn test_invalid() {
|
|
|
|
assert_eq!("--129".parse::<i8>().ok(), None);
|
2015-10-03 10:35:35 -05:00
|
|
|
assert_eq!("++129".parse::<i8>().ok(), None);
|
2015-07-18 16:59:38 -05:00
|
|
|
assert_eq!("Съешь".parse::<u8>().ok(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
assert_eq!("-".parse::<i8>().ok(), None);
|
2015-10-03 10:35:35 -05:00
|
|
|
assert_eq!("+".parse::<i8>().ok(), None);
|
2015-07-18 16:59:38 -05:00
|
|
|
assert_eq!("".parse::<u8>().ok(), None);
|
2015-02-23 19:40:32 -06:00
|
|
|
}
|
2015-10-14 21:23:44 -05:00
|
|
|
|
|
|
|
macro_rules! test_impl_from {
|
|
|
|
($fn_name: ident, $Small: ty, $Large: ty) => {
|
|
|
|
#[test]
|
|
|
|
fn $fn_name() {
|
|
|
|
let small_max = <$Small>::max_value();
|
|
|
|
let small_min = <$Small>::min_value();
|
|
|
|
let large_max: $Large = small_max.into();
|
|
|
|
let large_min: $Large = small_min.into();
|
|
|
|
assert_eq!(large_max as $Small, small_max);
|
|
|
|
assert_eq!(large_min as $Small, small_min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsigned -> Unsigned
|
|
|
|
test_impl_from! { test_u8u16, u8, u16 }
|
|
|
|
test_impl_from! { test_u8u32, u8, u32 }
|
|
|
|
test_impl_from! { test_u8u64, u8, u64 }
|
|
|
|
test_impl_from! { test_u8usize, u8, usize }
|
|
|
|
test_impl_from! { test_u16u32, u16, u32 }
|
|
|
|
test_impl_from! { test_u16u64, u16, u64 }
|
|
|
|
test_impl_from! { test_u32u64, u32, u64 }
|
|
|
|
|
|
|
|
// Signed -> Signed
|
|
|
|
test_impl_from! { test_i8i16, i8, i16 }
|
|
|
|
test_impl_from! { test_i8i32, i8, i32 }
|
|
|
|
test_impl_from! { test_i8i64, i8, i64 }
|
|
|
|
test_impl_from! { test_i8isize, i8, isize }
|
|
|
|
test_impl_from! { test_i16i32, i16, i32 }
|
|
|
|
test_impl_from! { test_i16i64, i16, i64 }
|
|
|
|
test_impl_from! { test_i32i64, i32, i64 }
|
|
|
|
|
|
|
|
// Unsigned -> Signed
|
|
|
|
test_impl_from! { test_u8i16, u8, i16 }
|
|
|
|
test_impl_from! { test_u8i32, u8, i32 }
|
|
|
|
test_impl_from! { test_u8i64, u8, i64 }
|
|
|
|
test_impl_from! { test_u16i32, u16, i32 }
|
|
|
|
test_impl_from! { test_u16i64, u16, i64 }
|
|
|
|
test_impl_from! { test_u32i64, u32, i64 }
|
2014-11-14 22:52:00 -06:00
|
|
|
}
|