Test case for a C program

Test case for a C program

by Nikos Chatzigiannakis -
Number of replies: 4

I am totally new to Coderunner as an author. 

It works perfectly for me but I want to be sure that students have used *or not) some commands or functions in order to build their submitted code.

Is it possible to test it with test cases and how?

Thanks

Nikos

In reply to Nikos Chatzigiannakis

Re: Test case for a C program

by Hongchuan Liu -
Sure, please read the CodeRunner documentation.
In reply to Hongchuan Liu

Re: Test case for a C program

by Nikos Chatzigiannakis -
I had a look at the documentation and I am realy confused. I will appreceate if you could give me an example.
Thanks in advance

Nikos
In reply to Nikos Chatzigiannakis

Re: Test case for a C program

by Richard Lobb -
To check the student code before you run it, you need to modify the question template. To see the template, click the Customise check box.

There are two ways to proceed:
  1. If it's write-a-function question, then you could put the student answer into a string variable which you could then check (in C). For example, if you wanted to check for the presence of a '*' anywhere in the student's answer, you could write:
    #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() {
        char* ans = "{{ STUDENT_ANSWER | e('c')}}";
        if (strchr(ans, '*') == NULL) {
            fprintf(stderr, "No '*' found!");
            return 0;
        }
    {% for TEST in TESTCASES %}
       {
        {{ TEST.testcode }};
       }
        {% if not loop.last %}printf("%s\n", SEPARATOR);{% endif %}
    {% endfor %}
        return 0;
    }
    However, C is a dreadful language for doing string processing so this approach is rather limited. And of course checking for a '*' anywhere in the code isn't just checking for indirection - any multiply operator, or even a '*' in a comment, would pass that rudimentary test.
  2. For write-a-program questions, or when you want more powerful tools to analyse the student's code, you need to write a template that does all the checks first, then runs the student's code in a subprocess. See the c_via_python question type in the documentation. With this approach you can do much more elaborate checking before running the student code. Virtually all our in-house question types do this. In C, for example, we use a Python C parser to analyse the students code to check for required or disallowed constructs, and we also put it through a style checker.
In reply to Richard Lobb

Re: Test case for a C program

by Nikos Chatzigiannakis -
Thanks a lot! I used the c_via_python, I customized the template, and all was done. Thanks again.