lots of exercises in java... from https://github.com/exercism/java

BinarySearchTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import static org.junit.Assert.assertEquals;
  5. import static org.junit.Assert.assertFalse;
  6. import static org.junit.Assert.assertNotNull;
  7. import org.junit.Ignore;
  8. import org.junit.Rule;
  9. import org.junit.Test;
  10. import org.junit.rules.ExpectedException;
  11. public class BinarySearchTest {
  12. private static final List<Integer> SORTED_LIST
  13. = Collections.unmodifiableList(
  14. Arrays.asList(1, 2, 3, 4, 5, 6)
  15. );
  16. public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
  17. = Collections.unmodifiableList(
  18. Arrays.asList(0, 1, 2, 2, 3, 10, 12)
  19. );
  20. public static final List<Integer> UNSORTED_LIST
  21. = Collections.unmodifiableList(
  22. Arrays.asList(10, 2, 5, 1)
  23. );
  24. @Rule
  25. public ExpectedException thrown = ExpectedException.none();
  26. @Test
  27. public void shouldRequireASortedListOK() {
  28. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  29. List<Integer> actual = sut.getArray();
  30. assertNotNull(actual);
  31. assertFalse(actual.isEmpty());
  32. assertEquals(actual.size(), SORTED_LIST.size());
  33. assertEquals(actual, SORTED_LIST);
  34. }
  35. @Ignore
  36. @Test
  37. public void shouldRequireASortedListButNotSorted() {
  38. thrown.expect(IllegalArgumentException.class);
  39. thrown.expectMessage(BinarySearch.ARRAY_MUST_BE_SORTED);
  40. new BinarySearch<Integer>(UNSORTED_LIST);
  41. }
  42. @Ignore
  43. @Test
  44. public void shouldFindTheCorrectIndexInTheMiddleOfArray() {
  45. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  46. final int number = 3;
  47. final int actual = sut.indexOf(number);
  48. final int expected = 2;
  49. assertEquals(expected, actual);
  50. }
  51. @Ignore
  52. @Test
  53. public void shouldFindTheCorrectIndexAtTheBeginningOfArray() {
  54. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  55. final int number = 1;
  56. final int actual = sut.indexOf(number);
  57. final int expected = 0;
  58. assertEquals(expected, actual);
  59. }
  60. @Ignore
  61. @Test
  62. public void shouldFindTheCorrectIndexAtTheEndOfArray() {
  63. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  64. final int number = 6;
  65. final int actual = sut.indexOf(number);
  66. final int expected = 5;
  67. assertEquals(expected, actual);
  68. }
  69. @Ignore
  70. @Test
  71. public void shouldFindTheCorrectIndexInTheMiddleOfArrayOfOddLength() {
  72. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST_OF_ODD_LENGTH);
  73. final int number = 2;
  74. final int actual = sut.indexOf(number);
  75. final int expected = 3;
  76. assertEquals(expected, actual);
  77. }
  78. @Ignore
  79. @Test
  80. public void shouldReturnMinusOneIfNotFound() {
  81. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  82. final int number = 10;
  83. final int actual = sut.indexOf(number);
  84. final int expected = -1;
  85. assertEquals(expected, actual);
  86. }
  87. }