blob: f51c1ed0d0b723a7f6a45d25712f22c874269041 [file] [log] [blame]
Yi Kongce81bb62020-08-31 01:21:33 +08001// Translated from C to Rust. The original C code can be found at
2// https://github.com/ulfjack/ryu and carries the following license:
3//
4// Copyright 2018 Ulf Adams
5//
6// The contents of this file may be used under the terms of the Apache License,
7// Version 2.0.
8//
9// (See accompanying file LICENSE-Apache or copy at
10// http://www.apache.org/licenses/LICENSE-2.0)
11//
12// Alternatively, the contents of this file may be used under the terms of
13// the Boost Software License, Version 1.0.
14// (See accompanying file LICENSE-Boost or copy at
15// https://www.boost.org/LICENSE_1_0.txt)
16//
17// Unless required by applicable law or agreed to in writing, this software
18// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19// KIND, either express or implied.
20
21#![cfg(not(feature = "small"))]
22#![allow(dead_code)]
23
24#[path = "../src/common.rs"]
25mod common;
26
27#[path = "../src/d2s_full_table.rs"]
28mod d2s_full_table;
29
30#[path = "../src/d2s_intrinsics.rs"]
31mod d2s_intrinsics;
32
33#[path = "../src/d2s.rs"]
34mod d2s;
35
36#[path = "../src/s2d.rs"]
37mod s2d;
38
39#[path = "../src/parse.rs"]
40mod parse;
41
42use crate::parse::Error;
43use crate::s2d::s2d;
44
45impl PartialEq for Error {
46 fn eq(&self, other: &Self) -> bool {
47 *self as u8 == *other as u8
48 }
49}
50
51#[test]
52fn test_bad_input() {
53 assert_eq!(Error::MalformedInput, s2d(b"x").unwrap_err());
54 assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err());
55 assert_eq!(Error::MalformedInput, s2d(b"..").unwrap_err());
56 assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err());
57 assert_eq!(Error::MalformedInput, s2d(b"1ee1").unwrap_err());
58 assert_eq!(Error::MalformedInput, s2d(b"1e.1").unwrap_err());
59 assert_eq!(Error::InputTooShort, s2d(b"").unwrap_err());
60 assert_eq!(Error::InputTooLong, s2d(b"123456789012345678").unwrap_err());
61 assert_eq!(Error::InputTooLong, s2d(b"1e12345").unwrap_err());
62}
63
64#[test]
65fn test_basic() {
66 assert_eq!(0.0, s2d(b"0").unwrap());
67 assert_eq!(-0.0, s2d(b"-0").unwrap());
68 assert_eq!(1.0, s2d(b"1").unwrap());
69 assert_eq!(2.0, s2d(b"2").unwrap());
70 assert_eq!(123456789.0, s2d(b"123456789").unwrap());
71 assert_eq!(123.456, s2d(b"123.456").unwrap());
72 assert_eq!(123.456, s2d(b"123456e-3").unwrap());
73 assert_eq!(123.456, s2d(b"1234.56e-1").unwrap());
74 assert_eq!(1.453, s2d(b"1.453").unwrap());
75 assert_eq!(1453.0, s2d(b"1.453e+3").unwrap());
76 assert_eq!(0.0, s2d(b".0").unwrap());
77 assert_eq!(1.0, s2d(b"1e0").unwrap());
78 assert_eq!(1.0, s2d(b"1E0").unwrap());
79 assert_eq!(1.0, s2d(b"000001.000000").unwrap());
80}
81
82#[test]
83fn test_min_max() {
84 assert_eq!(
85 1.7976931348623157e308,
86 s2d(b"1.7976931348623157e308").unwrap(),
87 );
88 assert_eq!(5E-324, s2d(b"5E-324").unwrap());
89}
90
91#[test]
92fn test_mantissa_rounding_overflow() {
93 // This results in binary mantissa that is all ones and requires rounding up
94 // because it is closer to 1 than to the next smaller float. This is a
95 // regression test that the mantissa overflow is handled correctly by
96 // increasing the exponent.
97 assert_eq!(1.0, s2d(b"0.99999999999999999").unwrap());
98 // This number overflows the mantissa *and* the IEEE exponent.
99 assert_eq!(f64::INFINITY, s2d(b"1.7976931348623159e308").unwrap());
100}
101
102#[test]
103fn test_underflow() {
104 assert_eq!(0.0, s2d(b"2.4e-324").unwrap());
105 assert_eq!(0.0, s2d(b"1e-324").unwrap());
106 assert_eq!(0.0, s2d(b"9.99999e-325").unwrap());
107 // These are just about halfway between 0 and the smallest float.
108 // The first is just below the halfway point, the second just above.
109 assert_eq!(0.0, s2d(b"2.4703282292062327e-324").unwrap());
110 assert_eq!(5e-324, s2d(b"2.4703282292062328e-324").unwrap());
111}
112
113#[test]
114fn test_overflow() {
115 assert_eq!(f64::INFINITY, s2d(b"2e308").unwrap());
116 assert_eq!(f64::INFINITY, s2d(b"1e309").unwrap());
117}
118
119#[test]
120fn test_table_size_denormal() {
121 assert_eq!(5e-324, s2d(b"4.9406564584124654e-324").unwrap());
122}
123
124#[test]
125fn test_issue157() {
126 assert_eq!(
127 1.2999999999999999E+154,
128 s2d(b"1.2999999999999999E+154").unwrap(),
129 );
130}