mod.rs 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
use core::cmp::PartialEq;
12
use core::fmt::Debug;
13
use core::ops::{Add, Sub, Mul, Div, Rem};
N
Nick Cameron 已提交
14
use core::marker::Copy;
15

A
Alex Crichton 已提交
16
#[macro_use]
17
mod int_macros;
18

19 20 21 22
mod i8;
mod i16;
mod i32;
mod i64;
23

A
Alex Crichton 已提交
24
#[macro_use]
25
mod uint_macros;
26

27 28 29 30 31 32
mod u8;
mod u16;
mod u32;
mod u64;

/// Helper function for testing numeric operations
33
pub fn test_num<T>(ten: T, two: T) where
34
    T: PartialEq
J
Jorge Aparicio 已提交
35 36
     + Add<Output=T> + Sub<Output=T>
     + Mul<Output=T> + Div<Output=T>
37
     + Rem<Output=T> + Debug
J
Jorge Aparicio 已提交
38
     + Copy
39
{
J
Jorge Aparicio 已提交
40 41 42 43 44
    assert_eq!(ten.add(two),  ten + two);
    assert_eq!(ten.sub(two),  ten - two);
    assert_eq!(ten.mul(two),  ten * two);
    assert_eq!(ten.div(two),  ten / two);
    assert_eq!(ten.rem(two),  ten % two);
45
}
B
Brendan Zabarauskas 已提交
46 47

#[cfg(test)]
48
mod tests {
C
Corey Farwell 已提交
49 50
    use core::option::Option;
    use core::option::Option::{Some, None};
B
Brendan Zabarauskas 已提交
51 52 53 54
    use core::num::Float;

    #[test]
    fn from_str_issue7588() {
55
        let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
B
Brendan Zabarauskas 已提交
56
        assert_eq!(u, None);
57
        let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
B
Brendan Zabarauskas 已提交
58
        assert_eq!(s, None);
59 60
        let s = "10000000000000000000000000000000000000000";
        let f : Option<f32> = f32::from_str_radix(s, 10).ok();
61
        assert_eq!(f, Some(Float::infinity()));
62
        let fe : Option<f32> = f32::from_str_radix("1e40", 10).ok();
63
        assert_eq!(fe, Some(Float::infinity()));
B
Brendan Zabarauskas 已提交
64 65 66 67
    }

    #[test]
    fn test_from_str_radix_float() {
68
        let x1 : Option<f64> = f64::from_str_radix("-123.456", 10).ok();
B
Brendan Zabarauskas 已提交
69
        assert_eq!(x1, Some(-123.456));
70
        let x2 : Option<f32> = f32::from_str_radix("123.456", 10).ok();
B
Brendan Zabarauskas 已提交
71
        assert_eq!(x2, Some(123.456));
72
        let x3 : Option<f32> = f32::from_str_radix("-0.0", 10).ok();
B
Brendan Zabarauskas 已提交
73
        assert_eq!(x3, Some(-0.0));
74
        let x4 : Option<f32> = f32::from_str_radix("0.0", 10).ok();
B
Brendan Zabarauskas 已提交
75
        assert_eq!(x4, Some(0.0));
76
        let x4 : Option<f32> = f32::from_str_radix("1.0", 10).ok();
B
Brendan Zabarauskas 已提交
77
        assert_eq!(x4, Some(1.0));
78
        let x5 : Option<f32> = f32::from_str_radix("-1.0", 10).ok();
B
Brendan Zabarauskas 已提交
79 80 81 82 83
        assert_eq!(x5, Some(-1.0));
    }

    #[test]
    fn test_int_from_str_overflow() {
84
        let mut i8_val: i8 = 127;
A
Alex Crichton 已提交
85 86
        assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
        assert_eq!("128".parse::<i8>().ok(), None);
B
Brendan Zabarauskas 已提交
87

88
        i8_val = i8_val.wrapping_add(1);
A
Alex Crichton 已提交
89 90
        assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
        assert_eq!("-129".parse::<i8>().ok(), None);
B
Brendan Zabarauskas 已提交
91

92
        let mut i16_val: i16 = 32_767;
A
Alex Crichton 已提交
93 94
        assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
        assert_eq!("32768".parse::<i16>().ok(), None);
B
Brendan Zabarauskas 已提交
95

96
        i16_val = i16_val.wrapping_add(1);
A
Alex Crichton 已提交
97 98
        assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
        assert_eq!("-32769".parse::<i16>().ok(), None);
B
Brendan Zabarauskas 已提交
99

100
        let mut i32_val: i32 = 2_147_483_647;
A
Alex Crichton 已提交
101 102
        assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
        assert_eq!("2147483648".parse::<i32>().ok(), None);
B
Brendan Zabarauskas 已提交
103

104
        i32_val = i32_val.wrapping_add(1);
A
Alex Crichton 已提交
105 106
        assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
        assert_eq!("-2147483649".parse::<i32>().ok(), None);
B
Brendan Zabarauskas 已提交
107

108
        let mut i64_val: i64 = 9_223_372_036_854_775_807;
A
Alex Crichton 已提交
109 110
        assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
        assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
B
Brendan Zabarauskas 已提交
111

112
        i64_val = i64_val.wrapping_add(1);
A
Alex Crichton 已提交
113 114
        assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
        assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
B
Brendan Zabarauskas 已提交
115
    }
116 117

    #[test]
M
Michał Krasnoborski 已提交
118
    fn test_int_from_minus_sign() {
119 120
        assert_eq!("-".parse::<i32>().ok(), None);
    }
B
Brendan Zabarauskas 已提交
121
}