package ru.matevosyan;

import org.junit.Test;

import java.util.Iterator;
import java.util.NoSuchElementException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

/**
 * DynamicSetByLinkedListTest class for testing DynamicLinkedList class.
 * Created on 29.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class DynamicSetByLinkedListTest {

    /**
     * Create whenAddTwoElementsThanCheckOneOfThenGettingByIndex() method to check added element to the list.
     */

    @Test
    public void whenAddTwoElementsThanCheckOneOfThenGettingByIndex() {

        DynamicSetByLinkedList<Integer> integerDynamicLinkedList = new DynamicSetByLinkedList<>();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(42);

        Iterator<Integer> integerIterator = integerDynamicLinkedList.iterator();
        assertThat(integerIterator.next(), is(12));
        assertThat(integerIterator.next(), is(42));

    }


    /**
     * Create whenInvokeNextMethodWithoutAddingTheValuesToLinkedListThanCheckWaitingForException to check an exception.
     */

    @Test
    public void whenInvokeNextMethodWithoutAddingTheValuesToLinkedListThanCheckWaitingForException() {

        Throwable e = null;

        try {
            DynamicSetByLinkedList<Integer> integerDynamicLinkedList = new DynamicSetByLinkedList<>();
            Iterator<Integer> itr = integerDynamicLinkedList.iterator();
            itr.next();
        } catch (Throwable ex) {
            e = ex;
        }

        assertTrue(e instanceof NoSuchElementException);
    }


    /**
     * Create whenGetIteratorFromLinkedListAndUseHasNextAndNextThanCheckTheGetResults() to check next method.
     * In {@link DynamicSetByLinkedList#iterator()}.
     */

    @Test
    public void whenGetIteratorFromLinkedListAndUseHasNextAndNextThanCheckTheGetResults() {
        DynamicSetByLinkedList<Integer> integerDynamicLinkedList = new DynamicSetByLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(3);

        int value = 0;
        if (itr.hasNext()) {
            value = itr.next();
        }


        assertThat(value, is(12));
    }

    /**
     * Create whenInvokeHasNextThanCheckTheGetResultsIsGoingToBeTrue().
     * To check hasNext() in {@link DynamicSetByLinkedList#iterator()}.
     */

    @Test
    public void whenInvokeHasNextThanCheckTheGetResultsIsGoingToBeTrue() {
        DynamicSetByLinkedList<Integer> integerDynamicLinkedList = new DynamicSetByLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);

        boolean has = itr.hasNext();

        assertThat(has, is(true));
    }

    /**
     * Create whenInvokeHasNextAndNextThanCheckTheGetResultsIsGoingToBeFalse() to check hasNext().
     * when in the LinkedList does not have any element and expected return false.
     */

    @Test
    public void whenInvokeHasNextAndNextThanCheckTheGetResultsIsGoingToBeFalse() {
        DynamicSetByLinkedList<Integer> integerDynamicLinkedList = new DynamicSetByLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);

        itr.hasNext();
        int a = itr.next();
        int c = itr.next();
        boolean has = itr.hasNext();

        assertThat(has, is(false));
        assertThat(a, is(12));
        assertThat(c, is(2));

    }

    /**
     * Create whenAddManyDuplicateAndInvokeNextMoreTimesThanAddElementThanCheckNoSuchElementException().
     * to check NoSuchElementException, when invoke method next more times than element in the set.
     */

    @Test
    public void whenAddManyDuplicateAndInvokeNextMoreTimesThanAddElementThanCheckNoSuchElementException() {
        DynamicSetByLinkedList<Integer> integerDynamicLinkedList = new DynamicSetByLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(2);
        Throwable ex = null;

        try {
            itr.next();
            itr.next();
            itr.next();

        } catch (Throwable e) {
            ex = e;
        }

        assertThat(integerDynamicLinkedList.len(), is(2));
        assertTrue(ex instanceof NoSuchElementException);

    }



}
@@ -621,38 +621,32 @@
 ThanCheckOne
-OfThen
 GettingB
 yIndex() %7B%0A%0A
@@ -633,22 +633,21 @@
 GettingB
-yInd
+tN
 ex
+t
 () %7B%0A%0A  
@@ -954,32 +954,32 @@
 ext(), is(12));%0A
-
         assertTh
@@ -1014,32 +1014,1055 @@
 s(42));%0A%0A    %7D%0A%0A
+    /**%0A     * Create whenAddManyElementsThanCheckOrderIsNotTheSameBecauseOrderByHashcode() to check order is not save.%0A     */%0A%0A    @Test%0A    public void whenAddManyElementsThanCheckOrderIsNotTheSameBecauseOrderByHashcode() %7B%0A%0A        DynamicSetByLinkedList%3CInteger%3E integerDynamicLinkedList = new DynamicSetByLinkedList%3C%3E();%0A%0A        integerDynamicLinkedList.add(12);%0A        integerDynamicLinkedList.add(42);%0A        integerDynamicLinkedList.add(3);%0A        integerDynamicLinkedList.add(8);%0A        integerDynamicLinkedList.add(0);%0A        integerDynamicLinkedList.add(9);%0A        integerDynamicLinkedList.add(123);%0A        integerDynamicLinkedList.add(22);%0A%0A%0A%0A        Iterator%3CInteger%3E integerIterator = integerDynamicLinkedList.iterator();%0A        while (integerIterator.hasNext()) %7B%0A            System.out.println(integerIterator.next());%0A        %7D%0A//        integerIterator.next();%0A//        integerIterator.next();%0A//        integerIterator.next();%0A//        assertFalse(integerIterator.next().equals(8));%0A%0A%0A    %7D%0A%0A
 %0A    /**%0A     * 

add one test do not work