summaryrefslogtreecommitdiffstats
path: root/vendor/minimal-lexical/tests/bellerophon_tests.rs
blob: f5826c615d6d0bb67300e448ac405cbc58b2ce97 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#![cfg(feature = "compact")]

mod bellerophon;

use bellerophon::{bellerophon_test, compute_float32, compute_float64};
use minimal_lexical::num::Float;

#[test]
fn halfway_round_down_test() {
    // Halfway, round-down tests
    bellerophon_test::<f64>(9007199254740992, 0, false, 0, 1076);
    bellerophon_test::<f64>(
        9007199254740993,
        0,
        false,
        9223372036854776832,
        1065 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(9007199254740994, 0, false, 1, 1076);

    bellerophon_test::<f64>(18014398509481984, 0, false, 0, 1077);
    bellerophon_test::<f64>(
        18014398509481986,
        0,
        false,
        9223372036854776832,
        1066 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(18014398509481988, 0, false, 1, 1077);

    bellerophon_test::<f64>(9223372036854775808, 0, false, 0, 1086);
    bellerophon_test::<f64>(
        9223372036854776832,
        0,
        false,
        9223372036854776832,
        1075 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(9223372036854777856, 0, false, 1, 1086);

    // Add a 0 but say we're truncated.
    bellerophon_test::<f64>(9007199254740992000, -3, true, 0, 1076);
    bellerophon_test::<f64>(
        9007199254740993000,
        -3,
        true,
        9223372036854776832,
        1065 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(9007199254740994000, -3, true, 1, 1076);
}

#[test]
fn halfway_round_up_test() {
    // Halfway, round-up tests
    bellerophon_test::<f64>(9007199254740994, 0, false, 1, 1076);
    bellerophon_test::<f64>(
        9007199254740995,
        0,
        false,
        9223372036854778880,
        1065 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(9007199254740996, 0, false, 2, 1076);

    bellerophon_test::<f64>(18014398509481988, 0, false, 1, 1077);
    bellerophon_test::<f64>(
        18014398509481990,
        0,
        false,
        9223372036854778880,
        1066 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(18014398509481992, 0, false, 2, 1077);

    bellerophon_test::<f64>(9223372036854777856, 0, false, 1, 1086);
    bellerophon_test::<f64>(
        9223372036854778880,
        0,
        false,
        9223372036854778880,
        1075 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(9223372036854779904, 0, false, 2, 1086);

    // Add a 0 but say we're truncated.
    bellerophon_test::<f64>(9007199254740994000, -3, true, 1, 1076);
    bellerophon_test::<f64>(
        9007199254740994990,
        -3,
        true,
        9223372036854778869,
        1065 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(
        9007199254740995000,
        -3,
        true,
        9223372036854778879,
        1065 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(
        9007199254740995010,
        -3,
        true,
        9223372036854778890,
        1065 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(9007199254740995050, -3, true, 2, 1076);
    bellerophon_test::<f64>(9007199254740996000, -3, true, 2, 1076);
}

#[test]
fn extremes_test() {
    // Need to check we get proper results with rounding for near-infinity
    // and near-zero and/or denormal floats.
    bellerophon_test::<f64>(5, -324, false, 1, 0);
    bellerophon_test::<f64>(10, -324, false, 2, 0);
    // This is very close to 2.4703282292062327206e-342.
    bellerophon_test::<f64>(
        2470328229206232720,
        -342,
        false,
        18446744073709551608,
        -64 + f64::INVALID_FP,
    );
    bellerophon_test::<f64>(2470328229206232721, -342, false, 9223372036854775808, -32831);
    bellerophon_test::<f64>(2470328229206232725, -342, false, 9223372036854775824, -32831);
    bellerophon_test::<f64>(2470328229206232726, -342, false, 1, 0);
    bellerophon_test::<f64>(2470328229206232730, -342, false, 1, 0);
    // Check very close to literal infinity.
    // 17.976931348623155
    // 1.797693134862315508561243283845062402343434371574593359244049e+308
    // 1.797693134862315708145274237317043567980705675258449965989175e+308
    bellerophon_test::<f64>(17976931348623155, 292, false, 4503599627370494, 2046);
    bellerophon_test::<f64>(17976931348623156, 292, false, 4503599627370494, 2046);
    bellerophon_test::<f64>(1797693134862315605, 290, false, 4503599627370494, 2046);
    bellerophon_test::<f64>(1797693134862315607, 290, false, 4503599627370494, 2046);
    bellerophon_test::<f64>(1797693134862315608, 290, false, 18446744073709548540, -30733);
    bellerophon_test::<f64>(1797693134862315609, 290, false, 18446744073709548550, -30733);
    bellerophon_test::<f64>(179769313486231561, 291, false, 4503599627370495, 2046);
    bellerophon_test::<f64>(17976931348623157, 292, false, 4503599627370495, 2046);

    // Check existing issues and underflow.
    bellerophon_test::<f64>(2470328229206232726, -343, false, 0, 0);
    bellerophon_test::<f64>(2470328229206232726, -342, false, 1, 0);
    bellerophon_test::<f64>(1, -250, false, 1945308223406668, 192);
    bellerophon_test::<f64>(1, -150, false, 2867420733609077, 524);
    bellerophon_test::<f64>(1, -45, false, 1924152549665465, 873);
    bellerophon_test::<f64>(1, -40, false, 400386103400348, 890);
    bellerophon_test::<f64>(1, -20, false, 2142540351554083, 956);
    bellerophon_test::<f64>(1, 0, false, 0, 1023);
    bellerophon_test::<f64>(1, 20, false, 1599915997629504, 1089);
    bellerophon_test::<f64>(1, 40, false, 3768206498159781, 1155);
    bellerophon_test::<f64>(1, 150, false, 999684479948463, 1521);
    bellerophon_test::<f64>(1, 250, false, 1786584717939204, 1853);
    // Minimum positive normal float.
    bellerophon_test::<f64>(22250738585072014, -324, false, 0, 1);
    // Maximum positive subnormal float.
    bellerophon_test::<f64>(2225073858507201, -323, false, 4503599627370495, 0);
    // Next highest subnormal float.
    bellerophon_test::<f64>(22250738585072004, -324, false, 4503599627370494, 0);
    bellerophon_test::<f64>(22250738585072006, -324, false, 4503599627370494, 0);
    bellerophon_test::<f64>(22250738585072007, -324, false, 4503599627370495, 0);
    bellerophon_test::<f64>(222507385850720062, -325, false, 4503599627370494, 0);
    bellerophon_test::<f64>(222507385850720063, -325, false, 4503599627370494, 0);
    bellerophon_test::<f64>(222507385850720064, -325, false, 4503599627370494, 0);
    bellerophon_test::<f64>(2225073858507200641, -326, false, 18446744073709545462, -32779);
    bellerophon_test::<f64>(2225073858507200642, -326, false, 18446744073709545472, -32779);
    bellerophon_test::<f64>(222507385850720065, -325, false, 4503599627370495, 0);
}

#[test]
fn compute_float_f32_test() {
    // These test near-halfway cases for single-precision floats.
    assert_eq!(compute_float32(0, 16777216), (151, 0));
    assert_eq!(compute_float32(0, 16777217), (111 + f32::INVALID_FP, 9223372586610589696));
    assert_eq!(compute_float32(0, 16777218), (151, 1));
    assert_eq!(compute_float32(0, 16777219), (111 + f32::INVALID_FP, 9223373686122217472));
    assert_eq!(compute_float32(0, 16777220), (151, 2));

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(compute_float32(-10, 167772160000000000), (151, 0));
    assert_eq!(
        compute_float32(-10, 167772170000000000),
        (111 + f32::INVALID_FP, 9223372586610589696)
    );
    assert_eq!(compute_float32(-10, 167772180000000000), (151, 1));
    // Let's check the lines to see if anything is different in table...
    assert_eq!(
        compute_float32(-10, 167772190000000000),
        (111 + f32::INVALID_FP, 9223373686122217472)
    );
    assert_eq!(compute_float32(-10, 167772200000000000), (151, 2));
}

#[test]
fn compute_float_f64_test() {
    // These test near-halfway cases for double-precision floats.
    assert_eq!(compute_float64(0, 9007199254740992), (1076, 0));
    assert_eq!(compute_float64(0, 9007199254740993), (1065 + f64::INVALID_FP, 9223372036854776832));
    assert_eq!(compute_float64(0, 9007199254740994), (1076, 1));
    assert_eq!(compute_float64(0, 9007199254740995), (1065 + f64::INVALID_FP, 9223372036854778880));
    assert_eq!(compute_float64(0, 9007199254740996), (1076, 2));
    assert_eq!(compute_float64(0, 18014398509481984), (1077, 0));
    assert_eq!(
        compute_float64(0, 18014398509481986),
        (1066 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(compute_float64(0, 18014398509481988), (1077, 1));
    assert_eq!(
        compute_float64(0, 18014398509481990),
        (1066 + f64::INVALID_FP, 9223372036854778880)
    );
    assert_eq!(compute_float64(0, 18014398509481992), (1077, 2));

    // These are examples of the above tests, with
    // digits from the exponent shifted to the mantissa.
    assert_eq!(compute_float64(-3, 9007199254740992000), (1076, 0));
    assert_eq!(
        compute_float64(-3, 9007199254740993000),
        (1065 + f64::INVALID_FP, 9223372036854776832)
    );
    assert_eq!(compute_float64(-3, 9007199254740994000), (1076, 1));
    assert_eq!(
        compute_float64(-3, 9007199254740995000),
        (1065 + f64::INVALID_FP, 9223372036854778879)
    );
    assert_eq!(compute_float64(-3, 9007199254740996000), (1076, 2));
}