1 use core::convert::{TryFrom, TryInto};
2 use core::cmp::PartialEq;
4 use core::marker::Copy;
5 use core::num::TryFromIntError;
6 use core::ops::{Add, Sub, Mul, Div, Rem};
7 use core::option::Option;
8 use core::option::Option::{Some, None};
31 /// Adds the attribute to all items in the block.
32 macro_rules! cfg_block {
33 ($(#[$attr:meta]{$($it:item)*})*) => {$($(
39 /// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
40 /// support for larger/smaller pointer widths are added in the future.
41 macro_rules! assume_usize_width {
42 {$($it:item)*} => {#[cfg(not(any(
43 target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64")))]
44 compile_error!("The current tests of try_from on usize/isize assume that \
45 the pointer width is either 16, 32, or 64");
50 /// Helper function for testing numeric operations
51 pub fn test_num<T>(ten: T, two: T) where
53 + Add<Output=T> + Sub<Output=T>
54 + Mul<Output=T> + Div<Output=T>
55 + Rem<Output=T> + Debug
58 assert_eq!(ten.add(two), ten + two);
59 assert_eq!(ten.sub(two), ten - two);
60 assert_eq!(ten.mul(two), ten * two);
61 assert_eq!(ten.div(two), ten / two);
62 assert_eq!(ten.rem(two), ten % two);
66 fn from_str_issue7588() {
67 let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
69 let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
74 fn test_int_from_str_overflow() {
75 assert_eq!("127".parse::<i8>().ok(), Some(127i8));
76 assert_eq!("128".parse::<i8>().ok(), None);
78 assert_eq!("-128".parse::<i8>().ok(), Some(-128i8));
79 assert_eq!("-129".parse::<i8>().ok(), None);
81 assert_eq!("32767".parse::<i16>().ok(), Some(32_767i16));
82 assert_eq!("32768".parse::<i16>().ok(), None);
84 assert_eq!("-32768".parse::<i16>().ok(), Some(-32_768i16));
85 assert_eq!("-32769".parse::<i16>().ok(), None);
87 assert_eq!("2147483647".parse::<i32>().ok(), Some(2_147_483_647i32));
88 assert_eq!("2147483648".parse::<i32>().ok(), None);
90 assert_eq!("-2147483648".parse::<i32>().ok(), Some(-2_147_483_648i32));
91 assert_eq!("-2147483649".parse::<i32>().ok(), None);
93 assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(9_223_372_036_854_775_807i64));
94 assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
96 assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(-9_223_372_036_854_775_808i64));
97 assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
101 fn test_leading_plus() {
102 assert_eq!("+127".parse::<u8>().ok(), Some(127));
103 assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
108 assert_eq!("--129".parse::<i8>().ok(), None);
109 assert_eq!("++129".parse::<i8>().ok(), None);
110 assert_eq!("Съешь".parse::<u8>().ok(), None);
115 assert_eq!("-".parse::<i8>().ok(), None);
116 assert_eq!("+".parse::<i8>().ok(), None);
117 assert_eq!("".parse::<u8>().ok(), None);
121 fn test_infallible_try_from_int_error() {
122 let func = |x: i8| -> Result<i32, TryFromIntError> { Ok(x.try_into()?) };
124 assert!(func(0).is_ok());
127 macro_rules! test_impl_from {
128 ($fn_name:ident, bool, $target: ty) => {
131 let one: $target = 1;
132 let zero: $target = 0;
133 assert_eq!(one, <$target>::from(true));
134 assert_eq!(zero, <$target>::from(false));
137 ($fn_name: ident, $Small: ty, $Large: ty) => {
140 let small_max = <$Small>::max_value();
141 let small_min = <$Small>::min_value();
142 let large_max: $Large = small_max.into();
143 let large_min: $Large = small_min.into();
144 assert_eq!(large_max as $Small, small_max);
145 assert_eq!(large_min as $Small, small_min);
150 // Unsigned -> Unsigned
151 test_impl_from! { test_u8u16, u8, u16 }
152 test_impl_from! { test_u8u32, u8, u32 }
153 test_impl_from! { test_u8u64, u8, u64 }
154 test_impl_from! { test_u8usize, u8, usize }
155 test_impl_from! { test_u16u32, u16, u32 }
156 test_impl_from! { test_u16u64, u16, u64 }
157 test_impl_from! { test_u32u64, u32, u64 }
160 test_impl_from! { test_i8i16, i8, i16 }
161 test_impl_from! { test_i8i32, i8, i32 }
162 test_impl_from! { test_i8i64, i8, i64 }
163 test_impl_from! { test_i8isize, i8, isize }
164 test_impl_from! { test_i16i32, i16, i32 }
165 test_impl_from! { test_i16i64, i16, i64 }
166 test_impl_from! { test_i32i64, i32, i64 }
168 // Unsigned -> Signed
169 test_impl_from! { test_u8i16, u8, i16 }
170 test_impl_from! { test_u8i32, u8, i32 }
171 test_impl_from! { test_u8i64, u8, i64 }
172 test_impl_from! { test_u16i32, u16, i32 }
173 test_impl_from! { test_u16i64, u16, i64 }
174 test_impl_from! { test_u32i64, u32, i64 }
177 test_impl_from! { test_boolu8, bool, u8 }
178 test_impl_from! { test_boolu16, bool, u16 }
179 test_impl_from! { test_boolu32, bool, u32 }
180 test_impl_from! { test_boolu64, bool, u64 }
181 test_impl_from! { test_boolu128, bool, u128 }
182 test_impl_from! { test_booli8, bool, i8 }
183 test_impl_from! { test_booli16, bool, i16 }
184 test_impl_from! { test_booli32, bool, i32 }
185 test_impl_from! { test_booli64, bool, i64 }
186 test_impl_from! { test_booli128, bool, i128 }
189 test_impl_from! { test_i8f32, i8, f32 }
190 test_impl_from! { test_i8f64, i8, f64 }
191 test_impl_from! { test_i16f32, i16, f32 }
192 test_impl_from! { test_i16f64, i16, f64 }
193 test_impl_from! { test_i32f64, i32, f64 }
196 test_impl_from! { test_u8f32, u8, f32 }
197 test_impl_from! { test_u8f64, u8, f64 }
198 test_impl_from! { test_u16f32, u16, f32 }
199 test_impl_from! { test_u16f64, u16, f64 }
200 test_impl_from! { test_u32f64, u32, f64 }
207 let max: f64 = f32::MAX.into();
208 assert_eq!(max as f32, f32::MAX);
209 assert!(max.is_normal());
211 let min: f64 = f32::MIN.into();
212 assert_eq!(min as f32, f32::MIN);
213 assert!(min.is_normal());
215 let min_positive: f64 = f32::MIN_POSITIVE.into();
216 assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
217 assert!(min_positive.is_normal());
219 let epsilon: f64 = f32::EPSILON.into();
220 assert_eq!(epsilon as f32, f32::EPSILON);
221 assert!(epsilon.is_normal());
223 let zero: f64 = (0.0f32).into();
224 assert_eq!(zero as f32, 0.0f32);
225 assert!(zero.is_sign_positive());
227 let neg_zero: f64 = (-0.0f32).into();
228 assert_eq!(neg_zero as f32, -0.0f32);
229 assert!(neg_zero.is_sign_negative());
231 let infinity: f64 = f32::INFINITY.into();
232 assert_eq!(infinity as f32, f32::INFINITY);
233 assert!(infinity.is_infinite());
234 assert!(infinity.is_sign_positive());
236 let neg_infinity: f64 = f32::NEG_INFINITY.into();
237 assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
238 assert!(neg_infinity.is_infinite());
239 assert!(neg_infinity.is_sign_negative());
241 let nan: f64 = f32::NAN.into();
242 assert!(nan.is_nan());
246 /// Conversions where the full width of $source can be represented as $target
247 macro_rules! test_impl_try_from_always_ok {
248 ($fn_name:ident, $source:ty, $target: ty) => {
251 let max = <$source>::max_value();
252 let min = <$source>::min_value();
253 let zero: $source = 0;
254 assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
256 assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
258 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
264 test_impl_try_from_always_ok! { test_try_u8u8, u8, u8 }
265 test_impl_try_from_always_ok! { test_try_u8u16, u8, u16 }
266 test_impl_try_from_always_ok! { test_try_u8u32, u8, u32 }
267 test_impl_try_from_always_ok! { test_try_u8u64, u8, u64 }
268 test_impl_try_from_always_ok! { test_try_u8u128, u8, u128 }
269 test_impl_try_from_always_ok! { test_try_u8i16, u8, i16 }
270 test_impl_try_from_always_ok! { test_try_u8i32, u8, i32 }
271 test_impl_try_from_always_ok! { test_try_u8i64, u8, i64 }
272 test_impl_try_from_always_ok! { test_try_u8i128, u8, i128 }
274 test_impl_try_from_always_ok! { test_try_u16u16, u16, u16 }
275 test_impl_try_from_always_ok! { test_try_u16u32, u16, u32 }
276 test_impl_try_from_always_ok! { test_try_u16u64, u16, u64 }
277 test_impl_try_from_always_ok! { test_try_u16u128, u16, u128 }
278 test_impl_try_from_always_ok! { test_try_u16i32, u16, i32 }
279 test_impl_try_from_always_ok! { test_try_u16i64, u16, i64 }
280 test_impl_try_from_always_ok! { test_try_u16i128, u16, i128 }
282 test_impl_try_from_always_ok! { test_try_u32u32, u32, u32 }
283 test_impl_try_from_always_ok! { test_try_u32u64, u32, u64 }
284 test_impl_try_from_always_ok! { test_try_u32u128, u32, u128 }
285 test_impl_try_from_always_ok! { test_try_u32i64, u32, i64 }
286 test_impl_try_from_always_ok! { test_try_u32i128, u32, i128 }
288 test_impl_try_from_always_ok! { test_try_u64u64, u64, u64 }
289 test_impl_try_from_always_ok! { test_try_u64u128, u64, u128 }
290 test_impl_try_from_always_ok! { test_try_u64i128, u64, i128 }
292 test_impl_try_from_always_ok! { test_try_u128u128, u128, u128 }
294 test_impl_try_from_always_ok! { test_try_i8i8, i8, i8 }
295 test_impl_try_from_always_ok! { test_try_i8i16, i8, i16 }
296 test_impl_try_from_always_ok! { test_try_i8i32, i8, i32 }
297 test_impl_try_from_always_ok! { test_try_i8i64, i8, i64 }
298 test_impl_try_from_always_ok! { test_try_i8i128, i8, i128 }
300 test_impl_try_from_always_ok! { test_try_i16i16, i16, i16 }
301 test_impl_try_from_always_ok! { test_try_i16i32, i16, i32 }
302 test_impl_try_from_always_ok! { test_try_i16i64, i16, i64 }
303 test_impl_try_from_always_ok! { test_try_i16i128, i16, i128 }
305 test_impl_try_from_always_ok! { test_try_i32i32, i32, i32 }
306 test_impl_try_from_always_ok! { test_try_i32i64, i32, i64 }
307 test_impl_try_from_always_ok! { test_try_i32i128, i32, i128 }
309 test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
310 test_impl_try_from_always_ok! { test_try_i64i128, i64, i128 }
312 test_impl_try_from_always_ok! { test_try_i128i128, i128, i128 }
314 test_impl_try_from_always_ok! { test_try_usizeusize, usize, usize }
315 test_impl_try_from_always_ok! { test_try_isizeisize, isize, isize }
317 assume_usize_width! {
318 test_impl_try_from_always_ok! { test_try_u8usize, u8, usize }
319 test_impl_try_from_always_ok! { test_try_u8isize, u8, isize }
320 test_impl_try_from_always_ok! { test_try_i8isize, i8, isize }
322 test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
323 test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
325 test_impl_try_from_always_ok! { test_try_usizeu64, usize, u64 }
326 test_impl_try_from_always_ok! { test_try_usizeu128, usize, u128 }
327 test_impl_try_from_always_ok! { test_try_usizei128, usize, i128 }
329 test_impl_try_from_always_ok! { test_try_isizei64, isize, i64 }
330 test_impl_try_from_always_ok! { test_try_isizei128, isize, i128 }
333 #[cfg(target_pointer_width = "16")] {
334 test_impl_try_from_always_ok! { test_try_usizeu16, usize, u16 }
335 test_impl_try_from_always_ok! { test_try_isizei16, isize, i16 }
336 test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
337 test_impl_try_from_always_ok! { test_try_usizei32, usize, i32 }
338 test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
339 test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
342 #[cfg(target_pointer_width = "32")] {
343 test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
344 test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
345 test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
346 test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
347 test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
348 test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
351 #[cfg(target_pointer_width = "64")] {
352 test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
353 test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
354 test_impl_try_from_always_ok! { test_try_u32isize, u32, isize }
355 test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
356 test_impl_try_from_always_ok! { test_try_u64usize, u64, usize }
357 test_impl_try_from_always_ok! { test_try_i64isize, i64, isize }
362 /// Conversions where max of $source can be represented as $target,
363 macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
364 ($fn_name:ident, $source:ty, $target:ty) => {
367 let max = <$source>::max_value();
368 let min = <$source>::min_value();
369 let zero: $source = 0;
370 let neg_one: $source = -1;
371 assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
373 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
374 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
376 assert!(<$target as TryFrom<$source>>::try_from(neg_one).is_err());
381 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u8, i8, u8 }
382 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u16, i8, u16 }
383 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u32, i8, u32 }
384 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u64, i8, u64 }
385 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u128, i8, u128 }
387 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u16, i16, u16 }
388 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u32, i16, u32 }
389 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u64, i16, u64 }
390 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u128, i16, u128 }
392 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u32, i32, u32 }
393 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u64, i32, u64 }
394 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u128, i32, u128 }
396 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u64, i64, u64 }
397 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u128, i64, u128 }
399 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i128u128, i128, u128 }
401 assume_usize_width! {
402 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8usize, i8, usize }
403 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16usize, i16, usize }
405 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu64, isize, u64 }
406 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu128, isize, u128 }
407 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeusize, isize, usize }
410 #[cfg(target_pointer_width = "16")] {
411 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu16, isize, u16 }
412 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
415 #[cfg(target_pointer_width = "32")] {
416 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
418 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
421 #[cfg(target_pointer_width = "64")] {
422 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
423 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64usize, i64, usize }
428 /// Conversions where max of $source can not be represented as $target,
430 macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
431 ($fn_name:ident, $source:ty, $target:ty) => {
434 let max = <$source>::max_value();
435 let min = <$source>::min_value();
436 let zero: $source = 0;
437 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
438 assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
440 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
446 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
448 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i8, u16, i8 }
449 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i16, u16, i16 }
451 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i8, u32, i8 }
452 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i16, u32, i16 }
453 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i32, u32, i32 }
455 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i8, u64, i8 }
456 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i16, u64, i16 }
457 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i32, u64, i32 }
458 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i64, u64, i64 }
460 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i8, u128, i8 }
461 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i16, u128, i16 }
462 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i32, u128, i32 }
463 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i64, u128, i64 }
464 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i128, u128, i128 }
466 assume_usize_width! {
467 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64isize, u64, isize }
468 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128isize, u128, isize }
470 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei8, usize, i8 }
471 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei16, usize, i16 }
472 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizeisize, usize, isize }
475 #[cfg(target_pointer_width = "16")] {
476 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16isize, u16, isize }
477 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
480 #[cfg(target_pointer_width = "32")] {
481 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
482 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
485 #[cfg(target_pointer_width = "64")] {
486 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
487 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei64, usize, i64 }
492 /// Conversions where min/max of $source can not be represented as $target.
493 macro_rules! test_impl_try_from_same_sign_err {
494 ($fn_name:ident, $source:ty, $target:ty) => {
497 let max = <$source>::max_value();
498 let min = <$source>::min_value();
499 let zero: $source = 0;
500 let t_max = <$target>::max_value();
501 let t_min = <$target>::min_value();
502 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
504 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
506 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
508 assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
511 assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
518 test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
520 test_impl_try_from_same_sign_err! { test_try_u32u8, u32, u8 }
521 test_impl_try_from_same_sign_err! { test_try_u32u16, u32, u16 }
523 test_impl_try_from_same_sign_err! { test_try_u64u8, u64, u8 }
524 test_impl_try_from_same_sign_err! { test_try_u64u16, u64, u16 }
525 test_impl_try_from_same_sign_err! { test_try_u64u32, u64, u32 }
527 test_impl_try_from_same_sign_err! { test_try_u128u8, u128, u8 }
528 test_impl_try_from_same_sign_err! { test_try_u128u16, u128, u16 }
529 test_impl_try_from_same_sign_err! { test_try_u128u32, u128, u32 }
530 test_impl_try_from_same_sign_err! { test_try_u128u64, u128, u64 }
532 test_impl_try_from_same_sign_err! { test_try_i16i8, i16, i8 }
533 test_impl_try_from_same_sign_err! { test_try_isizei8, isize, i8 }
535 test_impl_try_from_same_sign_err! { test_try_i32i8, i32, i8 }
536 test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
538 test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
539 test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
540 test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
542 test_impl_try_from_same_sign_err! { test_try_i128i8, i128, i8 }
543 test_impl_try_from_same_sign_err! { test_try_i128i16, i128, i16 }
544 test_impl_try_from_same_sign_err! { test_try_i128i32, i128, i32 }
545 test_impl_try_from_same_sign_err! { test_try_i128i64, i128, i64 }
547 assume_usize_width! {
548 test_impl_try_from_same_sign_err! { test_try_usizeu8, usize, u8 }
549 test_impl_try_from_same_sign_err! { test_try_u128usize, u128, usize }
550 test_impl_try_from_same_sign_err! { test_try_i128isize, i128, isize }
553 #[cfg(target_pointer_width = "16")] {
554 test_impl_try_from_same_sign_err! { test_try_u32usize, u32, usize }
555 test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
557 test_impl_try_from_same_sign_err! { test_try_i32isize, i32, isize }
558 test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
561 #[cfg(target_pointer_width = "32")] {
562 test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
563 test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
565 test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
566 test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
569 #[cfg(target_pointer_width = "64")] {
570 test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
571 test_impl_try_from_same_sign_err! { test_try_usizeu32, usize, u32 }
573 test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
574 test_impl_try_from_same_sign_err! { test_try_isizei32, isize, i32 }
579 /// Conversions where neither the min nor the max of $source can be represented by
580 /// $target, but max/min of the target can be represented by the source.
581 macro_rules! test_impl_try_from_signed_to_unsigned_err {
582 ($fn_name:ident, $source:ty, $target:ty) => {
585 let max = <$source>::max_value();
586 let min = <$source>::min_value();
587 let zero: $source = 0;
588 let t_max = <$target>::max_value();
589 let t_min = <$target>::min_value();
590 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
591 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
592 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
594 assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
597 assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
604 test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
606 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
607 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
609 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
610 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
611 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }
613 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u8, i128, u8 }
614 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u16, i128, u16 }
615 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u32, i128, u32 }
616 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
618 assume_usize_width! {
619 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
620 test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
623 #[cfg(target_pointer_width = "16")] {
624 test_impl_try_from_signed_to_unsigned_err! { test_try_i32usize, i32, usize }
625 test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
627 #[cfg(target_pointer_width = "32")] {
628 test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
630 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
632 #[cfg(target_pointer_width = "64")] {
633 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
634 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu32, isize, u32 }
639 macro_rules! test_float {
640 ($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => { mod $modname {
641 // FIXME(nagisa): these tests should test for sign of -0.0
644 assert_eq!((0.0 as $fty).min(0.0), 0.0);
645 assert_eq!((-0.0 as $fty).min(-0.0), -0.0);
646 assert_eq!((9.0 as $fty).min(9.0), 9.0);
647 assert_eq!((-9.0 as $fty).min(0.0), -9.0);
648 assert_eq!((0.0 as $fty).min(9.0), 0.0);
649 assert_eq!((-0.0 as $fty).min(-9.0), -9.0);
650 assert_eq!(($inf as $fty).min(9.0), 9.0);
651 assert_eq!((9.0 as $fty).min($inf), 9.0);
652 assert_eq!(($inf as $fty).min(-9.0), -9.0);
653 assert_eq!((-9.0 as $fty).min($inf), -9.0);
654 assert_eq!(($neginf as $fty).min(9.0), $neginf);
655 assert_eq!((9.0 as $fty).min($neginf), $neginf);
656 assert_eq!(($neginf as $fty).min(-9.0), $neginf);
657 assert_eq!((-9.0 as $fty).min($neginf), $neginf);
658 assert_eq!(($nan as $fty).min(9.0), 9.0);
659 assert_eq!(($nan as $fty).min(-9.0), -9.0);
660 assert_eq!((9.0 as $fty).min($nan), 9.0);
661 assert_eq!((-9.0 as $fty).min($nan), -9.0);
662 assert!(($nan as $fty).min($nan).is_nan());
666 assert_eq!((0.0 as $fty).max(0.0), 0.0);
667 assert_eq!((-0.0 as $fty).max(-0.0), -0.0);
668 assert_eq!((9.0 as $fty).max(9.0), 9.0);
669 assert_eq!((-9.0 as $fty).max(0.0), 0.0);
670 assert_eq!((0.0 as $fty).max(9.0), 9.0);
671 assert_eq!((-0.0 as $fty).max(-9.0), -0.0);
672 assert_eq!(($inf as $fty).max(9.0), $inf);
673 assert_eq!((9.0 as $fty).max($inf), $inf);
674 assert_eq!(($inf as $fty).max(-9.0), $inf);
675 assert_eq!((-9.0 as $fty).max($inf), $inf);
676 assert_eq!(($neginf as $fty).max(9.0), 9.0);
677 assert_eq!((9.0 as $fty).max($neginf), 9.0);
678 assert_eq!(($neginf as $fty).max(-9.0), -9.0);
679 assert_eq!((-9.0 as $fty).max($neginf), -9.0);
680 assert_eq!(($nan as $fty).max(9.0), 9.0);
681 assert_eq!(($nan as $fty).max(-9.0), -9.0);
682 assert_eq!((9.0 as $fty).max($nan), 9.0);
683 assert_eq!((-9.0 as $fty).max($nan), -9.0);
684 assert!(($nan as $fty).max($nan).is_nan());
689 assert!($inf.rem_euclid(a).is_nan());
690 assert_eq!(a.rem_euclid($inf), a);
691 assert!(a.rem_euclid($nan).is_nan());
692 assert!($inf.rem_euclid($inf).is_nan());
693 assert!($inf.rem_euclid($nan).is_nan());
694 assert!($nan.rem_euclid($inf).is_nan());
699 assert_eq!(a.div_euclid($inf), 0.0);
700 assert!(a.div_euclid($nan).is_nan());
701 assert!($inf.div_euclid($inf).is_nan());
702 assert!($inf.div_euclid($nan).is_nan());
703 assert!($nan.div_euclid($inf).is_nan());
708 test_float!(f32, f32, ::core::f32::INFINITY, ::core::f32::NEG_INFINITY, ::core::f32::NAN);
709 test_float!(f64, f64, ::core::f64::INFINITY, ::core::f64::NEG_INFINITY, ::core::f64::NAN);