Skip to content

Instantly share code, notes, and snippets.

@mtov
Last active December 6, 2021 13:53

Revisions

  1. mtov revised this gist Jul 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Stack.java
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ public void push(T elem) {
    public T pop() throws EmptyStackException {
    if (isEmpty())
    throw new EmptyStackException();
    T elem = elements.get(size-1);
    T elem = elements.remove(size-1);
    size--;
    return elem;
    }
  2. mtov renamed this gist Nov 14, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mtov revised this gist Nov 14, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion StackTeste.java
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,6 @@ public class StackTest {
    public void init() {
    stack = new Stack<Integer>();
    }


    @Test
    public void testEmptyStack() {
  4. mtov created this gist Nov 14, 2019.
    28 changes: 28 additions & 0 deletions Stack.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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.get(size-1);
    size--;
    return elem;
    }
    }
    54 changes: 54 additions & 0 deletions StackTeste.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@

    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();
    }

    }