]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/num/mod.rs
rollup merge of #20482: kmcallister/macro-reform
[rust.git] / src / libcoretest / 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::cmp::PartialEq;
12 use core::fmt::Show;
13 use core::num::{NumCast, cast};
14 use core::ops::{Add, Sub, Mul, Div, Rem};
15 use core::kinds::Copy;
16
17 #[cfg_attr(stage0, macro_escape)]
18 #[cfg_attr(not(stage0), macro_use)]
19 mod int_macros;
20
21 mod i8;
22 mod i16;
23 mod i32;
24 mod i64;
25 mod int;
26
27 #[cfg_attr(stage0, macro_escape)]
28 #[cfg_attr(not(stage0), macro_use)]
29 mod uint_macros;
30
31 mod u8;
32 mod u16;
33 mod u32;
34 mod u64;
35 mod uint;
36
37 /// Helper function for testing numeric operations
38 pub fn test_num<T>(ten: T, two: T) where
39     T: PartialEq + NumCast
40      + Add<Output=T> + Sub<Output=T>
41      + Mul<Output=T> + Div<Output=T>
42      + Rem<Output=T> + Show
43      + Copy
44 {
45     assert_eq!(ten.add(two),  cast(12i).unwrap());
46     assert_eq!(ten.sub(two),  cast(8i).unwrap());
47     assert_eq!(ten.mul(two),  cast(20i).unwrap());
48     assert_eq!(ten.div(two),  cast(5i).unwrap());
49     assert_eq!(ten.rem(two),  cast(0i).unwrap());
50
51     assert_eq!(ten.add(two),  ten + two);
52     assert_eq!(ten.sub(two),  ten - two);
53     assert_eq!(ten.mul(two),  ten * two);
54     assert_eq!(ten.div(two),  ten / two);
55     assert_eq!(ten.rem(two),  ten % two);
56 }
57
58 #[cfg(test)]
59 mod test {
60     use core::option::Option;
61     use core::option::Option::{Some, None};
62     use core::num::Float;
63     use core::num::from_str_radix;
64
65     #[test]
66     fn from_str_issue7588() {
67         let u : Option<u8> = from_str_radix("1000", 10);
68         assert_eq!(u, None);
69         let s : Option<i16> = from_str_radix("80000", 10);
70         assert_eq!(s, None);
71         let f : Option<f32> = from_str_radix("10000000000000000000000000000000000000000", 10);
72         assert_eq!(f, Some(Float::infinity()));
73         let fe : Option<f32> = from_str_radix("1e40", 10);
74         assert_eq!(fe, Some(Float::infinity()));
75     }
76
77     #[test]
78     fn test_from_str_radix_float() {
79         let x1 : Option<f64> = from_str_radix("-123.456", 10);
80         assert_eq!(x1, Some(-123.456));
81         let x2 : Option<f32> = from_str_radix("123.456", 10);
82         assert_eq!(x2, Some(123.456));
83         let x3 : Option<f32> = from_str_radix("-0.0", 10);
84         assert_eq!(x3, Some(-0.0));
85         let x4 : Option<f32> = from_str_radix("0.0", 10);
86         assert_eq!(x4, Some(0.0));
87         let x4 : Option<f32> = from_str_radix("1.0", 10);
88         assert_eq!(x4, Some(1.0));
89         let x5 : Option<f32> = from_str_radix("-1.0", 10);
90         assert_eq!(x5, Some(-1.0));
91     }
92
93     #[test]
94     fn test_int_from_str_overflow() {
95         let mut i8_val: i8 = 127_i8;
96         assert_eq!("127".parse::<i8>(), Some(i8_val));
97         assert_eq!("128".parse::<i8>(), None);
98
99         i8_val += 1 as i8;
100         assert_eq!("-128".parse::<i8>(), Some(i8_val));
101         assert_eq!("-129".parse::<i8>(), None);
102
103         let mut i16_val: i16 = 32_767_i16;
104         assert_eq!("32767".parse::<i16>(), Some(i16_val));
105         assert_eq!("32768".parse::<i16>(), None);
106
107         i16_val += 1 as i16;
108         assert_eq!("-32768".parse::<i16>(), Some(i16_val));
109         assert_eq!("-32769".parse::<i16>(), None);
110
111         let mut i32_val: i32 = 2_147_483_647_i32;
112         assert_eq!("2147483647".parse::<i32>(), Some(i32_val));
113         assert_eq!("2147483648".parse::<i32>(), None);
114
115         i32_val += 1 as i32;
116         assert_eq!("-2147483648".parse::<i32>(), Some(i32_val));
117         assert_eq!("-2147483649".parse::<i32>(), None);
118
119         let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
120         assert_eq!("9223372036854775807".parse::<i64>(), Some(i64_val));
121         assert_eq!("9223372036854775808".parse::<i64>(), None);
122
123         i64_val += 1 as i64;
124         assert_eq!("-9223372036854775808".parse::<i64>(), Some(i64_val));
125         assert_eq!("-9223372036854775809".parse::<i64>(), None);
126     }
127 }