From 0b0e0b51dad30fba0f1b924e25b44e4a900f4d35 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Thu, 4 Jun 2020 03:59:14 +0200 Subject: [PATCH] Added tests --- boa/src/builtins/bigint/conversions.rs | 11 +++- boa/src/builtins/bigint/tests.rs | 84 ++++++++++++++++++++++++-- boa/src/exec/tests.rs | 26 +++++++- 3 files changed, 114 insertions(+), 7 deletions(-) diff --git a/boa/src/builtins/bigint/conversions.rs b/boa/src/builtins/bigint/conversions.rs index 275d18c51c2..572e2899769 100644 --- a/boa/src/builtins/bigint/conversions.rs +++ b/boa/src/builtins/bigint/conversions.rs @@ -1,6 +1,9 @@ use super::BigInt; -use crate::{builtins::value::Value, exec::Interpreter}; +use crate::{ + builtins::{Number, Value}, + exec::Interpreter, +}; use num_traits::cast::{FromPrimitive, ToPrimitive}; use std::convert::TryFrom; @@ -79,6 +82,12 @@ impl TryFrom for BigInt { type Error = TryFromF64Error; fn try_from(n: f64) -> Result { + // If the truncated version of the number is not the + // same as the non-truncated version then the floating-point + // number conains a fractional part. + if !Number::equal(n.trunc(), n) { + return Err(TryFromF64Error); + } match num_bigint::BigInt::from_f64(n) { Some(bigint) => Ok(BigInt(bigint)), None => Err(TryFromF64Error), diff --git a/boa/src/builtins/bigint/tests.rs b/boa/src/builtins/bigint/tests.rs index e8f1b84d1db..7dafb618f4f 100644 --- a/boa/src/builtins/bigint/tests.rs +++ b/boa/src/builtins/bigint/tests.rs @@ -1,4 +1,4 @@ -use crate::{forward, Interpreter, Realm}; +use crate::{forward, forward_val, Interpreter, Realm}; #[test] fn equality() { @@ -55,7 +55,7 @@ fn equality() { } #[test] -fn bigint_function_conversion() { +fn bigint_function_conversion_from_integer() { let realm = Realm::create(); let mut engine = Interpreter::new(realm); @@ -70,6 +70,83 @@ fn bigint_function_conversion() { ); } +#[test] +fn bigint_function_conversion_from_rational() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + assert_eq!(forward(&mut engine, "BigInt(0.0)"), "0n"); + assert_eq!(forward(&mut engine, "BigInt(1.0)"), "1n"); + assert_eq!(forward(&mut engine, "BigInt(10000.0)"), "10000n"); +} + +#[test] +fn bigint_function_conversion_from_rational_with_fractional_part() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + let scenario = r#" + var x = false; + try { + BigInt(0.1); + } catch (e) { + x = true; + } + "#; + forward_val(&mut engine, scenario).unwrap(); + assert_eq!(forward(&mut engine, "x"), "true"); +} + +#[test] +fn bigint_function_conversion_from_null() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + let scenario = r#" + var x = false; + try { + BigInt(null); + } catch (e) { + x = true; + } + "#; + forward_val(&mut engine, scenario).unwrap(); + assert_eq!(forward(&mut engine, "x"), "true"); +} + +#[test] +fn bigint_function_conversion_from_undefined() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + let scenario = r#" + var x = false; + try { + BigInt(undefined); + } catch (e) { + x = true; + } + "#; + forward_val(&mut engine, scenario).unwrap(); + assert_eq!(forward(&mut engine, "x"), "true"); +} + +#[test] +fn bigint_function_conversion_from_string() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + assert_eq!(forward(&mut engine, "BigInt('')"), "0n"); + assert_eq!( + forward(&mut engine, "BigInt('200000000000000000')"), + "200000000000000000n" + ); + assert_eq!( + forward(&mut engine, "BigInt('1000000000000000000000000000000000')"), + "1000000000000000000000000000000000n" + ); +} + #[test] fn add() { let realm = Realm::create(); @@ -138,10 +215,7 @@ fn to_string() { let mut engine = Interpreter::new(realm); assert_eq!(forward(&mut engine, "1000n.toString()"), "1000"); - assert_eq!(forward(&mut engine, "1000n.toString(2)"), "1111101000"); - assert_eq!(forward(&mut engine, "255n.toString(16)"), "ff"); - assert_eq!(forward(&mut engine, "1000n.toString(36)"), "rs"); } diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index a09c98904cb..64ee6e4d5fc 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -1,4 +1,4 @@ -use crate::{exec, exec::Interpreter, forward, realm::Realm}; +use crate::{builtins::Value, exec, exec::Interpreter, forward, realm::Realm}; #[test] fn empty_let_decl_undefined() { @@ -753,3 +753,27 @@ fn function_decl_hoisting() { "#; assert_eq!(&exec(scenario), "5"); } + +#[test] +fn to_bigint() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + assert!(engine.to_bigint(&Value::null()).is_err()); + assert!(engine.to_bigint(&Value::undefined()).is_err()); + assert!(engine.to_bigint(&Value::integer(55)).is_ok()); + assert!(engine.to_bigint(&Value::rational(10.0)).is_ok()); + assert!(engine.to_bigint(&Value::string("100")).is_ok()); +} + +#[test] +fn to_string() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + assert_eq!(engine.to_string(&Value::null()).unwrap(), "null"); + assert_eq!(engine.to_string(&Value::undefined()).unwrap(), "undefined"); + assert_eq!(engine.to_string(&Value::integer(55)).unwrap(), "55"); + assert_eq!(engine.to_string(&Value::rational(55.0)).unwrap(), "55"); + assert_eq!(engine.to_string(&Value::string("hello")).unwrap(), "hello"); +}