Skip to content

Commit

Permalink
More consistent test optimization levels (#1317)
Browse files Browse the repository at this point in the history
Ensure that more tests check multiple optimization levels and that they do it in a consistent way.

* use Utils.runWithAllOptimizationLevels for the ES6 tests
* use Utils.runWithAllOptimizationLevels for the ES2022 tests
* support the system property TEST_OPTLEVEL for more tests
use Utils.runWithAllOptimizationLevels at more places
* spotless
  • Loading branch information
rbri authored Apr 25, 2023
1 parent 4386f6a commit 6387bac
Show file tree
Hide file tree
Showing 17 changed files with 1,457 additions and 1,072 deletions.
95 changes: 60 additions & 35 deletions testsrc/org/mozilla/javascript/tests/Bug482203Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package org.mozilla.javascript.tests;

import java.io.IOException;
import java.io.InputStreamReader;
import junit.framework.TestCase;
import org.mozilla.javascript.Callable;
Expand All @@ -15,46 +16,70 @@
public class Bug482203Test extends TestCase {

public void testJsApi() throws Exception {
try (Context cx = Context.enter()) {
cx.setOptimizationLevel(-1);
InputStreamReader in =
new InputStreamReader(Bug482203Test.class.getResourceAsStream("Bug482203.js"));
Script script = cx.compileReader(in, "", 1, null);
Scriptable scope = cx.initStandardObjects();
script.exec(cx, scope);
int counter = 0;
for (; ; ) {
Object cont = ScriptableObject.getProperty(scope, "c");
if (cont == null) {
break;
}
counter++;
((Callable) cont).call(cx, scope, scope, new Object[] {null});
}
assertEquals(counter, 5);
assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
}
Utils.runWithAllOptimizationLevels(
cx -> {
Scriptable scope = cx.initStandardObjects();

try {
InputStreamReader in =
new InputStreamReader(
Bug482203Test.class.getResourceAsStream("Bug482203.js"));

Script script = cx.compileReader(in, "", 1, null);
script.exec(cx, scope);
int counter = 0;
for (; ; ) {
Object cont = ScriptableObject.getProperty(scope, "c");
if (cont == null) {
break;
}
counter++;
((Callable) cont).call(cx, scope, scope, new Object[] {null});
}
assertEquals(counter, 5);
assertEquals(
Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
} catch (IOException e) {
fail(e.getMessage());
}

return null;
});
}

public void testJavaApi() throws Exception {
Utils.runWithAllOptimizationLevels(
cx -> {
Scriptable scope = cx.initStandardObjects();

try {
InputStreamReader in =
new InputStreamReader(
Bug482203Test.class.getResourceAsStream("Bug482203.js"));

Script script = cx.compileReader(in, "", 1, null);
cx.executeScriptWithContinuations(script, scope);
int counter = 0;
for (; ; ) {
Object cont = ScriptableObject.getProperty(scope, "c");
if (cont == null) {
break;
}
counter++;
cx.resumeContinuation(cont, scope, null);
}
assertEquals(counter, 5);
assertEquals(
Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
} catch (IOException e) {
fail(e.getMessage());
}

return null;
});

try (Context cx = Context.enter()) {
cx.setOptimizationLevel(-1);
InputStreamReader in =
new InputStreamReader(Bug482203Test.class.getResourceAsStream("Bug482203.js"));
Script script = cx.compileReader(in, "", 1, null);
Scriptable scope = cx.initStandardObjects();
cx.executeScriptWithContinuations(script, scope);
int counter = 0;
for (; ; ) {
Object cont = ScriptableObject.getProperty(scope, "c");
if (cont == null) {
break;
}
counter++;
cx.resumeContinuation(cont, scope, null);
}
assertEquals(counter, 5);
assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
}
}
}
28 changes: 13 additions & 15 deletions testsrc/org/mozilla/javascript/tests/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,38 @@
*/
public class Utils {
/** The default set of levels to run tests at. */
public static final int[] DEFAULT_OPT_LEVELS = new int[] {-1, 0, 9};
public static final int[] DEFAULT_OPT_LEVELS = new int[] {-1};

/** Runs the action successively with all available optimization levels */
public static void runWithAllOptimizationLevels(final ContextAction action) {
runWithOptimizationLevel(action, -1);
runWithOptimizationLevel(action, 0);
runWithOptimizationLevel(action, 1);
public static void runWithAllOptimizationLevels(final ContextAction<?> action) {
for (int level : getTestOptLevels()) {
runWithOptimizationLevel(action, level);
}
}

/** Runs the action successively with all available optimization levels */
public static void runWithAllOptimizationLevels(
final ContextFactory contextFactory, final ContextAction action) {
runWithOptimizationLevel(contextFactory, action, -1);
runWithOptimizationLevel(contextFactory, action, 0);
runWithOptimizationLevel(contextFactory, action, 1);
final ContextFactory contextFactory, final ContextAction<?> action) {
for (int level : getTestOptLevels()) {
runWithOptimizationLevel(contextFactory, action, level);
}
}

/** Runs the provided action at the given optimization level */
public static void runWithOptimizationLevel(
final ContextAction action, final int optimizationLevel) {
final ContextAction<?> action, final int optimizationLevel) {
runWithOptimizationLevel(new ContextFactory(), action, optimizationLevel);
}

/** Runs the provided action at the given optimization level */
public static void runWithOptimizationLevel(
final ContextFactory contextFactory,
final ContextAction action,
final ContextAction<?> action,
final int optimizationLevel) {
final Context cx = contextFactory.enterContext();
try {

try (final Context cx = contextFactory.enterContext()) {
cx.setOptimizationLevel(optimizationLevel);
action.run(cx);
} finally {
Context.exit();
}
}

Expand Down
Loading

0 comments on commit 6387bac

Please sign in to comment.