package com.hithomelabs.princeton1.module4;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Iterator;

import static org.junit.jupiter.api.Assertions.*;

class ArrayStackTest {

    private Stack<String> stack;

    @BeforeEach
    void setUp() {
        stack = new ArrayStack<String>();
    }

    @AfterEach
    void tearDown() {
        stack = null;
    }

    @Test
    @DisplayName("Empty stack must return size zero")
    public void testEmptyStackSize(){
        assertEquals(0, stack.size());
    }

    @Test
    @DisplayName("Push must work as expected")
    public void testEnqueue(){
        stack.push("Hello");
        stack.push("World");
        assertEquals(2,stack.size());
        Iterator<String> iterator = stack.iterator();
        assertEquals("Hello", iterator.next());
        assertEquals("World", iterator.next());
    }

    @Test
    @DisplayName("Pop must work as expected")
    public void testDequeue(){
        stack.push("hello");
        stack.push("world");
        assertEquals("world", stack.pop());
        stack.push("I");
        assertEquals(2, stack.size());
        assertEquals("I", stack.pop());
        assertEquals("hello", stack.pop());
        assertEquals(0, stack.size());
        assertNull(stack.pop());
    }

    @Test
    @DisplayName("Testing resizable array and iterator")
    public void testResize(){
        stack.push("Hello");
        stack.push("world");
        stack.push("I");
        stack.push("am");
        stack.push("testing");
        stack.push("my");
        stack.push("implementation");
        stack.push("of");
        stack.push("Stacks");
        stack.push("Data");
        stack.push("Structure");
        assertEquals("Structure", stack.pop());
        int counter = 6;
        while(counter-- > 0)
            stack.pop();
        assertEquals(4,stack.size());
        String[] arr = new String[stack.size()];
        Iterator<String> iterator = stack.iterator();
        int count = 0;
        while(iterator.hasNext()){
            arr[count++] = iterator.next();
        }
        assertArrayEquals(new String[]{"Hello", "world", "I", "am"}, arr);
    }

}