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 LinkedQueueTest {

    private Queue<Integer> queue;

    @BeforeEach
    void setUp() {
        queue = new LinkedQueue<>();
    }

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


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

    @Test
    @DisplayName("Enqueuing must work as expected")
    public void testEnqueue(){
        queue.enqueue(1);
        queue.enqueue(2);
        assertEquals(2,queue.size());
        Iterator<Integer> iterator = queue.iterator();
        assertEquals(1, iterator.next());
        assertEquals(2, iterator.next());
    }

    @Test
    @DisplayName("Dequeueing must work as expected")
    public void testDequeue(){
        queue.enqueue(1);
        queue.enqueue(2);
        assertEquals(1, queue.dequeue());
        queue.enqueue(3);
        assertEquals(2, queue.size());
        assertEquals(2, queue.dequeue());
        assertEquals(3, queue.dequeue());
        assertEquals(0, queue.size());
        assertNull(queue.dequeue());
    }

    @Test
    @DisplayName("Testing resizable array and iterator")
    public void testResize(){
        for(int i = 0; i < 21; i++)
            queue.enqueue(i);
        for(int i = 0; i < 13; i++){
            queue.dequeue();
        }
        assertEquals(13, queue.dequeue());
        assertEquals(7,queue.size());
        Integer[] arr = new Integer[queue.size()];
        Iterator<Integer> iterator = queue.iterator();
        int count = 0;
        while(iterator.hasNext()){
            arr[count++] = iterator.next();
        }
        assertArrayEquals(new Integer[]{14, 15, 16, 17, 18, 19, 20}, arr);
    }
}