In this guide we will show how to create test data for different programming languages and input methods. To setup an exercise please follow How to set up a file-based exercise and simply replace the solution/test data files with those provided in this guide. Look at the menu to the left, to see an overview of the examples.
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!
public class HelloCodeJudge {
public static void main(String[] args) {
System.out.println("Hello CodeJudge!");
}
}
This should just be an empty file with the name Test01.in to make the system know there is a test case.
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).
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
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
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.
public class SumArguments {
public static void main(String[] args) {
System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
}
}
//@ Test
3 5
//@ Test
9 19
//@ Test
3201 52342
//@ Test
//@ Hint: Have you considered negative numbers?
-21312 -42523
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).
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);
}
}
public class Tests {
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));
//@ End
}
}
Create a recursive function factorial(n) that returns n! (should work for n >= 0).
module Factorial
let rec factorial n =
match n with
| 0 -> 1
| _ -> n * factorial(n-1)
//@ 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)
Create a function factorial(n) that returns n! (should work for n >= 0).
def factorial(n):
if (n == 0):
return 1
return n * factorial(n - 1)
#/@ 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 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).
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
:- begin_tests(factorial).
#/@ Test
test(test1) :- factorial(5, 120).
#/@ Test
test(test2) :- factorial(1, 1).
#/@ Test
test(test3) :- factorial(2, 2).
#/@ Test
test(test4) :- factorial(3, 6).
#/@ Test
test(test5) :- factorial(4, 24).
#/@ Test
test(test6) :- factorial(6, 720).
#/@ Test
test(test7) :- factorial(0, 1).
#/@ Test
test(test8) :- factorial(12, 479001600).
#/@ End
:- end_tests(factorial).
Create a function square(n) that returns n to the power of two.
function x = square(n)
x = n * n;
%/@ Test
square(4)
%/@ Test
square(16)
%/@ Test
square(10)
%/@ Test
square(0)
%/@ Test
square(1)
%/@ Test
%/@ Hint: Have you considered negative numbers?
square(-10)
Create a function square(n) that returns n to the power of two.
square <- function(x) {
return(x * x)
}
//@ Test
square(4)
//@ Test
square(16)
//@ Test
square(10)
//@ Test
square(0)
//@ Test
square(1)
//@ Test
//@ Hint: Have you considered negative numbers?
square(-10)
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.
#include "sum.h"
int sum(int a, int b) {
return a + b;
}
#ifndef _SUM_H_
#define _SUM_H_
int sum(int, int);
#endif
#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;
//@ End
return 0;
}
Create a Scala object Calculator that supports the method sum(v1: Int, v2: Int): Int that should return the sum of the intgers a and b.
object Calculator {
def sum(v1: Int, v2: Int): Int = {
return v1 + v2
}
}
object Tests extends App {
//@ Test
println(Calculator.sum(2, 5))
//@ Test
println(Calculator.sum(312, 523))
//@ Test
println(Calculator.sum(-9432, -123))
//@ End
}