diff --git a/Cargo.lock b/Cargo.lock index 36d808487fb..a390afa5915 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ dependencies = [ "bitflags", "chrono", "criterion", + "float-cmp", "gc", "indexmap", "jemallocator", @@ -345,6 +346,15 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f" +[[package]] +name = "float-cmp" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1267f4ac4f343772758f7b1bdcbe767c218bbab93bb432acbf5162bbf85a6c4" +dependencies = [ + "num-traits", +] + [[package]] name = "fs_extra" version = "1.1.0" diff --git a/boa/Cargo.toml b/boa/Cargo.toml index 104efb77a3e..cebba46a1d6 100644 --- a/boa/Cargo.toml +++ b/boa/Cargo.toml @@ -34,6 +34,7 @@ once_cell = { version = "1.4.1", optional = true } [dev-dependencies] criterion = "=0.3.2" +float-cmp = "0.8.0" [target.x86_64-unknown-linux-gnu.dev-dependencies] jemallocator = "0.3.2" diff --git a/boa/src/builtins/math/tests.rs b/boa/src/builtins/math/tests.rs index b0848284329..506528c6642 100644 --- a/boa/src/builtins/math/tests.rs +++ b/boa/src/builtins/math/tests.rs @@ -305,10 +305,26 @@ fn expm1() { assert_eq!(a, String::from("NaN")); assert_eq!(b, String::from("NaN")); - assert_eq!(c.to_number(&mut engine).unwrap(), 1.718_281_828_459_045); - assert_eq!(d.to_number(&mut engine).unwrap(), -0.632_120_558_828_557_7); - assert_eq!(e.to_number(&mut engine).unwrap(), 0_f64); - assert_eq!(f.to_number(&mut engine).unwrap(), 6.389_056_098_930_65); + assert!(float_cmp::approx_eq!( + f64, + c.to_number(&mut engine).unwrap(), + 1.718_281_828_459_045 + )); + assert!(float_cmp::approx_eq!( + f64, + d.to_number(&mut engine).unwrap(), + -0.632_120_558_828_557_7 + )); + assert!(float_cmp::approx_eq!( + f64, + e.to_number(&mut engine).unwrap(), + 0_f64 + )); + assert!(float_cmp::approx_eq!( + f64, + f.to_number(&mut engine).unwrap(), + 6.389_056_098_930_65 + )); } #[test] @@ -690,22 +706,24 @@ fn sqrt() { assert_eq!(c.to_number(&mut engine).unwrap(), 3_f64); } -// TODO: Precision is always off between ci and local. We proably need a better way to compare floats anyways - -// #[test] -// fn tan() { -// let realm = Realm::create(); -// let mut engine = Interpreter::new(realm); -// let init = r#" -// var a = Math.tan(1.1); -// "#; +#[test] +fn tan() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + let init = r#" + var a = Math.tan(1.1); + "#; -// eprintln!("{}", forward(&mut engine, init)); + eprintln!("{}", forward(&mut engine, init)); -// let a = forward_val(&mut engine, "a").unwrap(); + let a = forward_val(&mut engine, "a").unwrap(); -// assert_eq!(a.to_number(), f64::from(1.964_759_657_248_652_5)); -// } + assert!(float_cmp::approx_eq!( + f64, + a.to_number(&mut engine).unwrap(), + f64::from(1.964_759_657_248_652_5) + )); +} #[test] fn tanh() {