瀏覽代碼

Implemented recommended changes

Dmitry Noranovich 9 年之前
父節點
當前提交
109521098f

+ 0
- 1
exercises/binary-search/build.gradle 查看文件

8
 
8
 
9
 dependencies {
9
 dependencies {
10
   testCompile "junit:junit:4.12"
10
   testCompile "junit:junit:4.12"
11
-  testCompile "org.assertj:assertj-core:3.2.0"
12
 }
11
 }
13
 test {
12
 test {
14
   testLogging {
13
   testLogging {

+ 0
- 30
exercises/binary-search/src/example/java/BinarySearch.java 查看文件

3
 
3
 
4
 public class BinarySearch<T extends Comparable<T>> {
4
 public class BinarySearch<T extends Comparable<T>> {
5
 
5
 
6
-    public static String ARRAY_MUST_BE_SORTED = "Array should be sorted.";
7
-
8
     private List<T> array;
6
     private List<T> array;
9
     private int arraySize;
7
     private int arraySize;
10
 
8
 
11
     public BinarySearch(List<T> array) {
9
     public BinarySearch(List<T> array) {
12
-        if (!isSorted(array)) {
13
-            throw new IllegalArgumentException(ARRAY_MUST_BE_SORTED);
14
-        }
15
         this.array = array;
10
         this.array = array;
16
         this.arraySize = array.size();
11
         this.arraySize = array.size();
17
     }
12
     }
24
         return array;
19
         return array;
25
     }
20
     }
26
 
21
 
27
-    private boolean isSorted(List<T> list) {
28
-        T previous, next;
29
-        int listSize;
30
-
31
-        if (list == null || list.isEmpty()) {
32
-            return false;
33
-        }
34
-
35
-        listSize = list.size();
36
-        if (listSize == 1) {
37
-            return true;
38
-        }
39
-
40
-        previous = list.get(0);
41
-        for (int i = 0, n = listSize - 1; i < n; i++) {
42
-            next = list.get(i + 1);
43
-            if (previous.compareTo(next) > 0) {
44
-                return false;
45
-            }
46
-            previous = next;
47
-        }
48
-        return true;
49
-    }
50
-
51
     private int search(T value) {
22
     private int search(T value) {
52
         int left = 0;
23
         int left = 0;
53
         int right = this.arraySize - 1;
24
         int right = this.arraySize - 1;
67
         return -1;
38
         return -1;
68
     }
39
     }
69
 }
40
 }
70
-

+ 77
- 41
exercises/binary-search/src/test/java/BinarySearchTest.java 查看文件

1
 
1
 
2
+import java.util.ArrayList;
2
 import java.util.Arrays;
3
 import java.util.Arrays;
3
 import java.util.Collections;
4
 import java.util.Collections;
4
 import java.util.List;
5
 import java.util.List;
5
 import static org.junit.Assert.assertEquals;
6
 import static org.junit.Assert.assertEquals;
6
-import static org.junit.Assert.assertFalse;
7
-import static org.junit.Assert.assertNotNull;
8
 import org.junit.Ignore;
7
 import org.junit.Ignore;
9
-import org.junit.Rule;
10
 import org.junit.Test;
8
 import org.junit.Test;
11
-import org.junit.rules.ExpectedException;
12
 
9
 
13
 public class BinarySearchTest {
10
 public class BinarySearchTest {
14
 
11
 
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
+
15
     private static final List<Integer> SORTED_LIST
20
     private static final List<Integer> SORTED_LIST
16
             = Collections.unmodifiableList(
21
             = Collections.unmodifiableList(
17
-                    Arrays.asList(1, 2, 3, 4, 5, 6)
22
+                    Arrays.asList(1, 3, 4, 6, 8, 9, 11)
18
             );
23
             );
19
 
24
 
20
     public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
25
     public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
21
             = Collections.unmodifiableList(
26
             = Collections.unmodifiableList(
22
-                    Arrays.asList(0, 1, 2, 2, 3, 10, 12)
27
+                    Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
28
+                            89, 144, 233, 377, 634)
23
             );
29
             );
24
 
30
 
25
-    public static final List<Integer> UNSORTED_LIST
31
+    public static final List<Integer> SORTED_LIST_OF_EVEN_LENGTH
26
             = Collections.unmodifiableList(
32
             = Collections.unmodifiableList(
27
-                    Arrays.asList(10, 2, 5, 1)
33
+                    Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
34
+                            89, 144, 233, 377)
28
             );
35
             );
29
 
36
 
30
-    @Rule
31
-    public ExpectedException thrown = ExpectedException.none();
37
+    @Test
38
+    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);
44
+    }
32
 
45
 
46
+    @Ignore
33
     @Test
47
     @Test
34
-    public void shouldRequireASortedListOK() {
48
+    public void findsAValueInTheMiddleOfAnArray() {
35
         BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
49
         BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
36
-        List<Integer> actual = sut.getArray();
37
-        assertNotNull(actual);
38
-        assertFalse(actual.isEmpty());
39
-        assertEquals(actual.size(), SORTED_LIST.size());
40
-        assertEquals(actual, SORTED_LIST);
50
+        final int value = 6;
51
+        final int actual = sut.indexOf(value);
52
+        final int expected = 3;
53
+        assertEquals(expected, actual);
41
     }
54
     }
42
 
55
 
43
     @Ignore
56
     @Ignore
44
     @Test
57
     @Test
45
-    public void shouldRequireASortedListButNotSorted() {
46
-        thrown.expect(IllegalArgumentException.class);
47
-        thrown.expectMessage(BinarySearch.ARRAY_MUST_BE_SORTED);
48
-        new BinarySearch<Integer>(UNSORTED_LIST);
58
+    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);
49
     }
64
     }
50
 
65
 
51
     @Ignore
66
     @Ignore
52
     @Test
67
     @Test
53
-    public void shouldFindTheCorrectIndexInTheMiddleOfArray() {
68
+    public void findsAValueAtTheEndOfAnArray() {
54
         BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
69
         BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
55
-        final int number = 3;
56
-        final int actual = sut.indexOf(number);
57
-        final int expected = 2;
70
+        final int value = 11;
71
+        final int actual = sut.indexOf(value);
72
+        final int expected = 6;
58
         assertEquals(expected, actual);
73
         assertEquals(expected, actual);
59
     }
74
     }
60
 
75
 
61
     @Ignore
76
     @Ignore
62
     @Test
77
     @Test
63
-    public void shouldFindTheCorrectIndexAtTheBeginningOfArray() {
64
-        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
65
-        final int number = 1;
66
-        final int actual = sut.indexOf(number);
67
-        final int expected = 0;
78
+    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;
68
         assertEquals(expected, actual);
83
         assertEquals(expected, actual);
69
     }
84
     }
70
 
85
 
71
     @Ignore
86
     @Ignore
72
     @Test
87
     @Test
73
-    public void shouldFindTheCorrectIndexAtTheEndOfArray() {
74
-        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
75
-        final int number = 6;
76
-        final int actual = sut.indexOf(number);
88
+    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);
77
         final int expected = 5;
93
         final int expected = 5;
78
         assertEquals(expected, actual);
94
         assertEquals(expected, actual);
79
     }
95
     }
80
 
96
 
81
     @Ignore
97
     @Ignore
82
     @Test
98
     @Test
83
-    public void shouldFindTheCorrectIndexInTheMiddleOfArrayOfOddLength() {
84
-        BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST_OF_ODD_LENGTH);
85
-        final int number = 2;
86
-        final int actual = sut.indexOf(number);
87
-        final int expected = 3;
99
+    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;
88
         assertEquals(expected, actual);
104
         assertEquals(expected, actual);
89
     }
105
     }
90
 
106
 
91
     @Ignore
107
     @Ignore
92
     @Test
108
     @Test
93
-    public void shouldReturnMinusOneIfNotFound() {
109
+    public void aValueSmallerThanTheArraysSmallestValueIsNotIncluded() {
94
         BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
110
         BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
95
-        final int number = 10;
96
-        final int actual = sut.indexOf(number);
111
+        final int value = 0;
112
+        final int actual = sut.indexOf(value);
113
+        final int expected = -1;
114
+        assertEquals(expected, actual);
115
+    }
116
+
117
+    @Ignore
118
+    @Test
119
+    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);
125
+    }
126
+
127
+    @Ignore
128
+    @Test
129
+    public void nothingIsIncludedInAnEmptyArray() {
130
+        BinarySearch<Integer> sut = new BinarySearch<>(EMPTY_LIST);
131
+        final int value = 1;
132
+        final int actual = sut.indexOf(value);
97
         final int expected = -1;
133
         final int expected = -1;
98
         assertEquals(expected, actual);
134
         assertEquals(expected, actual);
99
     }
135
     }