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