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