package core.array;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

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

public class ArraysTest {

	@Test
	public void sortIntArray() {
		int[] numbers = {20, 10, 5};
		int[] expected = {5, 10, 20};

		Arrays.sort(numbers);

		assertArrayEquals(numbers, expected);
	}

	@Test
	public void sortStringArray() {
		String[] numbers = {"20", "10", "5"};
		String[] expected = {"10", "20", "5"};

		Arrays.sort(numbers);

		assertArrayEquals(numbers, expected);
	}

	@Test
	public void searchStringInSortedArray() {
		// You should sort array before binary searching
		String[] capitals = {"Berlin", "London", "Moscow", "Paris"};

		assertEquals(Arrays.binarySearch(capitals, "Berlin"), 0);
		assertEquals(Arrays.binarySearch(capitals, "London"), 1);
		assertEquals(Arrays.binarySearch(capitals, "Moscow"), 2);
		assertEquals(Arrays.binarySearch(capitals, "Paris"), 3);

		assertEquals(Arrays.binarySearch(capitals, "Athens"), -1);
		assertEquals(Arrays.binarySearch(capitals, "Helsinki"), -2);
		assertEquals(Arrays.binarySearch(capitals, "Madrid"), -3);
		assertEquals(Arrays.binarySearch(capitals, "Oslo"), -4);
		assertEquals(Arrays.binarySearch(capitals, "Stockholm"), -5);
	}

	@Test
	public void asList() {
		String[] capitals = {"Berlin", "London", "Moscow", "Paris"};
		List<String> list = Arrays.asList(capitals);

		for (int i = 0; i < capitals.length; i++) {
			assertEquals(capitals[i], list.get(i));
		}

		list.set(0, "Warsaw");

		assertEquals(capitals[0], "Warsaw");
	}
}

@@ -736,16 +736,54 @@
 ris%22%7D;%0A%0A
+%09%09// Returns index of searched string%0A
 %09%09assert
@@ -1010,24 +1010,115 @@
 ris%22), 3);%0A%0A
+%09%09// Computes potential index of searched string%0A%09%09// Then it inverts and decrements index%0A
 %09%09assertEqua
@@ -1511,16 +1511,50 @@
 aris%22%7D;%0A
+%09%09// Creates list backed by array%0A
 %09%09List%3CS

test refactoring