1// 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#![allow(
24 clippy::cast_lossless,
25 clippy::cast_possible_truncation,
26 clippy::cast_possible_wrap,
27 clippy::cast_sign_loss,
28 clippy::excessive_precision,
29 clippy::float_cmp,
30 clippy::manual_range_contains,
31 clippy::similar_names,
32 clippy::too_many_lines,
33 clippy::unreadable_literal,
34 clippy::unseparated_literal_suffix,
35 clippy::wildcard_imports
36)]
37
38#[path = "../src/common.rs"]
39mod common;
40
41#[cfg(not(feature = "small"))]
42#[path = "../src/d2s_full_table.rs"]
43mod d2s_full_table;
44
45#[path = "../src/d2s_intrinsics.rs"]
46mod d2s_intrinsics;
47
48#[cfg(feature = "small")]
49#[path = "../src/d2s_small_table.rs"]
50mod d2s_small_table;
51
52#[path = "../src/d2s.rs"]
53mod d2s;
54
55#[path = "../src/s2d.rs"]
56mod s2d;
57
58#[path = "../src/parse.rs"]
59mod parse;
60
61use crate::parse::Error;
62use crate::s2d::s2d;
63
64impl PartialEq for Error {
65 fn eq(&self, other: &Self) -> bool {
66 *self as u8 == *other as u8
67 }
68}
69
70#[test]
71fn test_bad_input() {
72 assert_eq!(Error::MalformedInput, s2d(b"x").unwrap_err());
73 assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err());
74 assert_eq!(Error::MalformedInput, s2d(b"..").unwrap_err());
75 assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err());
76 assert_eq!(Error::MalformedInput, s2d(b"1ee1").unwrap_err());
77 assert_eq!(Error::MalformedInput, s2d(b"1e.1").unwrap_err());
78 assert_eq!(Error::InputTooShort, s2d(b"").unwrap_err());
79 assert_eq!(Error::InputTooLong, s2d(b"123456789012345678").unwrap_err());
80 assert_eq!(Error::InputTooLong, s2d(b"1e12345").unwrap_err());
81}
82
83#[test]
84fn test_basic() {
85 assert_eq!(0.0, s2d(b"0").unwrap());
86 assert_eq!(-0.0, s2d(b"-0").unwrap());
87 assert_eq!(1.0, s2d(b"1").unwrap());
88 assert_eq!(2.0, s2d(b"2").unwrap());
89 assert_eq!(123456789.0, s2d(b"123456789").unwrap());
90 assert_eq!(123.456, s2d(b"123.456").unwrap());
91 assert_eq!(123.456, s2d(b"123456e-3").unwrap());
92 assert_eq!(123.456, s2d(b"1234.56e-1").unwrap());
93 assert_eq!(1.453, s2d(b"1.453").unwrap());
94 assert_eq!(1453.0, s2d(b"1.453e+3").unwrap());
95 assert_eq!(0.0, s2d(b".0").unwrap());
96 assert_eq!(1.0, s2d(b"1e0").unwrap());
97 assert_eq!(1.0, s2d(b"1E0").unwrap());
98 assert_eq!(1.0, s2d(b"000001.000000").unwrap());
99 assert_eq!(0.2316419, s2d(b"0.2316419").unwrap());
100}
101
102#[test]
103fn test_min_max() {
104 assert_eq!(
105 1.7976931348623157e308,
106 s2d(b"1.7976931348623157e308").unwrap(),
107 );
108 assert_eq!(5E-324, s2d(b"5E-324").unwrap());
109}
110
111#[test]
112fn test_mantissa_rounding_overflow() {
113 // This results in binary mantissa that is all ones and requires rounding up
114 // because it is closer to 1 than to the next smaller float. This is a
115 // regression test that the mantissa overflow is handled correctly by
116 // increasing the exponent.
117 assert_eq!(1.0, s2d(b"0.99999999999999999").unwrap());
118 // This number overflows the mantissa *and* the IEEE exponent.
119 assert_eq!(f64::INFINITY, s2d(b"1.7976931348623159e308").unwrap());
120}
121
122#[test]
123fn test_underflow() {
124 assert_eq!(0.0, s2d(b"2.4e-324").unwrap());
125 assert_eq!(0.0, s2d(b"1e-324").unwrap());
126 assert_eq!(0.0, s2d(b"9.99999e-325").unwrap());
127 // These are just about halfway between 0 and the smallest float.
128 // The first is just below the halfway point, the second just above.
129 assert_eq!(0.0, s2d(b"2.4703282292062327e-324").unwrap());
130 assert_eq!(5e-324, s2d(b"2.4703282292062328e-324").unwrap());
131}
132
133#[test]
134fn test_overflow() {
135 assert_eq!(f64::INFINITY, s2d(b"2e308").unwrap());
136 assert_eq!(f64::INFINITY, s2d(b"1e309").unwrap());
137}
138
139#[test]
140fn test_table_size_denormal() {
141 assert_eq!(5e-324, s2d(b"4.9406564584124654e-324").unwrap());
142}
143
144#[test]
145fn test_issue157() {
146 assert_eq!(
147 1.2999999999999999E+154,
148 s2d(b"1.2999999999999999E+154").unwrap(),
149 );
150}
151
152#[test]
153fn test_issue173() {
154 // Denormal boundary
155 assert_eq!(
156 2.2250738585072012e-308,
157 s2d(b"2.2250738585072012e-308").unwrap(),
158 );
159 assert_eq!(
160 2.2250738585072013e-308,
161 s2d(b"2.2250738585072013e-308").unwrap(),
162 );
163 assert_eq!(
164 2.2250738585072014e-308,
165 s2d(b"2.2250738585072014e-308").unwrap(),
166 );
167}
168