CS-PROJECTS / towerofhanoi / LinkedStackTest.java
LinkedStackTest.java
Raw
package towerofhanoi;

import java.util.EmptyStackException;

// Virginia Tech Honor Code Pledge:
//
// As a Hokie, I will conduct myself with honor and integrity at all times.
// I will not lie, cheat, or steal, nor will I accept the actions of those who
// do.
// -- Jordan Harrington (jordanha23)

/**
 * @author Jordan Harrington
 * @version <3/25/2020>
 */
public class LinkedStackTest extends student.TestCase {

    private LinkedStack test;
    private Object testNode;

    /**
     * setUp method
     */
    public void setUp() {
        test = new LinkedStack();
    }


    /**
     * Tests size method
     */
    public void testSize() {
        assertEquals(0, test.size());
    }


    /**
     * Tests push method
     */
    public void testPush() {
        test.push(testNode);
        assertNotNull(test);
    }


    /**
     * Tests pop method
     */
    public void testPop() {
        Exception thrown = null;
        try {
            test.pop();
        }
        catch (Exception exception) {
            thrown = exception;
        }

        assertNotNull(thrown);
        assertTrue(thrown instanceof EmptyStackException);

        test.push(testNode);
        assertEquals(testNode, test.pop());
    }


    /**
     * Tests isEmpty
     */
    public void testIsEmpty() {
        assertTrue(test.isEmpty());
        test.push(testNode);
        assertFalse(test.isEmpty());
    }


    /**
     * Tests peek
     */
    public void testPeek() {
        Exception thrown = null;
        try {
            test.peek();
        }
        catch (Exception exception) {
            thrown = exception;
        }

        assertNotNull(thrown);
        assertTrue(thrown instanceof EmptyStackException);

        test.push(testNode);
        assertEquals(testNode, test.peek());
    }


    /**
     * Tests clear
     */
    public void testClear() {
        test.push(testNode);
        assertFalse(test.isEmpty());
        test.clear();
        assertTrue(test.isEmpty());
        assertEquals(0, test.size());
    }


    /**
     * Tests toString
     */
    public void testToString() {
        test.push("Hi");
        test.push("Oh");
        test.push("Hello");
        assertEquals("[Hello, Oh, Hi]", test.toString());
    }
}