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