package ru.aveselov.lesson4;

import java.util.Iterator;
import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;

/**
 * Tests for MyListSet class.
 *
 * @author Veselov Alexey.
 * @version 1.00
 * @since 21.12.2016
 */
public class MyListSetTest {

    /**
     * Instance for tests.
     */
    private MyListSet<String> instance;
    /**
     * Iterator.
     */
    private Iterator<String> it;

    /**
     * Initialization before every test.
     */
    @Before
    public void setUp() {
        this.instance = new MyListSet<>();
    }

    /**
     * Test of add method.
     */
    @Test
    public void testAddOneElement() {
        final String one = "Vasya";
        instance.add(one);
        this.it = this.instance.iterator();
        assertEquals(one, it.next());
    }

    /**
     * Test of add method.
     */
    @Test
    public void testAddFewElements() {
        final String one = "Vasya";
        final String two = "Sveta";
        instance.add(one);
        instance.add(two);
        this.it = this.instance.iterator();
        it.next();
        assertEquals(two, it.next());
    }

    /**
     * Test of add method.
     */
    @Test(expected = NoSuchElementException.class)
    public void testAddNotUniqueElement() {
        final String one = "Vasya";
        final String two = "Sveta";
        final String three = "Sveta";
        instance.add(one);
        instance.add(two);
        instance.add(three);
        this.it = this.instance.iterator();
        it.next();
        it.next();
        assertEquals(two, it.next());
    }

    /**
     * Test of iterator method.
     */
    @Test
    public void testIteratorHasNextFalse() {
        final boolean expResult = false;
        this.it = this.instance.iterator();
        boolean result = it.hasNext();
        assertEquals(expResult, result);
    }

    /**
     * Test of iterator method.
     */
    @Test
    public void testIteratorHasNextTrue() {
        final String one = "Alexey";
        instance.add(one);
        this.it = this.instance.iterator();
        final boolean expResult = true;
        boolean result = it.hasNext();
        assertEquals(expResult, result);
    }

    /**
     * Test of iterator method.
     */
    @Test(expected = NoSuchElementException.class)
    public void testIteratorNextException() {
        final String one = "Alexey";
        final String expResult = "Alexey";
        this.it = this.instance.iterator();
        String result = it.next();
        assertEquals(expResult, result);
    }

    /**
     * Test of iterator method.
     */
    @Test
    public void testIteratorNext() {
        final String one = "Alexey";
        instance.add(one);
        this.it = this.instance.iterator();
        final String expResult = "Alexey";
        String result = it.next();
        assertEquals(expResult, result);
    }

    /**
     * Test show how many time need to add 300000 elements in this collection.
     */
    @Test
    @Ignore
    public void testTimeToAddingALotOfItems() {
        final int countOfElements = 100000;
        MyListSet<Double> container = new MyListSet<>();
        Double someNumber;
        System.out.println("Start test:");
        final long startTime = System.currentTimeMillis();
        for (int i = 0; i < countOfElements; i++) {
            someNumber = Math.random();
            container.add(someNumber);
        }
        final long endTime = System.currentTimeMillis();
        System.out.println(String.format("End of test, time spent: %,d ms", endTime - startTime));
    }
}
