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