2014-06-28 20:57:36 +00: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.
|
|
|
|
|
2017-09-01 08:57:05 +00:00
|
|
|
use core::convert::{TryFrom, TryInto};
|
2014-11-10 05:26:10 +00:00
|
|
|
use core::cmp::PartialEq;
|
2015-01-20 23:45:07 +00:00
|
|
|
use core::fmt::Debug;
|
2015-01-06 22:33:42 +00:00
|
|
|
use core::marker::Copy;
|
2017-09-01 08:57:05 +00:00
|
|
|
use core::num::TryFromIntError;
|
2016-05-05 05:42:14 +00:00
|
|
|
use core::ops::{Add, Sub, Mul, Div, Rem};
|
|
|
|
use core::option::Option;
|
|
|
|
use core::option::Option::{Some, None};
|
2014-06-28 20:57:36 +00:00
|
|
|
|
2015-01-06 17:24:46 +00:00
|
|
|
#[macro_use]
|
2014-06-28 20:57:36 +00:00
|
|
|
mod int_macros;
|
2014-12-19 04:09:57 +00:00
|
|
|
|
2014-06-28 20:57:36 +00:00
|
|
|
mod i8;
|
|
|
|
mod i16;
|
|
|
|
mod i32;
|
|
|
|
mod i64;
|
2014-12-19 04:09:57 +00:00
|
|
|
|
2015-01-06 17:24:46 +00:00
|
|
|
#[macro_use]
|
2014-06-28 20:57:36 +00:00
|
|
|
mod uint_macros;
|
2014-12-19 04:09:57 +00:00
|
|
|
|
2014-06-28 20:57:36 +00:00
|
|
|
mod u8;
|
|
|
|
mod u16;
|
|
|
|
mod u32;
|
|
|
|
mod u64;
|
|
|
|
|
2015-04-19 05:19:54 +00:00
|
|
|
mod flt2dec;
|
2015-07-26 15:50:29 +00:00
|
|
|
mod dec2flt;
|
2015-09-20 16:34:33 +00:00
|
|
|
mod bignum;
|
2015-04-19 05:19:54 +00:00
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
/// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
|
|
|
|
/// support for larger/smaller pointer widths are added in the future.
|
|
|
|
macro_rules! assume_usize_width {
|
|
|
|
{$($it:item)*} => {#[cfg(not(any(
|
|
|
|
target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64")))]
|
|
|
|
compile_error!("The current tests of try_from on usize/isize assume that \
|
|
|
|
the pointer width is either 16, 32, or 64");
|
|
|
|
$($it)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 20:57:36 +00:00
|
|
|
/// Helper function for testing numeric operations
|
2014-11-10 05:26:10 +00:00
|
|
|
pub fn test_num<T>(ten: T, two: T) where
|
2015-04-18 06:45:55 +00:00
|
|
|
T: PartialEq
|
2014-12-31 20:45:13 +00:00
|
|
|
+ Add<Output=T> + Sub<Output=T>
|
|
|
|
+ Mul<Output=T> + Div<Output=T>
|
2015-01-20 23:45:07 +00:00
|
|
|
+ Rem<Output=T> + Debug
|
2014-12-01 23:04:46 +00:00
|
|
|
+ Copy
|
2014-11-10 05:26:10 +00:00
|
|
|
{
|
2014-12-01 23:04:46 +00: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 20:57:36 +00:00
|
|
|
}
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
#[test]
|
|
|
|
fn from_str_issue7588() {
|
|
|
|
let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
|
|
|
|
assert_eq!(u, None);
|
|
|
|
let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
|
|
|
|
assert_eq!(s, None);
|
|
|
|
}
|
2015-12-12 13:13:43 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_int_from_str_overflow() {
|
|
|
|
let mut i8_val: i8 = 127;
|
|
|
|
assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
|
|
|
|
assert_eq!("128".parse::<i8>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
i8_val = i8_val.wrapping_add(1);
|
|
|
|
assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
|
|
|
|
assert_eq!("-129".parse::<i8>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
let mut i16_val: i16 = 32_767;
|
|
|
|
assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
|
|
|
|
assert_eq!("32768".parse::<i16>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
i16_val = i16_val.wrapping_add(1);
|
|
|
|
assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
|
|
|
|
assert_eq!("-32769".parse::<i16>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
let mut i32_val: i32 = 2_147_483_647;
|
|
|
|
assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
|
|
|
|
assert_eq!("2147483648".parse::<i32>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
i32_val = i32_val.wrapping_add(1);
|
|
|
|
assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
|
|
|
|
assert_eq!("-2147483649".parse::<i32>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
let mut i64_val: i64 = 9_223_372_036_854_775_807;
|
|
|
|
assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
|
|
|
|
assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
|
2014-11-15 04:52:00 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
i64_val = i64_val.wrapping_add(1);
|
|
|
|
assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
|
|
|
|
assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leading_plus() {
|
|
|
|
assert_eq!("+127".parse::<u8>().ok(), Some(127));
|
|
|
|
assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
|
|
|
|
}
|
2015-02-24 01:40:32 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid() {
|
|
|
|
assert_eq!("--129".parse::<i8>().ok(), None);
|
|
|
|
assert_eq!("++129".parse::<i8>().ok(), None);
|
|
|
|
assert_eq!("Съешь".parse::<u8>().ok(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
assert_eq!("-".parse::<i8>().ok(), None);
|
|
|
|
assert_eq!("+".parse::<i8>().ok(), None);
|
|
|
|
assert_eq!("".parse::<u8>().ok(), None);
|
|
|
|
}
|
|
|
|
|
2017-09-01 08:57:05 +00:00
|
|
|
#[test]
|
|
|
|
fn test_infallible_try_from_int_error() {
|
|
|
|
let func = |x: i8| -> Result<i32, TryFromIntError> { Ok(x.try_into()?) };
|
|
|
|
|
|
|
|
assert!(func(0).is_ok());
|
|
|
|
}
|
|
|
|
|
2016-05-05 05:42:14 +00: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);
|
|
|
|
}
|
2015-10-03 15:35:35 +00:00
|
|
|
}
|
2016-05-05 05:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
|
|
|
|
// Signed -> Float
|
|
|
|
test_impl_from! { test_i8f32, i8, f32 }
|
|
|
|
test_impl_from! { test_i8f64, i8, f64 }
|
|
|
|
test_impl_from! { test_i16f32, i16, f32 }
|
|
|
|
test_impl_from! { test_i16f64, i16, f64 }
|
|
|
|
test_impl_from! { test_i32f64, i32, f64 }
|
|
|
|
|
|
|
|
// Unsigned -> Float
|
|
|
|
test_impl_from! { test_u8f32, u8, f32 }
|
|
|
|
test_impl_from! { test_u8f64, u8, f64 }
|
|
|
|
test_impl_from! { test_u16f32, u16, f32 }
|
|
|
|
test_impl_from! { test_u16f64, u16, f64 }
|
|
|
|
test_impl_from! { test_u32f64, u32, f64 }
|
|
|
|
|
|
|
|
// Float -> Float
|
|
|
|
#[test]
|
|
|
|
fn test_f32f64() {
|
|
|
|
use core::f32;
|
|
|
|
|
|
|
|
let max: f64 = f32::MAX.into();
|
|
|
|
assert_eq!(max as f32, f32::MAX);
|
|
|
|
assert!(max.is_normal());
|
|
|
|
|
|
|
|
let min: f64 = f32::MIN.into();
|
|
|
|
assert_eq!(min as f32, f32::MIN);
|
|
|
|
assert!(min.is_normal());
|
|
|
|
|
|
|
|
let min_positive: f64 = f32::MIN_POSITIVE.into();
|
|
|
|
assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
|
|
|
|
assert!(min_positive.is_normal());
|
|
|
|
|
|
|
|
let epsilon: f64 = f32::EPSILON.into();
|
|
|
|
assert_eq!(epsilon as f32, f32::EPSILON);
|
|
|
|
assert!(epsilon.is_normal());
|
|
|
|
|
|
|
|
let zero: f64 = (0.0f32).into();
|
|
|
|
assert_eq!(zero as f32, 0.0f32);
|
|
|
|
assert!(zero.is_sign_positive());
|
|
|
|
|
|
|
|
let neg_zero: f64 = (-0.0f32).into();
|
|
|
|
assert_eq!(neg_zero as f32, -0.0f32);
|
|
|
|
assert!(neg_zero.is_sign_negative());
|
|
|
|
|
|
|
|
let infinity: f64 = f32::INFINITY.into();
|
|
|
|
assert_eq!(infinity as f32, f32::INFINITY);
|
|
|
|
assert!(infinity.is_infinite());
|
|
|
|
assert!(infinity.is_sign_positive());
|
|
|
|
|
|
|
|
let neg_infinity: f64 = f32::NEG_INFINITY.into();
|
|
|
|
assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
|
|
|
|
assert!(neg_infinity.is_infinite());
|
|
|
|
assert!(neg_infinity.is_sign_negative());
|
|
|
|
|
|
|
|
let nan: f64 = f32::NAN.into();
|
|
|
|
assert!(nan.is_nan());
|
|
|
|
}
|
2015-10-03 15:35:35 +00:00
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
|
|
|
|
/// Conversions where the full width of $source can be represented as $target
|
2016-05-05 05:42:14 +00:00
|
|
|
macro_rules! test_impl_try_from_always_ok {
|
|
|
|
($fn_name:ident, $source:ty, $target: ty) => {
|
|
|
|
#[test]
|
|
|
|
fn $fn_name() {
|
|
|
|
let max = <$source>::max_value();
|
|
|
|
let min = <$source>::min_value();
|
|
|
|
let zero: $source = 0;
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
|
|
|
|
max as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
|
|
|
|
min as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
|
|
|
|
zero as $target);
|
|
|
|
}
|
2015-07-18 21:59:38 +00:00
|
|
|
}
|
2016-05-05 05:42:14 +00:00
|
|
|
}
|
2015-07-18 21:59:38 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u8u8, u8, u8 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u8u16, u8, u16 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u8u32, u8, u32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u8u64, u8, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u8u128, u8, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u8i16, u8, i16 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u8i32, u8, i32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u8i64, u8, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u8i128, u8, i128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_u16u16, u16, u16 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u16u32, u16, u32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u16u64, u16, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u16u128, u16, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u16i32, u16, i32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u16i64, u16, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u16i128, u16, i128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_u32u32, u32, u32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u32u64, u32, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u32u128, u32, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u32i64, u32, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u32i128, u32, i128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_u64u64, u64, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u64u128, u64, u128 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_u64i128, u64, i128 }
|
|
|
|
|
2017-07-25 05:19:30 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u128u128, u128, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_i8i8, i8, i8 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i8i16, i8, i16 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i8i32, i8, i32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i8i64, i8, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_i8i128, i8, i128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_i16i16, i16, i16 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i16i32, i16, i32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i16i64, i16, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_i16i128, i16, i128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_i32i32, i32, i32 }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i32i64, i32, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_i32i128, i32, i128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_i64i128, i64, i128 }
|
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_i128i128, i128, i128 }
|
|
|
|
|
2017-07-25 05:19:30 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_usizeusize, usize, usize }
|
|
|
|
test_impl_try_from_always_ok! { test_try_isizeisize, isize, isize }
|
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
assume_usize_width! {
|
|
|
|
test_impl_try_from_always_ok! { test_try_u8usize, u8, usize }
|
2017-07-25 05:19:30 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_u8isize, u8, isize }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_always_ok! { test_try_i8isize, i8, isize }
|
|
|
|
|
|
|
|
test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
|
|
|
|
test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
|
|
|
|
}
|
2016-05-05 05:42:14 +00:00
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
/// Conversions where max of $source can be represented as $target,
|
2016-05-05 05:42:14 +00:00
|
|
|
macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
|
|
|
|
($fn_name:ident, $source:ty, $target:ty) => {
|
|
|
|
#[test]
|
|
|
|
fn $fn_name() {
|
|
|
|
let max = <$source>::max_value();
|
|
|
|
let min = <$source>::min_value();
|
|
|
|
let zero: $source = 0;
|
|
|
|
let neg_one: $source = -1;
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
|
|
|
|
max as $target);
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
|
|
|
|
zero as $target);
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(neg_one).is_err());
|
|
|
|
}
|
2015-02-24 01:40:32 +00:00
|
|
|
}
|
2016-05-05 05:42:14 +00:00
|
|
|
}
|
2015-10-15 02:23:44 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u8, i8, u8 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u16, i8, u16 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u32, i8, u32 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u64, i8, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u128, i8, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u16, i16, u16 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u32, i16, u32 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u64, i16, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u128, i16, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u32, i32, u32 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u64, i32, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u128, i32, u128 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u64, i64, u64 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u128, i64, u128 }
|
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i128u128, i128, u128 }
|
|
|
|
|
|
|
|
assume_usize_width! {
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8usize, i8, usize }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16usize, i16, usize }
|
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu64, isize, u64 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu128, isize, u128 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeusize, isize, usize }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversions where max of $source can not be represented as $target,
|
|
|
|
/// but min can.
|
2016-05-05 05:42:14 +00:00
|
|
|
macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
|
|
|
|
($fn_name:ident, $source:ty, $target:ty) => {
|
|
|
|
#[test]
|
|
|
|
fn $fn_name() {
|
|
|
|
let max = <$source>::max_value();
|
|
|
|
let min = <$source>::min_value();
|
|
|
|
let zero: $source = 0;
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
|
|
|
|
min as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
|
|
|
|
zero as $target);
|
2015-10-15 02:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 05:42:14 +00:00
|
|
|
}
|
2015-10-15 02:23:44 +00:00
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
|
|
|
|
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i8, u16, i8 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i16, u16, i16 }
|
|
|
|
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i8, u32, i8 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i16, u32, i16 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i32, u32, i32 }
|
|
|
|
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i8, u64, i8 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i16, u64, i16 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i32, u64, i32 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i64, u64, i64 }
|
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i8, u128, i8 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i16, u128, i16 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i32, u128, i32 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i64, u128, i64 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i128, u128, i128 }
|
|
|
|
|
|
|
|
assume_usize_width! {
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei8, usize, i8 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei16, usize, i16 }
|
|
|
|
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizeisize, usize, isize }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversions where min/max of $source can not be represented as $target.
|
2016-05-05 05:42:14 +00:00
|
|
|
macro_rules! test_impl_try_from_same_sign_err {
|
|
|
|
($fn_name:ident, $source:ty, $target:ty) => {
|
|
|
|
#[test]
|
|
|
|
fn $fn_name() {
|
|
|
|
let max = <$source>::max_value();
|
|
|
|
let min = <$source>::min_value();
|
|
|
|
let zero: $source = 0;
|
|
|
|
let t_max = <$target>::max_value();
|
|
|
|
let t_min = <$target>::min_value();
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
|
|
|
|
if min != 0 {
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
|
|
|
|
}
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
|
|
|
|
zero as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
|
|
|
|
.unwrap(),
|
|
|
|
t_max as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
|
|
|
|
.unwrap(),
|
|
|
|
t_min as $target);
|
|
|
|
}
|
2015-10-17 23:27:31 +00:00
|
|
|
}
|
2014-11-15 04:52:00 +00:00
|
|
|
}
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
|
|
|
|
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u32u8, u32, u8 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u32u16, u32, u16 }
|
|
|
|
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u64u8, u64, u8 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u64u16, u64, u16 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u64u32, u64, u32 }
|
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_same_sign_err! { test_try_u128u8, u128, u8 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u128u16, u128, u16 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u128u32, u128, u32 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_u128u64, u128, u64 }
|
|
|
|
|
2016-05-05 05:42:14 +00:00
|
|
|
test_impl_try_from_same_sign_err! { test_try_i16i8, i16, i8 }
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_same_sign_err! { test_try_isizei8, isize, i8 }
|
2016-05-05 05:42:14 +00:00
|
|
|
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i32i8, i32, i8 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
|
|
|
|
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
|
2017-02-04 23:10:28 +00:00
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_same_sign_err! { test_try_i128i8, i128, i8 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i128i16, i128, i16 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i128i32, i128, i32 }
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_i128i64, i128, i64 }
|
|
|
|
|
|
|
|
assume_usize_width! {
|
|
|
|
test_impl_try_from_same_sign_err! { test_try_usizeu8, usize, u8 }
|
|
|
|
}
|
|
|
|
|
2017-08-10 22:16:18 +00:00
|
|
|
/// Conversions where neither the min nor the max of $source can be represented by
|
2017-07-15 06:35:25 +00:00
|
|
|
/// $target, but max/min of the target can be represented by the source.
|
2017-02-04 23:10:28 +00:00
|
|
|
macro_rules! test_impl_try_from_signed_to_unsigned_err {
|
|
|
|
($fn_name:ident, $source:ty, $target:ty) => {
|
|
|
|
#[test]
|
|
|
|
fn $fn_name() {
|
|
|
|
let max = <$source>::max_value();
|
|
|
|
let min = <$source>::min_value();
|
|
|
|
let zero: $source = 0;
|
|
|
|
let t_max = <$target>::max_value();
|
|
|
|
let t_min = <$target>::min_value();
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
|
|
|
|
assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
|
|
|
|
zero as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
|
|
|
|
.unwrap(),
|
|
|
|
t_max as $target);
|
|
|
|
assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
|
|
|
|
.unwrap(),
|
|
|
|
t_min as $target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
|
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
|
|
|
|
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }
|
2017-06-04 18:39:00 +00:00
|
|
|
|
2017-07-15 06:35:25 +00:00
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u8, i128, u8 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u16, i128, u16 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u32, i128, u32 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
|
|
|
|
|
|
|
|
assume_usize_width! {
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
|
|
|
|
test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
|
|
|
|
}
|
|
|
|
|
2017-06-04 18:39:00 +00:00
|
|
|
macro_rules! test_float {
|
|
|
|
($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => { mod $modname {
|
|
|
|
// FIXME(nagisa): these tests should test for sign of -0.0
|
|
|
|
#[test]
|
|
|
|
fn min() {
|
2018-05-21 08:45:11 +00:00
|
|
|
assert_eq!((0.0 as $fty).min(0.0), 0.0);
|
|
|
|
assert_eq!((-0.0 as $fty).min(-0.0), -0.0);
|
|
|
|
assert_eq!((9.0 as $fty).min(9.0), 9.0);
|
|
|
|
assert_eq!((-9.0 as $fty).min(0.0), -9.0);
|
|
|
|
assert_eq!((0.0 as $fty).min(9.0), 0.0);
|
|
|
|
assert_eq!((-0.0 as $fty).min(-9.0), -9.0);
|
|
|
|
assert_eq!(($inf as $fty).min(9.0), 9.0);
|
|
|
|
assert_eq!((9.0 as $fty).min($inf), 9.0);
|
|
|
|
assert_eq!(($inf as $fty).min(-9.0), -9.0);
|
|
|
|
assert_eq!((-9.0 as $fty).min($inf), -9.0);
|
|
|
|
assert_eq!(($neginf as $fty).min(9.0), $neginf);
|
|
|
|
assert_eq!((9.0 as $fty).min($neginf), $neginf);
|
|
|
|
assert_eq!(($neginf as $fty).min(-9.0), $neginf);
|
|
|
|
assert_eq!((-9.0 as $fty).min($neginf), $neginf);
|
|
|
|
assert_eq!(($nan as $fty).min(9.0), 9.0);
|
|
|
|
assert_eq!(($nan as $fty).min(-9.0), -9.0);
|
|
|
|
assert_eq!((9.0 as $fty).min($nan), 9.0);
|
|
|
|
assert_eq!((-9.0 as $fty).min($nan), -9.0);
|
|
|
|
assert!(($nan as $fty).min($nan).is_nan());
|
2017-06-04 18:39:00 +00:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn max() {
|
2018-05-21 08:45:11 +00:00
|
|
|
assert_eq!((0.0 as $fty).max(0.0), 0.0);
|
|
|
|
assert_eq!((-0.0 as $fty).max(-0.0), -0.0);
|
|
|
|
assert_eq!((9.0 as $fty).max(9.0), 9.0);
|
|
|
|
assert_eq!((-9.0 as $fty).max(0.0), 0.0);
|
|
|
|
assert_eq!((0.0 as $fty).max(9.0), 9.0);
|
|
|
|
assert_eq!((-0.0 as $fty).max(-9.0), -0.0);
|
|
|
|
assert_eq!(($inf as $fty).max(9.0), $inf);
|
|
|
|
assert_eq!((9.0 as $fty).max($inf), $inf);
|
|
|
|
assert_eq!(($inf as $fty).max(-9.0), $inf);
|
|
|
|
assert_eq!((-9.0 as $fty).max($inf), $inf);
|
|
|
|
assert_eq!(($neginf as $fty).max(9.0), 9.0);
|
|
|
|
assert_eq!((9.0 as $fty).max($neginf), 9.0);
|
|
|
|
assert_eq!(($neginf as $fty).max(-9.0), -9.0);
|
|
|
|
assert_eq!((-9.0 as $fty).max($neginf), -9.0);
|
|
|
|
assert_eq!(($nan as $fty).max(9.0), 9.0);
|
|
|
|
assert_eq!(($nan as $fty).max(-9.0), -9.0);
|
|
|
|
assert_eq!((9.0 as $fty).max($nan), 9.0);
|
|
|
|
assert_eq!((-9.0 as $fty).max($nan), -9.0);
|
|
|
|
assert!(($nan as $fty).max($nan).is_nan());
|
2017-06-04 18:39:00 +00:00
|
|
|
}
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
|
|
|
test_float!(f32, f32, ::core::f32::INFINITY, ::core::f32::NEG_INFINITY, ::core::f32::NAN);
|
|
|
|
test_float!(f64, f64, ::core::f64::INFINITY, ::core::f64::NEG_INFINITY, ::core::f64::NAN);
|