aboutsummaryrefslogtreecommitdiff
path: root/test/compress/numbers.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-10-27 03:07:07 +0800
committerGitHub <noreply@github.com>2019-10-27 03:07:07 +0800
commit50a578c1f63e61654d71642cbfa35f41da9bd626 (patch)
treedc334902dc1eed057ab9d54a669b41a01126cce2 /test/compress/numbers.js
parent85237b08d46c62a97c9b338c4e62d71cdba76e24 (diff)
downloadtracifyjs-50a578c1f63e61654d71642cbfa35f41da9bd626.tar.gz
tracifyjs-50a578c1f63e61654d71642cbfa35f41da9bd626.zip
compress arithmetic expressions further (#3529)
Diffstat (limited to 'test/compress/numbers.js')
-rw-r--r--test/compress/numbers.js353
1 files changed, 348 insertions, 5 deletions
diff --git a/test/compress/numbers.js b/test/compress/numbers.js
index 000b9ab5..3f579781 100644
--- a/test/compress/numbers.js
+++ b/test/compress/numbers.js
@@ -51,17 +51,20 @@ comparisons: {
comparisons: true,
}
input: {
+ var x = "42", y = "0x30";
console.log(
~x === 42,
- x % n === 42
+ x % y === 42
);
}
expect: {
+ var x = "42", y = "0x30";
console.log(
42 == ~x,
- x % n == 42
+ x % y == 42
);
}
+ expect_stdout: "false true"
}
evaluate_1: {
@@ -103,12 +106,94 @@ evaluate_1: {
}
}
+evaluate_1_unsafe_math: {
+ options = {
+ evaluate: true,
+ unsafe_math: true,
+ }
+ input: {
+ console.log(
+ x + 1 + 2,
+ x * 1 * 2,
+ +x + 1 + 2,
+ 1 + x + 2 + 3,
+ 1 | x | 2 | 3,
+ 1 + x-- + 2 + 3,
+ 1 + (x*y + 2) + 3,
+ 1 + (2 + x + 3),
+ 1 + (2 + ~x + 3),
+ -y + (2 + ~x + 3),
+ 1 & (2 & x & 3),
+ 1 + (2 + (x |= 0) + 3)
+ );
+ }
+ expect: {
+ console.log(
+ x + 1 + 2,
+ 2 * x,
+ +x + 3,
+ 1 + x + 2 + 3,
+ 3 | x,
+ 6 + x--,
+ x*y + 6,
+ 1 + (2 + x + 3),
+ 6 + ~x,
+ 5 - y + ~x,
+ 0 & x,
+ 6 + (x |= 0)
+ );
+ }
+}
+
evaluate_2: {
options = {
evaluate: true,
+ unsafe_math: false,
+ }
+ input: {
+ var x = "42", y = null;
+ console.log(
+ x + 1 + 2,
+ x * 1 * 2,
+ +x + 1 + 2,
+ 1 + x + 2 + 3,
+ 1 | x | 2 | 3,
+ 1 + x-- + 2 + 3,
+ 1 + (x*y + 2) + 3,
+ 1 + (2 + x + 3),
+ 1 + (2 + ~x + 3),
+ -y + (2 + ~x + 3),
+ 1 & (2 & x & 3),
+ 1 + (2 + (x |= 0) + 3)
+ );
+ }
+ expect: {
+ var x = "42", y = null;
+ console.log(
+ x + 1 + 2,
+ 1 * x * 2,
+ +x + 1 + 2,
+ 1 + x + 2 + 3,
+ 3 | x,
+ 1 + x-- + 2 + 3,
+ x*y + 2 + 1 + 3,
+ 1 + (2 + x + 3),
+ 2 + ~x + 3 + 1,
+ 2 + ~x + 3 - y,
+ 0 & x,
+ 2 + (x |= 0) + 3 + 1
+ );
+ }
+ expect_stdout: "4212 84 45 14223 43 48 6 47 -36 -37 0 47"
+}
+
+evaluate_2_unsafe_math: {
+ options = {
+ evaluate: true,
unsafe_math: true,
}
input: {
+ var x = "42", y = null;
console.log(
x + 1 + 2,
x * 1 * 2,
@@ -118,24 +203,30 @@ evaluate_2: {
1 + x-- + 2 + 3,
1 + (x*y + 2) + 3,
1 + (2 + x + 3),
+ 1 + (2 + ~x + 3),
+ -y + (2 + ~x + 3),
1 & (2 & x & 3),
1 + (2 + (x |= 0) + 3)
);
}
expect: {
+ var x = "42", y = null;
console.log(
x + 1 + 2,
2 * x,
- 3 + +x,
+ +x + 3,
1 + x + 2 + 3,
3 | x,
6 + x--,
- 6 + x*y,
+ x*y + 6,
1 + (2 + x + 3),
+ 6 + ~x,
+ 5 + ~x - y,
0 & x,
6 + (x |= 0)
);
}
+ expect_stdout: "4212 84 45 14223 43 48 6 47 -36 -37 0 47"
}
evaluate_3: {
@@ -148,7 +239,7 @@ evaluate_3: {
console.log(1 + Number(x) + 2);
}
expect: {
- console.log(3 + +x);
+ console.log(+x + 3);
}
}
@@ -182,6 +273,258 @@ evaluate_4: {
}
}
+evaluate_5: {
+ options = {
+ evaluate: true,
+ unsafe_math: false,
+ }
+ input: {
+ var a = true;
+ console.log(
+ +a + 2 + 3,
+ +a + 2 - 3,
+ +a - 2 + 3,
+ +a - 2 - 3,
+ 2 + +a + 3,
+ 2 + +a - 3,
+ 2 - +a + 3,
+ 2 - +a - 3,
+ 2 + 3 + +a,
+ 2 + 3 - +a,
+ 2 - 3 + +a,
+ 2 - 3 - +a
+ );
+ }
+ expect: {
+ var a = true;
+ console.log(
+ +a + 2 + 3,
+ +a + 2 - 3,
+ a - 2 + 3,
+ a - 2 - 3,
+ +a + 2 + 3,
+ +a + 2 - 3,
+ 2 - a + 3,
+ 2 - a - 3,
+ +a + 5,
+ 5 - a,
+ +a - 1,
+ -1 - a
+ );
+ }
+ expect_stdout: "6 0 2 -4 6 0 4 -2 6 4 0 -2"
+}
+
+evaluate_5_unsafe_math: {
+ options = {
+ evaluate: true,
+ unsafe_math: true,
+ }
+ input: {
+ var a = true;
+ console.log(
+ +a + 2 + 3,
+ +a + 2 - 3,
+ +a - 2 + 3,
+ +a - 2 - 3,
+ 2 + +a + 3,
+ 2 + +a - 3,
+ 2 - +a + 3,
+ 2 - +a - 3,
+ 2 + 3 + +a,
+ 2 + 3 - +a,
+ 2 - 3 + +a,
+ 2 - 3 - +a
+ );
+ }
+ expect: {
+ var a = true;
+ console.log(
+ +a + 5,
+ +a + -1,
+ a - -1,
+ a - 5,
+ +a + 5,
+ +a + -1,
+ 5 - a,
+ -1 - a,
+ +a + 5,
+ 5 - a,
+ +a - 1,
+ -1 - a
+ );
+ }
+ expect_stdout: "6 0 2 -4 6 0 4 -2 6 4 0 -2"
+}
+
+evaluate_6: {
+ options = {
+ evaluate: true,
+ unsafe_math: false,
+ }
+ input: {
+ var a = true;
+ console.log(
+ -a + 2 + 3,
+ -a + 2 - 3,
+ -a - 2 + 3,
+ -a - 2 - 3,
+ 2 + -a + 3,
+ 2 + -a - 3,
+ 2 - -a + 3,
+ 2 - -a - 3,
+ 2 + 3 + -a,
+ 2 + 3 - -a,
+ 2 - 3 + -a,
+ 2 - 3 - -a
+ );
+ }
+ expect: {
+ var a = true;
+ console.log(
+ 2 - a + 3,
+ 2 - a - 3,
+ -a - 2 + 3,
+ -a - 2 - 3,
+ 2 - a + 3,
+ 2 - a - 3,
+ 2 + a + 3,
+ 2 + a - 3,
+ 5 - a,
+ 5 + a,
+ -1 - a,
+ -1 + a
+ );
+ }
+ expect_stdout: "4 -2 0 -6 4 -2 6 0 4 6 -2 0"
+}
+
+evaluate_6_unsafe_math: {
+ options = {
+ evaluate: true,
+ unsafe_math: true,
+ }
+ input: {
+ var a = true;
+ console.log(
+ -a + 2 + 3,
+ -a + 2 - 3,
+ -a - 2 + 3,
+ -a - 2 - 3,
+ 2 + -a + 3,
+ 2 + -a - 3,
+ 2 - -a + 3,
+ 2 - -a - 3,
+ 2 + 3 + -a,
+ 2 + 3 - -a,
+ 2 - 3 + -a,
+ 2 - 3 - -a
+ );
+ }
+ expect: {
+ var a = true;
+ console.log(
+ 5 - a,
+ -1 - a,
+ -a - -1,
+ -a - 5,
+ 5 - a,
+ -1 - a,
+ 2 + a + 3,
+ -1 + a,
+ 5 - a,
+ 5 + a,
+ -1 - a,
+ -1 + a
+ );
+ }
+ expect_stdout: "4 -2 0 -6 4 -2 6 0 4 6 -2 0"
+}
+
+evaluate_7: {
+ options = {
+ evaluate: true,
+ unsafe_math: false,
+ }
+ input: {
+ var x = "42", y;
+ console.log(
+ +x + 2 + (3 + !y),
+ +x + 2 + (3 - !y),
+ +x + 2 - (3 + !y),
+ +x + 2 - (3 - !y),
+ +x - 2 + (3 + !y),
+ +x - 2 + (3 - !y),
+ +x - 2 - (3 + !y),
+ +x - 2 - (3 - !y)
+ );
+ }
+ expect: {
+ var x = "42", y;
+ console.log(
+ +x + 2 + (3 + !y),
+ +x + 2 + (3 - !y),
+ +x + 2 - (3 + !y),
+ +x + 2 - (3 - !y),
+ x - 2 + (3 + !y),
+ x - 2 + (3 - !y),
+ x - 2 - (3 + !y),
+ x - 2 - (3 - !y)
+ );
+ }
+ expect_stdout: "48 46 40 42 44 42 36 38"
+}
+
+evaluate_7_unsafe_math: {
+ options = {
+ evaluate: true,
+ unsafe_math: true,
+ }
+ input: {
+ var x = "42", y;
+ console.log(
+ +x + 2 + (3 + !y),
+ +x + 2 + (3 - !y),
+ +x + 2 - (3 + !y),
+ +x + 2 - (3 - !y),
+ +x - 2 + (3 + !y),
+ +x - 2 + (3 - !y),
+ +x - 2 - (3 + !y),
+ +x - 2 - (3 - !y)
+ );
+ }
+ expect: {
+ var x = "42", y;
+ console.log(
+ +x + 5 + !y,
+ +x + 5 - !y,
+ +x + -1 - !y,
+ +x + -1 + !y,
+ x - -1 + !y,
+ x - -1 - !y,
+ x - 5 - !y,
+ x - 5 + !y
+ );
+ }
+ expect_stdout: "48 46 40 42 44 42 36 38"
+}
+
+NaN_redefined: {
+ options = {
+ evaluate: true,
+ unsafe_math: true,
+ }
+ input: {
+ var NaN;
+ console.log(1 / (0 / 0));
+ }
+ expect: {
+ var NaN;
+ console.log(0 / 0);
+ }
+ expect_stdout: "NaN"
+}
+
issue_1710: {
options = {
evaluate: true,