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"};

		// Returns index of searched string
		assertEquals(Arrays.binarySearch(capitals, "Berlin"), 0);
		assertEquals(Arrays.binarySearch(capitals, "London"), 1);
		assertEquals(Arrays.binarySearch(capitals, "Moscow"), 2);
		assertEquals(Arrays.binarySearch(capitals, "Paris"), 3);

		// Computes potential index of searched string
		// Then it inverts and decrements index
		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"};
		// Creates list backed by array
		List<String> list = Arrays.asList(capitals);

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

		// Change in list changes array
		list.set(0, "Warsaw");
		assertEquals(capitals[0], "Warsaw");

		// And vice versa
		capitals[0] = "Milan";
		assertEquals(list.get(0), "Milan");
	}
}

@@ -1489,38 +1489,19 @@
 %22London%22
-, %22Moscow%22, %22Paris%22
 %7D;%0A
+
 %09%09// Cre
@@ -1578,55 +1578,48 @@
 );%0A%0A
+%0A
 %09%09
-for (int i = 0; i %3C capitals.length; i++) %7B%0A%09
+assertEquals(%22Berlin%22, list.get(0));%0A
 %09%09as
@@ -1633,19 +1633,16 @@
 als(
-capitals%5Bi%5D
+%22London%22
 , li
@@ -1652,16 +1652,13 @@
 get(
-i
+1
 ));%0A
-%09%09%7D
 %0A%0A%09%09
@@ -1785,17 +1785,17 @@
 apitals%5B
-0
+1
 %5D = %22Mil
@@ -1823,17 +1823,17 @@
 ist.get(
-0
+1
 ), %22Mila

test refactoring