package ru.matevosyan;

import org.junit.Test;

import java.util.Iterator;

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

/**
 *  DynamicSimpleSetTest class.
 * Created on 27.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class DynamicSimpleSetTest {

    /**
     * Create whenAddDuplicateThanReturnWithoutDuplicates() to test {@link DynamicSimpleSet#add(Object)}.
     * if try to add duplicate to set than do not add.
     */

    @Test
    public void whenAddDuplicateThanReturnWithoutDuplicates() {
        DynamicSimpleSet<Integer> dynamicSimpleSet = new DynamicSimpleSet<>();

        dynamicSimpleSet.add(3);
        dynamicSimpleSet.add(3);
        dynamicSimpleSet.add(2);

        Integer[] integers = new Integer[2];
        integers[0] = 3;
        integers[1] = 2;

        assertThat(dynamicSimpleSet.get(0), is(integers[0]));
        assertThat(dynamicSimpleSet.length(), is(2));
    }

    /**
     * Create whenAddNotDuplicateThanCheckTheSameValues() to test {@link DynamicSimpleSet#add(Object)}.
     * if try to add not duplicate value to set than add it.
     */

    @Test
    public void whenAddNotDuplicateThanCheckTheSameValues() {
        DynamicSimpleSet<Integer> dynamicSimpleSet = new DynamicSimpleSet<>();

        dynamicSimpleSet.add(3);
        dynamicSimpleSet.add(2);
        dynamicSimpleSet.add(4);
        dynamicSimpleSet.add(5);

        Integer[] integers = new Integer[4];
        integers[0] = 3;
        integers[1] = 2;
        integers[2] = 4;
        integers[3] = 5;

        for (int i = 0; i < dynamicSimpleSet.length(); i++) {
            assertThat(dynamicSimpleSet.get(i), is(integers[i]));
        }
        assertThat(dynamicSimpleSet.length(), is(4));
    }

    /**
     * Create whenAddValuesThanWithIteratorCheckTheSameValues() to test {@link DynamicSimpleSet#add(Object)}.
     * if add value to set than, check values with iterator.
     */

    @Test
    public void whenAddValuesThanWithIteratorCheckTheSameValues() {
        DynamicSimpleSet<Integer> dynamicSimpleSet = new DynamicSimpleSet<>();

        dynamicSimpleSet.add(3);
        dynamicSimpleSet.add(3);
        dynamicSimpleSet.add(3);
        dynamicSimpleSet.add(5);

        Integer[] integers = new Integer[2];
        integers[0] = 3;
        integers[1] = 5;

        Iterator iterator = dynamicSimpleSet.iterator();
        int i = 0;

        while(iterator.hasNext()) {
            Integer integer = (Integer) iterator.next();
            assertThat(integer, is(integers[i++]));
        }

        assertThat(dynamicSimpleSet.length(), is(2));
    }

    /**
     * Create whenGetValuesFromIndexThatOutOfBoundsArrayThanCheckArrayIndexOutOfBoundsException() to test.
     * ArrayIndexOutOfBoundsException when get index that is out of bounds.
     */

    @Test
    public void whenGetValuesFromIndexThatOutOfBoundsArrayThanCheckArrayIndexOutOfBoundsException() {
        DynamicSimpleSet<Integer> dynamicSimpleSet = new DynamicSimpleSet<>();
        Throwable throwable = null;

        dynamicSimpleSet.add(3);

        try {
            dynamicSimpleSet.get(1);
        } catch (Throwable e) {
            throwable = e;
        }

        assertTrue(throwable instanceof ArrayIndexOutOfBoundsException);
    }

    /**
     * Create whenGetValuesFromIndexThatIllegalArgumentExceptionThanCheckIllegalArgumentException() to test.
     * IllegalArgumentException when get index that is illegal.
     */

    @Test
    public void whenGetValuesFromIndexThatIllegalArgumentExceptionThanCheckIllegalArgumentException() {
        DynamicSimpleSet<Integer> dynamicSimpleSet = new DynamicSimpleSet<>();
        Throwable throwable = null;
        dynamicSimpleSet.add(3);

        try {
            dynamicSimpleSet.get(-5);
        } catch (Throwable e) {
            throwable = e;
        }
        assertTrue(throwable instanceof IllegalArgumentException);
    }

}
@@ -1237,37 +1237,28 @@
 ateThanCheck
-TheSameValues
+Size
 () %7B%0A       
@@ -1467,292 +1467,8 @@
 );%0A%0A
-        Integer%5B%5D integers = new Integer%5B4%5D;%0A        integers%5B0%5D = 3;%0A        integers%5B1%5D = 2;%0A        integers%5B2%5D = 4;%0A        integers%5B3%5D = 5;%0A%0A        for (int i = 0; i %3C dynamicSimpleSet.length(); i++) %7B%0A            assertThat(dynamicSimpleSet.get(i), is(integers%5Bi%5D));%0A        %7D%0A
     
@@ -1990,33 +1990,33 @@
 icSimpleSet.add(
-5
+3
 );%0A%0A        Inte
@@ -2003,39 +2003,35 @@
 dd(3);%0A%0A        
-I
+i
 nt
-eger
 %5B%5D integers = ne
@@ -2024,39 +2024,35 @@
  integers = new 
-I
+i
 nt
-eger
 %5B2%5D;%0A        int
@@ -2068,33 +2068,8 @@
 = 3;
-%0A        integers%5B1%5D = 5;
 %0A%0A  
@@ -2195,88 +2195,44 @@
     
-Integer integer = (Integer) iterator.next();%0A            assertThat(integer, is(
+assertTrue((int) iterator.next() == 
 inte
@@ -2240,13 +2240,10 @@
 ers%5B
-i++%5D)
+0%5D
 );%0A 
@@ -2293,33 +2293,33 @@
 et.length(), is(
-2
+1
 ));%0A    %7D%0A%0A    /

change test