package ru.otus.kirillov.hw03;

import org.junit.After;
import org.junit.Test;
import ru.otus.kirillov.hw03.collection.MyArrayList;

import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by Александр on 29.10.2017.
 */
public class MyArrayListTest {

    private List<String> lst = new MyArrayList<>();

    @After
    public void prepareTest() {
        lst = new MyArrayList<>();
    }

    @Test
    public void addAllToEmptyArrayList() {
        List<String> expected = Arrays.asList("12", "13", "14");
        boolean isChanged = Collections.addAll(lst, (String[])expected.toArray());

        assertTrue("MyArrayList not changed after test elements add", isChanged);
        assertEquals(lst.toString() + " is not contains " + expected.toString(),
                expected, lst);
    }

    @Test
    public void addAllToNotEmptyArrayList() {
        List<String> expected = Arrays.asList("1", "2", "3", "4", "1", "2", "3", "4");
        String[] added = {"1", "2", "3", "4"};
        Collections.addAll(lst, added);

        boolean isChanged = Collections.addAll(lst, added);
        assertTrue("MyArrayList not changed after test elements add", isChanged);
        assertEquals(lst.toString() + " is not contains " + expected.toString(),
                expected, lst);
    }

    @Test(expected = NullPointerException.class)
    public void addAllNullElementsToNotEmptyArrayList() {
        lst.addAll(Arrays.asList("1", "2", "3", "THREE", "Zero"));
        Collections.addAll(lst, null);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void copyToEmptyArrayList() {
        List<String> src = Arrays.asList("1", "2", "3", "4");
        Collections.copy(lst, src);
    }

    @Test
    public void copyToNotEmptyArrayList() {
        List<String> src = Arrays.asList("1", "2", "3", "4");
        List<String> expected = new ArrayList<>(src);
        expected.add("Five");

        Collections.addAll(lst, "One", "Two", "Three", "Four", "Five");
        Collections.copy(lst, src);
        assertEquals(lst.toString() + " not equals with expected "
                + expected.toString() + " after  Collections.copy " + src.toString(),
                expected, lst);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void copyToSmallestArrayList() {
        List<String> src = Arrays.asList("1", "2", "3", "4");
        Collections.addAll(lst, "One");
        Collections.copy(lst, src);
    }

    @Test
    public void sortEmptyArrayList() {
        List<String> expected = Collections.emptyList();
        Collections.sort(lst);
        assertEquals(expected, lst);
    }

    @Test
    public void sortNotEmptyArrayList() {
        Collections.addAll(lst, "4", "3", "Tape Deck", "0", "100", "-10");
        List<String> expected = Arrays.asList("-10", "0", "100", "3", "4", "Tape Deck");
        Collections.sort(lst);
        assertEquals(lst.toString() + " not equals expected " + expected.toString(),
                expected, lst);
    }

    @Test
    public void sortEmptyArrayListWithSpecComparator() {
        List<String> expected = Collections.emptyList();
        Collections.sort(lst, Comparator.comparing(String::length, Comparator.naturalOrder()));
        assertEquals(lst.toString() + " not equals expected " + expected.toString(),
                expected, lst);
    }

    @Test
    public void sortNotEmptyArrayListWithSpecComparator() {
        Collections.addAll(lst, "1234567", "12", "1", "1234567890", "012", "8101");
        List<String> expected = Arrays.asList("1234567890", "1234567", "8101", "012", "12", "1");
        Collections.sort(lst, Comparator.comparing(String::length, Comparator.reverseOrder()));
        assertEquals(lst.toString() + " not equals expected " + expected.toString(),
                expected, lst);
    }
}
@@ -145,16 +145,86 @@
 .util.*;
+%0Aimport java.util.stream.Collector;%0Aimport java.util.stream.IntStream;
 %0A%0Aimport
@@ -1412,32 +1412,617 @@
 d, lst);%0A    %7D%0A%0A
+    @Test%0A    public void addAll1mlnToNotEmptyArrayList() %7B%0A        String%5B%5D testData = IntStream.range(0, 1_000_000)%0A                .mapToObj(Integer::toString)%0A                .toArray(String%5B%5D::new);%0A        List%3CString%3E expected = Arrays.asList(testData);%0A%0A        boolean isChanged = Collections.addAll(lst, testData);%0A        assertTrue(%22MyArrayList not changed after test elements add%22, isChanged);%0A        assertEquals(String.format(%22MyArrayList (size=%25d) is not equals of expected List(size=%25d) %22,%0A                        lst.size(), expected.size()), expected, lst);%0A    %7D%0A%0A
     @Test(expect

add test for addAll 1M objects