Skip to content

Commit

Permalink
update tests to latest StdLib
Browse files Browse the repository at this point in the history
- added missing natives to run StdLib initializers in unit tests
- run StdLib tests with compiletime translation enabled
  • Loading branch information
peq committed Jan 3, 2020
1 parent 6961d2a commit 05650e5
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.peeeq.wurstscript.intermediatelang.interpreter.ILStackFrame;
import de.peeeq.wurstscript.jassAst.JassProg;
import de.peeeq.wurstscript.jassprinter.JassPrinter;
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
import de.peeeq.wurstscript.utils.Utils;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -80,7 +81,7 @@ public CompilationProcess(WurstGui gui, RunArgs runArgs) {

if (runArgs.isRunTests()) {
timeTaker.measure("Run tests",
() -> runTests(compiler));
() -> runTests(compiler.getImTranslator(), compiler));
}

timeTaker.measure("Run compiletime functions",
Expand Down Expand Up @@ -145,7 +146,7 @@ private File writeMapscript(CharSequence mapScript) {
}
}

private void runTests(WurstCompilerJassImpl compiler) {
private void runTests(ImTranslator translator, WurstCompilerJassImpl compiler) {
PrintStream out = System.out;
// tests
gui.sendProgress("Running tests");
Expand All @@ -156,7 +157,7 @@ protected void print(String message) {
out.print(message);
}
};
runTests.runTests(compiler.getImProg(), null, null);
runTests.runTests(translator, compiler.getImProg(), null, null);

for (RunTests.TestFailure e : runTests.getFailTests()) {
gui.sendError(new CompileError(e.getFunction(), e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,13 @@ private ImExpr constantToExprHashtable(Element trace, ImVar htVar, IlConstHandle
ILconstBool iv = (ILconstBool) v;
ImFunction SaveBoolean = findNative("SaveBoolean", errorPos);
addCompiletimeStateInit(JassIm.ImFunctionCall(trace, SaveBoolean, JassIm.ImTypeArguments(), JassIm.ImExprs(
JassIm.ImVarAccess(htVar),
JassIm.ImIntVal(key.getParentkey()),
JassIm.ImIntVal(key.getChildkey()),
JassIm.ImBoolVal(iv.getVal())
JassIm.ImVarAccess(htVar),
JassIm.ImIntVal(key.getParentkey()),
JassIm.ImIntVal(key.getChildkey()),
JassIm.ImBoolVal(iv.getVal())
), false, CallType.NORMAL));
} else if (v instanceof ILconstNull) {
// treat null like no entry
} else {
throw new CompileError(errorPos, "Unsupported value stored in HashMap: " + v + " // " + v.getClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public void visit(ImCompiletimeExpr e) {
});
}

private ImTranslator getImTranslator() {
public ImTranslator getImTranslator() {
final ImTranslator t = imTranslator;
if (t != null) {
return t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;
import de.peeeq.wurstscript.intermediatelang.interpreter.NativesProvider;
import de.peeeq.wurstscript.intermediatelang.interpreter.NoSuchNativeException;
import de.peeeq.wurstscript.utils.Utils;

import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -42,6 +43,7 @@ public ReflectionNativeProvider(AbstractInterpreter interpreter) {
addProvider(new RegionProvider(interpreter));
addProvider(new ImageProvider(interpreter));
addProvider(new IntegerProvider(interpreter));
addProvider(new FrameProvider(interpreter));
}

public NativeJassFunction getFunctionPair(String funcName) {
Expand All @@ -68,7 +70,7 @@ private void addProvider(Provider provider) {
@Override
public ILconst invoke(String funcname, ILconst[] args) throws NoSuchNativeException {
String msg = "Calling method " + funcname + "(" +
Arrays.stream(args).map(Object::toString).collect(Collectors.joining(", ")) + ")";
Utils.printSep(", ", args) + ")";
WLogger.trace(msg);

NativeJassFunction candidate = methodMap.get(funcname);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.peeeq.wurstio.jassinterpreter.providers;

import de.peeeq.wurstio.jassinterpreter.mocks.RectMock;
import de.peeeq.wurstscript.intermediatelang.*;
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;

/**
* Some functions for frames, so that StdLib tests work.
*/
public class FrameProvider extends Provider {


public FrameProvider(AbstractInterpreter interpreter) {
super(interpreter);
}

public IlConstHandle BlzGetOriginFrame(IlConstHandle frameType, ILconstInt index) {
return new IlConstHandle("framehandle", new FrameHandle());
}

public IlConstHandle BlzCreateFrameByType(ILconstString typeName, ILconstString name, IlConstHandle owner, ILconstString inherits, ILconstInt createContext) {
return new IlConstHandle("framehandle", new FrameHandle());
}

public void BlzFrameSetSize(IlConstHandle frame, ILconstReal width, ILconstReal height) {
}

static class FrameHandle {
}


public IlConstHandle ConvertOriginFrameType(ILconstInt i) {
return new IlConstHandle("frameType", i.getVal());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ public Object execute(ModelManager modelManager) {
WLogger.info("test.funcToTest = " + Utils.printElement(funcToTest));


ImProg imProg = translateProg(modelManager);
ImTranslator translator = translateProg(modelManager);
ImProg imProg = translator.getImProg();
if (imProg == null) {
println("Could not run tests, because program did not compile.\n");
return "Could not translate program";
}

runTests(imProg, funcToTest, cu);
runTests(translator, imProg, funcToTest, cu);
return "ok";
}

Expand All @@ -136,12 +137,10 @@ public int getTotalTests() {

}

public TestResult runTests(ImProg imProg, @Nullable FuncDef funcToTest, @Nullable CompilationUnit cu) {
public TestResult runTests(ImTranslator translator, ImProg imProg, @Nullable FuncDef funcToTest, @Nullable CompilationUnit cu) {
WurstGui gui = new TestGui();

// dummy translator
ImTranslator tr = new ImTranslator(imProg.getTrace().getModel(), false, RunArgs.defaults());
CompiletimeFunctionRunner cfr = new CompiletimeFunctionRunner(tr, imProg, null, null, gui, CompiletimeFunctions);
CompiletimeFunctionRunner cfr = new CompiletimeFunctionRunner(translator, imProg, null, null, gui, CompiletimeFunctions);
ILInterpreter interpreter = cfr.getInterpreter();
ProgramState globalState = cfr.getGlobalState();
if (globalState == null) {
Expand Down Expand Up @@ -299,11 +298,13 @@ protected void print(String message) {
System.err.print(message);
}

private ImProg translateProg(ModelManager modelManager) {
ImTranslator imTranslator = new ImTranslator(modelManager.getModel(), false, new RunArgs());
private ImTranslator translateProg(ModelManager modelManager) {
// need to run compiletime functions for running unit tests
RunArgs runArgs = new RunArgs("-runcompiletimefunctions");
ImTranslator imTranslator = new ImTranslator(modelManager.getModel(), false, runArgs);
// will ignore udg_ variables which are not found
imTranslator.setEclipseMode(true);
return imTranslator.translateProg();
return imTranslator;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public boolean equals(@Nullable Object other) {

@Override
public int hashCode() {
throw new Error("hashcode not implemented");
throw new Error("hashcode not implemented for " + getClass() + "/" + this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.peeeq.wurstscript.types.WurstType;
import de.peeeq.wurstscript.types.WurstTypeInt;

import java.util.Objects;

public class ILconstInt extends ILconstAbstract implements ILconstNum {

private int val;
Expand Down Expand Up @@ -140,5 +142,17 @@ public boolean isEqualTo(ILconst other) {
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ILconstInt that = (ILconstInt) o;
return val == that.val;
}

@Override
public int hashCode() {
return Objects.hash(val);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.peeeq.wurstscript.types.WurstType;
import de.peeeq.wurstscript.types.WurstTypeInfer;
import org.eclipse.jdt.annotation.Nullable;

public class ILconstNull extends ILconstAbstract implements ILconstAddable {

Expand Down Expand Up @@ -39,4 +40,10 @@ public ILconstAddable add(ILconstAddable other) {
throw new Error("unsupported: " + other + " // " + other.getClass());
}
}

@Override
public int hashCode() {
return System.identityHashCode(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ public Object getObj() {
return obj;
}

@Override
public int hashCode() {
return super.hashCode() + name.hashCode() + obj.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import de.peeeq.wurstscript.jassinterpreter.TestSuccessException;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.translation.imtranslation.FunctionFlagEnum;
import de.peeeq.wurstscript.translation.imtranslation.ImHelper;
import org.eclipse.jdt.annotation.Nullable;

import java.io.File;
Expand Down Expand Up @@ -162,7 +163,11 @@ private static LocalState runBuiltinFunction(ProgramState globalState, ImFunctio
}
}
globalState.compilationError("function " + f.getName() + " cannot be used from the Wurst interpreter.\n" + errors);
return new LocalState();
if (f.getReturnType() instanceof ImVoid) {
return new LocalState();
}
ILconst returnValue = ImHelper.defaultValueForComplexType(f.getReturnType()).evaluate(globalState, new LocalState());
return new LocalState(returnValue);
}

private static boolean isCompiletimeNative(ImFunction f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private void mergeLocals(Map<ImStmt, Set<ImVar>> livenessInfo, ImFunction func)
nextVar:
while (!vars.isEmpty()) {
ImVar v = vars.poll();
// System.out.println("v = " + v + " // " + inferenceGraph.get(v));

// check if there is some other variable which is already assigned, has the same type and does not interfere
nextAssigned:
Expand All @@ -95,7 +94,6 @@ private void mergeLocals(Map<ImStmt, Set<ImVar>> livenessInfo, ImFunction func)

totalLocalsMerged += merges.size();

// System.out.println("merges = " + merges);
func.accept(new ImFunction.DefaultVisitor() {
@Override
public void visit(ImVarAccess va) {
Expand Down Expand Up @@ -129,7 +127,6 @@ private Map<ImVar, Set<ImVar>> calculateInferenceGraph(Map<ImStmt, Set<ImVar>> l
for (ImStmt s : keys) {
i++;
Set<ImVar> live = livenessInfo.get(s);
System.out.println("stmt " + i + "/" + keys.size() + " live vars: " + live.size());
for (ImVar v1 : live) {
Set<ImVar> inferenceSet = inferenceGraph.getOrDefault(v1, HashSet.empty());
inferenceSet = inferenceSet.addAll(live.filter(v2 -> v1.getType().equalsType(v2.getType())));
Expand Down Expand Up @@ -210,14 +207,6 @@ public Map<ImStmt, Set<ImVar>> calculateLiveness(ImFunction func) {
out.put(node, newOut);
}
}
// System.out.println("result after " + iterations + " iterations in func " + func.getName());
// System.out.println("//#########################################");
// System.out.println("// liveness for " + func.getName());
// for (Node node : nodes) {
// System.out.println(" // " + in.get(node));
// System.out.println(node);
// System.out.println(" // " + out.get(node));
// }

Map<ImStmt, Set<ImVar>> result = new HashMap<>();
for (Node node : cfg.getNodes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ImOptimizer {
private static final HashMap<String, Integer> totalCount = new HashMap<>();

static {
localPasses.add(new SimpleRewrites());
localPasses.add(new ConstantAndCopyPropagation());
localPasses.add(new UselessFunctionCallsRemover());
localPasses.add(new GlobalsInliner());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static <T> void printSep(StringBuilder sb, String seperator, T[] args) {
if (i > 0) {
sb.append(seperator);
}
sb.append(args[i].toString());
sb.append(args[i]);
}
}

Expand Down Expand Up @@ -130,14 +130,14 @@ public static int parseHexInt(String yytext, int offset) {
}
}

public static String printSep(String sep, String[] args) {
public static <T> String printSep(String sep, T[] args) {
StringBuilder sb = new StringBuilder();
printSep(sb, sep, args);
return sb.toString();
}

public static String printSep(String sep, List<?> args) {
return args.stream().map(Object::toString).collect(Collectors.joining(sep));
return args.stream().map(String::valueOf).collect(Collectors.joining(sep));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class StdLib {
/**
* version to use for the tests
*/
private final static String version = "e1a61914adb9cdb2a631603ce8cab651043ad288";
private final static String version = "51f3274640197f957c3c53fb8cbe97c4e96ae898";

/**
* flag so that initialization in only done once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ class TestConfig {
}

TestConfig withStdLib() {
this.withStdLib = true;
return this;
return withStdLib(true);
}

TestConfig withStdLib(boolean b) {
this.withStdLib = b;
if (withStdLib) {
// stdlib needs compiletime functions
this.runCompiletimeFunctions = true;
}
return this;
}

Expand Down Expand Up @@ -456,7 +459,7 @@ private void translateAndTest(String name, boolean executeProg,
if (!executeProgOnlyAfterTransforms) {
// we want to test that the interpreter works correctly before transforming the program in the translation step
if (executeTests) {
executeTests(gui, imProg);
executeTests(gui, compiler.getImTranslator(), imProg);
}
if (executeProg) {
executeImProg(gui, imProg);
Expand All @@ -471,7 +474,7 @@ private void translateAndTest(String name, boolean executeProg,
}

if (executeTests) {
executeTests(gui, imProg);
executeTests(gui, compiler.getImTranslator(), imProg);
}
if (executeProg) {
executeImProg(gui, imProg);
Expand Down Expand Up @@ -563,9 +566,9 @@ private void executeJassProg(JassProg prog)
throw new Error("Succeed function not called");
}

private void executeTests(WurstGui gui, ImProg imProg) {
private void executeTests(WurstGui gui, ImTranslator translator, ImProg imProg) {
RunTests runTests = new RunTests(null, 0, 0, null);
RunTests.TestResult res = runTests.runTests(imProg, null, null);
RunTests.TestResult res = runTests.runTests(translator, imProg, null, null);
if (res.getPassedTests() < res.getTotalTests()) {
throw new Error("tests failed: " + res.getPassedTests() + " / " + res.getTotalTests() + "\n" +
gui.getErrors());
Expand Down

0 comments on commit 05650e5

Please sign in to comment.