<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 31458  -->
  <question type="coderunner">
    <name>
      <text>PROTOTYPE Case insensitive C++</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[<p>The prototype for a C++ write-a-program question that accepts output case insensitively and with arbitrary white space.</p>]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1</defaultgrade>
    <penalty>0</penalty>
    <hidden>0</hidden>
    <idnumber></idnumber>
    <coderunnertype>case_insens_cpp_prog</coderunnertype>
    <prototypetype>2</prototypetype>
    <allornothing>1</allornothing>
    <penaltyregime>10, 20, ...</penaltyregime>
    <precheck>0</precheck>
    <hidecheck>0</hidecheck>
    <showsource>0</showsource>
    <answerboxlines>9</answerboxlines>
    <answerboxcolumns>100</answerboxcolumns>
    <answerpreload></answerpreload>
    <globalextra></globalextra>
    <useace>1</useace>
    <resultcolumns></resultcolumns>
    <template><![CDATA[""" The template grader for a question type that compiles and runs a student-submitted
    C++ program and checks the output matches the expected case-insensitively
    with arbitrary amounts of white space. Trailing white space is stripped.
    Extra white space at the start is an error (unless a non-zero amount is expected).
"""

import subprocess, json, re

def accepted(expected, got):
    """Return true if the output we got is deemed a match to the expected output"""
    expected_simplified = re.sub('\s+', ' ', expected.lower().rstrip())
    got_simplified = re.sub('\s+', ' ', got.lower().rstrip())
    return expected_simplified == got_simplified

# Write the student code to a file prog.cpp
student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
with open("prog.cpp", "w") as src:
    print(student_answer, file=src)

# Assume the worst
fraction = 0
abort = True
    
# Compile
compile = subprocess.run("g++ -o prog prog.cpp".split(), capture_output=True, text=True)
if compile.returncode != 0:
    got = "** Compilation failed. Testing aborted **\n" + compile.stderr

# If compile succeeded, run the code. Since this is a per-test template,
# stdin is already set up for the stdin text specified in the test case,
# so we can run the compiled program directly.
else:
    run = subprocess.run(["./prog"], capture_output=True, text=True)
    if run.returncode < 0:
        got = f"Task failed with signal {-run.returncode}\n" + run.stderr + run.stdout
    else:
        got = run.stdout
        expected = """{{ TEST.expected | e('py') }}"""
        fraction = 1 if accepted(expected, got) else 0
        abort = False
        
outcome = {"got": got, "fraction": fraction, "abort": abort}
print(json.dumps(outcome))

        ]]></template>
    <iscombinatortemplate>0</iscombinatortemplate>
    <allowmultiplestdins>0</allowmultiplestdins>
    <answer></answer>
    <validateonsave>0</validateonsave>
    <testsplitterre></testsplitterre>
    <language>python3</language>
    <acelang>cpp</acelang>
    <sandbox></sandbox>
    <grader>TemplateGrader</grader>
    <cputimelimitsecs></cputimelimitsecs>
    <memlimitmb></memlimitmb>
    <sandboxparams><![CDATA[{"linkargs": ["-lm"]}]]></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>
      <testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000" >
      <testcode>
                <text></text>
      </testcode>
      <stdin>
                <text></text>
      </stdin>
      <expected>
                <text>Hello world</text>
      </expected>
      <extra>
                <text></text>
      </extra>
      <display>
                <text>SHOW</text>
      </display>
    </testcase>
    </testcases>
  </question>

</quiz>