A few more tests for emscripten support for #7

This commit is contained in:
Chad Retz
2017-04-26 21:45:20 -05:00
parent b4140c8189
commit e9cdfc3b0f
8 changed files with 252 additions and 57 deletions

View File

@ -35,14 +35,6 @@ data class ScriptContext(
fun withHarnessRegistered(out: PrintWriter = PrintWriter(System.out, true)) =
withModuleRegistered("spectest", Module.Native(TestHarness(out)))
fun withEmscriptenRegistered(metadata: Emscripten.Metadata, out: OutputStream) =
Env(metadata.staticBump, out).let { env ->
val mods = Env.subModules.fold(listOf(Module.Native(env))) { mods, subMod ->
mods + Module.Native(subMod.apply(env))
}
withModuleRegistered("env", Module.Composite(mods))
}
fun withModuleRegistered(name: String, mod: Module) = copy(registrations = registrations + (name to mod))
fun runCommand(cmd: Script.Cmd) = when (cmd) {

View File

@ -6,6 +6,7 @@ import asmble.io.AstToSExpr
import asmble.io.SExprToStr
import org.junit.Assume
import org.junit.Test
import run.jvm.emscripten.Env
import java.io.ByteArrayOutputStream
import java.io.OutputStreamWriter
import java.io.PrintWriter
@ -39,7 +40,14 @@ abstract class TestRunner<out T : BaseTestUnit>(val unit: T) : TestBase() {
).withHarnessRegistered(PrintWriter(OutputStreamWriter(out, Charsets.UTF_8), true))
// If there's a staticBump, we are an emscripten mod and we need to include the env
unit.emscriptenMetadata?.also { scriptContext = scriptContext.withEmscriptenRegistered(it, out) }
var emEnv: Env? = null
unit.emscriptenMetadata?.also {
emEnv = Env(it.staticBump, out, "unknown-program")
val mods = Env.subModules.fold(listOf(Module.Native(emEnv!!))) { mods, subMod ->
mods + Module.Native(subMod.apply(emEnv))
}
scriptContext = scriptContext.withModuleRegistered("env", Module.Composite(mods))
}
// This will fail assertions as necessary
scriptContext = unit.script.commands.fold(scriptContext) { scriptContext, cmd ->
@ -58,12 +66,16 @@ abstract class TestRunner<out T : BaseTestUnit>(val unit: T) : TestBase() {
if (mainMethod.parameterTypes.isEmpty())
mainMethod.invoke(lastMod.instance(scriptContext))
else if (mainMethod.parameterTypes.asList() == listOf(Int::class.java, Int::class.java))
mainMethod.invoke(lastMod.instance(scriptContext), 0, 0)
mainMethod.invoke(lastMod.instance(scriptContext), emEnv?.argc ?: 0, emEnv?.argv ?: 0)
else
error("Unrecognized main method params for $mainMethod")
}
}
// If there was an emscripten env, we have to run the exit callbacks
emEnv?.also { it.runAtExitCallbacks(scriptContext.modules.last().instance(scriptContext)) }
// Check the output
unit.expectedOutput?.let {
// Sadly, sometimes the expected output is trimmed in Emscripten tests
assertEquals(it.trimEnd(), out.toByteArray().toString(Charsets.UTF_8).trimEnd())

View File

@ -7,7 +7,6 @@ import org.junit.runners.Parameterized
@RunWith(Parameterized::class)
class EmscriptenTest(unit: EmscriptenTestUnit) : TestRunner<EmscriptenTestUnit>(unit) {
companion object {
var failureReason: Throwable? = null

View File

@ -18,7 +18,28 @@ class EmscriptenTestUnit(
) : BaseTestUnit(name, wast, expectedOutput) {
companion object {
val knownGoodNames = listOf(
"core/test_addr_of_stacked"
"core/test_addr_of_stacked",
// TODO: this fails for me even in the browser on windows
// "core/test_alloca",
"core/test_alloca_stack",
"core/test_array2",
"core/test_array2b",
// TODO: use of undeclared identifier 'true'
// "core/test_assert",
// TODO: I'm running the callbacks, but nothing happening
// "core/test_atexit",
"core/test_atomic",
// TODO: lots of special calls, wait for emscripten-wasm to finish out
// "core/test_atomic_cxx",
"core/test_atoX",
// TODO: must use 'struct' tag to refer to type 'Struct'
// "core/test_bigarray",
// TODO: must use 'struct' tag to refer to type 'bitty'
// "core/test_bitfields"
"core/test_bsearch"
// TODO: unknown type name '_LIBCPP_BEGIN_NAMESPACE_STD'
// "core/test_bswap64"
// TODO: the rest...maybe if emscripten is cleaned up
)
val allUnits by lazy { loadUnits() }
@ -45,7 +66,8 @@ class EmscriptenTestUnit(
try {
// Run emcc on the cFile
val nameSansExt = cFile.fileName.toString().substringBeforeLast(".c")
val cmdArgs = emccCommand + cFile.toString() + "-s" + "WASM=1" + "-o" + "$nameSansExt.html"
val cmdArgs = emccCommand + cFile.toString() +
arrayOf("-s", "WASM=1", "-o", "$nameSansExt.html")
TestBase.logger.debug { "Running ${cmdArgs.joinToString(" ")}" }
val proc = ProcessBuilder(*cmdArgs).
directory(tempDir).
@ -55,10 +77,15 @@ class EmscriptenTestUnit(
proc.inputStream.bufferedReader().forEachLine { TestBase.logger.debug { "[OUT] $it" } }
Assert.assertTrue("Timeout", proc.waitFor(10, TimeUnit.SECONDS))
Assert.assertEquals(0, proc.exitValue())
var outFile = cFile.resolveSibling("$nameSansExt.out")
if (Files.notExists(outFile)) {
outFile = cFile.resolveSibling("$nameSansExt.txt")
require(Files.exists(outFile)) { "Cannot find out file for $cFile" }
}
EmscriptenTestUnit(
name = cFile.unitNameFromCFile(),
wast = File(tempDir, "$nameSansExt.wast").readText(),
expectedOutput = cFile.resolveSibling("$nameSansExt.out").toFile().readText()
expectedOutput = outFile.toFile().readText()
)
} catch (e: Exception) { throw Exception("Unable to compile $cFile", e) }
}