Question
Need help creating a test called stackConstructorTest using junit 4.12 Requirments below: test name: stackConstructorTest action: instantiate stack object result: size = 0, isEmpty =
Need help creating a test called stackConstructorTest using junit 4.12
Requirments below:
test name: stackConstructorTest action: instantiate stack object result: size = 0, isEmpty = true
Test code
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class MyStackTest { /* * Default constructor for test class GenericListTest */ public MyStackTest() { } @Test public void stackPush1Test() { GenericList stack = new GenericList(); stack.push("ABC"); assertEquals(1, stack.size()); assertFalse(stack.isEmpty()); String actual=stack.peek(); assertEquals("ABC", actual); assertEquals(1, stack.size()); String toStrActual=stack.toString(); assertEquals("top, ABC, bottom", toStrActual); } @Test public void stackPush2Test() { GenericList stack = new GenericList(); stack.push("ABC"); stack.push("DEF"); assertEquals(2, stack.size()); assertFalse(stack.isEmpty()); String actual=stack.peek(); assertEquals("DEF", actual); assertEquals(2, stack.size()); String toStrActual=stack.toString(); assertEquals("top, DEF, ABC, bottom", toStrActual); }
@Test public void stackPop1Test() { GenericList stack = new GenericList(); stack.push("ABC"); assertEquals(1, stack.size()); assertFalse(stack.isEmpty()); String actual=stack.pop(); assertEquals("ABC", actual); assertTrue(stack.isEmpty()); String toStrActual=stack.toString(); assertEquals("top, bottom", toStrActual); } @Test public void stackPop2Test() { GenericList stack = new GenericList(); stack.push("ABC"); stack.push("DEF"); assertEquals(2, stack.size()); assertFalse(stack.isEmpty()); String actual=stack.pop(); assertEquals("DEF", actual); assertFalse(stack.isEmpty()); String toStrActual=stack.toString(); assertEquals("top, ABC, bottom", toStrActual); actual=stack.pop(); assertEquals( "ABC", actual); //assertEquals(0, storage.size()); assertTrue(stack.isEmpty()); toStrActual=stack.toString(); assertEquals("top, bottom", toStrActual); }
@Test public void stackClearTest() { GenericList stack = new GenericList(); stack.push("ABC"); stack.push("DEF"); assertEquals(2, stack.size()); assertFalse(stack.isEmpty()); stack.clear(); assertEquals(0, stack.size()); assertTrue(stack.isEmpty()); String toStrActual=stack.toString(); assertEquals("top, bottom", toStrActual); //assertEquals("top, bottom", stack.toString()); }
@Test public void testToString() { GenericList stack = new GenericList(); stack.push("ABC"); stack.push("DEF"); String toStrActual=stack.toString(); System.out.println(toStrActual); assertEquals("top, DEF, ABC, bottom", toStrActual); }
}
Source code
public class GenericList
{ private T[] arr; private int size;
private void newArray() { arr = (T[]) new Object[10];
size = 0; } private void expandArray() { T[] arr2;
arr2 = (T[]) new Object[(int)(arr.length * 1.2)];
for (int i = 0; i < arr.length; i++)
arr2[i] = arr[i];
arr = arr2; } public GenericList() { this.newArray(); }
public int size() { return size; } public void add(T value) { if (size == arr.length)
{ this.expandArray(); }
arr[size] = value;
size++; } public T get(int index) throws ArrayIndexOutOfBoundsException { if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); else return arr[index]; }
public void clear() { this.newArray(); } public void insert(int index, T value) { if(index>=0 && index if (size == arr.length) { this.expandArray(); } for (int i = size; i > index; i--) arr[i] = arr[i - 1]; arr[index] = value; size++; } else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } public String toString() { String returnValue = "top"; if (size != 0) { returnValue = "top, "+String.valueOf(arr[size-1]); for (int i = size-2; i >= 0 ; i--) returnValue = returnValue + ", " + arr[i]; } return returnValue+", bottom"; } public void display() { for (int i = 0; i < size; i++)
System.out.println(i + ": " + arr[i]); if ( arr.length == size) System.out.println("List is full "); else System.out.println("List has " + (arr.length - size) + " spaces left "); } public void set(int index, T value) { { if(index>=0 && index arr[index] = value; } else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } }
public void remove(int index) { { if((index >=0) && (index < size)) { for (int i = index; i < size() - 1; i++) { arr[i] = arr[i + 1]; } --size; } else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } } public void swap(int index1,int index2){ if((index1 >=0) && (index1 < size)&&(index2 >=0) && (index2 < size)) { T temp = arr[index1]; arr[index1]=arr[index2]; arr[index2]=temp; } else if (index1 < 0 || index1 >= this.size || index2<0 ||index2>= this.size) throw new ArrayIndexOutOfBoundsException(); } public void push(T value) {
if (size >= arr.length) return; arr[size++] = value; } public T pop() { if(size != 0)
return arr[--size]; return null; } public T peek() { if(size!=0) return arr[size-1]; return null; } public boolean isEmpty() { return size == 0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started