]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/mod.rs
b34bd66a0ac68300c95268fc369b0cbafd559977
[rust.git] / library / core / tests / num / mod.rs
1 use core::cmp::PartialEq;
2 use core::convert::{TryFrom, TryInto};
3 use core::fmt::Debug;
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;
10
11 #[macro_use]
12 mod int_macros;
13
14 mod i128;
15 mod i16;
16 mod i32;
17 mod i64;
18 mod i8;
19
20 #[macro_use]
21 mod uint_macros;
22
23 mod u128;
24 mod u16;
25 mod u32;
26 mod u64;
27 mod u8;
28
29 mod bignum;
30 mod dec2flt;
31 mod flt2dec;
32 mod wrapping;
33
34 mod nan;
35
36 /// Adds the attribute to all items in the block.
37 macro_rules! cfg_block {
38     ($(#[$attr:meta]{$($it:item)*})*) => {$($(
39         #[$attr]
40         $it
41     )*)*}
42 }
43
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");
51                     $($it)*
52     }
53 }
54
55 /// Helper function for testing numeric operations
56 pub fn test_num<T>(ten: T, two: T)
57 where
58     T: PartialEq
59         + Add<Output = T>
60         + Sub<Output = T>
61         + Mul<Output = T>
62         + Div<Output = T>
63         + Rem<Output = T>
64         + Debug
65         + Copy,
66 {
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);
72 }
73
74 /// Helper function for asserting number parsing returns a specific error
75 fn test_parse<T>(num_str: &str, expected: Result<T, IntErrorKind>)
76 where
77     T: FromStr<Err = ParseIntError>,
78     Result<T, IntErrorKind>: PartialEq + Debug,
79 {
80     assert_eq!(num_str.parse::<T>().map_err(|e| e.kind().clone()), expected)
81 }
82
83 #[test]
84 fn from_str_issue7588() {
85     let u: Option<u8> = u8::from_str_radix("1000", 10).ok();
86     assert_eq!(u, None);
87     let s: Option<i16> = i16::from_str_radix("80000", 10).ok();
88     assert_eq!(s, None);
89 }
90
91 #[test]
92 fn test_int_from_str_overflow() {
93     test_parse::<i8>("127", Ok(127));
94     test_parse::<i8>("128", Err(IntErrorKind::PosOverflow));
95
96     test_parse::<i8>("-128", Ok(-128));
97     test_parse::<i8>("-129", Err(IntErrorKind::NegOverflow));
98
99     test_parse::<i16>("32767", Ok(32_767));
100     test_parse::<i16>("32768", Err(IntErrorKind::PosOverflow));
101
102     test_parse::<i16>("-32768", Ok(-32_768));
103     test_parse::<i16>("-32769", Err(IntErrorKind::NegOverflow));
104
105     test_parse::<i32>("2147483647", Ok(2_147_483_647));
106     test_parse::<i32>("2147483648", Err(IntErrorKind::PosOverflow));
107
108     test_parse::<i32>("-2147483648", Ok(-2_147_483_648));
109     test_parse::<i32>("-2147483649", Err(IntErrorKind::NegOverflow));
110
111     test_parse::<i64>("9223372036854775807", Ok(9_223_372_036_854_775_807));
112     test_parse::<i64>("9223372036854775808", Err(IntErrorKind::PosOverflow));
113
114     test_parse::<i64>("-9223372036854775808", Ok(-9_223_372_036_854_775_808));
115     test_parse::<i64>("-9223372036854775809", Err(IntErrorKind::NegOverflow));
116 }
117
118 #[test]
119 fn test_leading_plus() {
120     test_parse::<u8>("+127", Ok(127));
121     test_parse::<i64>("+9223372036854775807", Ok(9223372036854775807));
122 }
123
124 #[test]
125 fn test_invalid() {
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));
134 }
135
136 #[test]
137 fn test_empty() {
138     test_parse::<u8>("", Err(IntErrorKind::Empty));
139 }
140
141 #[test]
142 fn test_infallible_try_from_int_error() {
143     let func = |x: i8| -> Result<i32, TryFromIntError> { Ok(x.try_into()?) };
144
145     assert!(func(0).is_ok());
146 }
147
148 macro_rules! test_impl_from {
149     ($fn_name:ident, bool, $target: ty) => {
150         #[test]
151         fn $fn_name() {
152             let one: $target = 1;
153             let zero: $target = 0;
154             assert_eq!(one, <$target>::from(true));
155             assert_eq!(zero, <$target>::from(false));
156         }
157     };
158     ($fn_name: ident, $Small: ty, $Large: ty) => {
159         #[test]
160         fn $fn_name() {
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);
167         }
168     };
169 }
170
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 }
179
180 // Signed -> Signed
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 }
188
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 }
196
197 // Bool -> Integer
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 }
208
209 // Signed -> Float
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 }
215
216 // Unsigned -> Float
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 }
222
223 // Float -> Float
224 #[test]
225 fn test_f32f64() {
226     let max: f64 = f32::MAX.into();
227     assert_eq!(max as f32, f32::MAX);
228     assert!(max.is_normal());
229
230     let min: f64 = f32::MIN.into();
231     assert_eq!(min as f32, f32::MIN);
232     assert!(min.is_normal());
233
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());
237
238     let epsilon: f64 = f32::EPSILON.into();
239     assert_eq!(epsilon as f32, f32::EPSILON);
240     assert!(epsilon.is_normal());
241
242     let zero: f64 = (0.0f32).into();
243     assert_eq!(zero as f32, 0.0f32);
244     assert!(zero.is_sign_positive());
245
246     let neg_zero: f64 = (-0.0f32).into();
247     assert_eq!(neg_zero as f32, -0.0f32);
248     assert!(neg_zero.is_sign_negative());
249
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());
254
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());
259
260     let nan: f64 = f32::NAN.into();
261     assert!(nan.is_nan());
262 }
263
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) => {
267         #[test]
268         fn $fn_name() {
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);
275         }
276     };
277 }
278
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 }
288
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 }
296
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 }
302
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 }
306
307 test_impl_try_from_always_ok! { test_try_u128u128, u128, u128 }
308
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 }
314
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 }
319
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 }
323
324 test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
325 test_impl_try_from_always_ok! { test_try_i64i128, i64, i128 }
326
327 test_impl_try_from_always_ok! { test_try_i128i128, i128, i128 }
328
329 test_impl_try_from_always_ok! { test_try_usizeusize, usize, usize }
330 test_impl_try_from_always_ok! { test_try_isizeisize, isize, isize }
331
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 }
336
337     test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
338     test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
339
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 }
343
344     test_impl_try_from_always_ok! { test_try_isizei64, isize, i64 }
345     test_impl_try_from_always_ok! { test_try_isizei128, isize, i128 }
346
347     cfg_block!(
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 }
355         }
356
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 }
364         }
365
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 }
373         }
374     );
375 }
376
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) => {
380         #[test]
381         fn $fn_name() {
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());
390         }
391     };
392 }
393
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 }
399
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 }
404
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 }
408
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 }
411
412 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i128u128, i128, u128 }
413
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 }
417
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 }
421
422     cfg_block!(
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 }
426         }
427
428         #[cfg(target_pointer_width = "32")] {
429             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
430
431             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
432         }
433
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 }
437         }
438     );
439 }
440
441 /// Conversions where max of $source can not be represented as $target,
442 /// but min can.
443 macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
444     ($fn_name:ident, $source:ty, $target:ty) => {
445         #[test]
446         fn $fn_name() {
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);
453         }
454     };
455 }
456
457 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
458
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 }
461
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 }
465
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 }
470
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 }
476
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 }
480
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 }
484
485     cfg_block!(
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 }
489         }
490
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 }
494         }
495
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 }
499         }
500     );
501 }
502
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) => {
506         #[test]
507         fn $fn_name() {
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());
514             if min != 0 {
515                 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
516             }
517             assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
518             assert_eq!(
519                 <$target as TryFrom<$source>>::try_from(t_max as $source).unwrap(),
520                 t_max as $target
521             );
522             assert_eq!(
523                 <$target as TryFrom<$source>>::try_from(t_min as $source).unwrap(),
524                 t_min as $target
525             );
526         }
527     };
528 }
529
530 test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
531
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 }
534
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 }
538
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 }
543
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 }
546
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 }
549
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 }
553
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 }
558
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 }
563
564     cfg_block!(
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 }
568
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 }
571         }
572
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 }
576
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 }
579         }
580
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 }
584
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 }
587         }
588     );
589 }
590
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) => {
595         #[test]
596         fn $fn_name() {
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);
605             assert_eq!(
606                 <$target as TryFrom<$source>>::try_from(t_max as $source).unwrap(),
607                 t_max as $target
608             );
609             assert_eq!(
610                 <$target as TryFrom<$source>>::try_from(t_min as $source).unwrap(),
611                 t_min as $target
612             );
613         }
614     };
615 }
616
617 test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
618
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 }
621
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 }
625
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 }
630
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 }
634
635     cfg_block! {
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 }
639         }
640         #[cfg(target_pointer_width = "32")] {
641             test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
642
643             test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
644         }
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 }
648         }
649     }
650 }
651
652 macro_rules! test_float {
653     ($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => {
654         mod $modname {
655             #[test]
656             fn min() {
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());
681             }
682             #[test]
683             fn max() {
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());
711             }
712             #[test]
713             fn rem_euclid() {
714                 let a: $fty = 42.0;
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());
721             }
722             #[test]
723             fn div_euclid() {
724                 let a: $fty = 42.0;
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());
730             }
731         }
732     };
733 }
734
735 test_float!(f32, f32, f32::INFINITY, f32::NEG_INFINITY, f32::NAN);
736 test_float!(f64, f64, f64::INFINITY, f64::NEG_INFINITY, f64::NAN);