summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ryu/tests/s2f_test.rs
blob: 5bae935ff7dfff4b01268e49fc132850264922c0 (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
// Translated from C to Rust. The original C code can be found at
// https://github.com/ulfjack/ryu and carries the following license:
//
// Copyright 2018 Ulf Adams
//
// The contents of this file may be used under the terms of the Apache License,
// Version 2.0.
//
//    (See accompanying file LICENSE-Apache or copy at
//     http://www.apache.org/licenses/LICENSE-2.0)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE-Boost or copy at
//     https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

#![allow(dead_code)]
#![allow(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss,
    clippy::checked_conversions,
    clippy::float_cmp,
    clippy::manual_range_contains,
    clippy::similar_names,
    clippy::too_many_lines,
    clippy::unreadable_literal,
    clippy::unseparated_literal_suffix,
    clippy::wildcard_imports
)]

#[path = "../src/common.rs"]
mod common;

#[cfg(not(feature = "small"))]
#[path = "../src/d2s_full_table.rs"]
mod d2s_full_table;

#[path = "../src/d2s_intrinsics.rs"]
mod d2s_intrinsics;

#[cfg(feature = "small")]
#[path = "../src/d2s_small_table.rs"]
mod d2s_small_table;

#[path = "../src/d2s.rs"]
mod d2s;

#[path = "../src/f2s_intrinsics.rs"]
mod f2s_intrinsics;

#[path = "../src/f2s.rs"]
mod f2s;

#[path = "../src/s2f.rs"]
mod s2f;

#[path = "../src/parse.rs"]
mod parse;

use crate::parse::Error;
use crate::s2f::s2f;

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        *self as u8 == *other as u8
    }
}

#[test]
fn test_basic() {
    assert_eq!(0.0, s2f(b"0").unwrap());
    assert_eq!(-0.0, s2f(b"-0").unwrap());
    assert_eq!(1.0, s2f(b"1").unwrap());
    assert_eq!(-1.0, s2f(b"-1").unwrap());
    assert_eq!(123456792.0, s2f(b"123456789").unwrap());
    assert_eq!(299792448.0, s2f(b"299792458").unwrap());
}

#[test]
fn test_min_max() {
    assert_eq!(1e-45, s2f(b"1e-45").unwrap());
    assert_eq!(f32::MIN_POSITIVE, s2f(b"1.1754944e-38").unwrap());
    assert_eq!(f32::MAX, s2f(b"3.4028235e+38").unwrap());
}

#[test]
fn test_mantissa_rounding_overflow() {
    assert_eq!(1.0, s2f(b"0.999999999").unwrap());
    assert_eq!(f32::INFINITY, s2f(b"3.4028236e+38").unwrap());
    assert_eq!(1.1754944e-38, s2f(b"1.17549430e-38").unwrap()); // FLT_MIN
    assert_eq!(1.1754944e-38, s2f(b"1.17549431e-38").unwrap());
    assert_eq!(1.1754944e-38, s2f(b"1.17549432e-38").unwrap());
    assert_eq!(1.1754944e-38, s2f(b"1.17549433e-38").unwrap());
    assert_eq!(1.1754944e-38, s2f(b"1.17549434e-38").unwrap());
    assert_eq!(1.1754944e-38, s2f(b"1.17549435e-38").unwrap());
}

#[test]
fn test_trailing_zeros() {
    assert_eq!(26843550.0, s2f(b"26843549.5").unwrap());
    assert_eq!(50000004.0, s2f(b"50000002.5").unwrap());
    assert_eq!(99999992.0, s2f(b"99999989.5").unwrap());
}