Last active
December 6, 2021 13:53
-
-
Save mtov/3601acd0b32a1d0a85b4a81a43af4284 to your computer and use it in GitHub Desktop.
Teste de unidade de uma classe Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.EmptyStackException; | |
public class Stack<T> { | |
private ArrayList<T> elements = new ArrayList<T>(); | |
private int size = 0; | |
public int size() { | |
return size; | |
} | |
public boolean isEmpty(){ | |
return (size == 0); | |
} | |
public void push(T elem) { | |
elements.add(elem); | |
size++; | |
} | |
public T pop() throws EmptyStackException { | |
if (isEmpty()) | |
throw new EmptyStackException(); | |
T elem = elements.remove(size-1); | |
size--; | |
return elem; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.junit.Before; | |
import static org.junit.Assert.assertTrue; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertEquals; | |
public class StackTest { | |
Stack<Integer> stack; | |
@Before | |
public void init() { | |
stack = new Stack<Integer>(); | |
} | |
@Test | |
public void testEmptyStack() { | |
assertTrue(stack.isEmpty()); | |
} | |
@Test | |
public void testNotEmptyStack() { | |
stack.push(10); | |
assertFalse(stack.isEmpty()); | |
} | |
@Test | |
public void testSizeStack() { | |
stack.push(10); | |
stack.push(20); | |
stack.push(30); | |
int size = stack.size(); | |
assertEquals(3,size); | |
} | |
@Test | |
public void testPushPopStack() { | |
stack.push(10); | |
stack.push(20); | |
stack.push(30); | |
int result = stack.pop(); | |
result = stack.pop(); | |
assertEquals(20,result); | |
} | |
@Test(expected = java.util.EmptyStackException.class) | |
public void testEmptyStackException() { | |
stack.push(10); | |
int result = stack.pop(); | |
result = stack.pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment