<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 96600  -->
  <question type="coderunner">
    <name>
      <text>PROTOTYPE_KotlinFragments</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[<p>Kotlin Prototyp für Code-Fragmente.</p>
<p>Test-extra kann für top-level Definitionen genutzt werden.</p>
<p>In main wird eingefügt:</p>
<ul>
<li>testcode (Testfall)</li>
<li>studentanswer</li>
</ul>
<p>main wird dann ausgeführt.</p>]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1</defaultgrade>
    <penalty>0</penalty>
    <hidden>0</hidden>
    <idnumber></idnumber>
    <coderunnertype>kotlin-fragments</coderunnertype>
    <prototypetype>2</prototypetype>
    <allornothing>1</allornothing>
    <penaltyregime>10, 20, ...</penaltyregime>
    <precheck>0</precheck>
    <hidecheck>0</hidecheck>
    <showsource>0</showsource>
    <answerboxlines>18</answerboxlines>
    <answerboxcolumns>100</answerboxcolumns>
    <answerpreload></answerpreload>
    <globalextra></globalextra>
    <useace>1</useace>
    <resultcolumns></resultcolumns>
    <template><![CDATA[import subprocess
import sys
import os

# 1. Twig-Variablen abrufen
student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
test_code = """{{ TEST.testcode | e('py') }}"""
test_extra = """{{ TEST.extra | e('py') }}"""

# 2. Code zusammenbauen
full_code = test_extra + "\n" + "fun main(args: Array<String>) {\n" + test_code + "\n" + student_answer + "\n}"

with open("Solution.kt", "w") as src:
    src.write(full_code)

# Hilfsfunktion zum Filtern der JVM-Warnungen
def run_and_filter(cmd):
    # Startet den Prozess und fängt stdout/stderr ab
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
    stdout, stderr = proc.communicate()
    
    # Filtert die bekannten OpenJDK-Warnungen aus stderr
    filtered_lines = [
        line for line in stderr.splitlines() 
        if "OpenJDK 64-Bit Server VM warning" not in line 
        and "-Xverify:none" not in line 
        and "-noverify" not in line
    ]
    filtered_stderr = "\n".join(filtered_lines)
    
    return proc.returncode, stdout, filtered_stderr

# 3. Kompilierung
# kotlinc ist selbst ein Java-Prozess und erzeugt daher auch die Warnung
compile_cmd = [
    "kotlinc", "Solution.kt", "-d", "Solution.jar",
    "-J-Xmx1024m", "-J-Xss256k", "-J-XX:MaxMetaspaceSize=128m"
]

return_code, stdout_comp, stderr_comp = run_and_filter(compile_cmd)

if return_code != 0:
    print("** Kompilierung fehlgeschlagen **", file=sys.stderr)
    # Hier geben wir die gefilterten echten Compiler-Fehler aus
    if stderr_comp:
        print(stderr_comp, file=sys.stderr)
    sys.exit(0)

# 4. Ausführung
run_cmd = [
    "kotlin", "-cp", "Solution.jar", "SolutionKt",
    "-J-Xmx512m", "-J-Xss256k", "-J-XX:MaxMetaspaceSize=128m"
]

return_code, stdout_run, stderr_run = run_and_filter(run_cmd)

# 5. Finale Ausgabe an CodeRunner
if stderr_run:
    print(stderr_run, file=sys.stderr)

if stdout_run:
    print(stdout_run, end="")]]></template>
    <iscombinatortemplate>0</iscombinatortemplate>
    <allowmultiplestdins>0</allowmultiplestdins>
    <answer></answer>
    <validateonsave>1</validateonsave>
    <testsplitterre><![CDATA[|#<ab@17943918#@>#\n|ms]]></testsplitterre>
    <language>python3</language>
    <acelang>kotlin</acelang>
    <sandbox></sandbox>
    <grader>EqualityGrader</grader>
    <cputimelimitsecs>15</cputimelimitsecs>
    <memlimitmb>4096</memlimitmb>
    <sandboxparams></sandboxparams>
    <templateparams></templateparams>
    <hoisttemplateparams>1</hoisttemplateparams>
    <extractcodefromjson>1</extractcodefromjson>
    <templateparamslang>None</templateparamslang>
    <templateparamsevalpertry>0</templateparamsevalpertry>
    <templateparamsevald>{}</templateparamsevald>
    <twigall>0</twigall>
    <uiplugin>ace</uiplugin>
    <uiparameters></uiparameters>
    <attachments>0</attachments>
    <attachmentsrequired>0</attachmentsrequired>
    <maxfilesize>10240</maxfilesize>
    <filenamesregex></filenamesregex>
    <filenamesexplain></filenamesexplain>
    <displayfeedback>1</displayfeedback>
    <giveupallowed>0</giveupallowed>
    <prototypeextra></prototypeextra>
    <testcases>
    </testcases>
  </question>

</quiz>