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