From 464b243952f553e90fd665bae4ce3c3767c04256 Mon Sep 17 00:00:00 2001 From: nd419 <5161147+neeldug@users.noreply.github.com> Date: Wed, 26 Aug 2020 14:29:09 +0100 Subject: [PATCH 1/4] Added approx_eq! macro for expm1 tests. Added approx_eq! macro for expm1 tests due to floating point arithmetic inaccuracies, using default ULP & epsilon values. approx_eq! macro does not work for NaN values, however, for tests this should be okay anyway! Solves #664. --- Cargo.lock | 10 ++++++++++ boa/Cargo.toml | 3 ++- boa/src/builtins/math/tests.rs | 24 ++++++++++++++++++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7c61e2043a5..81d5385cd5f 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.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +[[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 cbb18af9b8f..87e748ad8bc 100644 --- a/boa/Cargo.toml +++ b/boa/Cargo.toml @@ -26,6 +26,7 @@ bitflags = "1.2.1" indexmap = "1.4.0" ryu-js = "0.2.0" chrono = "0.4" +float-cmp = { version = "0.8.0", features = ["std"] } # Optional Dependencies serde = { version = "1.0.114", features = ["derive"], optional = true } @@ -57,4 +58,4 @@ harness = false [[bench]] name = "full" -harness = false \ No newline at end of file +harness = false diff --git a/boa/src/builtins/math/tests.rs b/boa/src/builtins/math/tests.rs index b0848284329..76b0a360a40 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] From 9149ae8ce96573f0e443c2fe91d4d485ef8fd625 Mon Sep 17 00:00:00 2001 From: nd419 <5161147+neeldug@users.noreply.github.com> Date: Wed, 26 Aug 2020 16:38:52 +0100 Subject: [PATCH 2/4] Modified Cargo.toml to remove unneeded feature. Removed std feature as was unused in test. --- boa/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/Cargo.toml b/boa/Cargo.toml index 87e748ad8bc..f3ca01c947e 100644 --- a/boa/Cargo.toml +++ b/boa/Cargo.toml @@ -26,7 +26,7 @@ bitflags = "1.2.1" indexmap = "1.4.0" ryu-js = "0.2.0" chrono = "0.4" -float-cmp = { version = "0.8.0", features = ["std"] } +float-cmp = "0.8.0" # Optional Dependencies serde = { version = "1.0.114", features = ["derive"], optional = true } From 2627bd51c7242071e77ab442c4376a73906148e4 Mon Sep 17 00:00:00 2001 From: nd419 <5161147+neeldug@users.noreply.github.com> Date: Thu, 27 Aug 2020 15:17:03 +0100 Subject: [PATCH 3/4] Moves `float-cmp` to Dev Dependencies Refactors tan() unit test, previously unused, to use approx_eq!() macro for assertion to pass on CI. --- boa/Cargo.toml | 2 +- boa/src/builtins/math/tests.rs | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/boa/Cargo.toml b/boa/Cargo.toml index f3ca01c947e..634c3287c8e 100644 --- a/boa/Cargo.toml +++ b/boa/Cargo.toml @@ -26,7 +26,6 @@ bitflags = "1.2.1" indexmap = "1.4.0" ryu-js = "0.2.0" chrono = "0.4" -float-cmp = "0.8.0" # Optional Dependencies serde = { version = "1.0.114", features = ["derive"], optional = true } @@ -35,6 +34,7 @@ once_cell = { version = "1.4.0", 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 76b0a360a40..8580684aac0 100644 --- a/boa/src/builtins/math/tests.rs +++ b/boa/src/builtins/math/tests.rs @@ -706,22 +706,20 @@ 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() { From a0b55d97cf22cbaca8c4a3a7dcb084229975aa2c Mon Sep 17 00:00:00 2001 From: nd419 <5161147+neeldug@users.noreply.github.com> Date: Thu, 27 Aug 2020 15:21:02 +0100 Subject: [PATCH 4/4] Fixes cargo fmt errors --- boa/src/builtins/math/tests.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/boa/src/builtins/math/tests.rs b/boa/src/builtins/math/tests.rs index 8580684aac0..506528c6642 100644 --- a/boa/src/builtins/math/tests.rs +++ b/boa/src/builtins/math/tests.rs @@ -718,7 +718,11 @@ fn tan() { let a = forward_val(&mut engine, "a").unwrap(); - assert!(float_cmp::approx_eq!(f64, a.to_number(&mut engine).unwrap(), 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]