use super::*;
fn signed_zero(source: &str) -> String {
run(&format!("var v = {source}; v + '|' + (1 / v)"))
}
#[test]
fn the_constants_are_what_they_are_and_cannot_be_moved() {
assert_eq!(run("Math.PI"), "3.141592653589793");
assert_eq!(run("Math.E"), "2.718281828459045");
assert_eq!(run("Math.LN10"), "2.302585092994046");
assert_eq!(run("Math.LN2"), "0.6931471805599453");
assert_eq!(run("Math.LOG10E"), "0.4342944819032518");
assert_eq!(run("Math.LOG2E"), "1.4426950408889634");
assert_eq!(run("Math.SQRT1_2"), "0.7071067811865476");
assert_eq!(run("Math.SQRT2"), "1.4142135623730951");
assert_eq!(run("Math.PI = 3; Math.PI"), "3.141592653589793");
assert_eq!(
run("var d = Object.getOwnPropertyDescriptor(Math, 'PI'); \
d.writable + ',' + d.enumerable + ',' + d.configurable"),
"false,false,false"
);
assert_eq!(run("typeof Math"), "object");
assert_eq!(run("try { Math(); } catch (e) { e.name }"), "TypeError");
assert_eq!(run("try { new Math(); } catch (e) { e.name }"), "TypeError");
}
#[test]
fn a_half_rounds_upwards_and_not_away_from_zero() {
assert_eq!(run("Math.round(0.5)"), "1");
assert_eq!(run("Math.round(1.5)"), "2");
assert_eq!(run("Math.round(2.5)"), "3");
assert_eq!(run("Math.round(-0.5)"), "0");
assert_eq!(run("Math.round(-1.5)"), "-1");
assert_eq!(run("Math.round(-2.5)"), "-2");
assert_eq!(run("Math.round(0.4)"), "0");
assert_eq!(run("Math.round(-0.6)"), "-1");
assert_eq!(signed_zero("Math.round(-0.4)"), "0|-Infinity");
assert_eq!(signed_zero("Math.round(-0.5)"), "0|-Infinity");
assert_eq!(signed_zero("Math.round(-0)"), "0|-Infinity");
assert_eq!(signed_zero("Math.round(0.4)"), "0|Infinity");
assert_eq!(run("Math.round(4503599627370497)"), "4503599627370497");
assert_eq!(run("Math.round(0.49999999999999994)"), "0");
assert_eq!(
run("Math.round(NaN) + ',' + Math.round(Infinity) + ',' + Math.round(-Infinity)"),
"NaN,Infinity,-Infinity"
);
assert_eq!(run("Math.round(0.49999999999999994)"), "0");
assert_eq!(run("Math.round(0.5)"), "1");
assert_eq!(run("Math.round(-0.5)"), "0");
assert_eq!(run("Math.round(-0.5000000000000001)"), "-1");
assert_eq!(run("Math.round(1)"), "1");
assert_eq!(run("Math.round(-1)"), "-1");
assert_eq!(signed_zero("Math.round(0)"), "0|Infinity");
}
#[test]
fn the_roundings_and_the_sign_keep_a_negative_zero() {
assert_eq!(signed_zero("Math.ceil(-0.5)"), "0|-Infinity");
assert_eq!(signed_zero("Math.trunc(-0.5)"), "0|-Infinity");
assert_eq!(signed_zero("Math.floor(-0)"), "0|-Infinity");
assert_eq!(signed_zero("Math.ceil(0.5)"), "1|1");
assert_eq!(signed_zero("Math.sign(-0)"), "0|-Infinity");
assert_eq!(signed_zero("Math.sign(0)"), "0|Infinity");
assert_eq!(
run("Math.sign(-3) + ',' + Math.sign(3) + ',' + Math.sign(NaN)"),
"-1,1,NaN"
);
assert_eq!(signed_zero("Math.abs(-0)"), "0|Infinity");
}
#[test]
fn min_and_max_propagate_a_nan_and_can_tell_the_zeros_apart() {
assert_eq!(run("Math.max(1, 2, 3) + ',' + Math.min(1, 2, 3)"), "3,1");
assert_eq!(run("Math.max(-1, -2) + ',' + Math.min(-1, -2)"), "-1,-2");
assert_eq!(run("Math.max(1, NaN) + ',' + Math.max(NaN, 1)"), "NaN,NaN");
assert_eq!(run("Math.min(1, NaN) + ',' + Math.min(NaN, 1)"), "NaN,NaN");
assert_eq!(signed_zero("Math.max(0, -0)"), "0|Infinity");
assert_eq!(signed_zero("Math.max(-0, 0)"), "0|Infinity");
assert_eq!(signed_zero("Math.min(0, -0)"), "0|-Infinity");
assert_eq!(signed_zero("Math.min(-0, 0)"), "0|-Infinity");
assert_eq!(run("Math.max()"), "-Infinity");
assert_eq!(run("Math.min()"), "Infinity");
assert_eq!(run("Math.max(-5) + ',' + Math.min(-5)"), "-5,-5");
assert_eq!(run("Math.max(2, 2) + ',' + Math.min(2, 2)"), "2,2");
assert_eq!(signed_zero("Math.max(-0, -0)"), "0|-Infinity");
assert_eq!(signed_zero("Math.min(0, 0)"), "0|Infinity");
assert_eq!(run("Math.max(0, 1) + ',' + Math.max(1, 0)"), "1,1");
assert_eq!(run("Math.min(0, -1) + ',' + Math.min(-1, 0)"), "-1,-1");
}
#[test]
fn the_integer_operations_are_exact() {
assert_eq!(run("Math.clz32(0)"), "32");
assert_eq!(run("Math.clz32(1)"), "31");
assert_eq!(run("Math.clz32(4294967295)"), "0");
assert_eq!(run("Math.clz32(-1)"), "0");
assert_eq!(run("Math.imul(3, 4)"), "12");
assert_eq!(run("Math.imul(-5, 12)"), "-60");
assert_eq!(run("Math.imul(4294967295, 5)"), "-5");
assert_eq!(run("Math.imul(2, 4294967295)"), "-2");
assert_eq!(run("Math.fround(1.1)"), "1.100000023841858");
assert_eq!(run("Math.fround(1e40)"), "Infinity");
assert_eq!(run("Math.fround(1) + ',' + Math.fround(NaN)"), "1,NaN");
assert_eq!(run("Math.hypot()"), "0");
assert_eq!(run("Math.hypot(3, 4)"), "5");
assert_eq!(
run("Math.hypot(NaN, Infinity) + ',' + Math.hypot(Infinity, NaN)"),
"Infinity,Infinity"
);
assert_eq!(run("Math.hypot(NaN, 1)"), "NaN");
assert_eq!(run("Math.hypot(NaN)"), "NaN");
assert_eq!(run("Math.hypot(0, 0)"), "0");
assert_eq!(run("Math.hypot(-0)"), "0");
assert_eq!(run("Math.hypot(-3)"), "3");
assert_eq!(
run("var v = Math.hypot(1e300, 1e300); (v > 1e300) + ',' + (v < Infinity)"),
"true,true"
);
assert_eq!(run("Math.hypot(1e300, 1e300)"), "1.4142135623730952e+300");
}
#[test]
fn pow_is_the_operator_rather_than_a_second_copy_of_it() {
assert_eq!(run("Math.pow(1, NaN)"), "NaN");
assert_eq!(run("1 ** NaN"), "NaN");
assert_eq!(run("Math.pow(-1, Infinity)"), "NaN");
assert_eq!(run("(-1) ** Infinity"), "NaN");
assert_eq!(run("Math.pow(2, 10)"), "1024");
assert_eq!(run("Math.pow(2, 32) - 1"), "4294967295");
assert_eq!(run("Math.pow(0, 0)"), "1");
}
#[test]
fn a_missing_argument_is_not_an_error_and_random_stays_in_its_interval() {
assert_eq!(run("Math.abs()"), "NaN");
assert_eq!(run("Math.round()"), "NaN");
assert_eq!(run("Math.pow(2)"), "NaN");
assert_eq!(run("Math.atan2(1)"), "NaN");
assert_eq!(
run("var ok = true; for (var i = 0; i < 500; i = i + 1) { \
var r = Math.random(); if (!(r >= 0 && r < 1)) { ok = false; } } ok"),
"true"
);
assert_eq!(run("typeof Math.random()"), "number");
assert_eq!(
run("var same = 0; var first = Math.random(); \
for (var i = 0; i < 50; i = i + 1) { if (Math.random() === first) { same = same + 1; } } \
same < 50"),
"true"
);
}