Examining Software Testing Tools

by David C. Crowther and Peter J. Clarke



Listing One



//Simple stack implementation - Java

public class Stack

{

    protected static final int STACK_EMPTY = -1;

    protected static final int STACK_MAX = 1000;

    Object[] stackelements;

    int topelement = STACK_EMPTY;

    

    //create empty stack

    Stack()

    {

        stackelements = new Object[STACK_MAX];

    }

    //create stack with the objects stored in the array from bottom up

    Stack(Object[] o)

    {

        stackelements = new Object[STACK_MAX];

        for(int i=0;i<o. length;i++)

        {

            push(o[i]);

        }

    }

    //create stack as a duplicate of given stack

    Stack(Stack s)

    {

        stackelements = s. stackelements;

        topelement = s. topelement;

    }

    //push object onto the stack

    void push(Object o)

    {

        if(!isFull())

        {

            stackelements[++topelement] = o;

        }

    }

    //pop element off the stack

    Object pop()

    {

        if(isEmpty())

            return null;

       else return stackelements[topelement--];    

    }

//return true if stack is empty

    boolean isEmpty()

    {

        if(topelement == STACK_EMPTY)

            return true;

        else return false;

    }

//return true if stack is full

    boolean isFull()

    {

        if(topelement == STACK_MAX-1)

            return true;

        else return false;

    }

}





Listing Two



//Simple stack implementation - C#

public class Stack

{

    public const int STACK_EMPTY = -1;

    public const int STACK_MAX = 100;

    private Object[] stackelements;

    private int topelement = STACK_EMPTY;



    //create empty stack

    public Stack()

    {

        stackelements = new Object[STACK_MAX];

    }

    //create stack with the objects stored in the array from bottom up

    public Stack(Object[] o)

    {

        stackelements = new Object[STACK_MAX];

        topelement = o. Length-1;

        for(int i=0;i<o. Length;i++)

        {

            stackelements[i] = o[i];

        }

    }

//create stack as a duplicate of given stack

    public Stack(Stack s)

    {

        stackelements = s. stackelements;

        topelement = s. topelement;

    }

//push object onto the stack

    public void push(Object o)

    {

        if(!isFull())

        {

            stackelements[++topelement] = o;

        }

    }

//pop element off the stack

    public Object pop()

    {

        if(isEmpty())

            return null;

        else return stackelements[topelement--];    

    }

    //return true if stack is empty

    private bool isEmpty()

    {

        if(topelement == STACK_EMPTY)

            return true;

        else return false;

    }

//return true if stack is full

    private bool isFull()

    {

        if(topelement == STACK_MAX-1)

            return true;

        else return false;

    }   

}





Listing Three 



public void testSimplePop()

{

    String strA = "a";

    String[] st = new String[1];

    st[0] = strA;

    Stack stTested = new Stack(st);

    Stack stExpected = new Stack();

    Assert.assertTrue(strA. equals(stTested. pop()));

    Assert.assertTrue(stExpected. equals(stTested));

}   



Listing Four



/** Test for method: push(Object)

 * @see Stack#push(Object)

 * @author Jtest

 */

public void testPush2() {

    Object t0 = new Object();

    Object[] var0 = new Object[] {

    };

    Stack THIS = new Stack(var0);

    // jtest_tested_method

    THIS.push(t0);

    boolean var1 = THIS.equals((Stack) null);

    assertEquals(false, var1); // jtest_unverified

}   



Listing Five



[Test]

public void testPopEmpty() 

{

    Stack st = new Stack();

    Assert.True(st.pop()==null);

}





Listing Six



[TestMethod]

public void testPushFull(TestMethodRecord tmr)

{

    String strA = "a";

    Stack s1 = new Stack();

    for(int i=0;i<Stack. STACK_MAX;i++)

    {

        s1.push(strA + Convert. ToString(i));

    }

    Stack s2 = new Stack(s1);

    s1.push(strA);



    tmr. RunTest(s1.equals(s2),"Testing push to a full Stack");

}











4



