CodeJudge

Quick Start: How to setup test data for my exercise?

Introduction

In this guide we will show how to create test data for different programming languages and input methods. To setup an exercise please follow Quick Start: How to setup an exercise? and simply replace the solution/test data files with those provided in this guide. Look at the menu to the right, to see an overview of the examples.

No Input

This is the most simple kind of test as there is given no input to the program. It can only be used to test very simple programs, for instance a classic "Hello CodeJudge!" program. We have used Java to create the suggested solution, but it could be written in any language.

Create a program that outputs Hello CodeJudge!

Solution (HelloCodeJudge.java)

public class HelloCodeJudge {
    public static void main(String[] args) {
        System.out.println("Hello CodeJudge!");
    }
}

Test File (Test01.dummy)

This should just be an empty file with the name Test01.dummy to make the system know there is a test case.

Standard Input

Below it is shown how to create test data where the program should read the input from standard input (the console). We have used Java to create the suggested solution, but it could be written in any language.

Create a program that reads a line from standard input and print out the words in reverse order (words are separated by spaces).

Solution (ReverseWords.java)

import java.util.Scanner;

public class ReverseWords {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] words = scanner.nextLine().split(" ");
         
        for (int i = words.length - 1; i >= 0; i--) {
            System.out.print(words[i] + " ");
        }
    }
}

Test File (Tests.in)

/// Test
Welcome to CodeJudge
/// Test
1 2 3 4 5 6 7 8 9 10 11
/// Test
G F E D C B A
/// Test
/// Hint: Have you considered the case with only one word?
Hello
/// Test
job! good well, very works program Your

Arguments

Below it is shown how to create test data where the user must read the input from command line arguments. We have used Java to create the suggested solution, but it could be written in any language.

Create a program that reads two integers from the arguments and outputs the sum.

Solution (SumArguments.java)

public class SumArguments {
    public static void main(String[] args) {
        System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
    }
}

Test File (Tests.args)

/// Test
3 5
/// Test
9 19
/// Test
3201 52342
/// Test
/// Hint: Have you considered negative numbers?
-21312 -42523

Java Test Script

This is the example from "Quick Start: How to set up the first exercise?" and is just included here for completeness.

Create a Java class Calculator that supports the method int sum(int a, int b) that should return the sum of the intgers a and b. Furthermore Calculator should support the method int factorial(int a) that must return 1 * 2 * ... * a = a! (should work for a >= 0).

Solution (Calculator.java)

public class Calculator {
    public int sum(int a, int b) {
        return a + b;
    }

    public int factorial(int a) {
        if (a == 0) {
            return 1;
        }
        return a * factorial(a - 1);
    }
}

Test File (Test.java)

public class Test {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        /// Test
        System.out.println(calculator.sum(3, 6));
        /// Test
        System.out.println(calculator.sum(-5, -9));
        /// Test
        System.out.println(calculator.sum(33, 64));
        /// Test
        System.out.println(calculator.factorial(5));
        /// Test
        System.out.println(calculator.factorial(10));
        /// Test
        System.out.println(calculator.factorial(2));
        /// Test
        System.out.println(calculator.factorial(1));
        /// Test
        /// Hint: Remember 0! = 1
        System.out.println(calculator.factorial(0));
        /// EOT
    }
}        

F# Test Script

Create a recursive function factorial(n) that returns n! (should work for n >= 0).

Solution (Factorial.fs)

let rec factorial n =
    match n with
    | 0 -> 1
    | _ -> n * factorial(n-1)

Test File (Tests.fs)

/// Test
printfn "%A" (factorial 5)
/// Test
printfn "%A" (factorial 10)
/// Test
printfn "%A" (factorial 2)
/// Test
printfn "%A" (factorial 1)
/// Test
/// Hint: Did you make the base case right? Remember 0! = 1
printfn "%A" (factorial 0)

Python Test Script

Create a function factorial(n) that returns n! (should work for n >= 0).

Solution (Factorial.py)

def factorial(n):
    if (n == 0):
        return 1
    
    return n * factorial(n - 1)

Test File (Tests.py)

#/// Test
print(factorial(5))
#/// Test
print(factorial(10))
#/// Test
print(factorial(2))
#/// Test
print(factorial(1))
#/// Test
#/// Hint: Did you make the base case right? Remember 0! = 1
print(factorial(0))

Prolog Test Script

Prolog Test Scripts are a bit special as they built upon Prolog Unit Tests. The test script file should therefore follow these specifications, see the documentation for details. It is not required to upload a solution for these programs, since the expected output is already specified in the test file (but it is a good idea, since it may be used as suggested solution)

Create a Prolog program factorial(+N, ?Factorial) that succeeds iff. Factorial is the factorial of N (the factorial of n is n! = 1 · 2 · ... · n, must work for n >= 0).

Solution (Factorial.pl)

factorial(0, 1).
factorial(N, F) :-
  N > 0,
  N1 is N - 1,
  factorial(N1, F1),
  F is N * F1.

Test File (Tests.plt)

:- begin_tests(factorial).

test(test1) :- factorial(5, 120).
test(test2) :- factorial(1, 1).
test(test3) :- factorial(2, 2).
test(test4) :- factorial(3, 6).
test(test5) :- factorial(4, 24).
test(test6) :- factorial(6, 720).
test(test7) :- factorial(0, 1).
test(test8) :- factorial(12, 479001600).

:- end_tests(factorial).

Matlab Test Script

Create a function square(n) that returns n to the power of two.

Solution (square.m)

function x = square(n)
x = n * n;

Test File (Tests.m)

%/// Test
square(4)
%/// Test
square(16)
%/// Test
square(10)
%/// Test
square(0)
%/// Test
square(1)
%/// Test
%/// Hint: Have you considered negative numbers?
square(-10)

R Test Script

Create a function square(n) that returns n to the power of two.

Solution (square.R)

square <- function(x) {
    return(x * x)
}

Test File (Tests.R)

/// Test
square(4)
/// Test
square(16)
/// Test
square(10)
/// Test
square(0)
/// Test
square(1)
/// Test
/// Hint: Have you considered negative numbers?
square(-10)

C++ Test Script

Observe that we must use two test files for this example. In order to upload the Test Group, the files should be put in a zip-file (respectively overwrites/sum.h and Tests.cpp). It is a good idea to make the file "sum.h" available to the users, for instance by adding it under "Attached Files" for the exercise (they are not required to upload it together with their submissions, since it will automatically be added when their code is compiled).

Implement the function sum(a, b) defined in "sum.h" so it returns a + b.

Solution (sum.cpp)

#include "sum.h"
int sum(int a, int b) {
    return a + b;
}

Test File (overwrites/sum.h)

#ifndef _SUM_H_
#define _SUM_H_
int sum(int, int);
#endif

Test File (Tests.cpp)

#include <iostream>
#include "sum.h"

using namespace std;

int main() {
    /// Test
    cout << sum(2, 5) << endl;
    /// Test
    cout << sum(312, 523) << endl;
    /// Test
    cout << sum(-9432, -123) << endl;
    /// EOT

    return 0;
}