:: End of manually writing unit tests. Why not generate test cases automatically and smartly?

CUTE :: A Concolic Unit Testing Engine for C and Java

Open System Laboratory | Computer Science Department | University of Illinois at Urbana Champaign

Sample JUnit test case generated by jCUTE

Let us consider the following code present in the file Struct.java
package test;

import cute.Cute;

public class Struct {
    public int x;
    public Struct next;

    public static int f(int v){
        return 2*v+1;
    }

    public static void testme(Struct p,int y){
        if(y>0){
            if(p!=null){
                if(f(y)==p.x){
                    Cute.Assert(p.next!=p);
                }
            }
        }
    }

    public static void main(String[] args) {
        Struct s = (Struct)cute.Cute.input.Object("tests.Struct");
        int y = cute.Cute.input.Integer();
        testme(s,y);
    }
}
On testing the function test.Struct.main, jCUTE generates the following JUnit test containing 5 test cases. These 5 test cases are optimal for exploring all distinct execution paths of the program.
/* JUnit test case generated automatically by CUTE */
import junit.framework.*;

public class tests_Struct_main_Test extends TestCase implements cute.Input {
    private Object[] input;
    private int i;

    public tests_Struct_main_Test(String name){
        super(name);
    }

    public boolean Boolean() {
        return ((Boolean)input[i++]).booleanValue();
    }

    public short Short() {
        return ((Short)input[i++]).shortValue();
    }

    public int Integer() {
        return ((Integer)input[i++]).intValue();
    }

    public long Long() {
        return ((Long)input[i++]).longValue();
    }

    public float Float() {
        return ((Float)input[i++]).floatValue();
    }

    public double Double() {
        return ((Double)input[i++]).doubleValue();
    }

    public char Character() {
        return ((Character)input[i++]).charValue();
    }

    public byte Byte() {
        return ((Byte)input[i++]).byteValue();
    }

    public Object Object(String type) {
        return input[i++];
    }

    public void test1(){
        i=0;
        input = new Object[2];
        input[i++] = null;
        input[i++] = new Integer(0);
        i=0;
        cute.Cute.input = this;
        tests.Struct.main(null);
    }

    public void test2(){
        i=0;
        input = new Object[2];
        input[i++] = null;
        input[i++] = new Integer(1);
        i=0;
        cute.Cute.input = this;
        tests.Struct.main(null);
    }

    public void test3(){
        i=0;
        input = new Object[2];
        tests.Struct tmp1 = new tests.Struct();
        input[i++] = tmp1;
        tmp1.x = 0;
        tmp1.next = null;
        input[i++] = new Integer(1);
        i=0;
        cute.Cute.input = this;
        tests.Struct.main(null);
    }

    public void test4(){
        i=0;
        input = new Object[2];
        tests.Struct tmp1 = new tests.Struct();
        input[i++] = tmp1;
        tmp1.x = 3;
        tmp1.next = null;
        input[i++] = new Integer(1);
        i=0;
        cute.Cute.input = this;
        tests.Struct.main(null);
    }

    public void test5(){
        i=0;
        input = new Object[2];
        tests.Struct tmp1 = new tests.Struct();
        input[i++] = tmp1;
        tmp1.x = 3;
        tests.Struct tmp2 = tmp1;
        tmp1.next = tmp2;
        input[i++] = new Integer(1);
        i=0;
        cute.Cute.input = this;
        tests.Struct.main(null);
    }

}
Copyright © 2006 University of Illinois at Urbana Champaign. All rights Reserved