1 use core::cmp::PartialEq;
2 use core::convert::{TryFrom, TryInto};
4 use core::marker::Copy;
5 use core::num::{IntErrorKind, ParseIntError, TryFromIntError};
6 use core::ops::{Add, Div, Mul, Rem, Sub};
7 use core::option::Option;
8 use core::option::Option::None;
9 use core::str::FromStr;
36 /// Adds the attribute to all items in the block.
37 macro_rules! cfg_block {
38 ($(#[$attr:meta]{$($it:item)*})*) => {$($(
44 /// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
45 /// support for larger/smaller pointer widths are added in the future.
46 macro_rules! assume_usize_width {
47 {$($it:item)*} => {#[cfg(not(any(
48 target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64")))]
49 compile_error!("The current tests of try_from on usize/isize assume that \
50 the pointer width is either 16, 32, or 64");
55 /// Helper function for testing numeric operations
56 pub fn test_num<T>(ten: T, two: T)
67 assert_eq!(ten.add(two), ten + two);
68 assert_eq!(ten.sub(two), ten - two);
69 assert_eq!(ten.mul(two), ten * two);
70 assert_eq!(ten.div(two), ten / two);
71 assert_eq!(ten.rem(two), ten % two);
74 /// Helper function for asserting number parsing returns a specific error
75 fn test_parse<T>(num_str: &str, expected: Result<T, IntErrorKind>)
77 T: FromStr<Err = ParseIntError>,
78 Result<T, IntErrorKind>: PartialEq + Debug,
80 assert_eq!(num_str.parse::<T>().map_err(|e| e.kind().clone()), expected)
84 fn from_str_issue7588() {
85 let u: Option<u8> = u8::from_str_radix("1000", 10).ok();
87 let s: Option<i16> = i16::from_str_radix("80000", 10).ok();
92 fn test_int_from_str_overflow() {
93 test_parse::<i8>("127", Ok(127));
94 test_parse::<i8>("128", Err(IntErrorKind::PosOverflow));
96 test_parse::<i8>("-128", Ok(-128));
97 test_parse::<i8>("-129", Err(IntErrorKind::NegOverflow));
99 test_parse::<i16>("32767", Ok(32_767));
100 test_parse::<i16>("32768", Err(IntErrorKind::PosOverflow));
102 test_parse::<i16>("-32768", Ok(-32_768));
103 test_parse::<i16>("-32769", Err(IntErrorKind::NegOverflow));
105 test_parse::<i32>("2147483647", Ok(2_147_483_647));
106 test_parse::<i32>("2147483648", Err(IntErrorKind::PosOverflow));
108 test_parse::<i32>("-2147483648", Ok(-2_147_483_648));
109 test_parse::<i32>("-2147483649", Err(IntErrorKind::NegOverflow));
111 test_parse::<i64>("9223372036854775807", Ok(9_223_372_036_854_775_807));
112 test_parse::<i64>("9223372036854775808", Err(IntErrorKind::PosOverflow));
114 test_parse::<i64>("-9223372036854775808", Ok(-9_223_372_036_854_775_808));
115 test_parse::<i64>("-9223372036854775809", Err(IntErrorKind::NegOverflow));
119 fn test_leading_plus() {
120 test_parse::<u8>("+127", Ok(127));
121 test_parse::<i64>("+9223372036854775807", Ok(9223372036854775807));
126 test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit));
127 test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit));
128 test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit));
129 test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit));
130 test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit));
131 test_parse::<i8>("-", Err(IntErrorKind::InvalidDigit));
132 test_parse::<i8>("+", Err(IntErrorKind::InvalidDigit));
133 test_parse::<u8>("-1", Err(IntErrorKind::InvalidDigit));
138 test_parse::<u8>("", Err(IntErrorKind::Empty));
142 fn test_infallible_try_from_int_error() {
143 let func = |x: i8| -> Result<i32, TryFromIntError> { Ok(x.try_into()?) };
145 assert!(func(0).is_ok());
148 macro_rules! test_impl_from {
149 ($fn_name:ident, bool, $target: ty) => {
152 let one: $target = 1;
153 let zero: $target = 0;
154 assert_eq!(one, <$target>::from(true));
155 assert_eq!(zero, <$target>::from(false));
158 ($fn_name: ident, $Small: ty, $Large: ty) => {
161 let small_max = <$Small>::MAX;
162 let small_min = <$Small>::MIN;
163 let large_max: $Large = small_max.into();
164 let large_min: $Large = small_min.into();
165 assert_eq!(large_max as $Small, small_max);
166 assert_eq!(large_min as $Small, small_min);
171 // Unsigned -> Unsigned
172 test_impl_from! { test_u8u16, u8, u16 }
173 test_impl_from! { test_u8u32, u8, u32 }
174 test_impl_from! { test_u8u64, u8, u64 }
175 test_impl_from! { test_u8usize, u8, usize }
176 test_impl_from! { test_u16u32, u16, u32 }
177 test_impl_from! { test_u16u64, u16, u64 }
178 test_impl_from! { test_u32u64, u32, u64 }
181 test_impl_from! { test_i8i16, i8, i16 }
182 test_impl_from! { test_i8i32, i8, i32 }
183 test_impl_from! { test_i8i64, i8, i64 }
184 test_impl_from! { test_i8isize, i8, isize }
185 test_impl_from! { test_i16i32, i16, i32 }
186 test_impl_from! { test_i16i64, i16, i64 }
187 test_impl_from! { test_i32i64, i32, i64 }
189 // Unsigned -> Signed
190 test_impl_from! { test_u8i16, u8, i16 }
191 test_impl_from! { test_u8i32, u8, i32 }
192 test_impl_from! { test_u8i64, u8, i64 }
193 test_impl_from! { test_u16i32, u16, i32 }
194 test_impl_from! { test_u16i64, u16, i64 }
195 test_impl_from! { test_u32i64, u32, i64 }
198 test_impl_from! { test_boolu8, bool, u8 }
199 test_impl_from! { test_boolu16, bool, u16 }
200 test_impl_from! { test_boolu32, bool, u32 }
201 test_impl_from! { test_boolu64, bool, u64 }
202 test_impl_from! { test_boolu128, bool, u128 }
203 test_impl_from! { test_booli8, bool, i8 }
204 test_impl_from! { test_booli16, bool, i16 }
205 test_impl_from! { test_booli32, bool, i32 }
206 test_impl_from! { test_booli64, bool, i64 }
207 test_impl_from! { test_booli128, bool, i128 }
210 test_impl_from! { test_i8f32, i8, f32 }
211 test_impl_from! { test_i8f64, i8, f64 }
212 test_impl_from! { test_i16f32, i16, f32 }
213 test_impl_from! { test_i16f64, i16, f64 }
214 test_impl_from! { test_i32f64, i32, f64 }
217 test_impl_from! { test_u8f32, u8, f32 }
218 test_impl_from! { test_u8f64, u8, f64 }
219 test_impl_from! { test_u16f32, u16, f32 }
220 test_impl_from! { test_u16f64, u16, f64 }
221 test_impl_from! { test_u32f64, u32, f64 }
226 let max: f64 = f32::MAX.into();
227 assert_eq!(max as f32, f32::MAX);
228 assert!(max.is_normal());
230 let min: f64 = f32::MIN.into();
231 assert_eq!(min as f32, f32::MIN);
232 assert!(min.is_normal());
234 let min_positive: f64 = f32::MIN_POSITIVE.into();
235 assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
236 assert!(min_positive.is_normal());
238 let epsilon: f64 = f32::EPSILON.into();
239 assert_eq!(epsilon as f32, f32::EPSILON);
240 assert!(epsilon.is_normal());
242 let zero: f64 = (0.0f32).into();
243 assert_eq!(zero as f32, 0.0f32);
244 assert!(zero.is_sign_positive());
246 let neg_zero: f64 = (-0.0f32).into();
247 assert_eq!(neg_zero as f32, -0.0f32);
248 assert!(neg_zero.is_sign_negative());
250 let infinity: f64 = f32::INFINITY.into();
251 assert_eq!(infinity as f32, f32::INFINITY);
252 assert!(infinity.is_infinite());
253 assert!(infinity.is_sign_positive());
255 let neg_infinity: f64 = f32::NEG_INFINITY.into();
256 assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
257 assert!(neg_infinity.is_infinite());
258 assert!(neg_infinity.is_sign_negative());
260 let nan: f64 = f32::NAN.into();
261 assert!(nan.is_nan());
264 /// Conversions where the full width of $source can be represented as $target
265 macro_rules! test_impl_try_from_always_ok {
266 ($fn_name:ident, $source:ty, $target: ty) => {
269 let max = <$source>::MAX;
270 let min = <$source>::MIN;
271 let zero: $source = 0;
272 assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(), max as $target);
273 assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(), min as $target);
274 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
279 test_impl_try_from_always_ok! { test_try_u8u8, u8, u8 }
280 test_impl_try_from_always_ok! { test_try_u8u16, u8, u16 }
281 test_impl_try_from_always_ok! { test_try_u8u32, u8, u32 }
282 test_impl_try_from_always_ok! { test_try_u8u64, u8, u64 }
283 test_impl_try_from_always_ok! { test_try_u8u128, u8, u128 }
284 test_impl_try_from_always_ok! { test_try_u8i16, u8, i16 }
285 test_impl_try_from_always_ok! { test_try_u8i32, u8, i32 }
286 test_impl_try_from_always_ok! { test_try_u8i64, u8, i64 }
287 test_impl_try_from_always_ok! { test_try_u8i128, u8, i128 }
289 test_impl_try_from_always_ok! { test_try_u16u16, u16, u16 }
290 test_impl_try_from_always_ok! { test_try_u16u32, u16, u32 }
291 test_impl_try_from_always_ok! { test_try_u16u64, u16, u64 }
292 test_impl_try_from_always_ok! { test_try_u16u128, u16, u128 }
293 test_impl_try_from_always_ok! { test_try_u16i32, u16, i32 }
294 test_impl_try_from_always_ok! { test_try_u16i64, u16, i64 }
295 test_impl_try_from_always_ok! { test_try_u16i128, u16, i128 }
297 test_impl_try_from_always_ok! { test_try_u32u32, u32, u32 }
298 test_impl_try_from_always_ok! { test_try_u32u64, u32, u64 }
299 test_impl_try_from_always_ok! { test_try_u32u128, u32, u128 }
300 test_impl_try_from_always_ok! { test_try_u32i64, u32, i64 }
301 test_impl_try_from_always_ok! { test_try_u32i128, u32, i128 }
303 test_impl_try_from_always_ok! { test_try_u64u64, u64, u64 }
304 test_impl_try_from_always_ok! { test_try_u64u128, u64, u128 }
305 test_impl_try_from_always_ok! { test_try_u64i128, u64, i128 }
307 test_impl_try_from_always_ok! { test_try_u128u128, u128, u128 }
309 test_impl_try_from_always_ok! { test_try_i8i8, i8, i8 }
310 test_impl_try_from_always_ok! { test_try_i8i16, i8, i16 }
311 test_impl_try_from_always_ok! { test_try_i8i32, i8, i32 }
312 test_impl_try_from_always_ok! { test_try_i8i64, i8, i64 }
313 test_impl_try_from_always_ok! { test_try_i8i128, i8, i128 }
315 test_impl_try_from_always_ok! { test_try_i16i16, i16, i16 }
316 test_impl_try_from_always_ok! { test_try_i16i32, i16, i32 }
317 test_impl_try_from_always_ok! { test_try_i16i64, i16, i64 }
318 test_impl_try_from_always_ok! { test_try_i16i128, i16, i128 }
320 test_impl_try_from_always_ok! { test_try_i32i32, i32, i32 }
321 test_impl_try_from_always_ok! { test_try_i32i64, i32, i64 }
322 test_impl_try_from_always_ok! { test_try_i32i128, i32, i128 }
324 test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
325 test_impl_try_from_always_ok! { test_try_i64i128, i64, i128 }
327 test_impl_try_from_always_ok! { test_try_i128i128, i128, i128 }
329 test_impl_try_from_always_ok! { test_try_usizeusize, usize, usize }
330 test_impl_try_from_always_ok! { test_try_isizeisize, isize, isize }
332 assume_usize_width! {
333 test_impl_try_from_always_ok! { test_try_u8usize, u8, usize }
334 test_impl_try_from_always_ok! { test_try_u8isize, u8, isize }
335 test_impl_try_from_always_ok! { test_try_i8isize, i8, isize }
337 test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
338 test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
340 test_impl_try_from_always_ok! { test_try_usizeu64, usize, u64 }
341 test_impl_try_from_always_ok! { test_try_usizeu128, usize, u128 }
342 test_impl_try_from_always_ok! { test_try_usizei128, usize, i128 }
344 test_impl_try_from_always_ok! { test_try_isizei64, isize, i64 }
345 test_impl_try_from_always_ok! { test_try_isizei128, isize, i128 }
348 #[cfg(target_pointer_width = "16")] {
349 test_impl_try_from_always_ok! { test_try_usizeu16, usize, u16 }
350 test_impl_try_from_always_ok! { test_try_isizei16, isize, i16 }
351 test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
352 test_impl_try_from_always_ok! { test_try_usizei32, usize, i32 }
353 test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
354 test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
357 #[cfg(target_pointer_width = "32")] {
358 test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
359 test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
360 test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
361 test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
362 test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
363 test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
366 #[cfg(target_pointer_width = "64")] {
367 test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
368 test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
369 test_impl_try_from_always_ok! { test_try_u32isize, u32, isize }
370 test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
371 test_impl_try_from_always_ok! { test_try_u64usize, u64, usize }
372 test_impl_try_from_always_ok! { test_try_i64isize, i64, isize }
377 /// Conversions where max of $source can be represented as $target,
378 macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
379 ($fn_name:ident, $source:ty, $target:ty) => {
382 let max = <$source>::MAX;
383 let min = <$source>::MIN;
384 let zero: $source = 0;
385 let neg_one: $source = -1;
386 assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(), max as $target);
387 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
388 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
389 assert!(<$target as TryFrom<$source>>::try_from(neg_one).is_err());
394 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u8, i8, u8 }
395 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u16, i8, u16 }
396 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u32, i8, u32 }
397 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u64, i8, u64 }
398 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u128, i8, u128 }
400 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u16, i16, u16 }
401 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u32, i16, u32 }
402 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u64, i16, u64 }
403 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u128, i16, u128 }
405 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u32, i32, u32 }
406 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u64, i32, u64 }
407 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u128, i32, u128 }
409 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u64, i64, u64 }
410 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u128, i64, u128 }
412 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i128u128, i128, u128 }
414 assume_usize_width! {
415 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8usize, i8, usize }
416 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16usize, i16, usize }
418 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu64, isize, u64 }
419 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu128, isize, u128 }
420 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeusize, isize, usize }
423 #[cfg(target_pointer_width = "16")] {
424 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu16, isize, u16 }
425 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
428 #[cfg(target_pointer_width = "32")] {
429 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
431 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
434 #[cfg(target_pointer_width = "64")] {
435 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
436 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64usize, i64, usize }
441 /// Conversions where max of $source can not be represented as $target,
443 macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
444 ($fn_name:ident, $source:ty, $target:ty) => {
447 let max = <$source>::MAX;
448 let min = <$source>::MIN;
449 let zero: $source = 0;
450 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
451 assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(), min as $target);
452 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
457 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
459 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i8, u16, i8 }
460 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i16, u16, i16 }
462 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i8, u32, i8 }
463 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i16, u32, i16 }
464 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i32, u32, i32 }
466 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i8, u64, i8 }
467 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i16, u64, i16 }
468 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i32, u64, i32 }
469 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i64, u64, i64 }
471 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i8, u128, i8 }
472 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i16, u128, i16 }
473 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i32, u128, i32 }
474 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i64, u128, i64 }
475 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i128, u128, i128 }
477 assume_usize_width! {
478 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64isize, u64, isize }
479 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128isize, u128, isize }
481 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei8, usize, i8 }
482 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei16, usize, i16 }
483 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizeisize, usize, isize }
486 #[cfg(target_pointer_width = "16")] {
487 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16isize, u16, isize }
488 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
491 #[cfg(target_pointer_width = "32")] {
492 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
493 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
496 #[cfg(target_pointer_width = "64")] {
497 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
498 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei64, usize, i64 }
503 /// Conversions where min/max of $source can not be represented as $target.
504 macro_rules! test_impl_try_from_same_sign_err {
505 ($fn_name:ident, $source:ty, $target:ty) => {
508 let max = <$source>::MAX;
509 let min = <$source>::MIN;
510 let zero: $source = 0;
511 let t_max = <$target>::MAX;
512 let t_min = <$target>::MIN;
513 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
515 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
517 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
519 <$target as TryFrom<$source>>::try_from(t_max as $source).unwrap(),
523 <$target as TryFrom<$source>>::try_from(t_min as $source).unwrap(),
530 test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
532 test_impl_try_from_same_sign_err! { test_try_u32u8, u32, u8 }
533 test_impl_try_from_same_sign_err! { test_try_u32u16, u32, u16 }
535 test_impl_try_from_same_sign_err! { test_try_u64u8, u64, u8 }
536 test_impl_try_from_same_sign_err! { test_try_u64u16, u64, u16 }
537 test_impl_try_from_same_sign_err! { test_try_u64u32, u64, u32 }
539 test_impl_try_from_same_sign_err! { test_try_u128u8, u128, u8 }
540 test_impl_try_from_same_sign_err! { test_try_u128u16, u128, u16 }
541 test_impl_try_from_same_sign_err! { test_try_u128u32, u128, u32 }
542 test_impl_try_from_same_sign_err! { test_try_u128u64, u128, u64 }
544 test_impl_try_from_same_sign_err! { test_try_i16i8, i16, i8 }
545 test_impl_try_from_same_sign_err! { test_try_isizei8, isize, i8 }
547 test_impl_try_from_same_sign_err! { test_try_i32i8, i32, i8 }
548 test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
550 test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
551 test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
552 test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
554 test_impl_try_from_same_sign_err! { test_try_i128i8, i128, i8 }
555 test_impl_try_from_same_sign_err! { test_try_i128i16, i128, i16 }
556 test_impl_try_from_same_sign_err! { test_try_i128i32, i128, i32 }
557 test_impl_try_from_same_sign_err! { test_try_i128i64, i128, i64 }
559 assume_usize_width! {
560 test_impl_try_from_same_sign_err! { test_try_usizeu8, usize, u8 }
561 test_impl_try_from_same_sign_err! { test_try_u128usize, u128, usize }
562 test_impl_try_from_same_sign_err! { test_try_i128isize, i128, isize }
565 #[cfg(target_pointer_width = "16")] {
566 test_impl_try_from_same_sign_err! { test_try_u32usize, u32, usize }
567 test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
569 test_impl_try_from_same_sign_err! { test_try_i32isize, i32, isize }
570 test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
573 #[cfg(target_pointer_width = "32")] {
574 test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
575 test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
577 test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
578 test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
581 #[cfg(target_pointer_width = "64")] {
582 test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
583 test_impl_try_from_same_sign_err! { test_try_usizeu32, usize, u32 }
585 test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
586 test_impl_try_from_same_sign_err! { test_try_isizei32, isize, i32 }
591 /// Conversions where neither the min nor the max of $source can be represented by
592 /// $target, but max/min of the target can be represented by the source.
593 macro_rules! test_impl_try_from_signed_to_unsigned_err {
594 ($fn_name:ident, $source:ty, $target:ty) => {
597 let max = <$source>::MAX;
598 let min = <$source>::MIN;
599 let zero: $source = 0;
600 let t_max = <$target>::MAX;
601 let t_min = <$target>::MIN;
602 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
603 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
604 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
606 <$target as TryFrom<$source>>::try_from(t_max as $source).unwrap(),
610 <$target as TryFrom<$source>>::try_from(t_min as $source).unwrap(),
617 test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
619 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
620 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
622 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
623 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
624 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }
626 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u8, i128, u8 }
627 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u16, i128, u16 }
628 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u32, i128, u32 }
629 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
631 assume_usize_width! {
632 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
633 test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
636 #[cfg(target_pointer_width = "16")] {
637 test_impl_try_from_signed_to_unsigned_err! { test_try_i32usize, i32, usize }
638 test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
640 #[cfg(target_pointer_width = "32")] {
641 test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
643 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
645 #[cfg(target_pointer_width = "64")] {
646 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
647 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu32, isize, u32 }
652 macro_rules! test_float {
653 ($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => {
657 assert_eq!((0.0 as $fty).min(0.0), 0.0);
658 assert!((0.0 as $fty).min(0.0).is_sign_positive());
659 assert_eq!((-0.0 as $fty).min(-0.0), -0.0);
660 assert!((-0.0 as $fty).min(-0.0).is_sign_negative());
661 assert_eq!((9.0 as $fty).min(9.0), 9.0);
662 assert_eq!((-9.0 as $fty).min(0.0), -9.0);
663 assert_eq!((0.0 as $fty).min(9.0), 0.0);
664 assert!((0.0 as $fty).min(9.0).is_sign_positive());
665 assert_eq!((-0.0 as $fty).min(9.0), -0.0);
666 assert!((-0.0 as $fty).min(9.0).is_sign_negative());
667 assert_eq!((-0.0 as $fty).min(-9.0), -9.0);
668 assert_eq!(($inf as $fty).min(9.0), 9.0);
669 assert_eq!((9.0 as $fty).min($inf), 9.0);
670 assert_eq!(($inf as $fty).min(-9.0), -9.0);
671 assert_eq!((-9.0 as $fty).min($inf), -9.0);
672 assert_eq!(($neginf as $fty).min(9.0), $neginf);
673 assert_eq!((9.0 as $fty).min($neginf), $neginf);
674 assert_eq!(($neginf as $fty).min(-9.0), $neginf);
675 assert_eq!((-9.0 as $fty).min($neginf), $neginf);
676 assert_eq!(($nan as $fty).min(9.0), 9.0);
677 assert_eq!(($nan as $fty).min(-9.0), -9.0);
678 assert_eq!((9.0 as $fty).min($nan), 9.0);
679 assert_eq!((-9.0 as $fty).min($nan), -9.0);
680 assert!(($nan as $fty).min($nan).is_nan());
684 assert_eq!((0.0 as $fty).max(0.0), 0.0);
685 assert!((0.0 as $fty).max(0.0).is_sign_positive());
686 assert_eq!((-0.0 as $fty).max(-0.0), -0.0);
687 assert!((-0.0 as $fty).max(-0.0).is_sign_negative());
688 assert_eq!((9.0 as $fty).max(9.0), 9.0);
689 assert_eq!((-9.0 as $fty).max(0.0), 0.0);
690 assert!((-9.0 as $fty).max(0.0).is_sign_positive());
691 assert_eq!((-9.0 as $fty).max(-0.0), -0.0);
692 assert!((-9.0 as $fty).max(-0.0).is_sign_negative());
693 assert_eq!((0.0 as $fty).max(9.0), 9.0);
694 assert_eq!((0.0 as $fty).max(-9.0), 0.0);
695 assert!((0.0 as $fty).max(-9.0).is_sign_positive());
696 assert_eq!((-0.0 as $fty).max(-9.0), -0.0);
697 assert!((-0.0 as $fty).max(-9.0).is_sign_negative());
698 assert_eq!(($inf as $fty).max(9.0), $inf);
699 assert_eq!((9.0 as $fty).max($inf), $inf);
700 assert_eq!(($inf as $fty).max(-9.0), $inf);
701 assert_eq!((-9.0 as $fty).max($inf), $inf);
702 assert_eq!(($neginf as $fty).max(9.0), 9.0);
703 assert_eq!((9.0 as $fty).max($neginf), 9.0);
704 assert_eq!(($neginf as $fty).max(-9.0), -9.0);
705 assert_eq!((-9.0 as $fty).max($neginf), -9.0);
706 assert_eq!(($nan as $fty).max(9.0), 9.0);
707 assert_eq!(($nan as $fty).max(-9.0), -9.0);
708 assert_eq!((9.0 as $fty).max($nan), 9.0);
709 assert_eq!((-9.0 as $fty).max($nan), -9.0);
710 assert!(($nan as $fty).max($nan).is_nan());
715 assert!($inf.rem_euclid(a).is_nan());
716 assert_eq!(a.rem_euclid($inf), a);
717 assert!(a.rem_euclid($nan).is_nan());
718 assert!($inf.rem_euclid($inf).is_nan());
719 assert!($inf.rem_euclid($nan).is_nan());
720 assert!($nan.rem_euclid($inf).is_nan());
725 assert_eq!(a.div_euclid($inf), 0.0);
726 assert!(a.div_euclid($nan).is_nan());
727 assert!($inf.div_euclid($inf).is_nan());
728 assert!($inf.div_euclid($nan).is_nan());
729 assert!($nan.div_euclid($inf).is_nan());
735 test_float!(f32, f32, f32::INFINITY, f32::NEG_INFINITY, f32::NAN);
736 test_float!(f64, f64, f64::INFINITY, f64::NEG_INFINITY, f64::NAN);