Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/Bug782363Test.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.tests;
6
7 import static java.lang.String.format;
8 import static java.util.Arrays.asList;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.IOException;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mozilla.javascript.CompilerEnvirons;
23 import org.mozilla.javascript.Context;
24 import org.mozilla.javascript.EvaluatorException;
25 import org.mozilla.javascript.IRFactory;
26 import org.mozilla.javascript.Parser;
27 import org.mozilla.javascript.ScriptableObject;
28 import org.mozilla.javascript.ast.AstRoot;
29 import org.mozilla.javascript.ast.FunctionNode;
30 import org.mozilla.javascript.ast.ScriptNode;
31 import org.mozilla.javascript.optimizer.Codegen;
32 import org.mozilla.javascript.optimizer.OptFunctionNode;
33
34 /**
35 * @author André Bargull
36 *
37 */
38 public class Bug782363Test {
39 private Context cx;
40
41 @Before
42 public void setUp() {
43 cx = Context.enter();
44 cx.setLanguageVersion(Context.VERSION_1_8);
45 cx.setOptimizationLevel(9);
46 }
47
48 @After
49 public void tearDown() {
50 Context.exit();
51 }
52
53 /**
54 * Compiles {@code source} and returns the transformed and optimized
55 * {@link ScriptNode}
56 */
57 protected ScriptNode compile(CharSequence source) {
58 final String mainMethodClassName = "Main";
59 final String scriptClassName = "Main";
60
61 CompilerEnvirons compilerEnv = new CompilerEnvirons();
62 compilerEnv.initFromContext(cx);
63 Parser p = new Parser(compilerEnv);
64 AstRoot ast = p.parse(source.toString(), "<eval>", 1);
65 IRFactory irf = new IRFactory(compilerEnv);
66 ScriptNode tree = irf.transformTree(ast);
67
68 Codegen codegen = new Codegen();
69 codegen.setMainMethodClass(mainMethodClassName);
70 codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(),
71 false);
72
73 return tree;
74 }
75
76 /**
77 * Checks every variable {@code v} in {@code source} is marked as a
78 * number-variable iff {@code numbers} contains {@code v}
79 */
80 protected void assertNumberVars(CharSequence source, String... numbers) {
81 // wrap source in function
82 ScriptNode tree = compile("function f(){" + source + "}");
83
84 FunctionNode fnode = tree.getFunctionNode(0);
85 assertNotNull(fnode);
86 OptFunctionNode opt = OptFunctionNode.get(fnode);
87 assertNotNull(opt);
88 assertSame(fnode, opt.fnode);
89
90 for (int i = 0, c = fnode.getParamCount(); i < c; ++i) {
91 assertTrue(opt.isParameter(i));
92 assertFalse(opt.isNumberVar(i));
93 }
94
95 Set<String> set = new HashSet<String>(asList(numbers));
96 for (int i = fnode.getParamCount(), c = fnode.getParamAndVarCount(); i < c; ++i) {
97 assertFalse(opt.isParameter(i));
98 String name = fnode.getParamOrVarName(i);
99 String msg = format("{%s -> number? = %b}", name, opt.isNumberVar(i));
100 assertEquals(msg, set.contains(name), opt.isNumberVar(i));
101 }
102 }
103
104 @Test
105 public void testConst() {
106 assertNumberVars("const a");
107 assertNumberVars("const a=0", "a");
108 assertNumberVars("const a; a=0");
109 // inc/dec
110 assertNumberVars("const a; a++");
111 assertNumberVars("const a=0; a++", "a");
112 assertNumberVars("const a; a=0; a++");
113 // used before defined
114 assertNumberVars("a; const a");
115 assertNumberVars("a; const a=0");
116 assertNumberVars("a; const a; a=0");
117 // re-assignment
118 assertNumberVars("const a=0; a=1", "a");
119 assertNumberVars("const a=0; a='z'", "a");
120 assertNumberVars("const a='z'; a=1");
121 }
122
123 @Test
124 public void testMaxLocals() throws IOException {
125 test(339);
126 try {
127 test(340);
128 } catch (EvaluatorException e) {
129 // may fail with 'out of locals' exception
130 }
131 }
132
133 private void test(int variables) {
134 double expected = (variables * (variables - 1)) / 2d;
135
136 StringBuilder sb = new StringBuilder();
137 sb.append("function F (){\n");
138 for (int i = 0; i < variables; ++i) {
139 sb.append("const x_").append(i).append("=").append(i).append(";");
140 }
141 sb.append("return 0");
142 for (int i = 0; i < variables; ++i) {
143 sb.append("+").append("x_").append(i);
144 }
145 sb.append("}; F()");
146
147 ScriptableObject scope = cx.initStandardObjects();
148 Object ret = cx.evaluateString(scope, sb.toString(), "<eval>", 1, null);
149 assertTrue(ret instanceof Number);
150 assertEquals(expected, ((Number) ret).doubleValue(), 0);
151 }
152 }