summaryrefslogtreecommitdiffstats
path: root/third_party/rust/minimal-lexical/tests/number_tests.rs
blob: 947be394c94f19f7f07ada20b65f8581eb966207 (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
use minimal_lexical::number::Number;

#[test]
fn is_fast_path_test() {
    let mut number = Number {
        exponent: -4,
        mantissa: 12345,
        many_digits: false,
    };
    assert_eq!(number.is_fast_path::<f32>(), true);
    assert_eq!(number.is_fast_path::<f64>(), true);

    number.exponent = -15;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), true);

    number.exponent = -25;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), false);

    number.exponent = 25;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), true);

    number.exponent = 36;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), true);

    number.exponent = 38;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), false);

    number.mantissa = 1 << 25;
    number.exponent = 0;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), true);

    number.mantissa = 1 << 54;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), false);

    number.mantissa = 1 << 52;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), true);

    number.many_digits = true;
    assert_eq!(number.is_fast_path::<f32>(), false);
    assert_eq!(number.is_fast_path::<f64>(), false);
}

#[test]
fn try_fast_path_test() {
    let mut number = Number {
        exponent: -4,
        mantissa: 12345,
        many_digits: false,
    };
    assert_eq!(number.try_fast_path::<f32>(), Some(1.2345));
    assert_eq!(number.try_fast_path::<f64>(), Some(1.2345));

    number.exponent = -10;
    assert_eq!(number.try_fast_path::<f32>(), Some(1.2345e-6));
    assert_eq!(number.try_fast_path::<f64>(), Some(1.2345e-6));

    number.exponent = -20;
    assert_eq!(number.try_fast_path::<f32>(), None);
    assert_eq!(number.try_fast_path::<f64>(), Some(1.2345e-16));

    number.exponent = -25;
    assert_eq!(number.try_fast_path::<f32>(), None);
    assert_eq!(number.try_fast_path::<f64>(), None);

    number.exponent = 12;
    assert_eq!(number.try_fast_path::<f32>(), Some(1.2345e16));
    assert_eq!(number.try_fast_path::<f64>(), Some(1.2345e16));

    number.exponent = 25;
    assert_eq!(number.try_fast_path::<f32>(), None);
    assert_eq!(number.try_fast_path::<f64>(), Some(1.2345e29));

    number.exponent = 32;
    assert_eq!(number.try_fast_path::<f32>(), None);
    assert_eq!(number.try_fast_path::<f64>(), Some(1.2345e36));

    number.exponent = 36;
    assert_eq!(number.try_fast_path::<f32>(), None);
    assert_eq!(number.try_fast_path::<f64>(), None);
}