blob: f49824a69897c4d4ffab01097bfa7c06b3036bdc [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#![allow(dead_code)]
22
23#[path = "../src/common.rs"]
24mod common;
25
26#[path = "../src/d2s_full_table.rs"]
27mod d2s_full_table;
28
29#[path = "../src/d2s_intrinsics.rs"]
30mod d2s_intrinsics;
31
32#[path = "../src/d2s.rs"]
33mod d2s;
34
35#[path = "../src/f2s_intrinsics.rs"]
36mod f2s_intrinsics;
37
38#[path = "../src/f2s.rs"]
39mod f2s;
40
41#[path = "../src/s2f.rs"]
42mod s2f;
43
44#[path = "../src/parse.rs"]
45mod parse;
46
47use crate::parse::Error;
48use crate::s2f::s2f;
49
50impl PartialEq for Error {
51 fn eq(&self, other: &Self) -> bool {
52 *self as u8 == *other as u8
53 }
54}
55
56#[test]
57fn test_basic() {
58 assert_eq!(0.0, s2f(b"0").unwrap());
59 assert_eq!(-0.0, s2f(b"-0").unwrap());
60 assert_eq!(1.0, s2f(b"1").unwrap());
61 assert_eq!(-1.0, s2f(b"-1").unwrap());
62 assert_eq!(123456792.0, s2f(b"123456789").unwrap());
63 assert_eq!(299792448.0, s2f(b"299792458").unwrap());
64}
65
66#[test]
67fn test_min_max() {
68 assert_eq!(1e-45, s2f(b"1e-45").unwrap());
69 assert_eq!(f32::MIN_POSITIVE, s2f(b"1.1754944e-38").unwrap());
70 assert_eq!(f32::MAX, s2f(b"3.4028235e+38").unwrap());
71}
72
73#[test]
74fn test_mantissa_rounding_overflow() {
75 assert_eq!(1.0, s2f(b"0.999999999").unwrap());
76 assert_eq!(f32::INFINITY, s2f(b"3.4028236e+38").unwrap());
77}