summaryrefslogtreecommitdiffstats
path: root/vendor/serde_json/tests/lexical/algorithm.rs
blob: 7f3a2c6241a58c3e8ad25fe7e6de4660f06e8227 (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
100
101
102
103
104
105
106
107
108
109
110
// Adapted from https://github.com/Alexhuszagh/rust-lexical.

use crate::lexical::algorithm::*;
use crate::lexical::num::Float;

#[test]
fn float_fast_path_test() {
    // valid
    let mantissa = (1 << f32::MANTISSA_SIZE) - 1;
    let (min_exp, max_exp) = f32::exponent_limit();
    for exp in min_exp..=max_exp {
        let f = fast_path::<f32>(mantissa, exp);
        assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp));
    }

    // Check slightly above valid exponents
    let f = fast_path::<f32>(123, 15);
    assert_eq!(f, Some(1.23e+17));

    // Exponent is 1 too high, pushes over the mantissa.
    let f = fast_path::<f32>(123, 16);
    assert!(f.is_none());

    // Mantissa is too large, checked_mul should overflow.
    let f = fast_path::<f32>(mantissa, 11);
    assert!(f.is_none());

    // invalid exponents
    let (min_exp, max_exp) = f32::exponent_limit();
    let f = fast_path::<f32>(mantissa, min_exp - 1);
    assert!(f.is_none(), "exponent under min_exp");

    let f = fast_path::<f32>(mantissa, max_exp + 1);
    assert!(f.is_none(), "exponent above max_exp");
}

#[test]
fn double_fast_path_test() {
    // valid
    let mantissa = (1 << f64::MANTISSA_SIZE) - 1;
    let (min_exp, max_exp) = f64::exponent_limit();
    for exp in min_exp..=max_exp {
        let f = fast_path::<f64>(mantissa, exp);
        assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp));
    }

    // invalid exponents
    let (min_exp, max_exp) = f64::exponent_limit();
    let f = fast_path::<f64>(mantissa, min_exp - 1);
    assert!(f.is_none(), "exponent under min_exp");

    let f = fast_path::<f64>(mantissa, max_exp + 1);
    assert!(f.is_none(), "exponent above max_exp");

    assert_eq!(
        Some(0.04628372940652459),
        fast_path::<f64>(4628372940652459, -17)
    );
    assert_eq!(None, fast_path::<f64>(26383446160308229, -272));
}

#[test]
fn moderate_path_test() {
    let (f, valid) = moderate_path::<f64>(1234567890, -1, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.0);

    let (f, valid) = moderate_path::<f64>(1234567891, -1, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.1);

    let (f, valid) = moderate_path::<f64>(12345678912, -2, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.12);

    let (f, valid) = moderate_path::<f64>(123456789123, -3, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.123);

    let (f, valid) = moderate_path::<f64>(1234567891234, -4, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.1234);

    let (f, valid) = moderate_path::<f64>(12345678912345, -5, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.12345);

    let (f, valid) = moderate_path::<f64>(123456789123456, -6, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.123456);

    let (f, valid) = moderate_path::<f64>(1234567891234567, -7, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.1234567);

    let (f, valid) = moderate_path::<f64>(12345678912345679, -8, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 123456789.12345679);

    let (f, valid) = moderate_path::<f64>(4628372940652459, -17, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 0.04628372940652459);

    let (f, valid) = moderate_path::<f64>(26383446160308229, -272, false);
    assert!(valid, "should be valid");
    assert_eq!(f.into_float::<f64>(), 2.6383446160308229e-256);

    let (_, valid) = moderate_path::<f64>(26383446160308230, -272, false);
    assert!(!valid, "should be invalid");
}