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