Print this chapterPrint this chapter

CodeRunner Documentation (V3.1.0)

5.2 Combinator templates

The template for a question is by default defined by the code runner question type, which itself is defined by a special "prototype" question, to be explained later. You can inspect the template of any question by clicking the customise box in the question authoring form. You'll also find a checkbox labelled Is combinator. If this checkbox is checked the template is a combinator template. Such templates take the STUDENT_ANSWER template variable as shown above, but rather than taking just a single TEST variable, they take a TESTCASES variable, which is is a list of all the individual TEST objects.

The actual template used by the built-in C function question type is not actually a per-test template as suggested above, but is the following combinator template.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define SEPARATOR "#<ab@17943918#@>#"

{{ STUDENT_ANSWER }}

int main() {
{% for TEST in TESTCASES %}
   {
    {{ TEST.testcode }};
   }
    {% if not loop.last %}printf("%s\n", SEPARATOR);{% endif %}
{% endfor %}
    return 0;
}

The Twig template language control structures are wrapped in {% and %}. If a C-function question had two three test cases, the above template might expand to something like the following:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define SEPARATOR "#<ab@17943918#@>#"

int sqr(int n) {
    return n * n;
}

int main() {
    printf("%d\n", sqr(-9));
    printf("%s\n", SEPARATOR);
    printf("%d\n", sqr(11));
    printf("%s\n", SEPARATOR);
    printf("%d\n", sqr(-13));
    return 0;
}

The output from the execution is then the outputs from the three tests separated by a special separator string (which can be customised for each question if desired). On receiving the output back from the sandbox, CodeRunner then splits the output using the separator into three separate test outputs, exactly as if a per-test template had been used on each test case separately.

This strategy can't be used with questions that require standard input as there's no reliable way to reset the standard input for each test. The strategy also fails if the student's code causes a premature abort due to a run error, such as a segmentation fault or a CPU time limit exceeded. In such cases, CodeRunner uses the combinator template as a per-test template simply by passing it one test case at a time in the TESTCASES variable.