|
|
|
@ -219,3 +219,151 @@ fn to_string() {
|
|
|
|
|
assert_eq!(forward(&mut engine, "255n.toString(16)"), "ff"); |
|
|
|
|
assert_eq!(forward(&mut engine, "1000n.toString(36)"), "rs"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn as_int_n() { |
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asIntN(0, 1n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asIntN(1, 1n)"), "-1n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asIntN(3, 10n)"), "2n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asIntN({}, 1n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asIntN(2, 0n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asIntN(2, -0n)"), "0n"); |
|
|
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asIntN(2, -123456789012345678901n)"), |
|
|
|
|
"-1n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asIntN(2, -123456789012345678900n)"), |
|
|
|
|
"0n" |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asIntN(2, 123456789012345678900n)"), |
|
|
|
|
"0n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asIntN(2, 123456789012345678901n)"), |
|
|
|
|
"1n" |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
forward( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asIntN(200, 0xcffffffffffffffffffffffffffffffffffffffffffffffffffn)" |
|
|
|
|
), |
|
|
|
|
"-1n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asIntN(201, 0xcffffffffffffffffffffffffffffffffffffffffffffffffffn)" |
|
|
|
|
), |
|
|
|
|
"1606938044258990275541962092341162602522202993782792835301375n" |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
forward( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asIntN(200, 0xc89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n)" |
|
|
|
|
), |
|
|
|
|
"-741470203160010616172516490008037905920749803227695190508460n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asIntN(201, 0xc89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n)" |
|
|
|
|
), |
|
|
|
|
"865467841098979659369445602333124696601453190555097644792916n" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn as_int_n_errors() { |
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
assert_throws(&mut engine, "BigInt.asIntN(-1, 0n)", "RangeError"); |
|
|
|
|
assert_throws(&mut engine, "BigInt.asIntN(-2.5, 0n)", "RangeError"); |
|
|
|
|
assert_throws( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asIntN(9007199254740992, 0n)", |
|
|
|
|
"RangeError", |
|
|
|
|
); |
|
|
|
|
assert_throws(&mut engine, "BigInt.asIntN(0n, 0n)", "TypeError"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn as_uint_n() { |
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(0, -2n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(0, -1n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(0, 0n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(0, 1n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(0, 2n)"), "0n"); |
|
|
|
|
|
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, -3n)"), "1n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, -2n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, -1n)"), "1n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, 0n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, 1n)"), "1n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, 2n)"), "0n"); |
|
|
|
|
assert_eq!(forward(&mut engine, "BigInt.asUintN(1, 3n)"), "1n"); |
|
|
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asUintN(1, -123456789012345678901n)"), |
|
|
|
|
"1n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asUintN(1, -123456789012345678900n)"), |
|
|
|
|
"0n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asUintN(1, 123456789012345678900n)"), |
|
|
|
|
"0n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward(&mut engine, "BigInt.asUintN(1, 123456789012345678901n)"), |
|
|
|
|
"1n" |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
forward( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asUintN(200, 0xbffffffffffffffffffffffffffffffffffffffffffffffffffn)" |
|
|
|
|
), |
|
|
|
|
"1606938044258990275541962092341162602522202993782792835301375n" |
|
|
|
|
); |
|
|
|
|
assert_eq!( |
|
|
|
|
forward( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asUintN(201, 0xbffffffffffffffffffffffffffffffffffffffffffffffffffn)" |
|
|
|
|
), |
|
|
|
|
"3213876088517980551083924184682325205044405987565585670602751n" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn as_uint_n_errors() { |
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
assert_throws(&mut engine, "BigInt.asUintN(-1, 0n)", "RangeError"); |
|
|
|
|
assert_throws(&mut engine, "BigInt.asUintN(-2.5, 0n)", "RangeError"); |
|
|
|
|
assert_throws( |
|
|
|
|
&mut engine, |
|
|
|
|
"BigInt.asUintN(9007199254740992, 0n)", |
|
|
|
|
"RangeError", |
|
|
|
|
); |
|
|
|
|
assert_throws(&mut engine, "BigInt.asUintN(0n, 0n)", "TypeError"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn assert_throws(engine: &mut Interpreter, src: &str, error_type: &str) { |
|
|
|
|
let result = forward(engine, src); |
|
|
|
|
assert!(result.contains(error_type)); |
|
|
|
|
} |
|
|
|
|