| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import static org.junit.Assert.assertEquals;
- import org.junit.Ignore;
- import org.junit.Test;
-
- public class BinarySearchTest {
-
- public static final List<Integer> EMPTY_LIST
- = Collections.unmodifiableList(new ArrayList<Integer>(0));
-
- public static final List<Integer> LIST_OF_UNIT_LENGTH
- = Collections.unmodifiableList(
- Arrays.asList(6)
- );
-
- private static final List<Integer> SORTED_LIST
- = Collections.unmodifiableList(
- Arrays.asList(1, 3, 4, 6, 8, 9, 11)
- );
-
- public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
- = Collections.unmodifiableList(
- Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
- 89, 144, 233, 377, 634)
- );
-
- public static final List<Integer> SORTED_LIST_OF_EVEN_LENGTH
- = Collections.unmodifiableList(
- Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
- 89, 144, 233, 377)
- );
-
- @Test
- public void findsAValueInAnArrayWithOneElement() {
- BinarySearch<Integer> sut = new BinarySearch<>(LIST_OF_UNIT_LENGTH);
- final int value = 6;
- final int actual = sut.indexOf(value);
- final int expected = 0;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void findsAValueInTheMiddleOfAnArray() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
- final int value = 6;
- final int actual = sut.indexOf(value);
- final int expected = 3;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void findsAValueAtTheBeginningOfAnArray() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
- final int value = 1;
- final int actual = sut.indexOf(value);
- final int expected = 0;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void findsAValueAtTheEndOfAnArray() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
- final int value = 11;
- final int actual = sut.indexOf(value);
- final int expected = 6;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void findsAValueInAnArrayOfOddLength() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST_OF_ODD_LENGTH);
- final int value = 144;
- final int actual = sut.indexOf(value);
- final int expected = 9;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void findsAValueInAnArrayOfEvenLength() {
- BinarySearch<Integer> sut
- = new BinarySearch<>(SORTED_LIST_OF_EVEN_LENGTH);
- final int value = 21;
- final int actual = sut.indexOf(value);
- final int expected = 5;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void identifiesThatAValueIsNotIncludedInTheArray() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
- final int value = 7;
- final int actual = sut.indexOf(value);
- final int expected = -1;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void aValueSmallerThanTheArraysSmallestValueIsNotIncluded() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
- final int value = 0;
- final int actual = sut.indexOf(value);
- final int expected = -1;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void aValueLargerThanTheArraysSmallestValueIsNotIncluded() {
- BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
- final int value = 13;
- final int actual = sut.indexOf(value);
- final int expected = -1;
- assertEquals(expected, actual);
- }
-
- @Ignore("Remove to run test")
- @Test
- public void nothingIsIncludedInAnEmptyArray() {
- BinarySearch<Integer> sut = new BinarySearch<>(EMPTY_LIST);
- final int value = 1;
- final int actual = sut.indexOf(value);
- final int expected = -1;
- assertEquals(expected, actual);
- }
- }
|