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