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.
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.
11 use core::cmp::PartialEq;
13 use core::num::{NumCast, cast};
14 use core::ops::{Add, Sub, Mul, Div, Rem};
15 use core::kinds::Copy;
16 use std::str::from_str;
31 /// Helper function for testing numeric operations
32 pub fn test_num<T>(ten: T, two: T) where
33 T: PartialEq + NumCast
34 + Add<Output=T> + Sub<Output=T>
35 + Mul<Output=T> + Div<Output=T>
36 + Rem<Output=T> + Show
39 assert_eq!(ten.add(two), cast(12i).unwrap());
40 assert_eq!(ten.sub(two), cast(8i).unwrap());
41 assert_eq!(ten.mul(two), cast(20i).unwrap());
42 assert_eq!(ten.div(two), cast(5i).unwrap());
43 assert_eq!(ten.rem(two), cast(0i).unwrap());
45 assert_eq!(ten.add(two), ten + two);
46 assert_eq!(ten.sub(two), ten - two);
47 assert_eq!(ten.mul(two), ten * two);
48 assert_eq!(ten.div(two), ten / two);
49 assert_eq!(ten.rem(two), ten % two);
54 use core::option::Option;
55 use core::option::Option::{Some, None};
57 use core::num::from_str_radix;
58 use core::str::from_str;
61 fn from_str_issue7588() {
62 let u : Option<u8> = from_str_radix("1000", 10);
64 let s : Option<i16> = from_str_radix("80000", 10);
66 let f : Option<f32> = from_str_radix("10000000000000000000000000000000000000000", 10);
67 assert_eq!(f, Some(Float::infinity()));
68 let fe : Option<f32> = from_str_radix("1e40", 10);
69 assert_eq!(fe, Some(Float::infinity()));
73 fn test_from_str_radix_float() {
74 let x1 : Option<f64> = from_str_radix("-123.456", 10);
75 assert_eq!(x1, Some(-123.456));
76 let x2 : Option<f32> = from_str_radix("123.456", 10);
77 assert_eq!(x2, Some(123.456));
78 let x3 : Option<f32> = from_str_radix("-0.0", 10);
79 assert_eq!(x3, Some(-0.0));
80 let x4 : Option<f32> = from_str_radix("0.0", 10);
81 assert_eq!(x4, Some(0.0));
82 let x4 : Option<f32> = from_str_radix("1.0", 10);
83 assert_eq!(x4, Some(1.0));
84 let x5 : Option<f32> = from_str_radix("-1.0", 10);
85 assert_eq!(x5, Some(-1.0));
89 fn test_int_from_str_overflow() {
90 let mut i8_val: i8 = 127_i8;
91 assert_eq!(from_str::<i8>("127"), Some(i8_val));
92 assert_eq!(from_str::<i8>("128"), None);
95 assert_eq!(from_str::<i8>("-128"), Some(i8_val));
96 assert_eq!(from_str::<i8>("-129"), None);
98 let mut i16_val: i16 = 32_767_i16;
99 assert_eq!(from_str::<i16>("32767"), Some(i16_val));
100 assert_eq!(from_str::<i16>("32768"), None);
103 assert_eq!(from_str::<i16>("-32768"), Some(i16_val));
104 assert_eq!(from_str::<i16>("-32769"), None);
106 let mut i32_val: i32 = 2_147_483_647_i32;
107 assert_eq!(from_str::<i32>("2147483647"), Some(i32_val));
108 assert_eq!(from_str::<i32>("2147483648"), None);
111 assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val));
112 assert_eq!(from_str::<i32>("-2147483649"), None);
114 let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
115 assert_eq!(from_str::<i64>("9223372036854775807"), Some(i64_val));
116 assert_eq!(from_str::<i64>("9223372036854775808"), None);
119 assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
120 assert_eq!(from_str::<i64>("-9223372036854775809"), None);