]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/mod.rs
Adds tests to ensure some base op traits exist.
[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 ops;
33 mod wrapping;
34
35 mod nan;
36
37 /// Adds the attribute to all items in the block.
38 macro_rules! cfg_block {
39     ($(#[$attr:meta]{$($it:item)*})*) => {$($(
40         #[$attr]
41         $it
42     )*)*}
43 }
44
45 /// Groups items that assume the pointer width is either 16/32/64, and has to be altered if
46 /// support for larger/smaller pointer widths are added in the future.
47 macro_rules! assume_usize_width {
48     {$($it:item)*} => {#[cfg(not(any(
49         target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64")))]
50            compile_error!("The current tests of try_from on usize/isize assume that \
51                            the pointer width is either 16, 32, or 64");
52                     $($it)*
53     }
54 }
55
56 /// Helper function for testing numeric operations
57 pub fn test_num<T>(ten: T, two: T)
58 where
59     T: PartialEq
60         + Add<Output = T>
61         + Sub<Output = T>
62         + Mul<Output = T>
63         + Div<Output = T>
64         + Rem<Output = T>
65         + Debug
66         + Copy,
67 {
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);
73 }
74
75 /// Helper function for asserting number parsing returns a specific error
76 fn test_parse<T>(num_str: &str, expected: Result<T, IntErrorKind>)
77 where
78     T: FromStr<Err = ParseIntError>,
79     Result<T, IntErrorKind>: PartialEq + Debug,
80 {
81     assert_eq!(num_str.parse::<T>().map_err(|e| e.kind().clone()), expected)
82 }
83
84 #[test]
85 fn from_str_issue7588() {
86     let u: Option<u8> = u8::from_str_radix("1000", 10).ok();
87     assert_eq!(u, None);
88     let s: Option<i16> = i16::from_str_radix("80000", 10).ok();
89     assert_eq!(s, None);
90 }
91
92 #[test]
93 fn test_int_from_str_overflow() {
94     test_parse::<i8>("127", Ok(127));
95     test_parse::<i8>("128", Err(IntErrorKind::PosOverflow));
96
97     test_parse::<i8>("-128", Ok(-128));
98     test_parse::<i8>("-129", Err(IntErrorKind::NegOverflow));
99
100     test_parse::<i16>("32767", Ok(32_767));
101     test_parse::<i16>("32768", Err(IntErrorKind::PosOverflow));
102
103     test_parse::<i16>("-32768", Ok(-32_768));
104     test_parse::<i16>("-32769", Err(IntErrorKind::NegOverflow));
105
106     test_parse::<i32>("2147483647", Ok(2_147_483_647));
107     test_parse::<i32>("2147483648", Err(IntErrorKind::PosOverflow));
108
109     test_parse::<i32>("-2147483648", Ok(-2_147_483_648));
110     test_parse::<i32>("-2147483649", Err(IntErrorKind::NegOverflow));
111
112     test_parse::<i64>("9223372036854775807", Ok(9_223_372_036_854_775_807));
113     test_parse::<i64>("9223372036854775808", Err(IntErrorKind::PosOverflow));
114
115     test_parse::<i64>("-9223372036854775808", Ok(-9_223_372_036_854_775_808));
116     test_parse::<i64>("-9223372036854775809", Err(IntErrorKind::NegOverflow));
117 }
118
119 #[test]
120 fn test_leading_plus() {
121     test_parse::<u8>("+127", Ok(127));
122     test_parse::<i64>("+9223372036854775807", Ok(9223372036854775807));
123 }
124
125 #[test]
126 fn test_invalid() {
127     test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit));
128     test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit));
129     test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit));
130     test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit));
131     test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit));
132     test_parse::<i8>("-", Err(IntErrorKind::InvalidDigit));
133     test_parse::<i8>("+", Err(IntErrorKind::InvalidDigit));
134     test_parse::<u8>("-1", Err(IntErrorKind::InvalidDigit));
135 }
136
137 #[test]
138 fn test_empty() {
139     test_parse::<u8>("", Err(IntErrorKind::Empty));
140 }
141
142 #[test]
143 fn test_infallible_try_from_int_error() {
144     let func = |x: i8| -> Result<i32, TryFromIntError> { Ok(x.try_into()?) };
145
146     assert!(func(0).is_ok());
147 }
148
149 macro_rules! test_impl_from {
150     ($fn_name:ident, bool, $target: ty) => {
151         #[test]
152         fn $fn_name() {
153             let one: $target = 1;
154             let zero: $target = 0;
155             assert_eq!(one, <$target>::from(true));
156             assert_eq!(zero, <$target>::from(false));
157         }
158     };
159     ($fn_name: ident, $Small: ty, $Large: ty) => {
160         #[test]
161         fn $fn_name() {
162             let small_max = <$Small>::MAX;
163             let small_min = <$Small>::MIN;
164             let large_max: $Large = small_max.into();
165             let large_min: $Large = small_min.into();
166             assert_eq!(large_max as $Small, small_max);
167             assert_eq!(large_min as $Small, small_min);
168         }
169     };
170 }
171
172 // Unsigned -> Unsigned
173 test_impl_from! { test_u8u16, u8, u16 }
174 test_impl_from! { test_u8u32, u8, u32 }
175 test_impl_from! { test_u8u64, u8, u64 }
176 test_impl_from! { test_u8usize, u8, usize }
177 test_impl_from! { test_u16u32, u16, u32 }
178 test_impl_from! { test_u16u64, u16, u64 }
179 test_impl_from! { test_u32u64, u32, u64 }
180
181 // Signed -> Signed
182 test_impl_from! { test_i8i16, i8, i16 }
183 test_impl_from! { test_i8i32, i8, i32 }
184 test_impl_from! { test_i8i64, i8, i64 }
185 test_impl_from! { test_i8isize, i8, isize }
186 test_impl_from! { test_i16i32, i16, i32 }
187 test_impl_from! { test_i16i64, i16, i64 }
188 test_impl_from! { test_i32i64, i32, i64 }
189
190 // Unsigned -> Signed
191 test_impl_from! { test_u8i16, u8, i16 }
192 test_impl_from! { test_u8i32, u8, i32 }
193 test_impl_from! { test_u8i64, u8, i64 }
194 test_impl_from! { test_u16i32, u16, i32 }
195 test_impl_from! { test_u16i64, u16, i64 }
196 test_impl_from! { test_u32i64, u32, i64 }
197
198 // Bool -> Integer
199 test_impl_from! { test_boolu8, bool, u8 }
200 test_impl_from! { test_boolu16, bool, u16 }
201 test_impl_from! { test_boolu32, bool, u32 }
202 test_impl_from! { test_boolu64, bool, u64 }
203 test_impl_from! { test_boolu128, bool, u128 }
204 test_impl_from! { test_booli8, bool, i8 }
205 test_impl_from! { test_booli16, bool, i16 }
206 test_impl_from! { test_booli32, bool, i32 }
207 test_impl_from! { test_booli64, bool, i64 }
208 test_impl_from! { test_booli128, bool, i128 }
209
210 // Signed -> Float
211 test_impl_from! { test_i8f32, i8, f32 }
212 test_impl_from! { test_i8f64, i8, f64 }
213 test_impl_from! { test_i16f32, i16, f32 }
214 test_impl_from! { test_i16f64, i16, f64 }
215 test_impl_from! { test_i32f64, i32, f64 }
216
217 // Unsigned -> Float
218 test_impl_from! { test_u8f32, u8, f32 }
219 test_impl_from! { test_u8f64, u8, f64 }
220 test_impl_from! { test_u16f32, u16, f32 }
221 test_impl_from! { test_u16f64, u16, f64 }
222 test_impl_from! { test_u32f64, u32, f64 }
223
224 // Float -> Float
225 #[test]
226 fn test_f32f64() {
227     let max: f64 = f32::MAX.into();
228     assert_eq!(max as f32, f32::MAX);
229     assert!(max.is_normal());
230
231     let min: f64 = f32::MIN.into();
232     assert_eq!(min as f32, f32::MIN);
233     assert!(min.is_normal());
234
235     let min_positive: f64 = f32::MIN_POSITIVE.into();
236     assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
237     assert!(min_positive.is_normal());
238
239     let epsilon: f64 = f32::EPSILON.into();
240     assert_eq!(epsilon as f32, f32::EPSILON);
241     assert!(epsilon.is_normal());
242
243     let zero: f64 = (0.0f32).into();
244     assert_eq!(zero as f32, 0.0f32);
245     assert!(zero.is_sign_positive());
246
247     let neg_zero: f64 = (-0.0f32).into();
248     assert_eq!(neg_zero as f32, -0.0f32);
249     assert!(neg_zero.is_sign_negative());
250
251     let infinity: f64 = f32::INFINITY.into();
252     assert_eq!(infinity as f32, f32::INFINITY);
253     assert!(infinity.is_infinite());
254     assert!(infinity.is_sign_positive());
255
256     let neg_infinity: f64 = f32::NEG_INFINITY.into();
257     assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
258     assert!(neg_infinity.is_infinite());
259     assert!(neg_infinity.is_sign_negative());
260
261     let nan: f64 = f32::NAN.into();
262     assert!(nan.is_nan());
263 }
264
265 /// Conversions where the full width of $source can be represented as $target
266 macro_rules! test_impl_try_from_always_ok {
267     ($fn_name:ident, $source:ty, $target: ty) => {
268         #[test]
269         fn $fn_name() {
270             let max = <$source>::MAX;
271             let min = <$source>::MIN;
272             let zero: $source = 0;
273             assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(), max as $target);
274             assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(), min as $target);
275             assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
276         }
277     };
278 }
279
280 test_impl_try_from_always_ok! { test_try_u8u8, u8, u8 }
281 test_impl_try_from_always_ok! { test_try_u8u16, u8, u16 }
282 test_impl_try_from_always_ok! { test_try_u8u32, u8, u32 }
283 test_impl_try_from_always_ok! { test_try_u8u64, u8, u64 }
284 test_impl_try_from_always_ok! { test_try_u8u128, u8, u128 }
285 test_impl_try_from_always_ok! { test_try_u8i16, u8, i16 }
286 test_impl_try_from_always_ok! { test_try_u8i32, u8, i32 }
287 test_impl_try_from_always_ok! { test_try_u8i64, u8, i64 }
288 test_impl_try_from_always_ok! { test_try_u8i128, u8, i128 }
289
290 test_impl_try_from_always_ok! { test_try_u16u16, u16, u16 }
291 test_impl_try_from_always_ok! { test_try_u16u32, u16, u32 }
292 test_impl_try_from_always_ok! { test_try_u16u64, u16, u64 }
293 test_impl_try_from_always_ok! { test_try_u16u128, u16, u128 }
294 test_impl_try_from_always_ok! { test_try_u16i32, u16, i32 }
295 test_impl_try_from_always_ok! { test_try_u16i64, u16, i64 }
296 test_impl_try_from_always_ok! { test_try_u16i128, u16, i128 }
297
298 test_impl_try_from_always_ok! { test_try_u32u32, u32, u32 }
299 test_impl_try_from_always_ok! { test_try_u32u64, u32, u64 }
300 test_impl_try_from_always_ok! { test_try_u32u128, u32, u128 }
301 test_impl_try_from_always_ok! { test_try_u32i64, u32, i64 }
302 test_impl_try_from_always_ok! { test_try_u32i128, u32, i128 }
303
304 test_impl_try_from_always_ok! { test_try_u64u64, u64, u64 }
305 test_impl_try_from_always_ok! { test_try_u64u128, u64, u128 }
306 test_impl_try_from_always_ok! { test_try_u64i128, u64, i128 }
307
308 test_impl_try_from_always_ok! { test_try_u128u128, u128, u128 }
309
310 test_impl_try_from_always_ok! { test_try_i8i8, i8, i8 }
311 test_impl_try_from_always_ok! { test_try_i8i16, i8, i16 }
312 test_impl_try_from_always_ok! { test_try_i8i32, i8, i32 }
313 test_impl_try_from_always_ok! { test_try_i8i64, i8, i64 }
314 test_impl_try_from_always_ok! { test_try_i8i128, i8, i128 }
315
316 test_impl_try_from_always_ok! { test_try_i16i16, i16, i16 }
317 test_impl_try_from_always_ok! { test_try_i16i32, i16, i32 }
318 test_impl_try_from_always_ok! { test_try_i16i64, i16, i64 }
319 test_impl_try_from_always_ok! { test_try_i16i128, i16, i128 }
320
321 test_impl_try_from_always_ok! { test_try_i32i32, i32, i32 }
322 test_impl_try_from_always_ok! { test_try_i32i64, i32, i64 }
323 test_impl_try_from_always_ok! { test_try_i32i128, i32, i128 }
324
325 test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
326 test_impl_try_from_always_ok! { test_try_i64i128, i64, i128 }
327
328 test_impl_try_from_always_ok! { test_try_i128i128, i128, i128 }
329
330 test_impl_try_from_always_ok! { test_try_usizeusize, usize, usize }
331 test_impl_try_from_always_ok! { test_try_isizeisize, isize, isize }
332
333 assume_usize_width! {
334     test_impl_try_from_always_ok! { test_try_u8usize, u8, usize }
335     test_impl_try_from_always_ok! { test_try_u8isize, u8, isize }
336     test_impl_try_from_always_ok! { test_try_i8isize, i8, isize }
337
338     test_impl_try_from_always_ok! { test_try_u16usize, u16, usize }
339     test_impl_try_from_always_ok! { test_try_i16isize, i16, isize }
340
341     test_impl_try_from_always_ok! { test_try_usizeu64, usize, u64 }
342     test_impl_try_from_always_ok! { test_try_usizeu128, usize, u128 }
343     test_impl_try_from_always_ok! { test_try_usizei128, usize, i128 }
344
345     test_impl_try_from_always_ok! { test_try_isizei64, isize, i64 }
346     test_impl_try_from_always_ok! { test_try_isizei128, isize, i128 }
347
348     cfg_block!(
349         #[cfg(target_pointer_width = "16")] {
350             test_impl_try_from_always_ok! { test_try_usizeu16, usize, u16 }
351             test_impl_try_from_always_ok! { test_try_isizei16, isize, i16 }
352             test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
353             test_impl_try_from_always_ok! { test_try_usizei32, usize, i32 }
354             test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
355             test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
356         }
357
358         #[cfg(target_pointer_width = "32")] {
359             test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
360             test_impl_try_from_always_ok! { test_try_usizeu32, usize, u32 }
361             test_impl_try_from_always_ok! { test_try_isizei32, isize, i32 }
362             test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
363             test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
364             test_impl_try_from_always_ok! { test_try_usizei64, usize, i64 }
365         }
366
367         #[cfg(target_pointer_width = "64")] {
368             test_impl_try_from_always_ok! { test_try_u16isize, u16, isize }
369             test_impl_try_from_always_ok! { test_try_u32usize, u32, usize }
370             test_impl_try_from_always_ok! { test_try_u32isize, u32, isize }
371             test_impl_try_from_always_ok! { test_try_i32isize, i32, isize }
372             test_impl_try_from_always_ok! { test_try_u64usize, u64, usize }
373             test_impl_try_from_always_ok! { test_try_i64isize, i64, isize }
374         }
375     );
376 }
377
378 /// Conversions where max of $source can be represented as $target,
379 macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
380     ($fn_name:ident, $source:ty, $target:ty) => {
381         #[test]
382         fn $fn_name() {
383             let max = <$source>::MAX;
384             let min = <$source>::MIN;
385             let zero: $source = 0;
386             let neg_one: $source = -1;
387             assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(), max as $target);
388             assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
389             assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
390             assert!(<$target as TryFrom<$source>>::try_from(neg_one).is_err());
391         }
392     };
393 }
394
395 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u8, i8, u8 }
396 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u16, i8, u16 }
397 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u32, i8, u32 }
398 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u64, i8, u64 }
399 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u128, i8, u128 }
400
401 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u16, i16, u16 }
402 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u32, i16, u32 }
403 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u64, i16, u64 }
404 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u128, i16, u128 }
405
406 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u32, i32, u32 }
407 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u64, i32, u64 }
408 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u128, i32, u128 }
409
410 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u64, i64, u64 }
411 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u128, i64, u128 }
412
413 test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i128u128, i128, u128 }
414
415 assume_usize_width! {
416     test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8usize, i8, usize }
417     test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16usize, i16, usize }
418
419     test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu64, isize, u64 }
420     test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu128, isize, u128 }
421     test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeusize, isize, usize }
422
423     cfg_block!(
424         #[cfg(target_pointer_width = "16")] {
425             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu16, isize, u16 }
426             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
427         }
428
429         #[cfg(target_pointer_width = "32")] {
430             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_isizeu32, isize, u32 }
431
432             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
433         }
434
435         #[cfg(target_pointer_width = "64")] {
436             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32usize, i32, usize }
437             test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64usize, i64, usize }
438         }
439     );
440 }
441
442 /// Conversions where max of $source can not be represented as $target,
443 /// but min can.
444 macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
445     ($fn_name:ident, $source:ty, $target:ty) => {
446         #[test]
447         fn $fn_name() {
448             let max = <$source>::MAX;
449             let min = <$source>::MIN;
450             let zero: $source = 0;
451             assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
452             assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(), min as $target);
453             assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
454         }
455     };
456 }
457
458 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
459
460 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i8, u16, i8 }
461 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i16, u16, i16 }
462
463 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i8, u32, i8 }
464 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i16, u32, i16 }
465 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i32, u32, i32 }
466
467 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i8, u64, i8 }
468 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i16, u64, i16 }
469 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i32, u64, i32 }
470 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i64, u64, i64 }
471
472 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i8, u128, i8 }
473 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i16, u128, i16 }
474 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i32, u128, i32 }
475 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i64, u128, i64 }
476 test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128i128, u128, i128 }
477
478 assume_usize_width! {
479     test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64isize, u64, isize }
480     test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u128isize, u128, isize }
481
482     test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei8, usize, i8 }
483     test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei16, usize, i16 }
484     test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizeisize, usize, isize }
485
486     cfg_block!(
487         #[cfg(target_pointer_width = "16")] {
488             test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16isize, u16, isize }
489             test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
490         }
491
492         #[cfg(target_pointer_width = "32")] {
493             test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32isize, u32, isize }
494             test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
495         }
496
497         #[cfg(target_pointer_width = "64")] {
498             test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei32, usize, i32 }
499             test_impl_try_from_unsigned_to_signed_upper_err! { test_try_usizei64, usize, i64 }
500         }
501     );
502 }
503
504 /// Conversions where min/max of $source can not be represented as $target.
505 macro_rules! test_impl_try_from_same_sign_err {
506     ($fn_name:ident, $source:ty, $target:ty) => {
507         #[test]
508         fn $fn_name() {
509             let max = <$source>::MAX;
510             let min = <$source>::MIN;
511             let zero: $source = 0;
512             let t_max = <$target>::MAX;
513             let t_min = <$target>::MIN;
514             assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
515             if min != 0 {
516                 assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
517             }
518             assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
519             assert_eq!(
520                 <$target as TryFrom<$source>>::try_from(t_max as $source).unwrap(),
521                 t_max as $target
522             );
523             assert_eq!(
524                 <$target as TryFrom<$source>>::try_from(t_min as $source).unwrap(),
525                 t_min as $target
526             );
527         }
528     };
529 }
530
531 test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
532
533 test_impl_try_from_same_sign_err! { test_try_u32u8, u32, u8 }
534 test_impl_try_from_same_sign_err! { test_try_u32u16, u32, u16 }
535
536 test_impl_try_from_same_sign_err! { test_try_u64u8, u64, u8 }
537 test_impl_try_from_same_sign_err! { test_try_u64u16, u64, u16 }
538 test_impl_try_from_same_sign_err! { test_try_u64u32, u64, u32 }
539
540 test_impl_try_from_same_sign_err! { test_try_u128u8, u128, u8 }
541 test_impl_try_from_same_sign_err! { test_try_u128u16, u128, u16 }
542 test_impl_try_from_same_sign_err! { test_try_u128u32, u128, u32 }
543 test_impl_try_from_same_sign_err! { test_try_u128u64, u128, u64 }
544
545 test_impl_try_from_same_sign_err! { test_try_i16i8, i16, i8 }
546 test_impl_try_from_same_sign_err! { test_try_isizei8, isize, i8 }
547
548 test_impl_try_from_same_sign_err! { test_try_i32i8, i32, i8 }
549 test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
550
551 test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
552 test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
553 test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
554
555 test_impl_try_from_same_sign_err! { test_try_i128i8, i128, i8 }
556 test_impl_try_from_same_sign_err! { test_try_i128i16, i128, i16 }
557 test_impl_try_from_same_sign_err! { test_try_i128i32, i128, i32 }
558 test_impl_try_from_same_sign_err! { test_try_i128i64, i128, i64 }
559
560 assume_usize_width! {
561     test_impl_try_from_same_sign_err! { test_try_usizeu8, usize, u8 }
562     test_impl_try_from_same_sign_err! { test_try_u128usize, u128, usize }
563     test_impl_try_from_same_sign_err! { test_try_i128isize, i128, isize }
564
565     cfg_block!(
566         #[cfg(target_pointer_width = "16")] {
567             test_impl_try_from_same_sign_err! { test_try_u32usize, u32, usize }
568             test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
569
570             test_impl_try_from_same_sign_err! { test_try_i32isize, i32, isize }
571             test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
572         }
573
574         #[cfg(target_pointer_width = "32")] {
575             test_impl_try_from_same_sign_err! { test_try_u64usize, u64, usize }
576             test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
577
578             test_impl_try_from_same_sign_err! { test_try_i64isize, i64, isize }
579             test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
580         }
581
582         #[cfg(target_pointer_width = "64")] {
583             test_impl_try_from_same_sign_err! { test_try_usizeu16, usize, u16 }
584             test_impl_try_from_same_sign_err! { test_try_usizeu32, usize, u32 }
585
586             test_impl_try_from_same_sign_err! { test_try_isizei16, isize, i16 }
587             test_impl_try_from_same_sign_err! { test_try_isizei32, isize, i32 }
588         }
589     );
590 }
591
592 /// Conversions where neither the min nor the max of $source can be represented by
593 /// $target, but max/min of the target can be represented by the source.
594 macro_rules! test_impl_try_from_signed_to_unsigned_err {
595     ($fn_name:ident, $source:ty, $target:ty) => {
596         #[test]
597         fn $fn_name() {
598             let max = <$source>::MAX;
599             let min = <$source>::MIN;
600             let zero: $source = 0;
601             let t_max = <$target>::MAX;
602             let t_min = <$target>::MIN;
603             assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
604             assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
605             assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(), zero as $target);
606             assert_eq!(
607                 <$target as TryFrom<$source>>::try_from(t_max as $source).unwrap(),
608                 t_max as $target
609             );
610             assert_eq!(
611                 <$target as TryFrom<$source>>::try_from(t_min as $source).unwrap(),
612                 t_min as $target
613             );
614         }
615     };
616 }
617
618 test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
619
620 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
621 test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
622
623 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
624 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
625 test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }
626
627 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u8, i128, u8 }
628 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u16, i128, u16 }
629 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u32, i128, u32 }
630 test_impl_try_from_signed_to_unsigned_err! { test_try_i128u64, i128, u64 }
631
632 assume_usize_width! {
633     test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu8, isize, u8 }
634     test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128, usize }
635
636     cfg_block! {
637         #[cfg(target_pointer_width = "16")] {
638             test_impl_try_from_signed_to_unsigned_err! { test_try_i32usize, i32, usize }
639             test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
640         }
641         #[cfg(target_pointer_width = "32")] {
642             test_impl_try_from_signed_to_unsigned_err! { test_try_i64usize, i64, usize }
643
644             test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
645         }
646         #[cfg(target_pointer_width = "64")] {
647             test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu16, isize, u16 }
648             test_impl_try_from_signed_to_unsigned_err! { test_try_isizeu32, isize, u32 }
649         }
650     }
651 }
652
653 macro_rules! test_float {
654     ($modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => {
655         mod $modname {
656             #[test]
657             fn min() {
658                 assert_eq!((0.0 as $fty).min(0.0), 0.0);
659                 assert!((0.0 as $fty).min(0.0).is_sign_positive());
660                 assert_eq!((-0.0 as $fty).min(-0.0), -0.0);
661                 assert!((-0.0 as $fty).min(-0.0).is_sign_negative());
662                 assert_eq!((9.0 as $fty).min(9.0), 9.0);
663                 assert_eq!((-9.0 as $fty).min(0.0), -9.0);
664                 assert_eq!((0.0 as $fty).min(9.0), 0.0);
665                 assert!((0.0 as $fty).min(9.0).is_sign_positive());
666                 assert_eq!((-0.0 as $fty).min(9.0), -0.0);
667                 assert!((-0.0 as $fty).min(9.0).is_sign_negative());
668                 assert_eq!((-0.0 as $fty).min(-9.0), -9.0);
669                 assert_eq!(($inf as $fty).min(9.0), 9.0);
670                 assert_eq!((9.0 as $fty).min($inf), 9.0);
671                 assert_eq!(($inf as $fty).min(-9.0), -9.0);
672                 assert_eq!((-9.0 as $fty).min($inf), -9.0);
673                 assert_eq!(($neginf as $fty).min(9.0), $neginf);
674                 assert_eq!((9.0 as $fty).min($neginf), $neginf);
675                 assert_eq!(($neginf as $fty).min(-9.0), $neginf);
676                 assert_eq!((-9.0 as $fty).min($neginf), $neginf);
677                 assert_eq!(($nan as $fty).min(9.0), 9.0);
678                 assert_eq!(($nan as $fty).min(-9.0), -9.0);
679                 assert_eq!((9.0 as $fty).min($nan), 9.0);
680                 assert_eq!((-9.0 as $fty).min($nan), -9.0);
681                 assert!(($nan as $fty).min($nan).is_nan());
682             }
683             #[test]
684             fn max() {
685                 assert_eq!((0.0 as $fty).max(0.0), 0.0);
686                 assert!((0.0 as $fty).max(0.0).is_sign_positive());
687                 assert_eq!((-0.0 as $fty).max(-0.0), -0.0);
688                 assert!((-0.0 as $fty).max(-0.0).is_sign_negative());
689                 assert_eq!((9.0 as $fty).max(9.0), 9.0);
690                 assert_eq!((-9.0 as $fty).max(0.0), 0.0);
691                 assert!((-9.0 as $fty).max(0.0).is_sign_positive());
692                 assert_eq!((-9.0 as $fty).max(-0.0), -0.0);
693                 assert!((-9.0 as $fty).max(-0.0).is_sign_negative());
694                 assert_eq!((0.0 as $fty).max(9.0), 9.0);
695                 assert_eq!((0.0 as $fty).max(-9.0), 0.0);
696                 assert!((0.0 as $fty).max(-9.0).is_sign_positive());
697                 assert_eq!((-0.0 as $fty).max(-9.0), -0.0);
698                 assert!((-0.0 as $fty).max(-9.0).is_sign_negative());
699                 assert_eq!(($inf as $fty).max(9.0), $inf);
700                 assert_eq!((9.0 as $fty).max($inf), $inf);
701                 assert_eq!(($inf as $fty).max(-9.0), $inf);
702                 assert_eq!((-9.0 as $fty).max($inf), $inf);
703                 assert_eq!(($neginf as $fty).max(9.0), 9.0);
704                 assert_eq!((9.0 as $fty).max($neginf), 9.0);
705                 assert_eq!(($neginf as $fty).max(-9.0), -9.0);
706                 assert_eq!((-9.0 as $fty).max($neginf), -9.0);
707                 assert_eq!(($nan as $fty).max(9.0), 9.0);
708                 assert_eq!(($nan as $fty).max(-9.0), -9.0);
709                 assert_eq!((9.0 as $fty).max($nan), 9.0);
710                 assert_eq!((-9.0 as $fty).max($nan), -9.0);
711                 assert!(($nan as $fty).max($nan).is_nan());
712             }
713             #[test]
714             fn rem_euclid() {
715                 let a: $fty = 42.0;
716                 assert!($inf.rem_euclid(a).is_nan());
717                 assert_eq!(a.rem_euclid($inf), a);
718                 assert!(a.rem_euclid($nan).is_nan());
719                 assert!($inf.rem_euclid($inf).is_nan());
720                 assert!($inf.rem_euclid($nan).is_nan());
721                 assert!($nan.rem_euclid($inf).is_nan());
722             }
723             #[test]
724             fn div_euclid() {
725                 let a: $fty = 42.0;
726                 assert_eq!(a.div_euclid($inf), 0.0);
727                 assert!(a.div_euclid($nan).is_nan());
728                 assert!($inf.div_euclid($inf).is_nan());
729                 assert!($inf.div_euclid($nan).is_nan());
730                 assert!($nan.div_euclid($inf).is_nan());
731             }
732         }
733     };
734 }
735
736 test_float!(f32, f32, f32::INFINITY, f32::NEG_INFINITY, f32::NAN);
737 test_float!(f64, f64, f64::INFINITY, f64::NEG_INFINITY, f64::NAN);