about summary refs log tree commit diff
path: root/src/test/stdtest/int.rs
blob: c8ec806189fb5f3692ddbcad65c1492a942c62e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import core::*;

use std;
import str;
import int;
import str::eq;
import str::bytes;

#[test]
fn test_from_str() {
    assert(int::from_str("0") == 0);
    assert(int::from_str("3") == 3);
    assert(int::from_str("10") == 10);
    assert(int::from_str("123456789") == 123456789);
    assert(int::from_str("00100") == 100);

    assert(int::from_str("-1") == -1);
    assert(int::from_str("-3") == -3);
    assert(int::from_str("-10") == -10);
    assert(int::from_str("-123456789") == -123456789);
    assert(int::from_str("-00100") == -100);
}

#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_from_str_fail_1() {
    int::from_str(" ");
}

#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_from_str_fail_2() {
    int::from_str("x");
}

#[test]
fn test_parse_buf() {
    assert (int::parse_buf(bytes("123"), 10u) == 123);
    assert (int::parse_buf(bytes("1001"), 2u) == 9);
    assert (int::parse_buf(bytes("123"), 8u) == 83);
    assert (int::parse_buf(bytes("123"), 16u) == 291);
    assert (int::parse_buf(bytes("ffff"), 16u) == 65535);
    assert (int::parse_buf(bytes("FFFF"), 16u) == 65535);
    assert (int::parse_buf(bytes("z"), 36u) == 35);
    assert (int::parse_buf(bytes("Z"), 36u) == 35);

    assert (int::parse_buf(bytes("-123"), 10u) == -123);
    assert (int::parse_buf(bytes("-1001"), 2u) == -9);
    assert (int::parse_buf(bytes("-123"), 8u) == -83);
    assert (int::parse_buf(bytes("-123"), 16u) == -291);
    assert (int::parse_buf(bytes("-ffff"), 16u) == -65535);
    assert (int::parse_buf(bytes("-FFFF"), 16u) == -65535);
    assert (int::parse_buf(bytes("-z"), 36u) == -35);
    assert (int::parse_buf(bytes("-Z"), 36u) == -35);
}

#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_parse_buf_fail_1() {
    int::parse_buf(bytes("Z"), 35u);
}

#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_parse_buf_fail_2() {
    int::parse_buf(bytes("-9"), 2u);
}

#[test]
fn test_to_str() {
    assert (eq(int::to_str(0, 10u), "0"));
    assert (eq(int::to_str(1, 10u), "1"));
    assert (eq(int::to_str(-1, 10u), "-1"));
    assert (eq(int::to_str(255, 16u), "ff"));
    assert (eq(int::to_str(100, 10u), "100"));
}

#[test]
fn test_pow() {
    assert (int::pow(0, 0u) == 1);
    assert (int::pow(0, 1u) == 0);
    assert (int::pow(0, 2u) == 0);
    assert (int::pow(-1, 0u) == 1);
    assert (int::pow(1, 0u) == 1);
    assert (int::pow(-3, 2u) == 9);
    assert (int::pow(-3, 3u) == -27);
    assert (int::pow(4, 9u) == 262144);
}

#[test]
fn test_overflows() {
   assert (int::max_value > 0);
   assert (int::min_value <= 0);
   assert (int::min_value + int::max_value + 1 == 0);
}