Files

58 lines
2.0 KiB
Rust

//! `js_number` against V8 (R5.1b, F1). The expected texts come from
//! `crates/core/tests/oracles/r5/numbers.json`, written by `String(value)` in Node
//! (`scripts/r5-number-oracle.mjs`); each value is given by its IEEE 754 bits, so no
//! decimal parsing on this side decides the expectation.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::json::js_number;
use serde_json::Value;
fn oracle_rows() -> Vec<(u64, String)> {
let oracle: Value = serde_json::from_str(&common::repo_file(
"crates/core/tests/oracles/r5/numbers.json",
))
.unwrap();
oracle["rows"]
.as_array()
.unwrap()
.iter()
.map(|row| {
let bits = u64::from_str_radix(row[0].as_str().unwrap(), 16).unwrap();
(bits, row[1].as_str().unwrap().to_owned())
})
.collect()
}
#[test]
fn every_oracle_value_prints_as_v8_prints_it() {
let rows = oracle_rows();
assert!(rows.len() > 40_000, "{} rows", rows.len());
let mismatches: Vec<String> = rows
.iter()
.filter_map(|(bits, expected)| {
let actual = js_number(f64::from_bits(*bits));
(actual != *expected).then(|| format!("{bits:016x}: {actual} vs V8 {expected}"))
})
.collect();
assert!(
mismatches.is_empty(),
"{} mismatches, first: {:?}",
mismatches.len(),
&mismatches[..mismatches.len().min(5)]
);
}
/// The value the R5.1 study found in the legacy baseline (`Horse.svg`, annotation `cx`):
/// exactly 840.16925048828125, halfway between the two 16-digit candidates; ECMAScript
/// picks the even last digit.
#[test]
fn an_exact_decimal_tie_rounds_to_the_even_digit() {
let value = f64::from_bits(0x408a_415a_a000_0000);
assert_eq!(js_number(value), "840.1692504882812");
assert_eq!(js_number(-value), "-840.1692504882812");
assert_eq!(js_number(21.699_905_395_507_812), "21.699905395507812");
assert_eq!(js_number(960.304_260_253_906_2), "960.3042602539062");
}