unit tests and run tests are splitted
run tests can use golden value and test data to process and compare results
This commit is contained in:
@@ -6,27 +6,33 @@ dependencies {
|
|||||||
cli_bc project(path: ':backend.native', configuration: 'cli_bc')
|
cli_bc project(path: ':backend.native', configuration: 'cli_bc')
|
||||||
}
|
}
|
||||||
|
|
||||||
public class KonanTest extends DefaultTask {
|
abstract class KonanTest extends DefaultTask {
|
||||||
protected String source
|
protected String source
|
||||||
def backendNative = project.project(":backend.native")
|
def backendNative = project.project(":backend.native")
|
||||||
def runtimeProject = project.project(":runtime")
|
def runtimeProject = project.project(":runtime")
|
||||||
def llvmLlc = llvmTool("llc")
|
def llvmLlc = llvmTool("llc")
|
||||||
def llvmC = llvmTool("clang")
|
def llvmC = llvmTool("clang")
|
||||||
|
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
||||||
|
def mainC = 'main.c'
|
||||||
|
String goldValue = null
|
||||||
|
String testData = null
|
||||||
|
|
||||||
public KonanTest(){
|
public KonanTest(){
|
||||||
dependsOn([project.project(":runtime").tasks['build'],
|
dependsOn([project.project(":runtime").tasks['build'],
|
||||||
project.parent.tasks['build']])
|
project.parent.tasks['build']])
|
||||||
}
|
}
|
||||||
|
|
||||||
@TaskAction
|
|
||||||
public void compileBc() {
|
|
||||||
def sourceKt = project.file(source)
|
abstract void compileTest(File sourceS, File runtimeS, File out)
|
||||||
|
|
||||||
|
private File kt2bc(String ktSource) {
|
||||||
|
def sourceKt = project.file(ktSource)
|
||||||
def sourceBc = new File("${sourceKt.absolutePath}.bc")
|
def sourceBc = new File("${sourceKt.absolutePath}.bc")
|
||||||
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
|
||||||
println "${runtimeBc}"
|
println "${runtimeBc}"
|
||||||
|
|
||||||
project.javaexec {
|
project.javaexec {
|
||||||
main='org.jetbrains.kotlin.cli.bc.K2NativeKt'
|
main = 'org.jetbrains.kotlin.cli.bc.K2NativeKt'
|
||||||
classpath = project.configurations.cli_bc
|
classpath = project.configurations.cli_bc
|
||||||
args "-output", "${sourceBc.absolutePath}",
|
args "-output", "${sourceBc.absolutePath}",
|
||||||
"-runtime", "${runtimeBc.absolutePath}",
|
"-runtime", "${runtimeBc.absolutePath}",
|
||||||
@@ -35,20 +41,30 @@ public class KonanTest extends DefaultTask {
|
|||||||
|
|
||||||
jvmArgs "-Djava.library.path=${backendNative.buildDir.canonicalPath}/nativelibs"
|
jvmArgs "-Djava.library.path=${backendNative.buildDir.canonicalPath}/nativelibs"
|
||||||
}
|
}
|
||||||
|
return sourceBc
|
||||||
|
}
|
||||||
|
|
||||||
def runtimeS = bc2s(runtimeBc)
|
@TaskAction
|
||||||
def sourceS = bc2s(sourceBc)
|
void executeTest() {
|
||||||
def testC = sourceS.absolutePath.replace(".kt.S", "-test.c")
|
def sourceS = bc2s(kt2bc(source))
|
||||||
def mainC = "main.c"
|
def exe = new File(sourceS.absolutePath.replace(".kt.S", ""))
|
||||||
def exe = sourceS.absolutePath.replace(".kt.S", "")
|
compileTest(sourceS, bc2s(runtimeBc), exe)
|
||||||
|
println "execution :${exe.absolutePath}"
|
||||||
project.exec {
|
if (testData == null) {
|
||||||
commandLine "${llvmC}", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}",
|
project.exec {
|
||||||
"-o", "${exe}"
|
commandLine "${exe.absolutePath}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
project.exec {
|
println "testData:$testData"
|
||||||
commandLine "${exe}"
|
def out = new ByteArrayOutputStream()
|
||||||
|
project.exec {
|
||||||
|
commandLine "${exe.absolutePath}"
|
||||||
|
standardInput = new ByteArrayInputStream(testData.bytes)
|
||||||
|
standardOutput = out
|
||||||
|
}
|
||||||
|
if (goldValue != out.toString())
|
||||||
|
throw new RuntimeException("test failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,15 +83,34 @@ public class KonanTest extends DefaultTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UnitKonanTest extends KonanTest {
|
||||||
|
void compileTest(File sourceS, File runtimeS, File exe) {
|
||||||
|
def testC = sourceS.absolutePath.replace(".kt.S", "-test.c")
|
||||||
|
project.exec {
|
||||||
|
commandLine "${llvmC}", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}",
|
||||||
|
"-o", "${exe.absolutePath}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RunKonanTest extends KonanTest {
|
||||||
|
void compileTest(File sourceS, File runtimeS, File exe) {
|
||||||
|
project.exec {
|
||||||
|
commandLine "${llvmC}", "-DRUN_TEST", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}",
|
||||||
|
"-o", "${exe.absolutePath}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
task run() {
|
task run() {
|
||||||
dependsOn(tasks.withType(KonanTest))
|
dependsOn(tasks.withType(KonanTest))
|
||||||
}
|
}
|
||||||
|
|
||||||
task sum (type:KonanTest) {
|
task sum (type:UnitKonanTest) {
|
||||||
source = "codegen/function/sum.kt"
|
source = "codegen/function/sum.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
task objectInitialization(type: KonanTest) {
|
task objectInitialization(type: UnitKonanTest) {
|
||||||
source = "codegen/object/initialization.kt"
|
source = "codegen/object/initialization.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,27 +118,29 @@ task objectInitialization(type: KonanTest) {
|
|||||||
source = "$codegen/klass/basic.kt"
|
source = "$codegen/klass/basic.kt"
|
||||||
} */
|
} */
|
||||||
|
|
||||||
task aritmetic(type: KonanTest) {
|
task aritmetic(type: UnitKonanTest) {
|
||||||
source = "codegen/function/arithmetic.kt"
|
source = "codegen/function/arithmetic.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
task sum1(type: KonanTest) {
|
task sum1(type: UnitKonanTest) {
|
||||||
source = "codegen/function/sum_foo_bar.kt"
|
source = "codegen/function/sum_foo_bar.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
task sum2(type: KonanTest) {
|
task sum2(type: UnitKonanTest) {
|
||||||
source = "codegen/function/sum_imm.kt"
|
source = "codegen/function/sum_imm.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
task sum_3const(type: KonanTest) {
|
task sum_3const(type: UnitKonanTest) {
|
||||||
source = "codegen/function/sum_3const.kt"
|
source = "codegen/function/sum_3const.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
task hello0(type: KonanTest) {
|
//task hello0(type: RunKonanTest) {
|
||||||
source = "runtime/basic/hello0.kt"
|
// source = "runtime/basic/hello0.kt"
|
||||||
}
|
//}
|
||||||
|
|
||||||
task hello1(type: KonanTest) {
|
task hello1(type: RunKonanTest) {
|
||||||
|
goldValue = "Hello World"
|
||||||
|
testData = "Hello World"
|
||||||
source = "runtime/basic/hello1.kt"
|
source = "runtime/basic/hello1.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,13 @@ void * resolve_symbol(char *name) {
|
|||||||
|
|
||||||
int
|
int
|
||||||
kotlinNativeMain() {
|
kotlinNativeMain() {
|
||||||
|
#ifdef RUN_TEST
|
||||||
|
void (*main)(void *) = resolve_symbol("kfun:main");
|
||||||
|
main((void *)0);
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
return run_test();
|
return run_test();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int ktype_kotlin_any asm("_ktype:kotlin.Any");
|
int ktype_kotlin_any asm("_ktype:kotlin.Any");
|
||||||
|
|||||||
Reference in New Issue
Block a user