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..634c3287c8e 100644 --- a/boa/Cargo.toml +++ b/boa/Cargo.toml @@ -34,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" @@ -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..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() {