]> git.proxmox.com Git - rustc.git/blame - src/libcoretest/num/mod.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcoretest / num / mod.rs
CommitLineData
1a4d82fc
JJ
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
11use core::cmp::PartialEq;
85aaf69f 12use core::fmt::Debug;
1a4d82fc
JJ
13use core::ops::{Add, Sub, Mul, Div, Rem};
14use core::marker::Copy;
15
16#[macro_use]
17mod int_macros;
18
19mod i8;
20mod i16;
21mod i32;
22mod i64;
1a4d82fc
JJ
23
24#[macro_use]
25mod uint_macros;
26
27mod u8;
28mod u16;
29mod u32;
30mod u64;
1a4d82fc 31
d9579d0f 32mod flt2dec;
e9174d1e 33mod dec2flt;
b039eaaf 34mod bignum;
d9579d0f 35
1a4d82fc
JJ
36/// Helper function for testing numeric operations
37pub fn test_num<T>(ten: T, two: T) where
9346a6ac 38 T: PartialEq
1a4d82fc
JJ
39 + Add<Output=T> + Sub<Output=T>
40 + Mul<Output=T> + Div<Output=T>
85aaf69f 41 + Rem<Output=T> + Debug
1a4d82fc
JJ
42 + Copy
43{
1a4d82fc
JJ
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)]
d9579d0f 52mod tests {
1a4d82fc
JJ
53 use core::option::Option;
54 use core::option::Option::{Some, None};
55 use core::num::Float;
1a4d82fc
JJ
56
57 #[test]
58 fn from_str_issue7588() {
9346a6ac 59 let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
1a4d82fc 60 assert_eq!(u, None);
9346a6ac 61 let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
1a4d82fc 62 assert_eq!(s, None);
1a4d82fc
JJ
63 }
64
65 #[test]
66 fn test_int_from_str_overflow() {
c34b1796 67 let mut i8_val: i8 = 127;
85aaf69f
SL
68 assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
69 assert_eq!("128".parse::<i8>().ok(), None);
1a4d82fc 70
c34b1796 71 i8_val = i8_val.wrapping_add(1);
85aaf69f
SL
72 assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
73 assert_eq!("-129".parse::<i8>().ok(), None);
1a4d82fc 74
c34b1796 75 let mut i16_val: i16 = 32_767;
85aaf69f
SL
76 assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
77 assert_eq!("32768".parse::<i16>().ok(), None);
1a4d82fc 78
c34b1796 79 i16_val = i16_val.wrapping_add(1);
85aaf69f
SL
80 assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
81 assert_eq!("-32769".parse::<i16>().ok(), None);
1a4d82fc 82
c34b1796 83 let mut i32_val: i32 = 2_147_483_647;
85aaf69f
SL
84 assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
85 assert_eq!("2147483648".parse::<i32>().ok(), None);
1a4d82fc 86
c34b1796 87 i32_val = i32_val.wrapping_add(1);
85aaf69f
SL
88 assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
89 assert_eq!("-2147483649".parse::<i32>().ok(), None);
1a4d82fc 90
c34b1796 91 let mut i64_val: i64 = 9_223_372_036_854_775_807;
85aaf69f
SL
92 assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
93 assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
1a4d82fc 94
c34b1796 95 i64_val = i64_val.wrapping_add(1);
85aaf69f
SL
96 assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
97 assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
1a4d82fc 98 }
c34b1796 99
b039eaaf
SL
100 #[test]
101 fn test_leading_plus() {
54a0048b
SL
102 assert_eq!("+127".parse::<u8>().ok(), Some(127));
103 assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
b039eaaf
SL
104 }
105
c34b1796 106 #[test]
c1a9b12d
SL
107 fn test_invalid() {
108 assert_eq!("--129".parse::<i8>().ok(), None);
b039eaaf 109 assert_eq!("++129".parse::<i8>().ok(), None);
c1a9b12d
SL
110 assert_eq!("Съешь".parse::<u8>().ok(), None);
111 }
112
113 #[test]
114 fn test_empty() {
115 assert_eq!("-".parse::<i8>().ok(), None);
b039eaaf 116 assert_eq!("+".parse::<i8>().ok(), None);
c1a9b12d 117 assert_eq!("".parse::<u8>().ok(), None);
c34b1796 118 }
b039eaaf
SL
119
120 macro_rules! test_impl_from {
121 ($fn_name: ident, $Small: ty, $Large: ty) => {
122 #[test]
123 fn $fn_name() {
124 let small_max = <$Small>::max_value();
125 let small_min = <$Small>::min_value();
126 let large_max: $Large = small_max.into();
127 let large_min: $Large = small_min.into();
128 assert_eq!(large_max as $Small, small_max);
129 assert_eq!(large_min as $Small, small_min);
130 }
131 }
132 }
133
134 // Unsigned -> Unsigned
135 test_impl_from! { test_u8u16, u8, u16 }
136 test_impl_from! { test_u8u32, u8, u32 }
137 test_impl_from! { test_u8u64, u8, u64 }
138 test_impl_from! { test_u8usize, u8, usize }
139 test_impl_from! { test_u16u32, u16, u32 }
140 test_impl_from! { test_u16u64, u16, u64 }
141 test_impl_from! { test_u32u64, u32, u64 }
142
143 // Signed -> Signed
144 test_impl_from! { test_i8i16, i8, i16 }
145 test_impl_from! { test_i8i32, i8, i32 }
146 test_impl_from! { test_i8i64, i8, i64 }
147 test_impl_from! { test_i8isize, i8, isize }
148 test_impl_from! { test_i16i32, i16, i32 }
149 test_impl_from! { test_i16i64, i16, i64 }
150 test_impl_from! { test_i32i64, i32, i64 }
151
152 // Unsigned -> Signed
153 test_impl_from! { test_u8i16, u8, i16 }
154 test_impl_from! { test_u8i32, u8, i32 }
155 test_impl_from! { test_u8i64, u8, i64 }
156 test_impl_from! { test_u16i32, u16, i32 }
157 test_impl_from! { test_u16i64, u16, i64 }
158 test_impl_from! { test_u32i64, u32, i64 }
92a42be0
SL
159
160 // Signed -> Float
161 test_impl_from! { test_i8f32, i8, f32 }
162 test_impl_from! { test_i8f64, i8, f64 }
163 test_impl_from! { test_i16f32, i16, f32 }
164 test_impl_from! { test_i16f64, i16, f64 }
165 test_impl_from! { test_i32f64, i32, f64 }
166
167 // Unsigned -> Float
168 test_impl_from! { test_u8f32, u8, f32 }
169 test_impl_from! { test_u8f64, u8, f64 }
170 test_impl_from! { test_u16f32, u16, f32 }
171 test_impl_from! { test_u16f64, u16, f64 }
172 test_impl_from! { test_u32f64, u32, f64 }
173
174 // Float -> Float
175 #[test]
176 fn test_f32f64() {
177 use core::f32;
178
179 let max: f64 = f32::MAX.into();
180 assert_eq!(max as f32, f32::MAX);
181 assert!(max.is_normal());
182
183 let min: f64 = f32::MIN.into();
184 assert_eq!(min as f32, f32::MIN);
185 assert!(min.is_normal());
186
187 let min_positive: f64 = f32::MIN_POSITIVE.into();
188 assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
189 assert!(min_positive.is_normal());
190
191 let epsilon: f64 = f32::EPSILON.into();
192 assert_eq!(epsilon as f32, f32::EPSILON);
193 assert!(epsilon.is_normal());
194
195 let zero: f64 = (0.0f32).into();
196 assert_eq!(zero as f32, 0.0f32);
197 assert!(zero.is_sign_positive());
198
199 let neg_zero: f64 = (-0.0f32).into();
200 assert_eq!(neg_zero as f32, -0.0f32);
201 assert!(neg_zero.is_sign_negative());
202
203 let infinity: f64 = f32::INFINITY.into();
204 assert_eq!(infinity as f32, f32::INFINITY);
205 assert!(infinity.is_infinite());
206 assert!(infinity.is_sign_positive());
207
208 let neg_infinity: f64 = f32::NEG_INFINITY.into();
209 assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
210 assert!(neg_infinity.is_infinite());
211 assert!(neg_infinity.is_sign_negative());
212
213 let nan: f64 = f32::NAN.into();
214 assert!(nan.is_nan());
215 }
1a4d82fc 216}