package algorithms.divide;

import java.util.Random;

import org.junit.Assert;
import org.junit.Test;

import utils.RandomIntegerGenerator;

public class IntegerArraySearchTest {

	@Test
	public void testZeroPositiveValue() throws Exception {
		int[] a = new int[] {};

		int actual = IntegerArraySearch.binarySearch(a, 1);

		Assert.assertEquals(IntegerArraySearch.RESULT_NOT_FOUND, actual);
	}

	@Test
	public void testOnePositiveValueOK() throws Exception {
		int[] a = new int[] { 7 };

		int actual = IntegerArraySearch.binarySearch(a, 7);

		Assert.assertEquals(0, actual);
	}

	@Test
	public void testOnePositiveValueNOK() throws Exception {
		int[] a = new int[] { 7 };

		int actual = IntegerArraySearch.binarySearch(a, 2);

		Assert.assertEquals(IntegerArraySearch.RESULT_NOT_FOUND, actual);
	}

	@Test
	public void testFewPositiveValues() throws Exception {
		int[] a = new int[] { 1, 6, 7 };
		int expectedIndex = 2;

		int actual = IntegerArraySearch.binarySearch(a, a[expectedIndex]);

		Assert.assertEquals(expectedIndex, actual);
	}

	@Test
	public void testMorePositiveValues() throws Exception {
		int[] a = new int[] { 1, 5, 8, 12, 13 };
		int[] expectedIndexes = new int[] { 2, 0, -1, 0, -1 };
		int[] numbers = new int[] { 8, 1, 23, 1, 11 };

		for (int i = 0; i < numbers.length; i++) {
			int actual = IntegerArraySearch.binarySearch(a, numbers[i]);
			Assert.assertEquals(expectedIndexes[i], actual);
		}
	}

	@Test
	public void testRandomValues() throws Exception {
		int amount = 10;
		int[] a = new RandomIntegerGenerator(amount, Integer.MAX_VALUE).toArray();
		int expectedIndex = new Random().nextInt(amount);

		int linearSearchResult = IntegerArraySearch.linearSearch(a, a[expectedIndex]);
		int binarySearchResult = IntegerArraySearch.binarySearch(a, a[expectedIndex]);

		Assert.assertEquals(linearSearchResult, binarySearchResult);
	}
}

@@ -25,34 +25,8 @@
 e;%0A%0A
-import java.util.Random;%0A%0A
 impo
@@ -74,46 +74,8 @@
 t;%0A%0A
-import utils.RandomIntegerGenerator;%0A%0A
 publ
@@ -1364,444 +1364,7 @@
 %7D%0A%09%7D
-%0A%0A%09@Test%0A%09public void testRandomValues() throws Exception %7B%0A%09%09int amount = 10;%0A%09%09int%5B%5D a = new RandomIntegerGenerator(amount, Integer.MAX_VALUE).toArray();%0A%09%09int expectedIndex = new Random().nextInt(amount);%0A%0A%09%09int linearSearchResult = IntegerArraySearch.linearSearch(a, a%5BexpectedIndex%5D);%0A%09%09int binarySearchResult = IntegerArraySearch.binarySearch(a, a%5BexpectedIndex%5D);%0A%0A%09%09Assert.assertEquals(linearSearchResult, binarySearchResult);%0A%09%7D
 %0A%7D%0A

Remove test because binary search does not find a first occurrence