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, TryInto}
;
12 use core
::cmp
::PartialEq
;
14 use core
::marker
::Copy
;
15 use core
::num
::TryFromIntError
;
16 use core
::ops
::{Add, Sub, Mul, Div, Rem}
;
17 use core
::option
::Option
;
18 use core
::option
::Option
::{Some, None}
;
41 /// Adds the attribute to all items in the block.
42 macro_rules
! cfg_block
{
43 ($
(#[$attr:meta]{$($it:item)*})*) => {$($(
49 /// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
50 /// support for larger/smaller pointer widths are added in the future.
51 macro_rules
! assume_usize_width
{
52 {$($it:item)*}
=> {#[cfg(not(any(
53 target_pointer_width
= "16", target_pointer_width
= "32", target_pointer_width
= "64")))]
54 compile_error
!("The current tests of try_from on usize/isize assume that \
55 the pointer width is either 16, 32, or 64");
60 /// Helper function for testing numeric operations
61 pub fn test_num
<T
>(ten
: T
, two
: T
) where
63 + Add
<Output
=T
> + Sub
<Output
=T
>
64 + Mul
<Output
=T
> + Div
<Output
=T
>
65 + Rem
<Output
=T
> + Debug
68 assert_eq
!(ten
.add(two
), ten
+ two
);
69 assert_eq
!(ten
.sub(two
), ten
- two
);
70 assert_eq
!(ten
.mul(two
), ten
* two
);
71 assert_eq
!(ten
.div(two
), ten
/ two
);
72 assert_eq
!(ten
.rem(two
), ten
% two
);
76 fn from_str_issue7588() {
77 let u
: Option
<u8> = u8::from_str_radix("1000", 10).ok();
79 let s
: Option
<i16> = i16::from_str_radix("80000", 10).ok();
84 fn test_int_from_str_overflow() {
85 let mut i8_val
: i8 = 127;
86 assert_eq
!("127".parse
::<i8>().ok(), Some(i8_val
));
87 assert_eq
!("128".parse
::<i8>().ok(), None
);
89 i8_val
= i8_val
.wrapping_add(1);
90 assert_eq
!("-128".parse
::<i8>().ok(), Some(i8_val
));
91 assert_eq
!("-129".parse
::<i8>().ok(), None
);
93 let mut i16_val
: i16 = 32_767;
94 assert_eq
!("32767".parse
::<i16>().ok(), Some(i16_val
));
95 assert_eq
!("32768".parse
::<i16>().ok(), None
);
97 i16_val
= i16_val
.wrapping_add(1);
98 assert_eq
!("-32768".parse
::<i16>().ok(), Some(i16_val
));
99 assert_eq
!("-32769".parse
::<i16>().ok(), None
);
101 let mut i32_val
: i32 = 2_147_483_647;
102 assert_eq
!("2147483647".parse
::<i32>().ok(), Some(i32_val
));
103 assert_eq
!("2147483648".parse
::<i32>().ok(), None
);
105 i32_val
= i32_val
.wrapping_add(1);
106 assert_eq
!("-2147483648".parse
::<i32>().ok(), Some(i32_val
));
107 assert_eq
!("-2147483649".parse
::<i32>().ok(), None
);
109 let mut i64_val
: i64 = 9_223_372_036_854_775_807;
110 assert_eq
!("9223372036854775807".parse
::<i64>().ok(), Some(i64_val
));
111 assert_eq
!("9223372036854775808".parse
::<i64>().ok(), None
);
113 i64_val
= i64_val
.wrapping_add(1);
114 assert_eq
!("-9223372036854775808".parse
::<i64>().ok(), Some(i64_val
));
115 assert_eq
!("-9223372036854775809".parse
::<i64>().ok(), None
);
119 fn test_leading_plus() {
120 assert_eq
!("+127".parse
::<u8>().ok(), Some(127));
121 assert_eq
!("+9223372036854775807".parse
::<i64>().ok(), Some(9223372036854775807));
126 assert_eq
!("--129".parse
::<i8>().ok(), None
);
127 assert_eq
!("++129".parse
::<i8>().ok(), None
);
128 assert_eq
!("Съешь".parse
::<u8>().ok(), None
);
133 assert_eq
!("-".parse
::<i8>().ok(), None
);
134 assert_eq
!("+".parse
::<i8>().ok(), None
);
135 assert_eq
!("".parse
::<u8>().ok(), None
);
139 fn test_infallible_try_from_int_error() {
140 let func
= |x
: i8| -> Result
<i32, TryFromIntError
> { Ok(x.try_into()?) }
;
142 assert
!(func(0).is_ok());
145 macro_rules
! test_impl_from
{
146 ($fn_name
: ident
, $Small
: ty
, $Large
: ty
) => {
149 let small_max
= <$Small
>::max_value();
150 let small_min
= <$Small
>::min_value();
151 let large_max
: $Large
= small_max
.into();
152 let large_min
: $Large
= small_min
.into();
153 assert_eq
!(large_max
as $Small
, small_max
);
154 assert_eq
!(large_min
as $Small
, small_min
);
159 // Unsigned -> Unsigned
160 test_impl_from
! { test_u8u16, u8, u16 }
161 test_impl_from
! { test_u8u32, u8, u32 }
162 test_impl_from
! { test_u8u64, u8, u64 }
163 test_impl_from
! { test_u8usize, u8, usize }
164 test_impl_from
! { test_u16u32, u16, u32 }
165 test_impl_from
! { test_u16u64, u16, u64 }
166 test_impl_from
! { test_u32u64, u32, u64 }
169 test_impl_from
! { test_i8i16, i8, i16 }
170 test_impl_from
! { test_i8i32, i8, i32 }
171 test_impl_from
! { test_i8i64, i8, i64 }
172 test_impl_from
! { test_i8isize, i8, isize }
173 test_impl_from
! { test_i16i32, i16, i32 }
174 test_impl_from
! { test_i16i64, i16, i64 }
175 test_impl_from
! { test_i32i64, i32, i64 }
177 // Unsigned -> Signed
178 test_impl_from
! { test_u8i16, u8, i16 }
179 test_impl_from
! { test_u8i32, u8, i32 }
180 test_impl_from
! { test_u8i64, u8, i64 }
181 test_impl_from
! { test_u16i32, u16, i32 }
182 test_impl_from
! { test_u16i64, u16, i64 }
183 test_impl_from
! { test_u32i64, u32, i64 }
186 test_impl_from
! { test_i8f32, i8, f32 }
187 test_impl_from
! { test_i8f64, i8, f64 }
188 test_impl_from
! { test_i16f32, i16, f32 }
189 test_impl_from
! { test_i16f64, i16, f64 }
190 test_impl_from
! { test_i32f64, i32, f64 }
193 test_impl_from
! { test_u8f32, u8, f32 }
194 test_impl_from
! { test_u8f64, u8, f64 }
195 test_impl_from
! { test_u16f32, u16, f32 }
196 test_impl_from
! { test_u16f64, u16, f64 }
197 test_impl_from
! { test_u32f64, u32, f64 }
200 #[cfg_attr(all(target_arch = "wasm32", target_os = "emscripten"), ignore)] // issue 42630
205 let max
: f64 = f32::MAX
.into();
206 assert_eq
!(max
as f32, f32::MAX
);
207 assert
!(max
.is_normal());
209 let min
: f64 = f32::MIN
.into();
210 assert_eq
!(min
as f32, f32::MIN
);
211 assert
!(min
.is_normal());
213 let min_positive
: f64 = f32::MIN_POSITIVE
.into();
214 assert_eq
!(min_positive
as f32, f32::MIN_POSITIVE
);
215 assert
!(min_positive
.is_normal());
217 let epsilon
: f64 = f32::EPSILON
.into();
218 assert_eq
!(epsilon
as f32, f32::EPSILON
);
219 assert
!(epsilon
.is_normal());
221 let zero
: f64 = (0.0f32).into();
222 assert_eq
!(zero
as f32, 0.0f32);
223 assert
!(zero
.is_sign_positive());
225 let neg_zero
: f64 = (-0.0f32).into();
226 assert_eq
!(neg_zero
as f32, -0.0f32);
227 assert
!(neg_zero
.is_sign_negative());
229 let infinity
: f64 = f32::INFINITY
.into();
230 assert_eq
!(infinity
as f32, f32::INFINITY
);
231 assert
!(infinity
.is_infinite());
232 assert
!(infinity
.is_sign_positive());
234 let neg_infinity
: f64 = f32::NEG_INFINITY
.into();
235 assert_eq
!(neg_infinity
as f32, f32::NEG_INFINITY
);
236 assert
!(neg_infinity
.is_infinite());
237 assert
!(neg_infinity
.is_sign_negative());
239 let nan
: f64 = f32::NAN
.into();
240 assert
!(nan
.is_nan());
244 /// Conversions where the full width of $source can be represented as $target
245 macro_rules
! test_impl_try_from_always_ok
{
246 ($fn_name
:ident
, $source
:ty
, $target
: ty
) => {
249 let max
= <$source
>::max_value();
250 let min
= <$source
>::min_value();
251 let zero
: $source
= 0;
252 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(max
).unwrap(),
254 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(min
).unwrap(),
256 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(zero
).unwrap(),
262 test_impl_try_from_always_ok
! { test_try_u8u8, u8, u8 }
263 test_impl_try_from_always_ok
! { test_try_u8u16, u8, u16 }
264 test_impl_try_from_always_ok
! { test_try_u8u32, u8, u32 }
265 test_impl_try_from_always_ok
! { test_try_u8u64, u8, u64 }
266 test_impl_try_from_always_ok
! { test_try_u8u128, u8, u128 }
267 test_impl_try_from_always_ok
! { test_try_u8i16, u8, i16 }
268 test_impl_try_from_always_ok
! { test_try_u8i32, u8, i32 }
269 test_impl_try_from_always_ok
! { test_try_u8i64, u8, i64 }
270 test_impl_try_from_always_ok
! { test_try_u8i128, u8, i128 }
272 test_impl_try_from_always_ok
! { test_try_u16u16, u16, u16 }
273 test_impl_try_from_always_ok
! { test_try_u16u32, u16, u32 }
274 test_impl_try_from_always_ok
! { test_try_u16u64, u16, u64 }
275 test_impl_try_from_always_ok
! { test_try_u16u128, u16, u128 }
276 test_impl_try_from_always_ok
! { test_try_u16i32, u16, i32 }
277 test_impl_try_from_always_ok
! { test_try_u16i64, u16, i64 }
278 test_impl_try_from_always_ok
! { test_try_u16i128, u16, i128 }
280 test_impl_try_from_always_ok
! { test_try_u32u32, u32, u32 }
281 test_impl_try_from_always_ok
! { test_try_u32u64, u32, u64 }
282 test_impl_try_from_always_ok
! { test_try_u32u128, u32, u128 }
283 test_impl_try_from_always_ok
! { test_try_u32i64, u32, i64 }
284 test_impl_try_from_always_ok
! { test_try_u32i128, u32, i128 }
286 test_impl_try_from_always_ok
! { test_try_u64u64, u64, u64 }
287 test_impl_try_from_always_ok
! { test_try_u64u128, u64, u128 }
288 test_impl_try_from_always_ok
! { test_try_u64i128, u64, i128 }
290 test_impl_try_from_always_ok
! { test_try_u128u128, u128, u128 }
292 test_impl_try_from_always_ok
! { test_try_i8i8, i8, i8 }
293 test_impl_try_from_always_ok
! { test_try_i8i16, i8, i16 }
294 test_impl_try_from_always_ok
! { test_try_i8i32, i8, i32 }
295 test_impl_try_from_always_ok
! { test_try_i8i64, i8, i64 }
296 test_impl_try_from_always_ok
! { test_try_i8i128, i8, i128 }
298 test_impl_try_from_always_ok
! { test_try_i16i16, i16, i16 }
299 test_impl_try_from_always_ok
! { test_try_i16i32, i16, i32 }
300 test_impl_try_from_always_ok
! { test_try_i16i64, i16, i64 }
301 test_impl_try_from_always_ok
! { test_try_i16i128, i16, i128 }
303 test_impl_try_from_always_ok
! { test_try_i32i32, i32, i32 }
304 test_impl_try_from_always_ok
! { test_try_i32i64, i32, i64 }
305 test_impl_try_from_always_ok
! { test_try_i32i128, i32, i128 }
307 test_impl_try_from_always_ok
! { test_try_i64i64, i64, i64 }
308 test_impl_try_from_always_ok
! { test_try_i64i128, i64, i128 }
310 test_impl_try_from_always_ok
! { test_try_i128i128, i128, i128 }
312 test_impl_try_from_always_ok
! { test_try_usizeusize, usize, usize }
313 test_impl_try_from_always_ok
! { test_try_isizeisize, isize, isize }
315 assume_usize_width
! {
316 test_impl_try_from_always_ok
! { test_try_u8usize, u8, usize }
317 test_impl_try_from_always_ok
! { test_try_u8isize, u8, isize }
318 test_impl_try_from_always_ok
! { test_try_i8isize, i8, isize }
320 test_impl_try_from_always_ok
! { test_try_u16usize, u16, usize }
321 test_impl_try_from_always_ok
! { test_try_i16isize, i16, isize }
323 test_impl_try_from_always_ok
! { test_try_usizeu64, usize, u64 }
324 test_impl_try_from_always_ok
! { test_try_usizeu128, usize, u128 }
325 test_impl_try_from_always_ok
! { test_try_usizei128, usize, i128 }
327 test_impl_try_from_always_ok
! { test_try_isizei64, isize, i64 }
328 test_impl_try_from_always_ok
! { test_try_isizei128, isize, i128 }
331 #[cfg(target_pointer_width = "16")] {
332 test_impl_try_from_always_ok
! { test_try_usizeu16, usize, u16 }
333 test_impl_try_from_always_ok
! { test_try_isizei16, isize, i16 }
334 test_impl_try_from_always_ok
! { test_try_usizeu32, usize, u32 }
335 test_impl_try_from_always_ok
! { test_try_usizei32, usize, i32 }
336 test_impl_try_from_always_ok
! { test_try_isizei32, isize, i32 }
337 test_impl_try_from_always_ok
! { test_try_usizei64, usize, i64 }
340 #[cfg(target_pointer_width = "32")] {
341 test_impl_try_from_always_ok
! { test_try_u16isize, u16, isize }
342 test_impl_try_from_always_ok
! { test_try_usizeu32, usize, u32 }
343 test_impl_try_from_always_ok
! { test_try_isizei32, isize, i32 }
344 test_impl_try_from_always_ok
! { test_try_u32usize, u32, usize }
345 test_impl_try_from_always_ok
! { test_try_i32isize, i32, isize }
346 test_impl_try_from_always_ok
! { test_try_usizei64, usize, i64 }
349 #[cfg(target_pointer_width = "64")] {
350 test_impl_try_from_always_ok
! { test_try_u16isize, u16, isize }
351 test_impl_try_from_always_ok
! { test_try_u32usize, u32, usize }
352 test_impl_try_from_always_ok
! { test_try_u32isize, u32, isize }
353 test_impl_try_from_always_ok
! { test_try_i32isize, i32, isize }
354 test_impl_try_from_always_ok
! { test_try_u64usize, u64, usize }
355 test_impl_try_from_always_ok
! { test_try_i64isize, i64, isize }
360 /// Conversions where max of $source can be represented as $target,
361 macro_rules
! test_impl_try_from_signed_to_unsigned_upper_ok
{
362 ($fn_name
:ident
, $source
:ty
, $target
:ty
) => {
365 let max
= <$source
>::max_value();
366 let min
= <$source
>::min_value();
367 let zero
: $source
= 0;
368 let neg_one
: $source
= -1;
369 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(max
).unwrap(),
371 assert
!(<$target
as TryFrom
<$source
>>::try_from(min
).is_err());
372 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(zero
).unwrap(),
374 assert
!(<$target
as TryFrom
<$source
>>::try_from(neg_one
).is_err());
379 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i8u8, i8, u8 }
380 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i8u16, i8, u16 }
381 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i8u32, i8, u32 }
382 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i8u64, i8, u64 }
383 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i8u128, i8, u128 }
385 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i16u16, i16, u16 }
386 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i16u32, i16, u32 }
387 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i16u64, i16, u64 }
388 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i16u128, i16, u128 }
390 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i32u32, i32, u32 }
391 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i32u64, i32, u64 }
392 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i32u128, i32, u128 }
394 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i64u64, i64, u64 }
395 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i64u128, i64, u128 }
397 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i128u128, i128, u128 }
399 assume_usize_width
! {
400 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i8usize, i8, usize }
401 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i16usize, i16, usize }
403 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_isizeu64, isize, u64 }
404 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_isizeu128, isize, u128 }
405 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_isizeusize, isize, usize }
408 #[cfg(target_pointer_width = "16")] {
409 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_isizeu16, isize, u16 }
410 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_isizeu32, isize, u32 }
413 #[cfg(target_pointer_width = "32")] {
414 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_isizeu32, isize, u32 }
416 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i32usize, i32, usize }
419 #[cfg(target_pointer_width = "64")] {
420 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i32usize, i32, usize }
421 test_impl_try_from_signed_to_unsigned_upper_ok
! { test_try_i64usize, i64, usize }
426 /// Conversions where max of $source can not be represented as $target,
428 macro_rules
! test_impl_try_from_unsigned_to_signed_upper_err
{
429 ($fn_name
:ident
, $source
:ty
, $target
:ty
) => {
432 let max
= <$source
>::max_value();
433 let min
= <$source
>::min_value();
434 let zero
: $source
= 0;
435 assert
!(<$target
as TryFrom
<$source
>>::try_from(max
).is_err());
436 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(min
).unwrap(),
438 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(zero
).unwrap(),
444 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u8i8, u8, i8 }
446 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u16i8, u16, i8 }
447 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u16i16, u16, i16 }
449 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u32i8, u32, i8 }
450 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u32i16, u32, i16 }
451 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u32i32, u32, i32 }
453 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u64i8, u64, i8 }
454 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u64i16, u64, i16 }
455 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u64i32, u64, i32 }
456 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u64i64, u64, i64 }
458 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u128i8, u128, i8 }
459 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u128i16, u128, i16 }
460 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u128i32, u128, i32 }
461 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u128i64, u128, i64 }
462 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u128i128, u128, i128 }
464 assume_usize_width
! {
465 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u64isize, u64, isize }
466 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u128isize, u128, isize }
468 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_usizei8, usize, i8 }
469 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_usizei16, usize, i16 }
470 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_usizeisize, usize, isize }
473 #[cfg(target_pointer_width = "16")] {
474 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u16isize, u16, isize }
475 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u32isize, u32, isize }
478 #[cfg(target_pointer_width = "32")] {
479 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_u32isize, u32, isize }
480 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_usizei32, usize, i32 }
483 #[cfg(target_pointer_width = "64")] {
484 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_usizei32, usize, i32 }
485 test_impl_try_from_unsigned_to_signed_upper_err
! { test_try_usizei64, usize, i64 }
490 /// Conversions where min/max of $source can not be represented as $target.
491 macro_rules
! test_impl_try_from_same_sign_err
{
492 ($fn_name
:ident
, $source
:ty
, $target
:ty
) => {
495 let max
= <$source
>::max_value();
496 let min
= <$source
>::min_value();
497 let zero
: $source
= 0;
498 let t_max
= <$target
>::max_value();
499 let t_min
= <$target
>::min_value();
500 assert
!(<$target
as TryFrom
<$source
>>::try_from(max
).is_err());
502 assert
!(<$target
as TryFrom
<$source
>>::try_from(min
).is_err());
504 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(zero
).unwrap(),
506 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(t_max
as $source
)
509 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(t_min
as $source
)
516 test_impl_try_from_same_sign_err
! { test_try_u16u8, u16, u8 }
518 test_impl_try_from_same_sign_err
! { test_try_u32u8, u32, u8 }
519 test_impl_try_from_same_sign_err
! { test_try_u32u16, u32, u16 }
521 test_impl_try_from_same_sign_err
! { test_try_u64u8, u64, u8 }
522 test_impl_try_from_same_sign_err
! { test_try_u64u16, u64, u16 }
523 test_impl_try_from_same_sign_err
! { test_try_u64u32, u64, u32 }
525 test_impl_try_from_same_sign_err
! { test_try_u128u8, u128, u8 }
526 test_impl_try_from_same_sign_err
! { test_try_u128u16, u128, u16 }
527 test_impl_try_from_same_sign_err
! { test_try_u128u32, u128, u32 }
528 test_impl_try_from_same_sign_err
! { test_try_u128u64, u128, u64 }
530 test_impl_try_from_same_sign_err
! { test_try_i16i8, i16, i8 }
531 test_impl_try_from_same_sign_err
! { test_try_isizei8, isize, i8 }
533 test_impl_try_from_same_sign_err
! { test_try_i32i8, i32, i8 }
534 test_impl_try_from_same_sign_err
! { test_try_i32i16, i32, i16 }
536 test_impl_try_from_same_sign_err
! { test_try_i64i8, i64, i8 }
537 test_impl_try_from_same_sign_err
! { test_try_i64i16, i64, i16 }
538 test_impl_try_from_same_sign_err
! { test_try_i64i32, i64, i32 }
540 test_impl_try_from_same_sign_err
! { test_try_i128i8, i128, i8 }
541 test_impl_try_from_same_sign_err
! { test_try_i128i16, i128, i16 }
542 test_impl_try_from_same_sign_err
! { test_try_i128i32, i128, i32 }
543 test_impl_try_from_same_sign_err
! { test_try_i128i64, i128, i64 }
545 assume_usize_width
! {
546 test_impl_try_from_same_sign_err
! { test_try_usizeu8, usize, u8 }
547 test_impl_try_from_same_sign_err
! { test_try_u128usize, u128, usize }
548 test_impl_try_from_same_sign_err
! { test_try_i128isize, i128, isize }
551 #[cfg(target_pointer_width = "16")] {
552 test_impl_try_from_same_sign_err
! { test_try_u32usize, u32, usize }
553 test_impl_try_from_same_sign_err
! { test_try_u64usize, u64, usize }
555 test_impl_try_from_same_sign_err
! { test_try_i32isize, i32, isize }
556 test_impl_try_from_same_sign_err
! { test_try_i64isize, i64, isize }
559 #[cfg(target_pointer_width = "32")] {
560 test_impl_try_from_same_sign_err
! { test_try_u64usize, u64, usize }
561 test_impl_try_from_same_sign_err
! { test_try_usizeu16, usize, u16 }
563 test_impl_try_from_same_sign_err
! { test_try_i64isize, i64, isize }
564 test_impl_try_from_same_sign_err
! { test_try_isizei16, isize, i16 }
567 #[cfg(target_pointer_width = "64")] {
568 test_impl_try_from_same_sign_err
! { test_try_usizeu16, usize, u16 }
569 test_impl_try_from_same_sign_err
! { test_try_usizeu32, usize, u32 }
571 test_impl_try_from_same_sign_err
! { test_try_isizei16, isize, i16 }
572 test_impl_try_from_same_sign_err
! { test_try_isizei32, isize, i32 }
577 /// Conversions where neither the min nor the max of $source can be represented by
578 /// $target, but max/min of the target can be represented by the source.
579 macro_rules
! test_impl_try_from_signed_to_unsigned_err
{
580 ($fn_name
:ident
, $source
:ty
, $target
:ty
) => {
583 let max
= <$source
>::max_value();
584 let min
= <$source
>::min_value();
585 let zero
: $source
= 0;
586 let t_max
= <$target
>::max_value();
587 let t_min
= <$target
>::min_value();
588 assert
!(<$target
as TryFrom
<$source
>>::try_from(max
).is_err());
589 assert
!(<$target
as TryFrom
<$source
>>::try_from(min
).is_err());
590 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(zero
).unwrap(),
592 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(t_max
as $source
)
595 assert_eq
!(<$target
as TryFrom
<$source
>>::try_from(t_min
as $source
)
602 test_impl_try_from_signed_to_unsigned_err
! { test_try_i16u8, i16, u8 }
604 test_impl_try_from_signed_to_unsigned_err
! { test_try_i32u8, i32, u8 }
605 test_impl_try_from_signed_to_unsigned_err
! { test_try_i32u16, i32, u16 }
607 test_impl_try_from_signed_to_unsigned_err
! { test_try_i64u8, i64, u8 }
608 test_impl_try_from_signed_to_unsigned_err
! { test_try_i64u16, i64, u16 }
609 test_impl_try_from_signed_to_unsigned_err
! { test_try_i64u32, i64, u32 }
611 test_impl_try_from_signed_to_unsigned_err
! { test_try_i128u8, i128, u8 }
612 test_impl_try_from_signed_to_unsigned_err
! { test_try_i128u16, i128, u16 }
613 test_impl_try_from_signed_to_unsigned_err
! { test_try_i128u32, i128, u32 }
614 test_impl_try_from_signed_to_unsigned_err
! { test_try_i128u64, i128, u64 }
616 assume_usize_width
! {
617 test_impl_try_from_signed_to_unsigned_err
! { test_try_isizeu8, isize, u8 }
618 test_impl_try_from_signed_to_unsigned_err
! { test_try_i128usize, i128, usize }
621 #[cfg(target_pointer_width = "16")] {
622 test_impl_try_from_signed_to_unsigned_err
! { test_try_i32usize, i32, usize }
623 test_impl_try_from_signed_to_unsigned_err
! { test_try_i64usize, i64, usize }
625 #[cfg(target_pointer_width = "32")] {
626 test_impl_try_from_signed_to_unsigned_err
! { test_try_i64usize, i64, usize }
628 test_impl_try_from_signed_to_unsigned_err
! { test_try_isizeu16, isize, u16 }
630 #[cfg(target_pointer_width = "64")] {
631 test_impl_try_from_signed_to_unsigned_err
! { test_try_isizeu16, isize, u16 }
632 test_impl_try_from_signed_to_unsigned_err
! { test_try_isizeu32, isize, u32 }
637 macro_rules
! test_float
{
638 ($modname
: ident
, $fty
: ty
, $inf
: expr
, $neginf
: expr
, $nan
: expr
) => { mod $modname
{
639 use core
::num
::Float
;
640 // FIXME(nagisa): these tests should test for sign of -0.0
643 assert_eq
!(0.0.min(0.0), 0.0);
644 assert_eq
!((-0.0).min(-0.0), -0.0);
645 assert_eq
!(9.0.min(9.0), 9.0);
646 assert_eq
!((-9.0).min(0.0), -9.0);
647 assert_eq
!(0.0.min(9.0), 0.0);
648 assert_eq
!((-0.0).min(-9.0), -9.0);
649 assert_eq
!($inf
.min(9.0), 9.0);
650 assert_eq
!(9.0.min($inf
), 9.0);
651 assert_eq
!($inf
.min(-9.0), -9.0);
652 assert_eq
!((-9.0).min($inf
), -9.0);
653 assert_eq
!($neginf
.min(9.0), $neginf
);
654 assert_eq
!(9.0.min($neginf
), $neginf
);
655 assert_eq
!($neginf
.min(-9.0), $neginf
);
656 assert_eq
!((-9.0).min($neginf
), $neginf
);
657 assert_eq
!($nan
.min(9.0), 9.0);
658 assert_eq
!($nan
.min(-9.0), -9.0);
659 assert_eq
!(9.0.min($nan
), 9.0);
660 assert_eq
!((-9.0).min($nan
), -9.0);
661 assert
!($nan
.min($nan
).is_nan());
665 assert_eq
!(0.0.max(0.0), 0.0);
666 assert_eq
!((-0.0).max(-0.0), -0.0);
667 assert_eq
!(9.0.max(9.0), 9.0);
668 assert_eq
!((-9.0).max(0.0), 0.0);
669 assert_eq
!(0.0.max(9.0), 9.0);
670 assert_eq
!((-0.0).max(-9.0), -0.0);
671 assert_eq
!($inf
.max(9.0), $inf
);
672 assert_eq
!(9.0.max($inf
), $inf
);
673 assert_eq
!($inf
.max(-9.0), $inf
);
674 assert_eq
!((-9.0).max($inf
), $inf
);
675 assert_eq
!($neginf
.max(9.0), 9.0);
676 assert_eq
!(9.0.max($neginf
), 9.0);
677 assert_eq
!($neginf
.max(-9.0), -9.0);
678 assert_eq
!((-9.0).max($neginf
), -9.0);
679 assert_eq
!($nan
.max(9.0), 9.0);
680 assert_eq
!($nan
.max(-9.0), -9.0);
681 assert_eq
!(9.0.max($nan
), 9.0);
682 assert_eq
!((-9.0).max($nan
), -9.0);
683 assert
!($nan
.max($nan
).is_nan());
688 test_float
!(f32, f32, ::core
::f32::INFINITY
, ::core
::f32::NEG_INFINITY
, ::core
::f32::NAN
);
689 test_float
!(f64, f64, ::core
::f64::INFINITY
, ::core
::f64::NEG_INFINITY
, ::core
::f64::NAN
);