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

BinarySearchTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import static org.junit.Assert.assertEquals;
  6. import org.junit.Ignore;
  7. import org.junit.Test;
  8. public class BinarySearchTest {
  9. public static final List<Integer> EMPTY_LIST
  10. = Collections.unmodifiableList(new ArrayList<Integer>(0));
  11. public static final List<Integer> LIST_OF_UNIT_LENGTH
  12. = Collections.unmodifiableList(
  13. Arrays.asList(6)
  14. );
  15. private static final List<Integer> SORTED_LIST
  16. = Collections.unmodifiableList(
  17. Arrays.asList(1, 3, 4, 6, 8, 9, 11)
  18. );
  19. public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
  20. = Collections.unmodifiableList(
  21. Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
  22. 89, 144, 233, 377, 634)
  23. );
  24. public static final List<Integer> SORTED_LIST_OF_EVEN_LENGTH
  25. = Collections.unmodifiableList(
  26. Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
  27. 89, 144, 233, 377)
  28. );
  29. @Test
  30. public void findsAValueInAnArrayWithOneElement() {
  31. BinarySearch<Integer> sut = new BinarySearch<>(LIST_OF_UNIT_LENGTH);
  32. final int value = 6;
  33. final int actual = sut.indexOf(value);
  34. final int expected = 0;
  35. assertEquals(expected, actual);
  36. }
  37. @Ignore("Remove to run test")
  38. @Test
  39. public void findsAValueInTheMiddleOfAnArray() {
  40. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  41. final int value = 6;
  42. final int actual = sut.indexOf(value);
  43. final int expected = 3;
  44. assertEquals(expected, actual);
  45. }
  46. @Ignore("Remove to run test")
  47. @Test
  48. public void findsAValueAtTheBeginningOfAnArray() {
  49. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  50. final int value = 1;
  51. final int actual = sut.indexOf(value);
  52. final int expected = 0;
  53. assertEquals(expected, actual);
  54. }
  55. @Ignore("Remove to run test")
  56. @Test
  57. public void findsAValueAtTheEndOfAnArray() {
  58. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  59. final int value = 11;
  60. final int actual = sut.indexOf(value);
  61. final int expected = 6;
  62. assertEquals(expected, actual);
  63. }
  64. @Ignore("Remove to run test")
  65. @Test
  66. public void findsAValueInAnArrayOfOddLength() {
  67. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST_OF_ODD_LENGTH);
  68. final int value = 144;
  69. final int actual = sut.indexOf(value);
  70. final int expected = 9;
  71. assertEquals(expected, actual);
  72. }
  73. @Ignore("Remove to run test")
  74. @Test
  75. public void findsAValueInAnArrayOfEvenLength() {
  76. BinarySearch<Integer> sut
  77. = new BinarySearch<>(SORTED_LIST_OF_EVEN_LENGTH);
  78. final int value = 21;
  79. final int actual = sut.indexOf(value);
  80. final int expected = 5;
  81. assertEquals(expected, actual);
  82. }
  83. @Ignore("Remove to run test")
  84. @Test
  85. public void identifiesThatAValueIsNotIncludedInTheArray() {
  86. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  87. final int value = 7;
  88. final int actual = sut.indexOf(value);
  89. final int expected = -1;
  90. assertEquals(expected, actual);
  91. }
  92. @Ignore("Remove to run test")
  93. @Test
  94. public void aValueSmallerThanTheArraysSmallestValueIsNotIncluded() {
  95. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  96. final int value = 0;
  97. final int actual = sut.indexOf(value);
  98. final int expected = -1;
  99. assertEquals(expected, actual);
  100. }
  101. @Ignore("Remove to run test")
  102. @Test
  103. public void aValueLargerThanTheArraysSmallestValueIsNotIncluded() {
  104. BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
  105. final int value = 13;
  106. final int actual = sut.indexOf(value);
  107. final int expected = -1;
  108. assertEquals(expected, actual);
  109. }
  110. @Ignore("Remove to run test")
  111. @Test
  112. public void nothingIsIncludedInAnEmptyArray() {
  113. BinarySearch<Integer> sut = new BinarySearch<>(EMPTY_LIST);
  114. final int value = 1;
  115. final int actual = sut.indexOf(value);
  116. final int expected = -1;
  117. assertEquals(expected, actual);
  118. }
  119. }