1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use core::convert::TryFrom;
12 use core::cmp::PartialEq;
14 use core::marker::Copy;
15 use core::ops::{Add, Sub, Mul, Div, Rem};
16 use core::option::Option;
17 use core::option::Option::{Some, None};
40 /// Adds the attribute to all items in the block.
41 macro_rules! cfg_block {
42 ($(#[$attr:meta]{$($it:item)*})*) => {$($(
48 /// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
49 /// support for larger/smaller pointer widths are added in the future.
50 macro_rules! assume_usize_width {
51 {$($it:item)*} => {#[cfg(not(any(
52 target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64")))]
53 compile_error!("The current tests of try_from on usize/isize assume that \
54 the pointer width is either 16, 32, or 64");
59 /// Helper function for testing numeric operations
60 pub fn test_num<T>(ten: T, two: T) where
62 + Add<Output=T> + Sub<Output=T>
63 + Mul<Output=T> + Div<Output=T>
64 + Rem<Output=T> + Debug
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);
75 fn from_str_issue7588() {
76 let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
78 let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
83 fn test_int_from_str_overflow() {
84 let mut i8_val: i8 = 127;
85 assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
86 assert_eq!("128".parse::<i8>().ok(), None);
88 i8_val = i8_val.wrapping_add(1);
89 assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
90 assert_eq!("-129".parse::<i8>().ok(), None);
92 let mut i16_val: i16 = 32_767;
93 assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
94 assert_eq!("32768".parse::<i16>().ok(), None);
96 i16_val = i16_val.wrapping_add(1);
97 assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
98 assert_eq!("-32769".parse::<i16>().ok(), None);
100 let mut i32_val: i32 = 2_147_483_647;
101 assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
102 assert_eq!("2147483648".parse::<i32>().ok(), None);
104 i32_val = i32_val.wrapping_add(1);
105 assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
106 assert_eq!("-2147483649".parse::<i32>().ok(), None);
108 let mut i64_val: i64 = 9_223_372_036_854_775_807;
109 assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
110 assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
112 i64_val = i64_val.wrapping_add(1);
113 assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
114 assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
118 fn test_leading_plus() {
119 assert_eq!("+127".parse::<u8>().ok(), Some(127));
120 assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
125 assert_eq!("--129".parse::<i8>().ok(), None);
126 assert_eq!("++129".parse::<i8>().ok(), None);
127 assert_eq!("Съешь".parse::<u8>().ok(), None);
132 assert_eq!("-".parse::<i8>().ok(), None);
133 assert_eq!("+".parse::<i8>().ok(), None);
134 assert_eq!("".parse::<u8>().ok(), None);
137 macro_rules! test_impl_from {
138 ($fn_name: ident, $Small: ty, $Large: ty) => {
141 let small_max = <$Small>::max_value();
142 let small_min = <$Small>::min_value();
143 let large_max: $Large = small_max.into();
144 let large_min: $Large = small_min.into();
145 assert_eq!(large_max as $Small, small_max);
146 assert_eq!(large_min as $Small, small_min);
151 // Unsigned -> Unsigned
152 test_impl_from! { test_u8u16, u8, u16 }
153 test_impl_from! { test_u8u32, u8, u32 }
154 test_impl_from! { test_u8u64, u8, u64 }
155 test_impl_from! { test_u8usize, u8, usize }
156 test_impl_from! { test_u16u32, u16, u32 }
157 test_impl_from! { test_u16u64, u16, u64 }
158 test_impl_from! { test_u32u64, u32, u64 }
161 test_impl_from! { test_i8i16, i8, i16 }
162 test_impl_from! { test_i8i32, i8, i32 }
163 test_impl_from! { test_i8i64, i8, i64 }
164 test_impl_from! { test_i8isize, i8, isize }
165 test_impl_from! { test_i16i32, i16, i32 }
166 test_impl_from! { test_i16i64, i16, i64 }
167 test_impl_from! { test_i32i64, i32, i64 }
169 // Unsigned -> Signed
170 test_impl_from! { test_u8i16, u8, i16 }
171 test_impl_from! { test_u8i32, u8, i32 }
172 test_impl_from! { test_u8i64, u8, i64 }
173 test_impl_from! { test_u16i32, u16, i32 }
174 test_impl_from! { test_u16i64, u16, i64 }
175 test_impl_from! { test_u32i64, u32, i64 }
178 test_impl_from! { test_i8f32, i8, f32 }
179 test_impl_from! { test_i8f64, i8, f64 }
180 test_impl_from! { test_i16f32, i16, f32 }
181 test_impl_from! { test_i16f64, i16, f64 }
182 test_impl_from! { test_i32f64, i32, f64 }
185 test_impl_from! { test_u8f32, u8, f32 }
186 test_impl_from! { test_u8f64, u8, f64 }
187 test_impl_from! { test_u16f32, u16, f32 }
188 test_impl_from! { test_u16f64, u16, f64 }
189 test_impl_from! { test_u32f64, u32, f64 }
192 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
197 let max: f64 = f32::MAX.into();
198 assert_eq!(max as f32, f32::MAX);
199 assert!(max.is_normal());
201 let min: f64 = f32::MIN.into();
202 assert_eq!(min as f32, f32::MIN);
203 assert!(min.is_normal());
205 let min_positive: f64 = f32::MIN_POSITIVE.into();
206 assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
207 assert!(min_positive.is_normal());
209 let epsilon: f64 = f32::EPSILON.into();
210 assert_eq!(epsilon as f32, f32::EPSILON);
211 assert!(epsilon.is_normal());
213 let zero: f64 = (0.0f32).into();
214 assert_eq!(zero as f32, 0.0f32);
215 assert!(zero.is_sign_positive());
217 let neg_zero: f64 = (-0.0f32).into();
218 assert_eq!(neg_zero as f32, -0.0f32);
219 assert!(neg_zero.is_sign_negative());
221 let infinity: f64 = f32::INFINITY.into();
222 assert_eq!(infinity as f32, f32::INFINITY);
223 assert!(infinity.is_infinite());
224 assert!(infinity.is_sign_positive());
226 let neg_infinity: f64 = f32::NEG_INFINITY.into();
227 assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
228 assert!(neg_infinity.is_infinite());
229 assert!(neg_infinity.is_sign_negative());
231 let nan: f64 = f32::NAN.into();
232 assert!(nan.is_nan());
236 /// Conversions where the full width of $source can be represented as $target
237 macro_rules! test_impl_try_from_always_ok {
238 ($fn_name:ident, $source:ty, $target: ty) => {
241 let max = <$source>::max_value();
242 let min = <$source>::min_value();
243 let zero: $source = 0;
244 assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
246 assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
248 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
254 test_impl_try_from_always_ok! { test_try_u8u8, u8, u8 }
255 test_impl_try_from_always_ok! { test_try_u8u16, u8, u16 }
256 test_impl_try_from_always_ok! { test_try_u8u32, u8, u32 }
257 test_impl_try_from_always_ok! { test_try_u8u64, u8, u64 }
258 test_impl_try_from_always_ok! { test_try_u8u128, u8, u128 }
259 test_impl_try_from_always_ok! { test_try_u8i16, u8, i16 }
260 test_impl_try_from_always_ok! { test_try_u8i32, u8, i32 }
261 test_impl_try_from_always_ok! { test_try_u8i64, u8, i64 }
262 test_impl_try_from_always_ok! { test_try_u8i128, u8, i128 }
264 test_impl_try_from_always_ok! { test_try_u16u16, u16, u16 }
265 test_impl_try_from_always_ok! { test_try_u16u32, u16, u32 }
266 test_impl_try_from_always_ok! { test_try_u16u64, u16, u64 }
267 test_impl_try_from_always_ok! { test_try_u16u128, u16, u128 }
268 test_impl_try_from_always_ok! { test_try_u16i32, u16, i32 }
269 test_impl_try_from_always_ok! { test_try_u16i64, u16, i64 }
270 test_impl_try_from_always_ok! { test_try_u16i128, u16, i128 }
272 test_impl_try_from_always_ok! { test_try_u32u32, u32, u32 }
273 test_impl_try_from_always_ok! { test_try_u32u64, u32, u64 }
274 test_impl_try_from_always_ok! { test_try_u32u128, u32, u128 }
275 test_impl_try_from_always_ok! { test_try_u32i64, u32, i64 }
276 test_impl_try_from_always_ok! { test_try_u32i128, u32, i128 }
278 test_impl_try_from_always_ok! { test_try_u64u64, u64, u64 }
279 test_impl_try_from_always_ok! { test_try_u64u128, u64, u128 }
280 test_impl_try_from_always_ok! { test_try_u64i128, u64, i128 }
282 test_impl_try_from_always_ok! { test_try_u128, u128, u128 }
284 test_impl_try_from_always_ok! { test_try_i8i8, i8, i8 }
285 test_impl_try_from_always_ok! { test_try_i8i16, i8, i16 }
286 test_impl_try_from_always_ok! { test_try_i8i32, i8, i32 }
287 test_impl_try_from_always_ok! { test_try_i8i64, i8, i64 }
288 test_impl_try_from_always_ok! { test_try_i8i128, i8, i128 }
290 test_impl_try_from_always_ok! { test_try_i16i16, i16, i16 }
291 test_impl_try_from_always_ok! { test_try_i16i32, i16, i32 }
292 test_impl_try_from_always_ok! { test_try_i16i64, i16, i64 }
293 test_impl_try_from_always_ok! { test_try_i16i128, i16, i128 }
295 test_impl_try_from_always_ok! { test_try_i32i32, i32, i32 }
296 test_impl_try_from_always_ok! { test_try_i32i64, i32, i64 }
297 test_impl_try_from_always_ok! { test_try_i32i128, i32, i128 }
299 test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
300 test_impl_try_from_always_ok! { test_try_i64i128, i64, i128 }
302 test_impl_try_from_always_ok! { test_try_i128i128, i128, i128 }
304 assume_usize_width! {
305 test_impl_try_from_always_ok! { test_try_u8usize, u8, usize }
306 test_impl_try_from_always_ok! { test_try_i8isize, i8, isize }
308 test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
309 test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
311 test_impl_try_from_always_ok! { test_try_usizeu64, usize, u64 }
312 test_impl_try_from_always_ok! { test_try_usizeu128, usize, u128 }
313 test_impl_try_from_always_ok! { test_try_usizei128, usize, i128 }
315 test_impl_try_from_always_ok! { test_try_isizei64, isize, i64 }
316 test_impl_try_from_always_ok! { test_try_isizei128, isize, i128 }
319 #[cfg(target_pointer_width = "16")] {
320 test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
321 test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
324 #[cfg(target_pointer_width = "32")] {
325 test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
326 test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
327 test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
328 test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
331 #[cfg(target_pointer_width = "64")] {
332 test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
333 test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
334 test_impl_try_from_always_ok! { test_try_u64usize, u64, usize }
335 test_impl_try_from_always_ok! { test_try_i64isize, i64, isize }
340 /// Conversions where max of $source can be represented as $target,
341 macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
342 ($fn_name:ident, $source:ty, $target:ty) => {
345 let max = <$source>::max_value();
346 let min = <$source>::min_value();
347 let zero: $source = 0;
348 let neg_one: $source = -1;
349 assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
351 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
352 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
354 assert!(<$target as TryFrom<$source>>::try_from(neg_one).is_err());
359 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u8, i8, u8 }
360 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u16, i8, u16 }
361 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u32, i8, u32 }
362 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u64, i8, u64 }
363 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u128, i8, u128 }
365 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u16, i16, u16 }
366 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u32, i16, u32 }
367 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u64, i16, u64 }
368 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u128, i16, u128 }
370 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u32, i32, u32 }
371 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u64, i32, u64 }
372 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u128, i32, u128 }
374 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u64, i64, u64 }
375 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u128, i64, u128 }
377 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i128u128, i128, u128 }
379 assume_usize_width! {
380 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8usize, i8, usize }
381 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16usize, i16, usize }
383 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu64, isize, u64 }
384 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu128, isize, u128 }
385 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeusize, isize, usize }
388 #[cfg(target_pointer_width = "16")] {
389 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu16, isize, u16 }
390 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
393 #[cfg(target_pointer_width = "32")] {
394 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
396 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
399 #[cfg(target_pointer_width = "64")] {
400 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
401 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64usize, i64, usize }
406 /// Conversions where max of $source can not be represented as $target,
408 macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
409 ($fn_name:ident, $source:ty, $target:ty) => {
412 let max = <$source>::max_value();
413 let min = <$source>::min_value();
414 let zero: $source = 0;
415 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
416 assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
418 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
424 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
426 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i8, u16, i8 }
427 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i16, u16, i16 }
429 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i8, u32, i8 }
430 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i16, u32, i16 }
431 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i32, u32, i32 }
433 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i8, u64, i8 }
434 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i16, u64, i16 }
435 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i32, u64, i32 }
436 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i64, u64, i64 }
438 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i8, u128, i8 }
439 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i16, u128, i16 }
440 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i32, u128, i32 }
441 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i64, u128, i64 }
442 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i128, u128, i128 }
444 assume_usize_width! {
445 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64isize, u64, isize }
446 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128isize, u128, isize }
448 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei8, usize, i8 }
449 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei16, usize, i16 }
450 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizeisize, usize, isize }
453 #[cfg(target_pointer_width = "16")] {
454 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
455 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u64, isize }
458 #[cfg(target_pointer_width = "32")] {
459 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
460 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
463 #[cfg(target_pointer_width = "64")] {
464 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
465 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei64, usize, i64 }
470 /// Conversions where min/max of $source can not be represented as $target.
471 macro_rules! test_impl_try_from_same_sign_err {
472 ($fn_name:ident, $source:ty, $target:ty) => {
475 let max = <$source>::max_value();
476 let min = <$source>::min_value();
477 let zero: $source = 0;
478 let t_max = <$target>::max_value();
479 let t_min = <$target>::min_value();
480 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
482 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
484 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
486 assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
489 assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
496 test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
498 test_impl_try_from_same_sign_err! { test_try_u32u8, u32, u8 }
499 test_impl_try_from_same_sign_err! { test_try_u32u16, u32, u16 }
501 test_impl_try_from_same_sign_err! { test_try_u64u8, u64, u8 }
502 test_impl_try_from_same_sign_err! { test_try_u64u16, u64, u16 }
503 test_impl_try_from_same_sign_err! { test_try_u64u32, u64, u32 }
505 test_impl_try_from_same_sign_err! { test_try_u128u8, u128, u8 }
506 test_impl_try_from_same_sign_err! { test_try_u128u16, u128, u16 }
507 test_impl_try_from_same_sign_err! { test_try_u128u32, u128, u32 }
508 test_impl_try_from_same_sign_err! { test_try_u128u64, u128, u64 }
510 test_impl_try_from_same_sign_err! { test_try_i16i8, i16, i8 }
511 test_impl_try_from_same_sign_err! { test_try_isizei8, isize, i8 }
513 test_impl_try_from_same_sign_err! { test_try_i32i8, i32, i8 }
514 test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
516 test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
517 test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
518 test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
520 test_impl_try_from_same_sign_err! { test_try_i128i8, i128, i8 }
521 test_impl_try_from_same_sign_err! { test_try_i128i16, i128, i16 }
522 test_impl_try_from_same_sign_err! { test_try_i128i32, i128, i32 }
523 test_impl_try_from_same_sign_err! { test_try_i128i64, i128, i64 }
525 assume_usize_width! {
526 test_impl_try_from_same_sign_err! { test_try_usizeu8, usize, u8 }
527 test_impl_try_from_same_sign_err! { test_try_u128usize, u128, usize }
528 test_impl_try_from_same_sign_err! { test_try_i128isize, i128, isize }
531 #[cfg(target_pointer_width = "16")] {
532 test_impl_try_from_same_sign_err! { test_try_u32usize, u32, usize }
533 test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
535 test_impl_try_from_same_sign_err! { test_try_i32isize, i32, isize }
536 test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
539 #[cfg(target_pointer_width = "32")] {
540 test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
541 test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
543 test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
544 test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
547 #[cfg(target_pointer_width = "64")] {
548 test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
549 test_impl_try_from_same_sign_err! { test_try_usizeu32, usize, u32 }
551 test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
552 test_impl_try_from_same_sign_err! { test_try_isizei32, isize, i32 }
557 /// Conversinos where neither the min nor the max of $source can be represented by
558 /// $target, but max/min of the target can be represented by the source.
559 macro_rules! test_impl_try_from_signed_to_unsigned_err {
560 ($fn_name:ident, $source:ty, $target:ty) => {
563 let max = <$source>::max_value();
564 let min = <$source>::min_value();
565 let zero: $source = 0;
566 let t_max = <$target>::max_value();
567 let t_min = <$target>::min_value();
568 assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
569 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
570 assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
572 assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
575 assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
582 test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
584 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
585 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
587 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
588 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
589 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }
591 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u8, i128, u8 }
592 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u16, i128, u16 }
593 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u32, i128, u32 }
594 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
596 assume_usize_width! {
597 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
598 test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
601 #[cfg(target_pointer_width = "16")] {
602 test_impl_try_from_signed_to_unsigned_err! { test_try_i32usize, i32, usize }
603 test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
605 #[cfg(target_pointer_width = "32")] {
606 test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
608 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
610 #[cfg(target_pointer_width = "64")] {
611 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
612 test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu32, isize, u32 }
617 macro_rules! test_float {
618 ($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => { mod $modname {
619 use core::num::Float;
620 // FIXME(nagisa): these tests should test for sign of -0.0
623 assert_eq!(0.0.min(0.0), 0.0);
624 assert_eq!((-0.0).min(-0.0), -0.0);
625 assert_eq!(9.0.min(9.0), 9.0);
626 assert_eq!((-9.0).min(0.0), -9.0);
627 assert_eq!(0.0.min(9.0), 0.0);
628 assert_eq!((-0.0).min(-9.0), -9.0);
629 assert_eq!($inf.min(9.0), 9.0);
630 assert_eq!(9.0.min($inf), 9.0);
631 assert_eq!($inf.min(-9.0), -9.0);
632 assert_eq!((-9.0).min($inf), -9.0);
633 assert_eq!($neginf.min(9.0), $neginf);
634 assert_eq!(9.0.min($neginf), $neginf);
635 assert_eq!($neginf.min(-9.0), $neginf);
636 assert_eq!((-9.0).min($neginf), $neginf);
637 assert_eq!($nan.min(9.0), 9.0);
638 assert_eq!($nan.min(-9.0), -9.0);
639 assert_eq!(9.0.min($nan), 9.0);
640 assert_eq!((-9.0).min($nan), -9.0);
641 assert!($nan.min($nan).is_nan());
645 assert_eq!(0.0.max(0.0), 0.0);
646 assert_eq!((-0.0).max(-0.0), -0.0);
647 assert_eq!(9.0.max(9.0), 9.0);
648 assert_eq!((-9.0).max(0.0), 0.0);
649 assert_eq!(0.0.max(9.0), 9.0);
650 assert_eq!((-0.0).max(-9.0), -0.0);
651 assert_eq!($inf.max(9.0), $inf);
652 assert_eq!(9.0.max($inf), $inf);
653 assert_eq!($inf.max(-9.0), $inf);
654 assert_eq!((-9.0).max($inf), $inf);
655 assert_eq!($neginf.max(9.0), 9.0);
656 assert_eq!(9.0.max($neginf), 9.0);
657 assert_eq!($neginf.max(-9.0), -9.0);
658 assert_eq!((-9.0).max($neginf), -9.0);
659 assert_eq!($nan.max(9.0), 9.0);
660 assert_eq!($nan.max(-9.0), -9.0);
661 assert_eq!(9.0.max($nan), 9.0);
662 assert_eq!((-9.0).max($nan), -9.0);
663 assert!($nan.max($nan).is_nan());
668 test_float!(f32, f32, ::core::f32::INFINITY, ::core::f32::NEG_INFINITY, ::core::f32::NAN);
669 test_float!(f64, f64, ::core::f64::INFINITY, ::core::f64::NEG_INFINITY, ::core::f64::NAN);